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