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