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