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