main.c revision 1.16 1 /* $NetBSD: main.c,v 1.16 1997/02/03 22:02:58 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 acknowledgement:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 char copyright[] =
37 "@(#) Copyright (c) 1983, 1988, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
40 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/5/93";
41 #elif defined(__NetBSD__)
42 static char rcsid[] = "$NetBSD: main.c,v 1.16 1997/02/03 22:02:58 christos Exp $";
43 #endif
44
45 #include "defs.h"
46 #include "pathnames.h"
47 #ifdef sgi
48 #include "math.h"
49 #endif
50 #include <signal.h>
51 #include <fcntl.h>
52 #include <sys/file.h>
53
54 pid_t mypid;
55
56 naddr myaddr; /* system address */
57 char myname[MAXHOSTNAMELEN+1];
58
59 int supplier; /* supply or broadcast updates */
60 int supplier_set;
61 int ipforwarding = 1; /* kernel forwarding on */
62
63 int default_gateway; /* 1=advertise default */
64 int background = 1;
65 int ridhosts; /* 1=reduce host routes */
66 int mhome; /* 1=want multi-homed host route */
67 int advertise_mhome; /* 1=must continue adverising it */
68 int auth_ok = 1; /* 1=ignore auth if we do not care */
69
70 struct timeval epoch; /* when started */
71 struct timeval clk, prev_clk;
72 struct timeval now; /* current idea of time */
73 time_t now_stale;
74 time_t now_expire;
75 time_t now_garbage;
76
77 struct timeval next_bcast; /* next general broadcast */
78 struct timeval no_flash = {EPOCH+SUPPLY_INTERVAL}; /* inhibit flash update */
79
80 fd_set fdbits;
81 int sock_max;
82 int rip_sock = -1; /* RIP socket */
83 struct interface *rip_sock_mcast; /* current multicast interface */
84 int rt_sock; /* routing socket */
85 int rt_sock_seqno;
86
87
88 static int get_rip_sock(naddr, int);
89 static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
90
91 int
92 main(int argc,
93 char *argv[])
94 {
95 int n, mib[4], off;
96 size_t len;
97 char *p, *q;
98 struct timeval wtime, t2;
99 time_t dt;
100 fd_set ibits;
101 naddr p_net, p_mask;
102 struct interface *ifp;
103 struct parm parm;
104 char *tracename = 0;
105
106
107 /* Some shells are badly broken and send SIGHUP to backgrounded
108 * processes.
109 */
110 signal(SIGHUP, SIG_IGN);
111
112 openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
113 ftrace = stdout;
114
115 gettimeofday(&clk, 0);
116 prev_clk = clk;
117 epoch = clk;
118 epoch.tv_sec -= EPOCH;
119 now.tv_sec = EPOCH;
120 now_stale = EPOCH - STALE_TIME;
121 now_expire = EPOCH - EXPIRE_TIME;
122 now_garbage = EPOCH - GARBAGE_TIME;
123 wtime.tv_sec = 0;
124
125 (void)gethostname(myname, sizeof(myname)-1);
126 (void)gethost(myname, &myaddr);
127
128 while ((n = getopt(argc, argv, "sqdghmpAtT:F:P:")) != EOF) {
129 switch (n) {
130 case 's':
131 supplier = 1;
132 supplier_set = 1;
133 break;
134
135 case 'q':
136 supplier = 0;
137 supplier_set = 1;
138 break;
139
140 case 'd':
141 background = 0;
142 break;
143
144 case 'g':
145 bzero(&parm, sizeof(parm));
146 parm.parm_d_metric = 1;
147 p = check_parms(&parm);
148 if (p != 0)
149 msglog("bad -g: %s", p);
150 else
151 default_gateway = 1;
152 break;
153
154 case 'h': /* suppress extra host routes */
155 ridhosts = 1;
156 break;
157
158 case 'm': /* advertise host route */
159 mhome = 1; /* on multi-homed hosts */
160 break;
161
162 case 'A':
163 /* Ignore authentication if we do not care.
164 * Crazy as it is, that is what RFC 1723 requires.
165 */
166 auth_ok = 0;
167 break;
168
169 case 't':
170 new_tracelevel++;
171 break;
172
173 case 'T':
174 tracename = optarg;
175 break;
176
177 case 'F': /* minimal routes for SLIP */
178 n = FAKE_METRIC;
179 p = strchr(optarg,',');
180 if (p && *p != '\0') {
181 n = (int)strtoul(p+1, &q, 0);
182 if (*q == '\0'
183 && n <= HOPCNT_INFINITY-1
184 && n >= 1)
185 *p = '\0';
186 }
187 if (!getnet(optarg, &p_net, &p_mask)) {
188 msglog("bad network; \"-F %s\"",
189 optarg);
190 break;
191 }
192 bzero(&parm, sizeof(parm));
193 parm.parm_net = p_net;
194 parm.parm_mask = p_mask;
195 parm.parm_d_metric = n;
196 p = check_parms(&parm);
197 if (p != 0)
198 msglog("bad -F: %s", p);
199 break;
200
201 case 'P':
202 /* handle arbirary, (usually) per-interface
203 * parameters.
204 */
205 p = parse_parms(optarg, 0);
206 if (p != 0) {
207 if (strcasecmp(p,optarg))
208 msglog("%s in \"%s\"", p, optarg);
209 else
210 msglog("bad \"-P %s\"", optarg);
211 }
212 break;
213
214 default:
215 goto usage;
216 }
217 }
218 argc -= optind;
219 argv += optind;
220
221 if (tracename == 0 && argc >= 1) {
222 tracename = *argv++;
223 argc--;
224 }
225 if (tracename != 0 && tracename[0] == '\0')
226 goto usage;
227 if (argc != 0) {
228 usage:
229 logbad(0, "usage: routed [-sqdghmpAt] [-T tracefile]"
230 " [-F net[,metric]] [-P parms]");
231 }
232 if (geteuid() != 0)
233 logbad(0, "requires UID 0");
234
235 mib[0] = CTL_NET;
236 mib[1] = PF_INET;
237 mib[2] = IPPROTO_IP;
238 mib[3] = IPCTL_FORWARDING;
239 len = sizeof(ipforwarding);
240 if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0)
241 LOGERR("sysctl(IPCTL_FORWARDING)");
242
243 if (!ipforwarding) {
244 if (supplier)
245 msglog("-s incompatible with ipforwarding=0");
246 if (default_gateway) {
247 msglog("-g incompatible with ipforwarding=0");
248 default_gateway = 0;
249 }
250 supplier = 0;
251 supplier_set = 1;
252 }
253 if (default_gateway) {
254 if (supplier_set && !supplier) {
255 msglog("-g and -q incompatible");
256 } else {
257 supplier = 1;
258 supplier_set = 1;
259 }
260 }
261
262
263 signal(SIGALRM, sigalrm);
264 if (!background)
265 signal(SIGHUP, sigterm); /* SIGHUP fatal during debugging */
266 signal(SIGTERM, sigterm);
267 signal(SIGINT, sigterm);
268 signal(SIGUSR1, sigtrace_on);
269 signal(SIGUSR2, sigtrace_off);
270
271 /* get into the background */
272 #ifdef sgi
273 if (0 > _daemonize(background ? 0 : (_DF_NOCHDIR|_DF_NOFORK),
274 new_tracelevel == 0 ? -1 : STDOUT_FILENO,
275 new_tracelevel == 0 ? -1 : STDERR_FILENO,
276 -1))
277 BADERR(0, "_daemonize()");
278 #else
279 if (background && daemon(0, new_tracelevel) < 0)
280 BADERR(0,"daemon()");
281 #endif
282
283 mypid = getpid();
284 srandom((int)(clk.tv_sec ^ clk.tv_usec ^ mypid));
285
286 /* prepare socket connected to the kernel.
287 */
288 rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
289 if (rt_sock < 0)
290 BADERR(1,"rt_sock = socket()");
291 if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
292 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
293 off = 0;
294 if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
295 &off,sizeof(off)) < 0)
296 LOGERR("setsockopt(SO_USELOOPBACK,0)");
297
298 fix_select();
299
300
301 if (background && new_tracelevel == 0)
302 ftrace = 0;
303 if (tracename != 0) {
304 strncpy(inittracename, tracename, sizeof(inittracename)-1);
305 set_tracefile(inittracename, "%s", -1);
306 } else {
307 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
308 }
309
310 bufinit();
311
312 /* initialize radix tree */
313 rtinit();
314
315 /* Pick a random part of the second for our output to minimize
316 * collisions.
317 *
318 * Start broadcasting after hearing from other routers, and
319 * at a random time so a bunch of systems do not get synchronized
320 * after a power failure.
321 */
322 intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
323 age_timer.tv_usec = next_bcast.tv_usec;
324 age_timer.tv_sec = EPOCH+MIN_WAITTIME;
325 rdisc_timer = next_bcast;
326 ifinit_timer.tv_usec = next_bcast.tv_usec;
327
328 /* Collect an initial view of the world by checking the interface
329 * configuration and the kludge file.
330 */
331 gwkludge();
332 ifinit();
333 flush_kern();
334
335 /* Ask for routes */
336 rip_query();
337 rdisc_sol();
338
339 /* Loop forever, listening and broadcasting.
340 */
341 for (;;) {
342 prev_clk = clk;
343 gettimeofday(&clk, 0);
344 timevalsub(&t2, &clk, &prev_clk);
345 if (t2.tv_sec < 0
346 || t2.tv_sec > wtime.tv_sec + 5) {
347 /* Deal with time changes before other housekeeping to
348 * keep everything straight.
349 */
350 dt = t2.tv_sec;
351 if (dt > 0)
352 dt -= wtime.tv_sec;
353 trace_act("time changed by %d sec", dt);
354 epoch.tv_sec += dt;
355 }
356 timevalsub(&now, &clk, &epoch);
357 now_stale = now.tv_sec - STALE_TIME;
358 now_expire = now.tv_sec - EXPIRE_TIME;
359 now_garbage = now.tv_sec - GARBAGE_TIME;
360
361 /* deal with signals that should affect tracing */
362 set_tracelevel();
363
364 if (stopint != 0) {
365 rip_bcast(0);
366 rdisc_adv();
367 trace_off("exiting with signal %d", stopint);
368 exit(stopint | 128);
369 }
370
371 /* look for new or dead interfaces */
372 timevalsub(&wtime, &ifinit_timer, &now);
373 if (wtime.tv_sec <= 0) {
374 wtime.tv_sec = 0;
375 ifinit();
376 rip_query();
377 continue;
378 }
379
380 /* If it is time, then broadcast our routes.
381 */
382 if (supplier || advertise_mhome) {
383 timevalsub(&t2, &next_bcast, &now);
384 if (t2.tv_sec <= 0) {
385 /* Synchronize the aging and broadcast
386 * timers to minimize awakenings
387 */
388 age(0);
389
390 rip_bcast(0);
391
392 /* It is desirable to send routing updates
393 * regularly. So schedule the next update
394 * 30 seconds after the previous one was
395 * secheduled, instead of 30 seconds after
396 * the previous update was finished.
397 * Even if we just started after discovering
398 * a 2nd interface or were otherwise delayed,
399 * pick a 30-second aniversary of the
400 * original broadcast time.
401 */
402 n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
403 next_bcast.tv_sec += n*SUPPLY_INTERVAL;
404
405 continue;
406 }
407
408 if (timercmp(&t2, &wtime, <))
409 wtime = t2;
410 }
411
412 /* If we need a flash update, either do it now or
413 * set the delay to end when it is time.
414 *
415 * If we are within MIN_WAITTIME seconds of a full update,
416 * do not bother.
417 */
418 if (need_flash
419 && supplier
420 && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
421 /* accurate to the millisecond */
422 if (!timercmp(&no_flash, &now, >))
423 rip_bcast(1);
424 timevalsub(&t2, &no_flash, &now);
425 if (timercmp(&t2, &wtime, <))
426 wtime = t2;
427 }
428
429 /* trigger the main aging timer.
430 */
431 timevalsub(&t2, &age_timer, &now);
432 if (t2.tv_sec <= 0) {
433 age(0);
434 continue;
435 }
436 if (timercmp(&t2, &wtime, <))
437 wtime = t2;
438
439 /* update the kernel routing table
440 */
441 timevalsub(&t2, &need_kern, &now);
442 if (t2.tv_sec <= 0) {
443 age(0);
444 continue;
445 }
446 if (timercmp(&t2, &wtime, <))
447 wtime = t2;
448
449 /* take care of router discovery,
450 * but do it to the millisecond
451 */
452 if (!timercmp(&rdisc_timer, &now, >)) {
453 rdisc_age(0);
454 continue;
455 }
456 timevalsub(&t2, &rdisc_timer, &now);
457 if (timercmp(&t2, &wtime, <))
458 wtime = t2;
459
460
461 /* wait for input or a timer to expire.
462 */
463 trace_flush();
464 ibits = fdbits;
465 n = select(sock_max, &ibits, 0, 0, &wtime);
466 if (n <= 0) {
467 if (n < 0 && errno != EINTR && errno != EAGAIN)
468 BADERR(1,"select");
469 continue;
470 }
471
472 if (FD_ISSET(rt_sock, &ibits)) {
473 read_rt();
474 n--;
475 }
476 if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
477 read_d();
478 n--;
479 }
480 if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
481 read_rip(rip_sock, 0);
482 n--;
483 }
484
485 for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) {
486 if (ifp->int_rip_sock >= 0
487 && FD_ISSET(ifp->int_rip_sock, &ibits)) {
488 read_rip(ifp->int_rip_sock, ifp);
489 n--;
490 }
491 }
492 }
493 }
494
495
496 /* ARGSUSED */
497 void
498 sigalrm(int s)
499 {
500 /* Historically, SIGALRM would cause the daemon to check for
501 * new and broken interfaces.
502 */
503 ifinit_timer.tv_sec = now.tv_sec;
504 trace_act("SIGALRM");
505 }
506
507
508 /* watch for fatal signals */
509 void
510 sigterm(int sig)
511 {
512 stopint = sig;
513 (void)signal(sig, SIG_DFL); /* catch it only once */
514 }
515
516
517 void
518 fix_select(void)
519 {
520 struct interface *ifp;
521
522
523 FD_ZERO(&fdbits);
524 sock_max = 0;
525
526 FD_SET(rt_sock, &fdbits);
527 if (sock_max <= rt_sock)
528 sock_max = rt_sock+1;
529 if (rip_sock >= 0) {
530 FD_SET(rip_sock, &fdbits);
531 if (sock_max <= rip_sock)
532 sock_max = rip_sock+1;
533 }
534 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
535 if (ifp->int_rip_sock >= 0) {
536 FD_SET(ifp->int_rip_sock, &fdbits);
537 if (sock_max <= ifp->int_rip_sock)
538 sock_max = ifp->int_rip_sock+1;
539 }
540 }
541 if (rdisc_sock >= 0) {
542 FD_SET(rdisc_sock, &fdbits);
543 if (sock_max <= rdisc_sock)
544 sock_max = rdisc_sock+1;
545 }
546 }
547
548
549 void
550 fix_sock(int sock,
551 char *name)
552 {
553 int on;
554 #define MIN_SOCKBUF (4*1024)
555 static int rbuf;
556
557 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
558 logbad(1, "fcntl(%s) O_NONBLOCK: %s",
559 name, strerror(errno));
560 on = 1;
561 if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST, &on,sizeof(on)) < 0)
562 msglog("setsockopt(%s,SO_BROADCAST): %s",
563 name, strerror(errno));
564 #ifdef USE_PASSIFNAME
565 on = 1;
566 if (setsockopt(sock, SOL_SOCKET, SO_PASSIFNAME, &on,sizeof(on)) < 0)
567 msglog("setsockopt(%s,SO_PASSIFNAME): %s",
568 name, strerror(errno));
569 #endif
570
571 if (rbuf >= MIN_SOCKBUF) {
572 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
573 &rbuf, sizeof(rbuf)) < 0)
574 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
575 name, rbuf, strerror(errno));
576 } else {
577 for (rbuf = 60*1024; ; rbuf -= 4096) {
578 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
579 &rbuf, sizeof(rbuf)) == 0) {
580 trace_act("RCVBUF=%d", rbuf);
581 break;
582 }
583 if (rbuf < MIN_SOCKBUF) {
584 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
585 name, rbuf, strerror(errno));
586 break;
587 }
588 }
589 }
590 }
591
592
593 /* get a rip socket
594 */
595 static int /* <0 or file descriptor */
596 get_rip_sock(naddr addr,
597 int serious) /* 1=failure to bind is serious */
598 {
599 struct sockaddr_in sin;
600 unsigned char ttl;
601 int s;
602
603
604 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
605 BADERR(1,"rip_sock = socket()");
606
607 bzero(&sin,sizeof(sin));
608 #ifdef _HAVE_SIN_LEN
609 sin.sin_len = sizeof(sin);
610 #endif
611 sin.sin_family = AF_INET;
612 sin.sin_port = htons(RIP_PORT);
613 sin.sin_addr.s_addr = addr;
614 if (bind(s, (struct sockaddr *)&sin,sizeof(sin)) < 0) {
615 if (serious)
616 BADERR(errno != EADDRINUSE, "bind(rip_sock)");
617 return -1;
618 }
619 fix_sock(s,"rip_sock");
620
621 ttl = 1;
622 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
623 &ttl, sizeof(ttl)) < 0)
624 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
625
626 return s;
627 }
628
629
630 /* turn off main RIP socket */
631 void
632 rip_off(void)
633 {
634 struct interface *ifp;
635 register naddr addr;
636
637
638 if (rip_sock >= 0 && !mhome) {
639 trace_act("turn off RIP");
640
641 (void)close(rip_sock);
642 rip_sock = -1;
643
644 /* get non-broadcast sockets to listen to queries.
645 */
646 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
647 if (ifp->int_state & IS_REMOTE)
648 continue;
649 if (ifp->int_rip_sock < 0) {
650 addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
651 ? ifp->int_dstaddr
652 : ifp->int_addr);
653 ifp->int_rip_sock = get_rip_sock(addr, 0);
654 }
655 }
656
657 fix_select();
658
659 age(0);
660 }
661 }
662
663
664 /* turn on RIP multicast input via an interface
665 */
666 static void
667 rip_mcast_on(struct interface *ifp)
668 {
669 struct ip_mreq m;
670
671 if (!IS_RIP_IN_OFF(ifp->int_state)
672 && (ifp->int_if_flags & IFF_MULTICAST)
673 #ifdef MCAST_PPP_BUG
674 && !(ifp->int_if_flags & IFF_POINTOPOINT)
675 #endif
676 && !(ifp->int_state & IS_ALIAS)) {
677 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
678 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
679 ? ifp->int_dstaddr
680 : ifp->int_addr);
681 if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP,
682 &m, sizeof(m)) < 0)
683 LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
684 }
685 }
686
687
688 /* Prepare socket used for RIP.
689 */
690 void
691 rip_on(struct interface *ifp)
692 {
693 /* If the main RIP socket is already alive, only start receiving
694 * multicasts for this interface.
695 */
696 if (rip_sock >= 0) {
697 if (ifp != 0)
698 rip_mcast_on(ifp);
699 return;
700 }
701
702 /* If the main RIP socket is off and it makes sense to turn it on,
703 * then turn it on for all of the interfaces.
704 */
705 if (rip_interfaces > 0 && !rdisc_ok) {
706 trace_act("turn on RIP");
707
708 /* Close all of the query sockets so that we can open
709 * the main socket. SO_REUSEPORT is not a solution,
710 * since that would let two daemons bind to the broadcast
711 * socket.
712 */
713 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
714 if (ifp->int_rip_sock >= 0) {
715 (void)close(ifp->int_rip_sock);
716 ifp->int_rip_sock = -1;
717 }
718 }
719
720 rip_sock = get_rip_sock(INADDR_ANY, 1);
721 rip_sock_mcast = 0;
722
723 /* Do not advertise anything until we have heard something
724 */
725 if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
726 next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
727
728 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
729 ifp->int_query_time = NEVER;
730 rip_mcast_on(ifp);
731 }
732 ifinit_timer.tv_sec = now.tv_sec;
733
734 } else if (ifp != 0
735 && !(ifp->int_state & IS_REMOTE)
736 && ifp->int_rip_sock < 0) {
737 /* RIP is off, so ensure there are sockets on which
738 * to listen for queries.
739 */
740 ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
741 }
742
743 fix_select();
744 }
745
746
747 /* die if malloc(3) fails
748 */
749 void *
750 rtmalloc(size_t size,
751 char *msg)
752 {
753 void *p = malloc(size);
754 if (p == 0)
755 logbad(1,"malloc() failed in %s", msg);
756 return p;
757 }
758
759
760 /* get a random instant in an interval
761 */
762 void
763 intvl_random(struct timeval *tp, /* put value here */
764 u_long lo, /* value is after this second */
765 u_long hi) /* and before this */
766 {
767 tp->tv_sec = (time_t)(hi == lo
768 ? lo
769 : (lo + random() % ((hi - lo))));
770 tp->tv_usec = random() % 1000000;
771 }
772
773
774 void
775 timevaladd(struct timeval *t1,
776 struct timeval *t2)
777 {
778
779 t1->tv_sec += t2->tv_sec;
780 if ((t1->tv_usec += t2->tv_usec) > 1000000) {
781 t1->tv_sec++;
782 t1->tv_usec -= 1000000;
783 }
784 }
785
786
787 /* t1 = t2 - t3
788 */
789 static void
790 timevalsub(struct timeval *t1,
791 struct timeval *t2,
792 struct timeval *t3)
793 {
794 t1->tv_sec = t2->tv_sec - t3->tv_sec;
795 if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
796 t1->tv_sec--;
797 t1->tv_usec += 1000000;
798 }
799 }
800
801
802 /* put a message into the system log
803 */
804 void
805 msglog(char *p, ...)
806 {
807 va_list args;
808
809 trace_flush();
810
811 va_start(args, p);
812 vsyslog(LOG_ERR, p, args);
813
814 if (ftrace != 0) {
815 if (ftrace == stdout)
816 (void)fputs("routed: ", ftrace);
817 (void)vfprintf(ftrace, p, args);
818 (void)fputc('\n', ftrace);
819 }
820 }
821
822
823 /* Put a message about a bad system into the system log if
824 * we have not complained about it recently.
825 *
826 * It is desirable to complain about all bad systems, but not too often.
827 * In the worst case, it is not practical to keep track of all bad systems.
828 * For example, there can be many systems with the wrong password.
829 */
830 void
831 msglim(struct msg_limit *lim, naddr addr, char *p, ...)
832 {
833 va_list args;
834 int i;
835 struct msg_sub *ms1, *ms;
836 char *p1;
837
838 va_start(args, p);
839
840 /* look for the oldest slot in the table
841 * or the slot for the bad router.
842 */
843 ms = ms1 = lim->subs;
844 for (i = MSG_SUBJECT_N; ; i--, ms1++) {
845 if (i == 0) {
846 /* Reuse a slot at most once every 10 minutes.
847 */
848 if (lim->reuse > now.tv_sec) {
849 ms = 0;
850 } else {
851 ms = ms1;
852 lim->reuse = now.tv_sec + 10*60;
853 }
854 break;
855 }
856 if (ms->addr == addr) {
857 /* Repeat a complaint about a given system at
858 * most once an hour.
859 */
860 if (ms->until > now.tv_sec)
861 ms = 0;
862 break;
863 }
864 if (ms->until < ms1->until)
865 ms = ms1;
866 }
867 if (ms != 0) {
868 ms->addr = addr;
869 ms->until = now.tv_sec + 60*60; /* 60 minutes */
870
871 trace_flush();
872 for (p1 = p; *p1 == ' '; p1++)
873 continue;
874 vsyslog(LOG_ERR, p1, args);
875 }
876
877 /* always display the message if tracing */
878 if (ftrace != 0) {
879 (void)vfprintf(ftrace, p, args);
880 (void)fputc('\n', ftrace);
881 }
882 }
883
884
885 void
886 logbad(int dump, char *p, ...)
887 {
888 va_list args;
889
890 trace_flush();
891
892 va_start(args, p);
893 vsyslog(LOG_ERR, p, args);
894
895 (void)fputs("routed: ", stderr);
896 (void)vfprintf(stderr, p, args);
897 (void)fputs("; giving up\n",stderr);
898 (void)fflush(stderr);
899
900 if (dump)
901 abort();
902 exit(1);
903 }
904