1*2e9df72eSandvar /* $NetBSD: main.c,v 1.43 2022/04/07 19:33:37 andvar Exp $ */
20114e805Scgd
361f28255Scgd /*
44c8599d3Smycroft * Copyright (c) 1983, 1988, 1993
54c8599d3Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1561f28255Scgd * 3. All advertising materials mentioning features or use of this software
1694b2d428Schristos * must display the following acknowledgment:
1761f28255Scgd * This product includes software developed by the University of
1861f28255Scgd * California, Berkeley and its contributors.
1961f28255Scgd * 4. Neither the name of the University nor the names of its contributors
2061f28255Scgd * may be used to endorse or promote products derived from this software
2161f28255Scgd * without specific prior written permission.
2261f28255Scgd *
2361f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361f28255Scgd * SUCH DAMAGE.
3461f28255Scgd */
3561f28255Scgd
3661f28255Scgd #include "defs.h"
37fc1a5246Sthorpej #include "pathnames.h"
38cad376bdSchristos #if defined(__NetBSD__)
39cad376bdSchristos #include <util.h>
40cad376bdSchristos #endif
41fc1a5246Sthorpej #include <signal.h>
42fc1a5246Sthorpej #include <fcntl.h>
4361f28255Scgd #include <sys/file.h>
4461f28255Scgd
456543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
466543a91fSlukem The Regents of the University of California. All rights reserved.");
474fea751dSchristos #ifdef __NetBSD__
48*2e9df72eSandvar __RCSID("$NetBSD: main.c,v 1.43 2022/04/07 19:33:37 andvar Exp $");
494fea751dSchristos #elif defined(__FreeBSD__)
504fea751dSchristos __RCSID("$FreeBSD$");
514fea751dSchristos #else
52f93fe60aSchristos __RCSID("Revision: 2.27 ");
53f93fe60aSchristos #ident "Revision: 2.27 "
544fea751dSchristos #endif
554fea751dSchristos
56fc1a5246Sthorpej pid_t mypid;
5761f28255Scgd
58fc1a5246Sthorpej naddr myaddr; /* system address */
59fc1a5246Sthorpej char myname[MAXHOSTNAMELEN+1];
6061f28255Scgd
616d8ef4dfSthorpej int verbose;
626d8ef4dfSthorpej
63fc1a5246Sthorpej int supplier; /* supply or broadcast updates */
64fc1a5246Sthorpej int supplier_set;
65fc1a5246Sthorpej int ipforwarding = 1; /* kernel forwarding on */
6661f28255Scgd
67fc1a5246Sthorpej int default_gateway; /* 1=advertise default */
68fc1a5246Sthorpej int background = 1;
69fc1a5246Sthorpej int ridhosts; /* 1=reduce host routes */
70fc1a5246Sthorpej int mhome; /* 1=want multi-homed host route */
7194b2d428Schristos int advertise_mhome; /* 1=must continue advertising it */
72fc1a5246Sthorpej int auth_ok = 1; /* 1=ignore auth if we do not care */
73fc1a5246Sthorpej
74fc1a5246Sthorpej struct timeval epoch; /* when started */
75fc1a5246Sthorpej struct timeval clk, prev_clk;
76cce4e6d1Schristos static int usec_fudge;
77fc1a5246Sthorpej struct timeval now; /* current idea of time */
78fc1a5246Sthorpej time_t now_stale;
794d3fba59Schristos time_t now_expire;
80fc1a5246Sthorpej time_t now_garbage;
81fc1a5246Sthorpej
82fc1a5246Sthorpej struct timeval next_bcast; /* next general broadcast */
834fea751dSchristos struct timeval no_flash = { /* inhibit flash update */
844fea751dSchristos EPOCH+SUPPLY_INTERVAL, 0
854fea751dSchristos };
86fc1a5246Sthorpej
876d8ef4dfSthorpej struct timeval flush_kern_timer;
886d8ef4dfSthorpej
89d9be8318Sitojun fd_set *fdbitsp;
90fc1a5246Sthorpej int sock_max;
91fc1a5246Sthorpej int rip_sock = -1; /* RIP socket */
92fc1a5246Sthorpej struct interface *rip_sock_mcast; /* current multicast interface */
93fc1a5246Sthorpej int rt_sock; /* routing socket */
94fc1a5246Sthorpej int rt_sock_seqno;
95fc1a5246Sthorpej
96fc1a5246Sthorpej
97fc1a5246Sthorpej static int get_rip_sock(naddr, int);
98fc1a5246Sthorpej static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
99e072e2aeScgd
100e072e2aeScgd int
main(int argc,char * argv[])101fc1a5246Sthorpej main(int argc,
102fc1a5246Sthorpej char *argv[])
10361f28255Scgd {
104fc1a5246Sthorpej int n, mib[4], off;
105fc1a5246Sthorpej size_t len;
106fc1a5246Sthorpej char *p, *q;
107756b1291Schristos const char *cp;
108fc1a5246Sthorpej struct timeval wtime, t2;
109fc1a5246Sthorpej time_t dt;
110b3f17521Senami fd_set *ibitsp = NULL;
111e7512e5aSchristos naddr p_net, p_mask;
112fc1a5246Sthorpej struct interface *ifp;
113fc1a5246Sthorpej struct parm parm;
114fc1a5246Sthorpej char *tracename = 0;
11561f28255Scgd
116fc1a5246Sthorpej
117e7512e5aSchristos /* Some shells are badly broken and send SIGHUP to backgrounded
118e7512e5aSchristos * processes.
119e7512e5aSchristos */
120e7512e5aSchristos signal(SIGHUP, SIG_IGN);
121e7512e5aSchristos
122790602fdSlukem openlog("routed", LOG_PID, LOG_DAEMON);
123fc1a5246Sthorpej ftrace = stdout;
124fc1a5246Sthorpej
125fc1a5246Sthorpej gettimeofday(&clk, 0);
126fc1a5246Sthorpej prev_clk = clk;
127fc1a5246Sthorpej epoch = clk;
128fc1a5246Sthorpej epoch.tv_sec -= EPOCH;
129fc1a5246Sthorpej now.tv_sec = EPOCH;
130fc1a5246Sthorpej now_stale = EPOCH - STALE_TIME;
1314d3fba59Schristos now_expire = EPOCH - EXPIRE_TIME;
132fc1a5246Sthorpej now_garbage = EPOCH - GARBAGE_TIME;
133fc1a5246Sthorpej wtime.tv_sec = 0;
134fc1a5246Sthorpej
135cce4e6d1Schristos (void)gethostname(myname, sizeof(myname) - 1);
136fc1a5246Sthorpej (void)gethost(myname, &myaddr);
137fc1a5246Sthorpej
1380a1db662Swiz while ((n = getopt(argc, argv, "sqdghmAtvT:F:P:")) != -1) {
139fc1a5246Sthorpej switch (n) {
140fc1a5246Sthorpej case 's':
14161f28255Scgd supplier = 1;
142fc1a5246Sthorpej supplier_set = 1;
143fc1a5246Sthorpej break;
14461f28255Scgd
145fc1a5246Sthorpej case 'q':
14661f28255Scgd supplier = 0;
147fc1a5246Sthorpej supplier_set = 1;
148fc1a5246Sthorpej break;
149fc1a5246Sthorpej
150fc1a5246Sthorpej case 'd':
151fc1a5246Sthorpej background = 0;
152fc1a5246Sthorpej break;
153fc1a5246Sthorpej
154fc1a5246Sthorpej case 'g':
1553f50343aSlukem memset(&parm, 0, sizeof(parm));
156fc1a5246Sthorpej parm.parm_d_metric = 1;
157756b1291Schristos cp = check_parms(&parm);
158756b1291Schristos if (cp != 0)
159756b1291Schristos msglog("bad -g: %s", cp);
16061f28255Scgd else
161fc1a5246Sthorpej default_gateway = 1;
162fc1a5246Sthorpej break;
16361f28255Scgd
164fc1a5246Sthorpej case 'h': /* suppress extra host routes */
165fc1a5246Sthorpej ridhosts = 1;
166fc1a5246Sthorpej break;
167fc1a5246Sthorpej
168fc1a5246Sthorpej case 'm': /* advertise host route */
169fc1a5246Sthorpej mhome = 1; /* on multi-homed hosts */
170fc1a5246Sthorpej break;
171fc1a5246Sthorpej
172fc1a5246Sthorpej case 'A':
173fc1a5246Sthorpej /* Ignore authentication if we do not care.
174fc1a5246Sthorpej * Crazy as it is, that is what RFC 1723 requires.
17561f28255Scgd */
176fc1a5246Sthorpej auth_ok = 0;
177fc1a5246Sthorpej break;
178fc1a5246Sthorpej
179fc1a5246Sthorpej case 't':
180fc1a5246Sthorpej new_tracelevel++;
181fc1a5246Sthorpej break;
182fc1a5246Sthorpej
183fc1a5246Sthorpej case 'T':
184fc1a5246Sthorpej tracename = optarg;
185fc1a5246Sthorpej break;
186fc1a5246Sthorpej
187fc1a5246Sthorpej case 'F': /* minimal routes for SLIP */
188e7512e5aSchristos n = FAKE_METRIC;
189fc1a5246Sthorpej p = strchr(optarg,',');
190fc1a5246Sthorpej if (p && *p != '\0') {
191fc1a5246Sthorpej n = (int)strtoul(p+1, &q, 0);
192fc1a5246Sthorpej if (*q == '\0'
193fc1a5246Sthorpej && n <= HOPCNT_INFINITY-1
194fc1a5246Sthorpej && n >= 1)
195fc1a5246Sthorpej *p = '\0';
19661f28255Scgd }
197e7512e5aSchristos if (!getnet(optarg, &p_net, &p_mask)) {
198fc1a5246Sthorpej msglog("bad network; \"-F %s\"",
199fc1a5246Sthorpej optarg);
200fc1a5246Sthorpej break;
201fc1a5246Sthorpej }
2023f50343aSlukem memset(&parm, 0, sizeof(parm));
203e7512e5aSchristos parm.parm_net = p_net;
204fc1a5246Sthorpej parm.parm_mask = p_mask;
205fc1a5246Sthorpej parm.parm_d_metric = n;
206756b1291Schristos cp = check_parms(&parm);
207756b1291Schristos if (cp != 0)
208756b1291Schristos msglog("bad -F: %s", cp);
209fc1a5246Sthorpej break;
210fc1a5246Sthorpej
211fc1a5246Sthorpej case 'P':
2126d8ef4dfSthorpej /* handle arbitrary parameters.
21361f28255Scgd */
2146d8ef4dfSthorpej q = strdup(optarg);
215756b1291Schristos cp = parse_parms(q, 0);
216756b1291Schristos if (cp != 0)
217756b1291Schristos msglog("%s in \"-P %s\"", cp, optarg);
2186d8ef4dfSthorpej free(q);
2196d8ef4dfSthorpej break;
2206d8ef4dfSthorpej
2216d8ef4dfSthorpej case 'v':
2226d8ef4dfSthorpej /* display version */
2236d8ef4dfSthorpej verbose++;
224cad376bdSchristos msglog("version 2.32");
225fc1a5246Sthorpej break;
226fc1a5246Sthorpej
227fc1a5246Sthorpej default:
228fc1a5246Sthorpej goto usage;
22961f28255Scgd }
23061f28255Scgd }
231fc1a5246Sthorpej argc -= optind;
232fc1a5246Sthorpej argv += optind;
233fc1a5246Sthorpej
234fc1a5246Sthorpej if (tracename == 0 && argc >= 1) {
235fc1a5246Sthorpej tracename = *argv++;
236fc1a5246Sthorpej argc--;
23761f28255Scgd }
238e7512e5aSchristos if (tracename != 0 && tracename[0] == '\0')
239e7512e5aSchristos goto usage;
240fc1a5246Sthorpej if (argc != 0) {
241fc1a5246Sthorpej usage:
242a63a57aaSwiz logbad(0, "usage: routed [-sqdghmAtv] [-T tracefile]"
243de7136f8Swiz " [-F net[/mask[,metric]]] [-P parms]");
244fc1a5246Sthorpej }
2456d8ef4dfSthorpej if (geteuid() != 0) {
2466d8ef4dfSthorpej if (verbose)
2476d8ef4dfSthorpej exit(0);
248fc1a5246Sthorpej logbad(0, "requires UID 0");
2496d8ef4dfSthorpej }
250fc1a5246Sthorpej
251fc1a5246Sthorpej mib[0] = CTL_NET;
252fc1a5246Sthorpej mib[1] = PF_INET;
253fc1a5246Sthorpej mib[2] = IPPROTO_IP;
254fc1a5246Sthorpej mib[3] = IPCTL_FORWARDING;
255fc1a5246Sthorpej len = sizeof(ipforwarding);
256fc1a5246Sthorpej if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0)
257fc1a5246Sthorpej LOGERR("sysctl(IPCTL_FORWARDING)");
258fc1a5246Sthorpej
259fc1a5246Sthorpej if (!ipforwarding) {
260fc1a5246Sthorpej if (supplier)
261fc1a5246Sthorpej msglog("-s incompatible with ipforwarding=0");
262fc1a5246Sthorpej if (default_gateway) {
263fc1a5246Sthorpej msglog("-g incompatible with ipforwarding=0");
264fc1a5246Sthorpej default_gateway = 0;
265fc1a5246Sthorpej }
266fc1a5246Sthorpej supplier = 0;
267fc1a5246Sthorpej supplier_set = 1;
268fc1a5246Sthorpej }
269fc1a5246Sthorpej if (default_gateway) {
270fc1a5246Sthorpej if (supplier_set && !supplier) {
271fc1a5246Sthorpej msglog("-g and -q incompatible");
272fc1a5246Sthorpej } else {
273fc1a5246Sthorpej supplier = 1;
274fc1a5246Sthorpej supplier_set = 1;
275fc1a5246Sthorpej }
276fc1a5246Sthorpej }
277fc1a5246Sthorpej
278fc1a5246Sthorpej
2794d3fba59Schristos signal(SIGALRM, sigalrm);
2804d3fba59Schristos if (!background)
2814d3fba59Schristos signal(SIGHUP, sigterm); /* SIGHUP fatal during debugging */
2824d3fba59Schristos signal(SIGTERM, sigterm);
2834d3fba59Schristos signal(SIGINT, sigterm);
2844d3fba59Schristos signal(SIGUSR1, sigtrace_on);
2854d3fba59Schristos signal(SIGUSR2, sigtrace_off);
2864d3fba59Schristos
287fc1a5246Sthorpej /* get into the background */
2886d8ef4dfSthorpej if (background && daemon(0, 1) < 0)
289fc1a5246Sthorpej BADERR(0,"daemon()");
290fc1a5246Sthorpej
291eb2854e1Sthorpej #if defined(__NetBSD__)
292eb2854e1Sthorpej pidfile(NULL);
293eb2854e1Sthorpej #endif
294fc1a5246Sthorpej mypid = getpid();
295fc1a5246Sthorpej
296fc1a5246Sthorpej /* prepare socket connected to the kernel.
297fc1a5246Sthorpej */
298756b1291Schristos rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
299fc1a5246Sthorpej if (rt_sock < 0)
300fc1a5246Sthorpej BADERR(1,"rt_sock = socket()");
301fc1a5246Sthorpej if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
302fc1a5246Sthorpej logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
303fc1a5246Sthorpej off = 0;
304fc1a5246Sthorpej if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
305fc1a5246Sthorpej &off,sizeof(off)) < 0)
306fc1a5246Sthorpej LOGERR("setsockopt(SO_USELOOPBACK,0)");
307fc1a5246Sthorpej
308fc1a5246Sthorpej fix_select();
309fc1a5246Sthorpej
310fc1a5246Sthorpej
311fc1a5246Sthorpej if (tracename != 0) {
31216435cdbSitojun strlcpy(inittracename, tracename, sizeof(inittracename));
313e7512e5aSchristos set_tracefile(inittracename, "%s", -1);
314e7512e5aSchristos } else {
315e7512e5aSchristos tracelevel_msg("%s", -1); /* turn on tracing to stdio */
316fc1a5246Sthorpej }
317e7512e5aSchristos
318e7512e5aSchristos bufinit();
319fc1a5246Sthorpej
320fc1a5246Sthorpej /* initialize radix tree */
321fc1a5246Sthorpej rtinit();
322fc1a5246Sthorpej
323fc1a5246Sthorpej /* Pick a random part of the second for our output to minimize
324fc1a5246Sthorpej * collisions.
325fc1a5246Sthorpej *
326fc1a5246Sthorpej * Start broadcasting after hearing from other routers, and
327fc1a5246Sthorpej * at a random time so a bunch of systems do not get synchronized
328fc1a5246Sthorpej * after a power failure.
329fc1a5246Sthorpej */
330fc1a5246Sthorpej intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
331fc1a5246Sthorpej age_timer.tv_usec = next_bcast.tv_usec;
332fc1a5246Sthorpej age_timer.tv_sec = EPOCH+MIN_WAITTIME;
333fc1a5246Sthorpej rdisc_timer = next_bcast;
334fc1a5246Sthorpej ifinit_timer.tv_usec = next_bcast.tv_usec;
335fc1a5246Sthorpej
336fc1a5246Sthorpej /* Collect an initial view of the world by checking the interface
337fc1a5246Sthorpej * configuration and the kludge file.
338fc1a5246Sthorpej */
339fc1a5246Sthorpej gwkludge();
340fc1a5246Sthorpej ifinit();
341fc1a5246Sthorpej
342fc1a5246Sthorpej /* Ask for routes */
343fc1a5246Sthorpej rip_query();
344fc1a5246Sthorpej rdisc_sol();
345fc1a5246Sthorpej
3466d8ef4dfSthorpej /* Now turn off stdio if not tracing */
3476d8ef4dfSthorpej if (new_tracelevel == 0)
3486d8ef4dfSthorpej trace_close(background);
3496d8ef4dfSthorpej
350fc1a5246Sthorpej /* Loop forever, listening and broadcasting.
351fc1a5246Sthorpej */
352fc1a5246Sthorpej for (;;) {
353fc1a5246Sthorpej prev_clk = clk;
354fc1a5246Sthorpej gettimeofday(&clk, 0);
355cce4e6d1Schristos if (prev_clk.tv_sec == clk.tv_sec
356cce4e6d1Schristos && prev_clk.tv_usec == clk.tv_usec+usec_fudge) {
357cce4e6d1Schristos /* Much of `routed` depends on time always advancing.
358cce4e6d1Schristos * On systems that do not guarantee that gettimeofday()
359cce4e6d1Schristos * produces unique timestamps even if called within
360cce4e6d1Schristos * a single tick, use trickery like that in classic
361cce4e6d1Schristos * BSD kernels.
362cce4e6d1Schristos */
363cce4e6d1Schristos clk.tv_usec += ++usec_fudge;
364cce4e6d1Schristos
365cce4e6d1Schristos } else {
366cce4e6d1Schristos usec_fudge = 0;
367cce4e6d1Schristos
368fc1a5246Sthorpej timevalsub(&t2, &clk, &prev_clk);
369fc1a5246Sthorpej if (t2.tv_sec < 0
370fc1a5246Sthorpej || t2.tv_sec > wtime.tv_sec + 5) {
371cce4e6d1Schristos /* Deal with time changes before other
372cce4e6d1Schristos * housekeeping to keep everything straight.
373fc1a5246Sthorpej */
374fc1a5246Sthorpej dt = t2.tv_sec;
375fc1a5246Sthorpej if (dt > 0)
376fc1a5246Sthorpej dt -= wtime.tv_sec;
377756b1291Schristos trace_act("time changed by %d sec", (int)dt);
378fc1a5246Sthorpej epoch.tv_sec += dt;
379fc1a5246Sthorpej }
380cce4e6d1Schristos }
381fc1a5246Sthorpej timevalsub(&now, &clk, &epoch);
382fc1a5246Sthorpej now_stale = now.tv_sec - STALE_TIME;
3834d3fba59Schristos now_expire = now.tv_sec - EXPIRE_TIME;
384fc1a5246Sthorpej now_garbage = now.tv_sec - GARBAGE_TIME;
385fc1a5246Sthorpej
386e7512e5aSchristos /* deal with signals that should affect tracing */
387fc1a5246Sthorpej set_tracelevel();
388fc1a5246Sthorpej
389fc1a5246Sthorpej if (stopint != 0) {
390fc1a5246Sthorpej rip_bcast(0);
391fc1a5246Sthorpej rdisc_adv();
392e7512e5aSchristos trace_off("exiting with signal %d", stopint);
393fc1a5246Sthorpej exit(stopint | 128);
394fc1a5246Sthorpej }
395fc1a5246Sthorpej
396fc1a5246Sthorpej /* look for new or dead interfaces */
397fc1a5246Sthorpej timevalsub(&wtime, &ifinit_timer, &now);
398fc1a5246Sthorpej if (wtime.tv_sec <= 0) {
399fc1a5246Sthorpej wtime.tv_sec = 0;
400fc1a5246Sthorpej ifinit();
401fc1a5246Sthorpej rip_query();
402fc1a5246Sthorpej continue;
403fc1a5246Sthorpej }
404fc1a5246Sthorpej
405*2e9df72eSandvar /* Check the kernel table occasionally for mysteriously
4066d8ef4dfSthorpej * evaporated routes
4076d8ef4dfSthorpej */
4086d8ef4dfSthorpej timevalsub(&t2, &flush_kern_timer, &now);
4096d8ef4dfSthorpej if (t2.tv_sec <= 0) {
4106d8ef4dfSthorpej flush_kern();
4116d8ef4dfSthorpej flush_kern_timer.tv_sec = (now.tv_sec
4126d8ef4dfSthorpej + CHECK_QUIET_INTERVAL);
4136d8ef4dfSthorpej continue;
4146d8ef4dfSthorpej }
4156d8ef4dfSthorpej if (timercmp(&t2, &wtime, <))
4166d8ef4dfSthorpej wtime = t2;
4176d8ef4dfSthorpej
418fc1a5246Sthorpej /* If it is time, then broadcast our routes.
419fc1a5246Sthorpej */
420fc1a5246Sthorpej if (supplier || advertise_mhome) {
421fc1a5246Sthorpej timevalsub(&t2, &next_bcast, &now);
422fc1a5246Sthorpej if (t2.tv_sec <= 0) {
423fc1a5246Sthorpej /* Synchronize the aging and broadcast
424fc1a5246Sthorpej * timers to minimize awakenings
425fc1a5246Sthorpej */
426fc1a5246Sthorpej age(0);
427fc1a5246Sthorpej
428fc1a5246Sthorpej rip_bcast(0);
429fc1a5246Sthorpej
430fc1a5246Sthorpej /* It is desirable to send routing updates
431fc1a5246Sthorpej * regularly. So schedule the next update
432fc1a5246Sthorpej * 30 seconds after the previous one was
43394b2d428Schristos * scheduled, instead of 30 seconds after
434fc1a5246Sthorpej * the previous update was finished.
435fc1a5246Sthorpej * Even if we just started after discovering
436fc1a5246Sthorpej * a 2nd interface or were otherwise delayed,
437fc1a5246Sthorpej * pick a 30-second aniversary of the
438fc1a5246Sthorpej * original broadcast time.
439fc1a5246Sthorpej */
440fc1a5246Sthorpej n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
441fc1a5246Sthorpej next_bcast.tv_sec += n*SUPPLY_INTERVAL;
442fc1a5246Sthorpej
443fc1a5246Sthorpej continue;
444fc1a5246Sthorpej }
445fc1a5246Sthorpej
446fc1a5246Sthorpej if (timercmp(&t2, &wtime, <))
447fc1a5246Sthorpej wtime = t2;
448fc1a5246Sthorpej }
449fc1a5246Sthorpej
450fc1a5246Sthorpej /* If we need a flash update, either do it now or
451fc1a5246Sthorpej * set the delay to end when it is time.
452fc1a5246Sthorpej *
453fc1a5246Sthorpej * If we are within MIN_WAITTIME seconds of a full update,
454fc1a5246Sthorpej * do not bother.
455fc1a5246Sthorpej */
456fc1a5246Sthorpej if (need_flash
457fc1a5246Sthorpej && supplier
458fc1a5246Sthorpej && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
459fc1a5246Sthorpej /* accurate to the millisecond */
460fc1a5246Sthorpej if (!timercmp(&no_flash, &now, >))
461fc1a5246Sthorpej rip_bcast(1);
462fc1a5246Sthorpej timevalsub(&t2, &no_flash, &now);
463fc1a5246Sthorpej if (timercmp(&t2, &wtime, <))
464fc1a5246Sthorpej wtime = t2;
465fc1a5246Sthorpej }
466fc1a5246Sthorpej
467fc1a5246Sthorpej /* trigger the main aging timer.
468fc1a5246Sthorpej */
469fc1a5246Sthorpej timevalsub(&t2, &age_timer, &now);
470fc1a5246Sthorpej if (t2.tv_sec <= 0) {
471fc1a5246Sthorpej age(0);
472fc1a5246Sthorpej continue;
473fc1a5246Sthorpej }
474fc1a5246Sthorpej if (timercmp(&t2, &wtime, <))
475fc1a5246Sthorpej wtime = t2;
476fc1a5246Sthorpej
477fc1a5246Sthorpej /* update the kernel routing table
478fc1a5246Sthorpej */
479fc1a5246Sthorpej timevalsub(&t2, &need_kern, &now);
480fc1a5246Sthorpej if (t2.tv_sec <= 0) {
481fc1a5246Sthorpej age(0);
482fc1a5246Sthorpej continue;
483fc1a5246Sthorpej }
484fc1a5246Sthorpej if (timercmp(&t2, &wtime, <))
485fc1a5246Sthorpej wtime = t2;
486fc1a5246Sthorpej
487fc1a5246Sthorpej /* take care of router discovery,
4886d8ef4dfSthorpej * but do it in the correct the millisecond
489fc1a5246Sthorpej */
490fc1a5246Sthorpej if (!timercmp(&rdisc_timer, &now, >)) {
491fc1a5246Sthorpej rdisc_age(0);
492fc1a5246Sthorpej continue;
493fc1a5246Sthorpej }
494fc1a5246Sthorpej timevalsub(&t2, &rdisc_timer, &now);
495fc1a5246Sthorpej if (timercmp(&t2, &wtime, <))
496fc1a5246Sthorpej wtime = t2;
497fc1a5246Sthorpej
498fc1a5246Sthorpej
499fc1a5246Sthorpej /* wait for input or a timer to expire.
500fc1a5246Sthorpej */
501fc1a5246Sthorpej trace_flush();
502d9be8318Sitojun if (ibitsp)
503d9be8318Sitojun free(ibitsp);
504d9be8318Sitojun ibitsp = (fd_set *)calloc(howmany(sock_max, NFDBITS),
505d9be8318Sitojun sizeof(fd_mask));
506d9be8318Sitojun if (ibitsp == NULL)
507d9be8318Sitojun BADERR(1, "calloc");
508d9be8318Sitojun memcpy(ibitsp, fdbitsp, howmany(sock_max, NFDBITS) *
509d9be8318Sitojun sizeof(fd_mask));
510d9be8318Sitojun n = select(sock_max, ibitsp, 0, 0, &wtime);
511fc1a5246Sthorpej if (n <= 0) {
512fc1a5246Sthorpej if (n < 0 && errno != EINTR && errno != EAGAIN)
513fc1a5246Sthorpej BADERR(1,"select");
514fc1a5246Sthorpej continue;
515fc1a5246Sthorpej }
516fc1a5246Sthorpej
517d9be8318Sitojun if (FD_ISSET(rt_sock, ibitsp)) {
518fc1a5246Sthorpej read_rt();
519fc1a5246Sthorpej n--;
520fc1a5246Sthorpej }
521d9be8318Sitojun if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, ibitsp)) {
522fc1a5246Sthorpej read_d();
523fc1a5246Sthorpej n--;
524fc1a5246Sthorpej }
525d9be8318Sitojun if (rip_sock >= 0 && FD_ISSET(rip_sock, ibitsp)) {
526fc1a5246Sthorpej read_rip(rip_sock, 0);
527fc1a5246Sthorpej n--;
528fc1a5246Sthorpej }
529fc1a5246Sthorpej
530fc1a5246Sthorpej for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) {
531fc1a5246Sthorpej if (ifp->int_rip_sock >= 0
532d9be8318Sitojun && FD_ISSET(ifp->int_rip_sock, ibitsp)) {
533fc1a5246Sthorpej read_rip(ifp->int_rip_sock, ifp);
534fc1a5246Sthorpej n--;
53561f28255Scgd }
53661f28255Scgd }
537fc1a5246Sthorpej }
538fc1a5246Sthorpej }
539fc1a5246Sthorpej
540fc1a5246Sthorpej
541fc1a5246Sthorpej /* ARGSUSED */
542fc1a5246Sthorpej void
sigalrm(int s UNUSED)543756b1291Schristos sigalrm(int s UNUSED)
544fc1a5246Sthorpej {
545fc1a5246Sthorpej /* Historically, SIGALRM would cause the daemon to check for
546fc1a5246Sthorpej * new and broken interfaces.
547fc1a5246Sthorpej */
548fc1a5246Sthorpej ifinit_timer.tv_sec = now.tv_sec;
549e7512e5aSchristos trace_act("SIGALRM");
550fc1a5246Sthorpej }
551fc1a5246Sthorpej
552fc1a5246Sthorpej
553fc1a5246Sthorpej /* watch for fatal signals */
554fc1a5246Sthorpej void
sigterm(int sig)555fc1a5246Sthorpej sigterm(int sig)
556fc1a5246Sthorpej {
557fc1a5246Sthorpej stopint = sig;
558fc1a5246Sthorpej (void)signal(sig, SIG_DFL); /* catch it only once */
559fc1a5246Sthorpej }
560fc1a5246Sthorpej
56161f28255Scgd
562e072e2aeScgd void
fix_select(void)563fc1a5246Sthorpej fix_select(void)
56461f28255Scgd {
565fc1a5246Sthorpej struct interface *ifp;
56661f28255Scgd
567fc1a5246Sthorpej sock_max = 0;
568fc1a5246Sthorpej
569fc1a5246Sthorpej if (sock_max <= rt_sock)
570fc1a5246Sthorpej sock_max = rt_sock + 1;
571d9be8318Sitojun if (rip_sock >= 0)
572fc1a5246Sthorpej if (sock_max <= rip_sock)
573fc1a5246Sthorpej sock_max = rip_sock + 1;
574fc1a5246Sthorpej for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
575d9be8318Sitojun if (ifp->int_rip_sock >= 0)
576fc1a5246Sthorpej if (sock_max <= ifp->int_rip_sock)
577fc1a5246Sthorpej sock_max = ifp->int_rip_sock + 1;
578fc1a5246Sthorpej }
579d9be8318Sitojun if (rdisc_sock >= 0)
580fc1a5246Sthorpej if (sock_max <= rdisc_sock)
581fc1a5246Sthorpej sock_max = rdisc_sock + 1;
582d9be8318Sitojun
583d9be8318Sitojun if (fdbitsp)
584d9be8318Sitojun free(fdbitsp);
585d9be8318Sitojun fdbitsp = (fd_set *)calloc(howmany(sock_max, NFDBITS),
586d9be8318Sitojun sizeof(fd_mask));
587d9be8318Sitojun if (fdbitsp == NULL)
588d9be8318Sitojun BADERR(1, "calloc");
589d9be8318Sitojun
590d9be8318Sitojun FD_SET(rt_sock, fdbitsp);
591d9be8318Sitojun if (rip_sock >= 0)
592d9be8318Sitojun FD_SET(rip_sock, fdbitsp);
593d9be8318Sitojun for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
594d9be8318Sitojun if (ifp->int_rip_sock >= 0)
595d9be8318Sitojun FD_SET(ifp->int_rip_sock, fdbitsp);
59661f28255Scgd }
597d9be8318Sitojun if (rdisc_sock >= 0)
598d9be8318Sitojun FD_SET(rdisc_sock, fdbitsp);
59961f28255Scgd }
60061f28255Scgd
601fc1a5246Sthorpej
602fc1a5246Sthorpej void
fix_sock(int sock,const char * name)603fc1a5246Sthorpej fix_sock(int sock,
604756b1291Schristos const char *name)
60561f28255Scgd {
606fc1a5246Sthorpej int on;
607fc1a5246Sthorpej #define MIN_SOCKBUF (4*1024)
608fc1a5246Sthorpej static int rbuf;
60961f28255Scgd
61061f28255Scgd if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
611fc1a5246Sthorpej logbad(1, "fcntl(%s) O_NONBLOCK: %s",
612fc1a5246Sthorpej name, strerror(errno));
613fc1a5246Sthorpej on = 1;
614e7512e5aSchristos if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST, &on,sizeof(on)) < 0)
615fc1a5246Sthorpej msglog("setsockopt(%s,SO_BROADCAST): %s",
616fc1a5246Sthorpej name, strerror(errno));
617e7512e5aSchristos #ifdef USE_PASSIFNAME
618e7512e5aSchristos on = 1;
619e7512e5aSchristos if (setsockopt(sock, SOL_SOCKET, SO_PASSIFNAME, &on,sizeof(on)) < 0)
620e7512e5aSchristos msglog("setsockopt(%s,SO_PASSIFNAME): %s",
621e7512e5aSchristos name, strerror(errno));
622e7512e5aSchristos #endif
623e7512e5aSchristos
624fc1a5246Sthorpej if (rbuf >= MIN_SOCKBUF) {
625fc1a5246Sthorpej if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
626fc1a5246Sthorpej &rbuf, sizeof(rbuf)) < 0)
627fc1a5246Sthorpej msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
628fc1a5246Sthorpej name, rbuf, strerror(errno));
629fc1a5246Sthorpej } else {
630fc1a5246Sthorpej for (rbuf = 60*1024; ; rbuf -= 4096) {
631fc1a5246Sthorpej if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
632fc1a5246Sthorpej &rbuf, sizeof(rbuf)) == 0) {
633e7512e5aSchristos trace_act("RCVBUF=%d", rbuf);
634fc1a5246Sthorpej break;
635fc1a5246Sthorpej }
636fc1a5246Sthorpej if (rbuf < MIN_SOCKBUF) {
637fc1a5246Sthorpej msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
638fc1a5246Sthorpej name, rbuf, strerror(errno));
639fc1a5246Sthorpej break;
640fc1a5246Sthorpej }
641fc1a5246Sthorpej }
642fc1a5246Sthorpej }
643fc1a5246Sthorpej }
644fc1a5246Sthorpej
645fc1a5246Sthorpej
646fc1a5246Sthorpej /* get a rip socket
647fc1a5246Sthorpej */
648fc1a5246Sthorpej static int /* <0 or file descriptor */
get_rip_sock(naddr addr,int serious)649fc1a5246Sthorpej get_rip_sock(naddr addr,
650fc1a5246Sthorpej int serious) /* 1=failure to bind is serious */
651fc1a5246Sthorpej {
652cbbd79f7Slukem struct sockaddr_in rsin;
653fc1a5246Sthorpej unsigned char ttl;
654fc1a5246Sthorpej int s;
655fc1a5246Sthorpej
656fc1a5246Sthorpej
657fc1a5246Sthorpej if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
658fc1a5246Sthorpej BADERR(1,"rip_sock = socket()");
659fc1a5246Sthorpej
660cbbd79f7Slukem memset(&rsin, 0, sizeof(rsin));
661fc1a5246Sthorpej #ifdef _HAVE_SIN_LEN
662cbbd79f7Slukem rsin.sin_len = sizeof(rsin);
663fc1a5246Sthorpej #endif
664cbbd79f7Slukem rsin.sin_family = AF_INET;
665cbbd79f7Slukem rsin.sin_port = htons(RIP_PORT);
666cbbd79f7Slukem rsin.sin_addr.s_addr = addr;
667cbbd79f7Slukem if (bind(s, (struct sockaddr *)&rsin, sizeof(rsin)) < 0) {
668fc1a5246Sthorpej if (serious)
669fc1a5246Sthorpej BADERR(errno != EADDRINUSE, "bind(rip_sock)");
670fc1a5246Sthorpej return -1;
671fc1a5246Sthorpej }
672fc1a5246Sthorpej fix_sock(s,"rip_sock");
673fc1a5246Sthorpej
674fc1a5246Sthorpej ttl = 1;
675fc1a5246Sthorpej if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
676fc1a5246Sthorpej &ttl, sizeof(ttl)) < 0)
677fc1a5246Sthorpej DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
678fc1a5246Sthorpej
679fc1a5246Sthorpej return s;
680fc1a5246Sthorpej }
681fc1a5246Sthorpej
682fc1a5246Sthorpej
683fc1a5246Sthorpej /* turn off main RIP socket */
684fc1a5246Sthorpej void
rip_off(void)685fc1a5246Sthorpej rip_off(void)
686fc1a5246Sthorpej {
687fc1a5246Sthorpej struct interface *ifp;
6883f50343aSlukem naddr addr;
689fc1a5246Sthorpej
690fc1a5246Sthorpej
691fc1a5246Sthorpej if (rip_sock >= 0 && !mhome) {
692e7512e5aSchristos trace_act("turn off RIP");
693fc1a5246Sthorpej
694fc1a5246Sthorpej (void)close(rip_sock);
695fc1a5246Sthorpej rip_sock = -1;
696fc1a5246Sthorpej
697fc1a5246Sthorpej /* get non-broadcast sockets to listen to queries.
698fc1a5246Sthorpej */
699fc1a5246Sthorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
700e7512e5aSchristos if (ifp->int_state & IS_REMOTE)
701e7512e5aSchristos continue;
702e7512e5aSchristos if (ifp->int_rip_sock < 0) {
703fc1a5246Sthorpej addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
704fc1a5246Sthorpej ? ifp->int_dstaddr
705fc1a5246Sthorpej : ifp->int_addr);
706fc1a5246Sthorpej ifp->int_rip_sock = get_rip_sock(addr, 0);
707fc1a5246Sthorpej }
708fc1a5246Sthorpej }
709fc1a5246Sthorpej
710fc1a5246Sthorpej fix_select();
711fc1a5246Sthorpej
712fc1a5246Sthorpej age(0);
713fc1a5246Sthorpej }
714fc1a5246Sthorpej }
715fc1a5246Sthorpej
716fc1a5246Sthorpej
717fc1a5246Sthorpej /* turn on RIP multicast input via an interface
718fc1a5246Sthorpej */
719fc1a5246Sthorpej static void
rip_mcast_on(struct interface * ifp)720fc1a5246Sthorpej rip_mcast_on(struct interface *ifp)
721fc1a5246Sthorpej {
722fc1a5246Sthorpej struct ip_mreq m;
723fc1a5246Sthorpej
724fc1a5246Sthorpej if (!IS_RIP_IN_OFF(ifp->int_state)
725fc1a5246Sthorpej && (ifp->int_if_flags & IFF_MULTICAST)
726fc1a5246Sthorpej #ifdef MCAST_PPP_BUG
727fc1a5246Sthorpej && !(ifp->int_if_flags & IFF_POINTOPOINT)
728fc1a5246Sthorpej #endif
729fc1a5246Sthorpej && !(ifp->int_state & IS_ALIAS)) {
730fc1a5246Sthorpej m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
7317846de58Sitojun #ifdef MCAST_IFINDEX
7327846de58Sitojun m.imr_interface.s_addr = htonl(ifp->int_index);
7337846de58Sitojun #else
734fc1a5246Sthorpej m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
735fc1a5246Sthorpej ? ifp->int_dstaddr
736fc1a5246Sthorpej : ifp->int_addr);
7377846de58Sitojun #endif
738fc1a5246Sthorpej if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP,
739fc1a5246Sthorpej &m, sizeof(m)) < 0)
740fc1a5246Sthorpej LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
741fc1a5246Sthorpej }
742fc1a5246Sthorpej }
743fc1a5246Sthorpej
744fc1a5246Sthorpej
745fc1a5246Sthorpej /* Prepare socket used for RIP.
746fc1a5246Sthorpej */
747fc1a5246Sthorpej void
rip_on(struct interface * ifp)748fc1a5246Sthorpej rip_on(struct interface *ifp)
749fc1a5246Sthorpej {
750fc1a5246Sthorpej /* If the main RIP socket is already alive, only start receiving
751fc1a5246Sthorpej * multicasts for this interface.
752fc1a5246Sthorpej */
753fc1a5246Sthorpej if (rip_sock >= 0) {
754fc1a5246Sthorpej if (ifp != 0)
755fc1a5246Sthorpej rip_mcast_on(ifp);
756fc1a5246Sthorpej return;
757fc1a5246Sthorpej }
758fc1a5246Sthorpej
759e7512e5aSchristos /* If the main RIP socket is off and it makes sense to turn it on,
760e7512e5aSchristos * then turn it on for all of the interfaces.
7616d8ef4dfSthorpej * It makes sense if either router discovery is off, or if
7626d8ef4dfSthorpej * router discover is on and at most one interface is doing RIP.
763fc1a5246Sthorpej */
7646d8ef4dfSthorpej if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
765e7512e5aSchristos trace_act("turn on RIP");
766fc1a5246Sthorpej
767fc1a5246Sthorpej /* Close all of the query sockets so that we can open
768fc1a5246Sthorpej * the main socket. SO_REUSEPORT is not a solution,
769fc1a5246Sthorpej * since that would let two daemons bind to the broadcast
770fc1a5246Sthorpej * socket.
771fc1a5246Sthorpej */
772fc1a5246Sthorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
773fc1a5246Sthorpej if (ifp->int_rip_sock >= 0) {
774fc1a5246Sthorpej (void)close(ifp->int_rip_sock);
775fc1a5246Sthorpej ifp->int_rip_sock = -1;
776fc1a5246Sthorpej }
777fc1a5246Sthorpej }
778fc1a5246Sthorpej
779fc1a5246Sthorpej rip_sock = get_rip_sock(INADDR_ANY, 1);
780fc1a5246Sthorpej rip_sock_mcast = 0;
781fc1a5246Sthorpej
782fc1a5246Sthorpej /* Do not advertise anything until we have heard something
783fc1a5246Sthorpej */
784fc1a5246Sthorpej if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
785fc1a5246Sthorpej next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
786fc1a5246Sthorpej
787fc1a5246Sthorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
788e7512e5aSchristos ifp->int_query_time = NEVER;
789fc1a5246Sthorpej rip_mcast_on(ifp);
790fc1a5246Sthorpej }
791fc1a5246Sthorpej ifinit_timer.tv_sec = now.tv_sec;
792fc1a5246Sthorpej
793fc1a5246Sthorpej } else if (ifp != 0
794e7512e5aSchristos && !(ifp->int_state & IS_REMOTE)
795e7512e5aSchristos && ifp->int_rip_sock < 0) {
796fc1a5246Sthorpej /* RIP is off, so ensure there are sockets on which
797fc1a5246Sthorpej * to listen for queries.
798fc1a5246Sthorpej */
799fc1a5246Sthorpej ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
800e7512e5aSchristos }
801fc1a5246Sthorpej
802fc1a5246Sthorpej fix_select();
803fc1a5246Sthorpej }
804fc1a5246Sthorpej
805fc1a5246Sthorpej
8064d3fba59Schristos /* die if malloc(3) fails
8074d3fba59Schristos */
8084d3fba59Schristos void *
rtmalloc(size_t size,const char * msg)8094d3fba59Schristos rtmalloc(size_t size,
810756b1291Schristos const char *msg)
8114d3fba59Schristos {
8124d3fba59Schristos void *p = malloc(size);
8134d3fba59Schristos if (p == 0)
814cce4e6d1Schristos logbad(1,"malloc(%lu) failed in %s", (u_long)size, msg);
8154d3fba59Schristos return p;
8164d3fba59Schristos }
8174d3fba59Schristos
8184d3fba59Schristos
819fc1a5246Sthorpej /* get a random instant in an interval
820fc1a5246Sthorpej */
821fc1a5246Sthorpej void
intvl_random(struct timeval * tp,u_long lo,u_long hi)822fc1a5246Sthorpej intvl_random(struct timeval *tp, /* put value here */
823fc1a5246Sthorpej u_long lo, /* value is after this second */
824fc1a5246Sthorpej u_long hi) /* and before this */
825fc1a5246Sthorpej {
826fc1a5246Sthorpej tp->tv_sec = (time_t)(hi == lo
827fc1a5246Sthorpej ? lo
828e2c411ceSitojun : (lo + arc4random() % ((hi - lo))));
829e2c411ceSitojun tp->tv_usec = arc4random() % 1000000;
830fc1a5246Sthorpej }
831fc1a5246Sthorpej
832fc1a5246Sthorpej
833fc1a5246Sthorpej void
timevaladd(struct timeval * t1,struct timeval * t2)834fc1a5246Sthorpej timevaladd(struct timeval *t1,
835fc1a5246Sthorpej struct timeval *t2)
836fc1a5246Sthorpej {
837fc1a5246Sthorpej
838fc1a5246Sthorpej t1->tv_sec += t2->tv_sec;
83994b2d428Schristos if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
840fc1a5246Sthorpej t1->tv_sec++;
841fc1a5246Sthorpej t1->tv_usec -= 1000000;
842fc1a5246Sthorpej }
843fc1a5246Sthorpej }
844fc1a5246Sthorpej
845fc1a5246Sthorpej
846fc1a5246Sthorpej /* t1 = t2 - t3
847fc1a5246Sthorpej */
848fc1a5246Sthorpej static void
timevalsub(struct timeval * t1,struct timeval * t2,struct timeval * t3)849fc1a5246Sthorpej timevalsub(struct timeval *t1,
850fc1a5246Sthorpej struct timeval *t2,
851fc1a5246Sthorpej struct timeval *t3)
852fc1a5246Sthorpej {
853fc1a5246Sthorpej t1->tv_sec = t2->tv_sec - t3->tv_sec;
854fc1a5246Sthorpej if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
855fc1a5246Sthorpej t1->tv_sec--;
856fc1a5246Sthorpej t1->tv_usec += 1000000;
857fc1a5246Sthorpej }
858fc1a5246Sthorpej }
859fc1a5246Sthorpej
860fc1a5246Sthorpej
861e7512e5aSchristos /* put a message into the system log
862e7512e5aSchristos */
863fc1a5246Sthorpej void
msglog(const char * p,...)864756b1291Schristos msglog(const char *p, ...)
865fc1a5246Sthorpej {
866fc1a5246Sthorpej va_list args;
867fc1a5246Sthorpej
868fc1a5246Sthorpej trace_flush();
869fc1a5246Sthorpej
870fc1a5246Sthorpej va_start(args, p);
871fc1a5246Sthorpej vsyslog(LOG_ERR, p, args);
8724c999163Swiz va_end(args);
873fc1a5246Sthorpej
874fc1a5246Sthorpej if (ftrace != 0) {
875fc1a5246Sthorpej if (ftrace == stdout)
876fc1a5246Sthorpej (void)fputs("routed: ", ftrace);
8774c999163Swiz va_start(args, p);
878fc1a5246Sthorpej (void)vfprintf(ftrace, p, args);
8794c999163Swiz va_end(args);
880fc1a5246Sthorpej (void)fputc('\n', ftrace);
881fc1a5246Sthorpej }
882fc1a5246Sthorpej }
883fc1a5246Sthorpej
884fc1a5246Sthorpej
885e7512e5aSchristos /* Put a message about a bad system into the system log if
886e7512e5aSchristos * we have not complained about it recently.
887e7512e5aSchristos *
888e7512e5aSchristos * It is desirable to complain about all bad systems, but not too often.
889e7512e5aSchristos * In the worst case, it is not practical to keep track of all bad systems.
890e7512e5aSchristos * For example, there can be many systems with the wrong password.
891e7512e5aSchristos */
892e7512e5aSchristos void
msglim(struct msg_limit * lim,naddr addr,const char * p,...)893756b1291Schristos msglim(struct msg_limit *lim, naddr addr, const char *p, ...)
894e7512e5aSchristos {
895e7512e5aSchristos va_list args;
896e7512e5aSchristos int i;
897e7512e5aSchristos struct msg_sub *ms1, *ms;
898756b1291Schristos const char *p1;
899e7512e5aSchristos
900e7512e5aSchristos /* look for the oldest slot in the table
901e7512e5aSchristos * or the slot for the bad router.
902e7512e5aSchristos */
903e7512e5aSchristos ms = ms1 = lim->subs;
904e7512e5aSchristos for (i = MSG_SUBJECT_N; ; i--, ms1++) {
905e7512e5aSchristos if (i == 0) {
906e7512e5aSchristos /* Reuse a slot at most once every 10 minutes.
907e7512e5aSchristos */
908e7512e5aSchristos if (lim->reuse > now.tv_sec) {
909e7512e5aSchristos ms = 0;
910e7512e5aSchristos } else {
911e7512e5aSchristos ms = ms1;
912e7512e5aSchristos lim->reuse = now.tv_sec + 10*60;
913e7512e5aSchristos }
914e7512e5aSchristos break;
915e7512e5aSchristos }
916e7512e5aSchristos if (ms->addr == addr) {
917e7512e5aSchristos /* Repeat a complaint about a given system at
918e7512e5aSchristos * most once an hour.
919e7512e5aSchristos */
920e7512e5aSchristos if (ms->until > now.tv_sec)
921e7512e5aSchristos ms = 0;
922e7512e5aSchristos break;
923e7512e5aSchristos }
924e7512e5aSchristos if (ms->until < ms1->until)
925e7512e5aSchristos ms = ms1;
926e7512e5aSchristos }
927e7512e5aSchristos if (ms != 0) {
928e7512e5aSchristos ms->addr = addr;
929e7512e5aSchristos ms->until = now.tv_sec + 60*60; /* 60 minutes */
930e7512e5aSchristos
931e7512e5aSchristos trace_flush();
932e7512e5aSchristos for (p1 = p; *p1 == ' '; p1++)
933e7512e5aSchristos continue;
9344c999163Swiz va_start(args, p);
9354c999163Swiz vsyslog(LOG_ERR, p1, args);
9364c999163Swiz va_end(args);
937e7512e5aSchristos }
938e7512e5aSchristos
939e7512e5aSchristos /* always display the message if tracing */
940e7512e5aSchristos if (ftrace != 0) {
9414c999163Swiz va_start(args, p);
942e7512e5aSchristos (void)vfprintf(ftrace, p, args);
943e7512e5aSchristos (void)fputc('\n', ftrace);
9444c999163Swiz va_end(args);
945e7512e5aSchristos }
946e7512e5aSchristos }
947e7512e5aSchristos
948e7512e5aSchristos
949fc1a5246Sthorpej void
logbad(int dump,const char * p,...)950756b1291Schristos logbad(int dump, const char *p, ...)
951fc1a5246Sthorpej {
952fc1a5246Sthorpej va_list args;
953fc1a5246Sthorpej
954fc1a5246Sthorpej trace_flush();
955fc1a5246Sthorpej
956fc1a5246Sthorpej va_start(args, p);
957fc1a5246Sthorpej vsyslog(LOG_ERR, p, args);
9584c999163Swiz va_end(args);
959fc1a5246Sthorpej
960fc1a5246Sthorpej (void)fputs("routed: ", stderr);
9614c999163Swiz va_start(args, p);
962fc1a5246Sthorpej (void)vfprintf(stderr, p, args);
9634c999163Swiz va_end(args);
964fc1a5246Sthorpej (void)fputs("; giving up\n",stderr);
965fc1a5246Sthorpej (void)fflush(stderr);
966fc1a5246Sthorpej
967fc1a5246Sthorpej if (dump)
968fc1a5246Sthorpej abort();
969fc1a5246Sthorpej exit(1);
97061f28255Scgd }
971