main.c revision 1.23 1 /* $NetBSD: main.c,v 1.23 1999/06/06 02:00:32 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 acknowledgment:
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 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
37 static char sccsid[] __attribute__((unused)) = "@(#)main.c 8.1 (Berkeley) 6/5/93";
38 #define __COPYRIGHT(a) char copyright[] = a;
39 #elif defined(__NetBSD__)
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: main.c,v 1.23 1999/06/06 02:00:32 thorpej Exp $");
42 #endif
43 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\
44 The Regents of the University of California. All rights reserved.\n");
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
55 #if defined(__NetBSD__)
56 #include <util.h>
57 #endif
58
59 pid_t mypid;
60
61 naddr myaddr; /* system address */
62 char myname[MAXHOSTNAMELEN+1];
63
64 int verbose;
65
66 int supplier; /* supply or broadcast updates */
67 int supplier_set;
68 int ipforwarding = 1; /* kernel forwarding on */
69
70 int default_gateway; /* 1=advertise default */
71 int background = 1;
72 int ridhosts; /* 1=reduce host routes */
73 int mhome; /* 1=want multi-homed host route */
74 int advertise_mhome; /* 1=must continue advertising it */
75 int auth_ok = 1; /* 1=ignore auth if we do not care */
76
77 struct timeval epoch; /* when started */
78 struct timeval clk, prev_clk;
79 struct timeval now; /* current idea of time */
80 time_t now_stale;
81 time_t now_expire;
82 time_t now_garbage;
83
84 struct timeval next_bcast; /* next general broadcast */
85 struct timeval no_flash = {EPOCH+SUPPLY_INTERVAL}; /* inhibit flash update */
86
87 struct timeval flush_kern_timer;
88
89 fd_set fdbits;
90 int sock_max;
91 int rip_sock = -1; /* RIP socket */
92 struct interface *rip_sock_mcast; /* current multicast interface */
93 int rt_sock; /* routing socket */
94 int rt_sock_seqno;
95
96
97 static int get_rip_sock(naddr, int);
98 static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
99
100 int
101 main(int argc,
102 char *argv[])
103 {
104 int n, mib[4], off;
105 size_t len;
106 char *p, *q;
107 const char *cp;
108 struct timeval wtime, t2;
109 time_t dt;
110 fd_set ibits;
111 naddr p_net, p_mask;
112 struct interface *ifp;
113 struct parm parm;
114 char *tracename = 0;
115
116
117 /* Some shells are badly broken and send SIGHUP to backgrounded
118 * processes.
119 */
120 signal(SIGHUP, SIG_IGN);
121
122 openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
123 ftrace = stdout;
124
125 gettimeofday(&clk, 0);
126 prev_clk = clk;
127 epoch = clk;
128 epoch.tv_sec -= EPOCH;
129 now.tv_sec = EPOCH;
130 now_stale = EPOCH - STALE_TIME;
131 now_expire = EPOCH - EXPIRE_TIME;
132 now_garbage = EPOCH - GARBAGE_TIME;
133 wtime.tv_sec = 0;
134
135 (void)gethostname(myname, sizeof(myname));
136 myname[sizeof(myname) - 1] = '\0';
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 cp = check_parms(&parm);
159 if (cp != 0)
160 msglog("bad -g: %s", cp);
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 cp = check_parms(&parm);
208 if (cp != 0)
209 msglog("bad -F: %s", cp);
210 break;
211
212 case 'P':
213 /* handle arbitrary parameters.
214 */
215 q = strdup(optarg);
216 cp = parse_parms(q, 0);
217 if (cp != 0)
218 msglog("%s in \"-P %s\"", cp, optarg);
219 free(q);
220 break;
221
222 case 'v':
223 /* display version */
224 verbose++;
225 msglog("version 2.16");
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 #if defined(__NetBSD__)
299 pidfile(NULL);
300 #endif
301 mypid = getpid();
302 srandom((int)(clk.tv_sec ^ clk.tv_usec ^ mypid));
303
304 /* prepare socket connected to the kernel.
305 */
306 rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
307 if (rt_sock < 0)
308 BADERR(1,"rt_sock = socket()");
309 if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
310 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
311 off = 0;
312 if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
313 &off,sizeof(off)) < 0)
314 LOGERR("setsockopt(SO_USELOOPBACK,0)");
315
316 fix_select();
317
318
319 if (tracename != 0) {
320 strncpy(inittracename, tracename, sizeof(inittracename)-1);
321 set_tracefile(inittracename, "%s", -1);
322 } else {
323 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
324 }
325
326 bufinit();
327
328 /* initialize radix tree */
329 rtinit();
330
331 /* Pick a random part of the second for our output to minimize
332 * collisions.
333 *
334 * Start broadcasting after hearing from other routers, and
335 * at a random time so a bunch of systems do not get synchronized
336 * after a power failure.
337 */
338 intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
339 age_timer.tv_usec = next_bcast.tv_usec;
340 age_timer.tv_sec = EPOCH+MIN_WAITTIME;
341 rdisc_timer = next_bcast;
342 ifinit_timer.tv_usec = next_bcast.tv_usec;
343
344 /* Collect an initial view of the world by checking the interface
345 * configuration and the kludge file.
346 */
347 gwkludge();
348 ifinit();
349
350 /* Ask for routes */
351 rip_query();
352 rdisc_sol();
353
354 /* Now turn off stdio if not tracing */
355 if (new_tracelevel == 0)
356 trace_close(background);
357
358 /* Loop forever, listening and broadcasting.
359 */
360 for (;;) {
361 prev_clk = clk;
362 gettimeofday(&clk, 0);
363 timevalsub(&t2, &clk, &prev_clk);
364 if (t2.tv_sec < 0
365 || t2.tv_sec > wtime.tv_sec + 5) {
366 /* Deal with time changes before other housekeeping to
367 * keep everything straight.
368 */
369 dt = t2.tv_sec;
370 if (dt > 0)
371 dt -= wtime.tv_sec;
372 trace_act("time changed by %d sec", (int)dt);
373 epoch.tv_sec += dt;
374 }
375 timevalsub(&now, &clk, &epoch);
376 now_stale = now.tv_sec - STALE_TIME;
377 now_expire = now.tv_sec - EXPIRE_TIME;
378 now_garbage = now.tv_sec - GARBAGE_TIME;
379
380 /* deal with signals that should affect tracing */
381 set_tracelevel();
382
383 if (stopint != 0) {
384 rip_bcast(0);
385 rdisc_adv();
386 trace_off("exiting with signal %d", stopint);
387 exit(stopint | 128);
388 }
389
390 /* look for new or dead interfaces */
391 timevalsub(&wtime, &ifinit_timer, &now);
392 if (wtime.tv_sec <= 0) {
393 wtime.tv_sec = 0;
394 ifinit();
395 rip_query();
396 continue;
397 }
398
399 /* Check the kernel table occassionally for mysteriously
400 * evaporated routes
401 */
402 timevalsub(&t2, &flush_kern_timer, &now);
403 if (t2.tv_sec <= 0) {
404 flush_kern();
405 flush_kern_timer.tv_sec = (now.tv_sec
406 + CHECK_QUIET_INTERVAL);
407 continue;
408 }
409 if (timercmp(&t2, &wtime, <))
410 wtime = t2;
411
412 /* If it is time, then broadcast our routes.
413 */
414 if (supplier || advertise_mhome) {
415 timevalsub(&t2, &next_bcast, &now);
416 if (t2.tv_sec <= 0) {
417 /* Synchronize the aging and broadcast
418 * timers to minimize awakenings
419 */
420 age(0);
421
422 rip_bcast(0);
423
424 /* It is desirable to send routing updates
425 * regularly. So schedule the next update
426 * 30 seconds after the previous one was
427 * scheduled, instead of 30 seconds after
428 * the previous update was finished.
429 * Even if we just started after discovering
430 * a 2nd interface or were otherwise delayed,
431 * pick a 30-second aniversary of the
432 * original broadcast time.
433 */
434 n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
435 next_bcast.tv_sec += n*SUPPLY_INTERVAL;
436
437 continue;
438 }
439
440 if (timercmp(&t2, &wtime, <))
441 wtime = t2;
442 }
443
444 /* If we need a flash update, either do it now or
445 * set the delay to end when it is time.
446 *
447 * If we are within MIN_WAITTIME seconds of a full update,
448 * do not bother.
449 */
450 if (need_flash
451 && supplier
452 && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
453 /* accurate to the millisecond */
454 if (!timercmp(&no_flash, &now, >))
455 rip_bcast(1);
456 timevalsub(&t2, &no_flash, &now);
457 if (timercmp(&t2, &wtime, <))
458 wtime = t2;
459 }
460
461 /* trigger the main aging timer.
462 */
463 timevalsub(&t2, &age_timer, &now);
464 if (t2.tv_sec <= 0) {
465 age(0);
466 continue;
467 }
468 if (timercmp(&t2, &wtime, <))
469 wtime = t2;
470
471 /* update the kernel routing table
472 */
473 timevalsub(&t2, &need_kern, &now);
474 if (t2.tv_sec <= 0) {
475 age(0);
476 continue;
477 }
478 if (timercmp(&t2, &wtime, <))
479 wtime = t2;
480
481 /* take care of router discovery,
482 * but do it in the correct the millisecond
483 */
484 if (!timercmp(&rdisc_timer, &now, >)) {
485 rdisc_age(0);
486 continue;
487 }
488 timevalsub(&t2, &rdisc_timer, &now);
489 if (timercmp(&t2, &wtime, <))
490 wtime = t2;
491
492
493 /* wait for input or a timer to expire.
494 */
495 trace_flush();
496 ibits = fdbits;
497 n = select(sock_max, &ibits, 0, 0, &wtime);
498 if (n <= 0) {
499 if (n < 0 && errno != EINTR && errno != EAGAIN)
500 BADERR(1,"select");
501 continue;
502 }
503
504 if (FD_ISSET(rt_sock, &ibits)) {
505 read_rt();
506 n--;
507 }
508 if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
509 read_d();
510 n--;
511 }
512 if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
513 read_rip(rip_sock, 0);
514 n--;
515 }
516
517 for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) {
518 if (ifp->int_rip_sock >= 0
519 && FD_ISSET(ifp->int_rip_sock, &ibits)) {
520 read_rip(ifp->int_rip_sock, ifp);
521 n--;
522 }
523 }
524 }
525 }
526
527
528 /* ARGSUSED */
529 void
530 sigalrm(int s UNUSED)
531 {
532 /* Historically, SIGALRM would cause the daemon to check for
533 * new and broken interfaces.
534 */
535 ifinit_timer.tv_sec = now.tv_sec;
536 trace_act("SIGALRM");
537 }
538
539
540 /* watch for fatal signals */
541 void
542 sigterm(int sig)
543 {
544 stopint = sig;
545 (void)signal(sig, SIG_DFL); /* catch it only once */
546 }
547
548
549 void
550 fix_select(void)
551 {
552 struct interface *ifp;
553
554
555 FD_ZERO(&fdbits);
556 sock_max = 0;
557
558 FD_SET(rt_sock, &fdbits);
559 if (sock_max <= rt_sock)
560 sock_max = rt_sock+1;
561 if (rip_sock >= 0) {
562 FD_SET(rip_sock, &fdbits);
563 if (sock_max <= rip_sock)
564 sock_max = rip_sock+1;
565 }
566 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
567 if (ifp->int_rip_sock >= 0) {
568 FD_SET(ifp->int_rip_sock, &fdbits);
569 if (sock_max <= ifp->int_rip_sock)
570 sock_max = ifp->int_rip_sock+1;
571 }
572 }
573 if (rdisc_sock >= 0) {
574 FD_SET(rdisc_sock, &fdbits);
575 if (sock_max <= rdisc_sock)
576 sock_max = rdisc_sock+1;
577 }
578 }
579
580
581 void
582 fix_sock(int sock,
583 const char *name)
584 {
585 int on;
586 #define MIN_SOCKBUF (4*1024)
587 static int rbuf;
588
589 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
590 logbad(1, "fcntl(%s) O_NONBLOCK: %s",
591 name, strerror(errno));
592 on = 1;
593 if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST, &on,sizeof(on)) < 0)
594 msglog("setsockopt(%s,SO_BROADCAST): %s",
595 name, strerror(errno));
596 #ifdef USE_PASSIFNAME
597 on = 1;
598 if (setsockopt(sock, SOL_SOCKET, SO_PASSIFNAME, &on,sizeof(on)) < 0)
599 msglog("setsockopt(%s,SO_PASSIFNAME): %s",
600 name, strerror(errno));
601 #endif
602
603 if (rbuf >= MIN_SOCKBUF) {
604 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
605 &rbuf, sizeof(rbuf)) < 0)
606 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
607 name, rbuf, strerror(errno));
608 } else {
609 for (rbuf = 60*1024; ; rbuf -= 4096) {
610 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
611 &rbuf, sizeof(rbuf)) == 0) {
612 trace_act("RCVBUF=%d", rbuf);
613 break;
614 }
615 if (rbuf < MIN_SOCKBUF) {
616 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
617 name, rbuf, strerror(errno));
618 break;
619 }
620 }
621 }
622 }
623
624
625 /* get a rip socket
626 */
627 static int /* <0 or file descriptor */
628 get_rip_sock(naddr addr,
629 int serious) /* 1=failure to bind is serious */
630 {
631 struct sockaddr_in sin;
632 unsigned char ttl;
633 int s;
634
635
636 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
637 BADERR(1,"rip_sock = socket()");
638
639 memset(&sin, 0, sizeof(sin));
640 #ifdef _HAVE_SIN_LEN
641 sin.sin_len = sizeof(sin);
642 #endif
643 sin.sin_family = AF_INET;
644 sin.sin_port = htons(RIP_PORT);
645 sin.sin_addr.s_addr = addr;
646 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
647 if (serious)
648 BADERR(errno != EADDRINUSE, "bind(rip_sock)");
649 return -1;
650 }
651 fix_sock(s,"rip_sock");
652
653 ttl = 1;
654 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
655 &ttl, sizeof(ttl)) < 0)
656 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
657
658 return s;
659 }
660
661
662 /* turn off main RIP socket */
663 void
664 rip_off(void)
665 {
666 struct interface *ifp;
667 naddr addr;
668
669
670 if (rip_sock >= 0 && !mhome) {
671 trace_act("turn off RIP");
672
673 (void)close(rip_sock);
674 rip_sock = -1;
675
676 /* get non-broadcast sockets to listen to queries.
677 */
678 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
679 if (ifp->int_state & IS_REMOTE)
680 continue;
681 if (ifp->int_rip_sock < 0) {
682 addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
683 ? ifp->int_dstaddr
684 : ifp->int_addr);
685 ifp->int_rip_sock = get_rip_sock(addr, 0);
686 }
687 }
688
689 fix_select();
690
691 age(0);
692 }
693 }
694
695
696 /* turn on RIP multicast input via an interface
697 */
698 static void
699 rip_mcast_on(struct interface *ifp)
700 {
701 struct ip_mreq m;
702
703 if (!IS_RIP_IN_OFF(ifp->int_state)
704 && (ifp->int_if_flags & IFF_MULTICAST)
705 #ifdef MCAST_PPP_BUG
706 && !(ifp->int_if_flags & IFF_POINTOPOINT)
707 #endif
708 && !(ifp->int_state & IS_ALIAS)) {
709 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
710 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
711 ? ifp->int_dstaddr
712 : ifp->int_addr);
713 if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP,
714 &m, sizeof(m)) < 0)
715 LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
716 }
717 }
718
719
720 /* Prepare socket used for RIP.
721 */
722 void
723 rip_on(struct interface *ifp)
724 {
725 /* If the main RIP socket is already alive, only start receiving
726 * multicasts for this interface.
727 */
728 if (rip_sock >= 0) {
729 if (ifp != 0)
730 rip_mcast_on(ifp);
731 return;
732 }
733
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.
738 */
739 if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
740 trace_act("turn on RIP");
741
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
745 * socket.
746 */
747 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
748 if (ifp->int_rip_sock >= 0) {
749 (void)close(ifp->int_rip_sock);
750 ifp->int_rip_sock = -1;
751 }
752 }
753
754 rip_sock = get_rip_sock(INADDR_ANY, 1);
755 rip_sock_mcast = 0;
756
757 /* Do not advertise anything until we have heard something
758 */
759 if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
760 next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
761
762 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
763 ifp->int_query_time = NEVER;
764 rip_mcast_on(ifp);
765 }
766 ifinit_timer.tv_sec = now.tv_sec;
767
768 } else if (ifp != 0
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.
773 */
774 ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
775 }
776
777 fix_select();
778 }
779
780
781 /* die if malloc(3) fails
782 */
783 void *
784 rtmalloc(size_t size,
785 const char *msg)
786 {
787 void *p = malloc(size);
788 if (p == 0)
789 logbad(1,"malloc(%ld) failed in %s", (long)size, msg);
790 return p;
791 }
792
793
794 /* get a random instant in an interval
795 */
796 void
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 */
800 {
801 tp->tv_sec = (time_t)(hi == lo
802 ? lo
803 : (lo + random() % ((hi - lo))));
804 tp->tv_usec = random() % 1000000;
805 }
806
807
808 void
809 timevaladd(struct timeval *t1,
810 struct timeval *t2)
811 {
812
813 t1->tv_sec += t2->tv_sec;
814 if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
815 t1->tv_sec++;
816 t1->tv_usec -= 1000000;
817 }
818 }
819
820
821 /* t1 = t2 - t3
822 */
823 static void
824 timevalsub(struct timeval *t1,
825 struct timeval *t2,
826 struct timeval *t3)
827 {
828 t1->tv_sec = t2->tv_sec - t3->tv_sec;
829 if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
830 t1->tv_sec--;
831 t1->tv_usec += 1000000;
832 }
833 }
834
835
836 /* put a message into the system log
837 */
838 void
839 msglog(const char *p, ...)
840 {
841 va_list args;
842
843 trace_flush();
844
845 va_start(args, p);
846 vsyslog(LOG_ERR, p, args);
847
848 if (ftrace != 0) {
849 if (ftrace == stdout)
850 (void)fputs("routed: ", ftrace);
851 (void)vfprintf(ftrace, p, args);
852 (void)fputc('\n', ftrace);
853 }
854 }
855
856
857 /* Put a message about a bad system into the system log if
858 * we have not complained about it recently.
859 *
860 * It is desirable to complain about all bad systems, but not too often.
861 * In the worst case, it is not practical to keep track of all bad systems.
862 * For example, there can be many systems with the wrong password.
863 */
864 void
865 msglim(struct msg_limit *lim, naddr addr, const char *p, ...)
866 {
867 va_list args;
868 int i;
869 struct msg_sub *ms1, *ms;
870 const char *p1;
871
872 va_start(args, p);
873
874 /* look for the oldest slot in the table
875 * or the slot for the bad router.
876 */
877 ms = ms1 = lim->subs;
878 for (i = MSG_SUBJECT_N; ; i--, ms1++) {
879 if (i == 0) {
880 /* Reuse a slot at most once every 10 minutes.
881 */
882 if (lim->reuse > now.tv_sec) {
883 ms = 0;
884 } else {
885 ms = ms1;
886 lim->reuse = now.tv_sec + 10*60;
887 }
888 break;
889 }
890 if (ms->addr == addr) {
891 /* Repeat a complaint about a given system at
892 * most once an hour.
893 */
894 if (ms->until > now.tv_sec)
895 ms = 0;
896 break;
897 }
898 if (ms->until < ms1->until)
899 ms = ms1;
900 }
901 if (ms != 0) {
902 ms->addr = addr;
903 ms->until = now.tv_sec + 60*60; /* 60 minutes */
904
905 trace_flush();
906 for (p1 = p; *p1 == ' '; p1++)
907 continue;
908 vsyslog(LOG_ERR, p1, args);
909 }
910
911 /* always display the message if tracing */
912 if (ftrace != 0) {
913 (void)vfprintf(ftrace, p, args);
914 (void)fputc('\n', ftrace);
915 }
916 }
917
918
919 void
920 logbad(int dump, const char *p, ...)
921 {
922 va_list args;
923
924 trace_flush();
925
926 va_start(args, p);
927 vsyslog(LOG_ERR, p, args);
928
929 (void)fputs("routed: ", stderr);
930 (void)vfprintf(stderr, p, args);
931 (void)fputs("; giving up\n",stderr);
932 (void)fflush(stderr);
933
934 if (dump)
935 abort();
936 exit(1);
937 }
938