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