2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
5 * Copyright (c) 1983, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgment:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/sbin/routed/main.c,v 1.14 2000/08/11 08:24:38 sheldonh Exp $
37 * char copyright[] = "@(#) Copyright (c) 1983, 1988, 1993\n"
38 * " The Regents of the University of California. All rights reserved.\n";
42 #include "pathnames.h"
49 #define IN_ROUTED_VERSION "2.22"
52 boolean_t supplier
; /* supply or broadcast updates */
53 boolean_t supplier_set
;
54 /* -S option. _B_TRUE=treat all RIP speakers as default routers. */
55 boolean_t save_space
= _B_FALSE
;
57 static boolean_t default_gateway
; /* _B_TRUE=advertise default */
58 static boolean_t background
= _B_TRUE
;
59 boolean_t ridhosts
; /* _B_TRUE=reduce host routes */
60 boolean_t mhome
; /* _B_TRUE=want multi-homed host route */
61 boolean_t advertise_mhome
; /* _B_TRUE=must continue advertising it */
62 boolean_t auth_ok
= _B_TRUE
; /* _B_TRUE=ignore auth if we don't care */
63 boolean_t no_install
; /* _B_TRUE=don't install in kernel */
65 struct timeval epoch
; /* when started */
67 static struct timeval prev_clk
;
68 static int usec_fudge
;
69 struct timeval now
; /* current idea of time */
70 /* If a route's rts_time is <= to now_stale, the route is stale. */
72 /* If a route's rts_time is <= to now_expire, the route is expired */
74 /* If a route's rts_time is <= to now_garbage, the route needs to be deleted */
77 static struct timeval next_bcast
; /* next general broadcast */
78 struct timeval no_flash
= { /* inhibit flash update */
79 EPOCH
+SUPPLY_INTERVAL
, 0
82 /* When now reaches this time, it's time to call sync_kern() */
83 static struct timeval sync_kern_timer
;
87 int rip_sock
= -1; /* RIP socket */
88 boolean_t rip_enabled
;
89 static boolean_t openlog_done
;
92 * The interface to which rip_sock is currently pointing for
95 struct interface
*rip_sock_interface
;
97 int rt_sock
; /* routing socket */
100 static int open_rip_sock();
101 static void timevalsub(struct timeval
*, struct timeval
*, struct timeval
*);
102 static void sigalrm(int);
103 static void sigterm(int);
106 main(int argc
, char *argv
[])
111 struct timeval select_timeout
, result
;
113 in_addr_t p_net
, p_mask
;
115 char *tracename
= NULL
;
116 boolean_t vflag
= _B_FALSE
;
117 boolean_t version
= _B_FALSE
;
120 mode_t pidmode
= (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
); /* 0644 */
122 (void) setlocale(LC_ALL
, "");
124 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
125 #define TEXT_DOMAIN "SYS_TEXT"
126 #endif /* ! TEXT_DOMAIN */
128 (void) textdomain(TEXT_DOMAIN
);
131 * Some shells are badly broken and send SIGHUP to backgrounded
134 if (signal(SIGHUP
, SIG_IGN
) == SIG_ERR
)
139 if (gettimeofday(&clk
, 0) == -1) {
140 logbad(_B_FALSE
, "gettimeofday: %s", rip_strerror(errno
));
144 epoch
.tv_sec
-= EPOCH
;
146 now_stale
= EPOCH
- STALE_TIME
;
147 now_expire
= EPOCH
- EXPIRE_TIME
;
148 now_garbage
= EPOCH
- GARBAGE_TIME
;
149 select_timeout
.tv_sec
= 0;
151 while ((n
= getopt(argc
, argv
, "sSqdghmpAztVvnT:F:P:")) != -1) {
155 * Ignore authentication if we do not care.
156 * Crazy as it is, that is what RFC 2453 requires.
162 if (new_tracelevel
< 2)
164 background
= _B_FALSE
;
167 case 'd': /* put in.routed in foreground */
168 background
= _B_FALSE
;
171 case 'F': /* minimal routes for SLIP */
173 p
= strchr(optarg
, ',');
175 n
= (int)strtoul(p
+1, &q
, 0);
176 if (*q
== '\0' && p
+1 != q
&&
177 n
<= HOPCNT_INFINITY
-1 && n
>= 1)
180 if (!getnet(optarg
, &p_net
, &p_mask
)) {
183 msglog(gettext("bad network; \"-F %s\""),
187 (void) memset(&parm
, 0, sizeof (parm
));
188 parm
.parm_net
= p_net
;
189 parm
.parm_mask
= p_mask
;
190 parm
.parm_d_metric
= n
;
191 cp
= insert_parm(&parm
);
193 msglog(gettext("bad -F: %s"), cp
);
197 (void) memset(&parm
, 0, sizeof (parm
));
198 parm
.parm_d_metric
= 1;
199 cp
= insert_parm(&parm
);
201 msglog(gettext("bad -g: %s"), cp
);
203 default_gateway
= _B_TRUE
;
206 case 'h': /* suppress extra host routes */
210 case 'm': /* advertise host route */
211 mhome
= _B_TRUE
; /* on multi-homed hosts */
214 case 'n': /* No-install mode */
215 no_install
= _B_TRUE
;
219 /* handle arbitrary parameters. */
222 logbad(_B_FALSE
, "strdup: %s",
223 rip_strerror(errno
));
224 cp
= parse_parms(q
, _B_FALSE
);
226 msglog(gettext("%1$s in \"-P %2$s\""), cp
,
233 supplier_set
= _B_TRUE
;
238 supplier_set
= _B_TRUE
;
241 case 'S': /* save-space option */
242 save_space
= _B_TRUE
;
250 /* display version */
252 msglog(gettext("version " IN_ROUTED_VERSION
));
256 /* display route changes to supplied logfile */
261 case 'z': /* increase debug-level */
272 if (tracename
== NULL
&& argc
>= 1) {
276 if (tracename
!= NULL
&& tracename
[0] == '\0')
278 if (vflag
&& tracename
== NULL
)
282 (void) fprintf(stderr
, gettext(
283 "usage: in.routed [-AdghmnqsStVvz] "
284 "[-T <tracefile>]\n"));
285 (void) fprintf(stderr
,
286 gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n"));
287 logbad(_B_FALSE
, gettext("excess arguments"));
289 if (geteuid() != 0) {
291 * Regular users are allowed to run in.routed for the
292 * sole purpose of obtaining the version number. In
293 * that case, exit(EXIT_SUCCESS) without complaining.
297 logbad(_B_FALSE
, gettext("requires UID 0"));
300 if (default_gateway
) {
301 if (supplier_set
&& !supplier
) {
302 msglog(gettext("-g and -q are incompatible"));
305 supplier_set
= _B_TRUE
;
309 if (signal(SIGALRM
, sigalrm
) == SIG_ERR
)
311 /* SIGHUP fatal during debugging */
313 if (signal(SIGHUP
, sigterm
) == SIG_ERR
)
315 if (signal(SIGTERM
, sigterm
) == SIG_ERR
)
317 if (signal(SIGINT
, sigterm
) == SIG_ERR
)
319 if (signal(SIGUSR1
, sigtrace_more
) == SIG_ERR
)
321 if (signal(SIGUSR2
, sigtrace_less
) == SIG_ERR
)
323 if (signal(SIGHUP
, sigtrace_dump
) == SIG_ERR
)
327 msglog("signal: %s", rip_strerror(sigerr
));
329 /* get into the background */
330 if (background
&& daemon(0, 0) < 0)
331 BADERR(_B_FALSE
, "daemon()");
333 /* Store our process id, blow away any existing file if it exists. */
334 if ((pidfp
= fopen(PATH_PID
, "w")) == NULL
) {
335 (void) fprintf(stderr
,
336 gettext("in.routed: unable to open " PATH_PID
": %s\n"),
339 (void) fprintf(pidfp
, "%ld\n", getpid());
340 (void) fclose(pidfp
);
341 (void) chmod(PATH_PID
, pidmode
);
344 srandom((int)(clk
.tv_sec
^ clk
.tv_usec
^ getpid()));
346 /* allocate the interface tables */
349 /* prepare socket connected to the kernel. */
350 rt_sock
= socket(PF_ROUTE
, SOCK_RAW
, AF_INET
);
352 BADERR(_B_TRUE
, "rt_sock = socket()");
353 if (fcntl(rt_sock
, F_SETFL
, O_NONBLOCK
) == -1)
354 logbad(_B_TRUE
, "fcntl(rt_sock) O_NONBLOCK: %s",
355 rip_strerror(errno
));
357 if (setsockopt(rt_sock
, SOL_SOCKET
, SO_USELOOPBACK
,
358 &off
, sizeof (off
)) < 0)
359 LOGERR("setsockopt(SO_USELOOPBACK,0)");
364 if (tracename
!= NULL
) {
365 (void) strlcpy(inittracename
, tracename
,
366 sizeof (inittracename
));
367 set_tracefile(inittracename
, "%s", -1);
369 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
374 /* initialize radix tree */
378 * Pick a random part of the second for our output to minimize
381 * Start broadcasting after hearing from other routers, and
382 * at a random time so a bunch of systems do not get synchronized
383 * after a power failure.
385 * Since now is the number of seconds since epoch (this is initially
386 * EPOCH seconds), these times are really relative to now.
388 intvl_random(&next_bcast
, EPOCH
+MIN_WAITTIME
, EPOCH
+SUPPLY_INTERVAL
);
389 age_timer
.tv_usec
= next_bcast
.tv_usec
;
390 age_timer
.tv_sec
= EPOCH
+MIN_WAITTIME
;
391 rdisc_timer
= next_bcast
;
392 ifscan_timer
.tv_usec
= next_bcast
.tv_usec
;
395 * Open the global rip socket. From now on, this socket can be
396 * assumed to be open. It will remain open until in.routed
399 rip_sock
= open_rip_sock();
402 * Collect an initial view of the world by checking the interface
403 * configuration and the kludge file.
405 * gwkludge() could call addroutefordefault(), resulting in a call to
406 * iflookup, and thus ifscan() to find the physical interfaces.
407 * ifscan() will attempt to use the rip_sock in order to join
408 * mcast groups, so gwkludge *must* be called after opening
419 /* Now turn off stdio if not tracing */
420 if (new_tracelevel
== 0)
421 trace_close(background
);
423 /* Loop until a fatal error occurs, listening and broadcasting. */
426 if (gettimeofday(&clk
, 0) == -1) {
427 logbad(_B_FALSE
, "gettimeofday: %s",
428 rip_strerror(errno
));
430 if (prev_clk
.tv_sec
== clk
.tv_sec
&&
431 prev_clk
.tv_usec
== clk
.tv_usec
+usec_fudge
) {
433 * Much of `in.routed` depends on time always advancing.
434 * On systems that do not guarantee that gettimeofday()
435 * produces unique timestamps even if called within
436 * a single tick, use trickery like that in classic
439 clk
.tv_usec
+= ++usec_fudge
;
446 timevalsub(&result
, &clk
, &prev_clk
);
447 if (result
.tv_sec
< 0 || result
.tv_sec
>
448 select_timeout
.tv_sec
+ 5) {
450 * Deal with time changes before other
451 * housekeeping to keep everything straight.
455 dt
-= select_timeout
.tv_sec
;
456 trace_act("time changed by %d sec", (int)dt
);
460 timevalsub(&now
, &clk
, &epoch
);
461 now_stale
= now
.tv_sec
- STALE_TIME
;
462 now_expire
= now
.tv_sec
- EXPIRE_TIME
;
463 now_garbage
= now
.tv_sec
- GARBAGE_TIME
;
465 /* deal with signals that should affect tracing */
469 trace_off("exiting with signal %d", stopint
);
473 /* look for new or dead interfaces */
474 timevalsub(&select_timeout
, &ifscan_timer
, &now
);
475 if (select_timeout
.tv_sec
<= 0) {
476 select_timeout
.tv_sec
= 0;
483 * Check the kernel table occassionally for mysteriously
486 timevalsub(&result
, &sync_kern_timer
, &now
);
487 if (result
.tv_sec
<= 0) {
489 sync_kern_timer
.tv_sec
= (now
.tv_sec
490 + CHECK_QUIET_INTERVAL
);
493 if (timercmp(&result
, &select_timeout
, < /* */))
494 select_timeout
= result
;
496 /* If it is time, then broadcast our routes. */
497 if (should_supply(NULL
) || advertise_mhome
) {
498 timevalsub(&result
, &next_bcast
, &now
);
499 if (result
.tv_sec
<= 0) {
501 * Synchronize the aging and broadcast
502 * timers to minimize awakenings
510 * It is desirable to send routing updates
511 * regularly. So schedule the next update
512 * 30 seconds after the previous one was
513 * scheduled, instead of 30 seconds after
514 * the previous update was finished.
515 * Even if we just started after discovering
516 * a 2nd interface or were otherwise delayed,
517 * pick a 30-second aniversary of the
518 * original broadcast time.
520 n
= 1 + (0-result
.tv_sec
)/SUPPLY_INTERVAL
;
521 next_bcast
.tv_sec
+= n
*SUPPLY_INTERVAL
;
526 if (timercmp(&result
, &select_timeout
, < /* */))
527 select_timeout
= result
;
531 * If we need a flash update, either do it now or
532 * set the delay to end when it is time.
534 * If we are within MIN_WAITTIME seconds of a full update,
537 if (need_flash
&& should_supply(NULL
) &&
538 no_flash
.tv_sec
+MIN_WAITTIME
< next_bcast
.tv_sec
) {
539 /* accurate to the millisecond */
540 if (!timercmp(&no_flash
, &now
, > /* */))
542 timevalsub(&result
, &no_flash
, &now
);
543 if (timercmp(&result
, &select_timeout
, < /* */))
544 select_timeout
= result
;
547 /* trigger the main aging timer. */
548 timevalsub(&result
, &age_timer
, &now
);
549 if (result
.tv_sec
<= 0) {
553 if (timercmp(&result
, &select_timeout
, < /* */))
554 select_timeout
= result
;
556 /* update the kernel routing table */
557 timevalsub(&result
, &need_kern
, &now
);
558 if (result
.tv_sec
<= 0) {
562 if (timercmp(&result
, &select_timeout
, < /* */))
563 select_timeout
= result
;
566 * take care of router discovery. We compare timeval
567 * structures here to have millisecond granularity.
569 if (!timercmp(&rdisc_timer
, &now
, > /* */)) {
573 timevalsub(&result
, &rdisc_timer
, &now
);
574 if (timercmp(&result
, &select_timeout
, < /* */))
575 select_timeout
= result
;
578 * Well-known bit of select(3c) silliness inherited
579 * from BSD: anything over 100 million seconds is
580 * considered an "error." Reset that to zero.
582 if (select_timeout
.tv_sec
> 100000000)
583 select_timeout
.tv_sec
= 0;
585 /* wait for input or a timer to expire. */
588 n
= select(sock_max
, &ibits
, 0, 0, &select_timeout
);
590 if (n
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
591 BADERR(_B_TRUE
, "select");
595 if (FD_ISSET(rt_sock
, &ibits
)) {
599 if (rdisc_sock
>= 0 && FD_ISSET(rdisc_sock
, &ibits
)) {
603 if (rdisc_mib_sock
>= 0 && FD_ISSET(rdisc_mib_sock
, &ibits
)) {
604 process_d_mib_sock();
607 if (rip_sock
>= 0 && FD_ISSET(rip_sock
, &ibits
)) {
608 if (read_rip() == -1) {
609 rip_enabled
= _B_FALSE
;
610 trace_off("main rip socket failed");
611 (void) close(rip_sock
);
621 (void) unlink(PATH_PID
);
622 return (stopint
| 128);
630 * Historically, SIGALRM would cause the daemon to check for
631 * new and broken interfaces.
633 ifscan_timer
.tv_sec
= now
.tv_sec
;
634 trace_act("SIGALRM");
635 if (signal(sig
, sigalrm
) == SIG_ERR
)
636 msglog("signal: %s", rip_strerror(errno
));
640 /* watch for fatal signals */
645 if (signal(sig
, SIG_DFL
) == SIG_ERR
) /* catch it only once */
646 msglog("signal: %s", rip_strerror(errno
));
653 (void) FD_ZERO(&fdbits
);
656 FD_SET(rt_sock
, &fdbits
);
657 if (sock_max
<= rt_sock
)
658 sock_max
= rt_sock
+1;
660 FD_SET(rip_sock
, &fdbits
);
661 if (sock_max
<= rip_sock
)
662 sock_max
= rip_sock
+1;
664 if (rdisc_sock
>= 0) {
665 FD_SET(rdisc_sock
, &fdbits
);
666 if (sock_max
<= rdisc_sock
)
667 sock_max
= rdisc_sock
+1;
668 FD_SET(rdisc_mib_sock
, &fdbits
);
669 if (sock_max
<= rdisc_mib_sock
)
670 sock_max
= rdisc_mib_sock
+1;
680 #define MIN_SOCKBUF (4*1024)
683 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) == -1)
684 logbad(_B_TRUE
, "fcntl(%s) O_NONBLOCK: %s", name
,
685 rip_strerror(errno
));
687 if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0)
688 msglog("setsockopt(%s,SO_BROADCAST): %s",
689 name
, rip_strerror(errno
));
691 if (rbuf
>= MIN_SOCKBUF
) {
692 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
693 &rbuf
, sizeof (rbuf
)) < 0)
694 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
695 name
, rbuf
, rip_strerror(errno
));
697 for (rbuf
= 60*1024; ; rbuf
-= 4096) {
698 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
699 &rbuf
, sizeof (rbuf
)) == 0) {
700 trace_act("RCVBUF=%d", rbuf
);
703 if (rbuf
< MIN_SOCKBUF
) {
704 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
705 name
, rbuf
, rip_strerror(errno
));
714 * Open and return the global rip socket. It is guaranteed to return
715 * a good file descriptor.
720 struct sockaddr_in sin
;
726 if ((s
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
727 BADERR(_B_TRUE
, "rip_sock = socket()");
729 (void) memset(&sin
, 0, sizeof (sin
));
730 sin
.sin_family
= AF_INET
;
731 sin
.sin_port
= htons(RIP_PORT
);
732 sin
.sin_addr
.s_addr
= INADDR_ANY
;
733 if (bind(s
, (struct sockaddr
*)&sin
, sizeof (sin
)) < 0) {
734 BADERR(_B_FALSE
, "bind(rip_sock)");
736 fix_sock(s
, "rip_sock");
739 if (setsockopt(s
, IPPROTO_IP
, IP_MULTICAST_TTL
,
740 &ttl
, sizeof (ttl
)) < 0)
741 DBGERR(_B_TRUE
, "rip_sock setsockopt(IP_MULTICAST_TTL)");
743 if (setsockopt(s
, IPPROTO_IP
, IP_RECVIF
, &on
, sizeof (on
)))
744 BADERR(_B_FALSE
, "setsockopt(IP_RECVIF)");
751 * Disable RIP. Note that we don't close the global rip socket since
752 * it is used even when RIP is disabled to receive and answer certain
759 struct interface
*ifp
;
760 char addrstr
[INET_ADDRSTRLEN
];
762 if (rip_enabled
&& !mhome
) {
763 trace_act("turn off RIP");
766 * Unsubscribe from the 224.0.0.9 RIP multicast
769 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
770 if ((ifp
->int_if_flags
& IFF_MULTICAST
) &&
771 !IS_IFF_QUIET(ifp
->int_if_flags
) &&
772 !IS_RIP_IN_OFF(ifp
->int_state
) &&
773 !(ifp
->int_state
& IS_DUP
)) {
774 m
.imr_multiaddr
.s_addr
=
775 htonl(INADDR_RIP_GROUP
);
776 m
.imr_interface
.s_addr
=
777 (ifp
->int_if_flags
& IFF_POINTOPOINT
) ?
778 ifp
->int_dstaddr
: ifp
->int_addr
;
779 (void) strlcpy(addrstr
,
780 inet_ntoa(m
.imr_multiaddr
),
782 if (setsockopt(rip_sock
, IPPROTO_IP
,
783 IP_DROP_MEMBERSHIP
, &m
,
785 errno
!= EADDRNOTAVAIL
&& errno
!= ENOENT
)
786 writelog(LOG_WARNING
,
787 "%s: setsockopt(IP_DROP_MEMBERSHIP "
788 "%s, %s): %s", ifp
->int_name
,
789 addrstr
, inet_ntoa(m
.imr_interface
),
790 rip_strerror(errno
));
793 rip_enabled
= _B_FALSE
;
800 /* turn on RIP multicast input via an interface */
802 rip_mcast_on(struct interface
*ifp
)
806 if (!IS_RIP_IN_OFF(ifp
->int_state
) &&
807 (ifp
->int_if_flags
& IFF_MULTICAST
) &&
808 !IS_IFF_QUIET(ifp
->int_if_flags
) &&
809 !(ifp
->int_state
& IS_DUP
)) {
810 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
811 m
.imr_interface
.s_addr
= (ifp
->int_if_flags
& IFF_POINTOPOINT
) ?
812 ifp
->int_dstaddr
: ifp
->int_addr
;
813 if ((setsockopt(rip_sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
814 &m
, sizeof (m
)) < 0) && !(ifp
->int_state
& IS_BROKE
))
815 writelog(LOG_WARNING
,
816 "Could not join 224.0.0.9 on interface %s: %s",
817 ifp
->int_name
, rip_strerror(errno
));
821 /* turn off RIP multicast input via an interface */
823 rip_mcast_off(struct interface
*ifp
)
827 if ((ifp
->int_if_flags
& IFF_MULTICAST
) &&
828 !IS_IFF_QUIET(ifp
->int_if_flags
) && rip_enabled
) {
829 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
830 m
.imr_interface
.s_addr
= (ifp
->int_if_flags
& IFF_POINTOPOINT
) ?
831 ifp
->int_dstaddr
: ifp
->int_addr
;
832 if ((setsockopt(rip_sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
,
833 &m
, sizeof (m
)) < 0) && errno
!= EADDRNOTAVAIL
&&
835 writelog(LOG_WARNING
,
836 "setsockopt(IP_DROP_MEMBERSHIP RIP) for %s: %s",
837 ifp
->int_name
, rip_strerror(errno
));
843 rip_on(struct interface
*ifp
)
846 * If RIP is already enabled, only start receiving
847 * multicasts for this interface.
856 * If RIP is disabled and it makes sense to enable it, then enable
857 * it on all of the interfaces. It makes sense if either router
858 * discovery is off, or if router discovery is on and at most one
859 * interface is doing RIP.
861 if (rip_interfaces
> 0 && (!rdisc_ok
|| rip_interfaces
> 1)) {
862 trace_act("turn on RIP");
864 rip_enabled
= _B_TRUE
;
865 rip_sock_interface
= NULL
;
867 /* Do not advertise anything until we have heard something */
868 if (next_bcast
.tv_sec
< now
.tv_sec
+MIN_WAITTIME
)
869 next_bcast
.tv_sec
= now
.tv_sec
+MIN_WAITTIME
;
871 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
872 ifp
->int_query_time
= NEVER
;
875 ifscan_timer
.tv_sec
= now
.tv_sec
;
882 /* die if malloc(3) fails */
884 rtmalloc(size_t size
,
887 void *p
= malloc(size
);
889 logbad(_B_TRUE
, "malloc(%lu) failed in %s: %s", (ulong_t
)size
,
890 msg
, rip_strerror(errno
));
895 /* get a random instant in an interval */
897 intvl_random(struct timeval
*tp
, /* put value here */
898 ulong_t lo
, /* value is after this second */
899 ulong_t hi
) /* and before this */
901 tp
->tv_sec
= (time_t)(hi
== lo
? lo
: (lo
+ random() % ((hi
- lo
))));
902 tp
->tv_usec
= random() % 1000000;
907 timevaladd(struct timeval
*t1
,
911 t1
->tv_sec
+= t2
->tv_sec
;
912 if ((t1
->tv_usec
+= t2
->tv_usec
) >= 1000000) {
914 t1
->tv_usec
-= 1000000;
921 timevalsub(struct timeval
*t1
,
925 t1
->tv_sec
= t2
->tv_sec
- t3
->tv_sec
;
926 if ((t1
->tv_usec
= t2
->tv_usec
- t3
->tv_usec
) < 0) {
928 t1
->tv_usec
+= 1000000;
935 openlog_done
= _B_TRUE
;
936 openlog("in.routed", LOG_PID
| LOG_ODELAY
, LOG_DAEMON
);
939 /* put a LOG_ERR message into the system log */
941 msglog(const char *p
, ...)
950 vsyslog(LOG_ERR
, p
, args
);
953 if (ftrace
== stdout
)
954 (void) fputs("in.routed: ", ftrace
);
955 (void) vfprintf(ftrace
, p
, args
);
956 (void) fputc('\n', ftrace
);
962 * Put a message about a bad system into the system log if
963 * we have not complained about it recently.
965 * It is desirable to complain about all bad systems, but not too often.
966 * In the worst case, it is not practical to keep track of all bad systems.
967 * For example, there can be many systems with the wrong password.
970 msglim(struct msg_limit
*lim
, in_addr_t addr
, const char *p
, ...)
974 struct msg_sub
*ms1
, *ms
;
980 * look for the oldest slot in the table
981 * or the slot for the bad router.
983 ms
= ms1
= lim
->subs
;
984 for (i
= MSG_SUBJECT_N
; ; i
--, ms1
++) {
986 /* Reuse a slot at most once every 10 minutes. */
987 if (lim
->reuse
> now
.tv_sec
) {
990 lim
->reuse
= now
.tv_sec
+ 10*60;
994 if (ms
->addr
== addr
) {
996 * Repeat a complaint about a given system at
999 if (ms
->until
> now
.tv_sec
)
1003 if (ms
->until
< ms1
->until
)
1008 ms
->until
= now
.tv_sec
+ 60*60; /* 60 minutes */
1013 for (p1
= p
; *p1
== ' '; p1
++)
1015 vsyslog(LOG_ERR
, p1
, args
);
1018 /* always display the message if tracing */
1020 (void) vfprintf(ftrace
, p
, args
);
1021 (void) fputc('\n', ftrace
);
1027 logbad(boolean_t dump
, const char *p
, ...)
1036 vsyslog(LOG_ERR
, p
, args
);
1038 (void) fputs(gettext("in.routed: "), stderr
);
1039 (void) vfprintf(stderr
, p
, args
);
1040 (void) fputs(gettext("; giving up\n"), stderr
);
1041 (void) fflush(stderr
);
1048 /* put a message into the system log */
1050 writelog(int level
, const char *p
, ...)
1059 vsyslog(level
, p
, args
);
1062 if (ftrace
== stdout
)
1063 (void) fputs("in.routed: ", ftrace
);
1064 (void) vfprintf(ftrace
, p
, args
);
1065 (void) fputc('\n', ftrace
);