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