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