xref: /netbsd-src/sbin/routed/main.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)main.c	8.1 (Berkeley) 6/5/93";*/
42 static char *rcsid = "$Id: main.c,v 1.6 1994/12/18 05:43:55 cgd Exp $";
43 #endif /* not lint */
44 
45 /*
46  * Routing Table Management Daemon
47  */
48 #include "defs.h"
49 #include <sys/ioctl.h>
50 #include <sys/file.h>
51 
52 #include <net/if.h>
53 
54 #include <sys/errno.h>
55 #include <sys/signal.h>
56 #include <sys/syslog.h>
57 #include "pathnames.h"
58 
59 int	supplier = -1;		/* process should supply updates */
60 int	gateway = 0;		/* 1 if we are a gateway to parts beyond */
61 int	debug = 0;
62 int	bufspace = 127*1024;	/* max. input buffer size to request */
63 struct	rip *msg = (struct rip *)packet;
64 
65 int getsocket __P((int, int, struct sockaddr_in *));
66 void timevalsub __P((struct timeval *, struct timeval *));
67 void process __P((int));
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char *argv[];
73 {
74 	int n, nfd, omask, tflags = 0;
75 	struct timeval *tvp, waittime;
76 	struct itimerval itval;
77 	register struct rip *query = msg;
78 	fd_set ibits;
79 
80 	argv0 = argv;
81 #if BSD >= 43
82 	openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
83 	setlogmask(LOG_UPTO(LOG_WARNING));
84 #else
85 	openlog("routed", LOG_PID);
86 #define LOG_UPTO(x) (x)
87 #define setlogmask(x) (x)
88 #endif
89 	sp = getservbyname("router", "udp");
90 	if (sp == NULL) {
91 		fprintf(stderr, "routed: router/udp: unknown service\n");
92 		exit(1);
93 	}
94 	addr.sin_family = AF_INET;
95 	addr.sin_port = sp->s_port;
96 	r = socket(AF_ROUTE, SOCK_RAW, 0);
97 	/* later, get smart about lookingforinterfaces */
98 	if (r)
99 		shutdown(r, 0); /* for now, don't want reponses */
100 	else {
101 		fprintf(stderr, "routed: no routing socket\n");
102 		exit(1);
103 	}
104 	s = getsocket(AF_INET, SOCK_DGRAM, &addr);
105 	if (s < 0)
106 		exit(1);
107 	argv++, argc--;
108 	while (argc > 0 && **argv == '-') {
109 		if (strcmp(*argv, "-s") == 0) {
110 			supplier = 1;
111 			argv++, argc--;
112 			continue;
113 		}
114 		if (strcmp(*argv, "-q") == 0) {
115 			supplier = 0;
116 			argv++, argc--;
117 			continue;
118 		}
119 		if (strcmp(*argv, "-t") == 0) {
120 			tflags++;
121 			setlogmask(LOG_UPTO(LOG_DEBUG));
122 			argv++, argc--;
123 			continue;
124 		}
125 		if (strcmp(*argv, "-d") == 0) {
126 			debug++;
127 			setlogmask(LOG_UPTO(LOG_DEBUG));
128 			argv++, argc--;
129 			continue;
130 		}
131 		if (strcmp(*argv, "-g") == 0) {
132 			gateway = 1;
133 			argv++, argc--;
134 			continue;
135 		}
136 		fprintf(stderr,
137 			"usage: routed [ -s ] [ -q ] [ -t ] [ -g ]\n");
138 		exit(1);
139 	}
140 
141 	if (debug == 0 && tflags == 0)
142 		daemon(0, 0);
143 	/*
144 	 * Any extra argument is considered
145 	 * a tracing log file.
146 	 */
147 	if (argc > 0)
148 		traceon(*argv);
149 	while (tflags-- > 0)
150 		bumploglevel();
151 
152 	(void) gettimeofday(&now, (struct timezone *)NULL);
153 	/*
154 	 * Collect an initial view of the world by
155 	 * checking the interface configuration and the gateway kludge
156 	 * file.  Then, send a request packet on all
157 	 * directly connected networks to find out what
158 	 * everyone else thinks.
159 	 */
160 	rtinit();
161 	ifinit();
162 	gwkludge();
163 	if (gateway > 0)
164 		rtdefault();
165 	if (supplier < 0)
166 		supplier = 0;
167 	query->rip_cmd = RIPCMD_REQUEST;
168 	query->rip_vers = RIPVERSION;
169 	if (sizeof(query->rip_nets[0].rip_dst.sa_family) > 1)	/* XXX */
170 		query->rip_nets[0].rip_dst.sa_family = htons((u_short)AF_UNSPEC);
171 	else
172 		query->rip_nets[0].rip_dst.sa_family = AF_UNSPEC;
173 	query->rip_nets[0].rip_metric = htonl((u_long)HOPCNT_INFINITY);
174 	toall(sndmsg, 0, NULL);
175 	signal(SIGALRM, timer);
176 	signal(SIGHUP, hup);
177 	signal(SIGTERM, hup);
178 	signal(SIGINT, rtdeleteall);
179 	signal(SIGUSR1, sigtrace);
180 	signal(SIGUSR2, sigtrace);
181 	itval.it_interval.tv_sec = TIMER_RATE;
182 	itval.it_value.tv_sec = TIMER_RATE;
183 	itval.it_interval.tv_usec = 0;
184 	itval.it_value.tv_usec = 0;
185 	srandom(getpid());
186 	if (setitimer(ITIMER_REAL, &itval, (struct itimerval *)NULL) < 0)
187 		syslog(LOG_ERR, "setitimer: %m\n");
188 
189 	FD_ZERO(&ibits);
190 	nfd = s + 1;			/* 1 + max(fd's) */
191 	for (;;) {
192 		FD_SET(s, &ibits);
193 		/*
194 		 * If we need a dynamic update that was held off,
195 		 * needupdate will be set, and nextbcast is the time
196 		 * by which we want select to return.  Compute time
197 		 * until dynamic update should be sent, and select only
198 		 * until then.  If we have already passed nextbcast,
199 		 * just poll.
200 		 */
201 		if (needupdate) {
202 			waittime = nextbcast;
203 			timevalsub(&waittime, &now);
204 			if (waittime.tv_sec < 0) {
205 				waittime.tv_sec = 0;
206 				waittime.tv_usec = 0;
207 			}
208 			if (traceactions)
209 				fprintf(ftrace,
210 				 "select until dynamic update %d/%d sec/usec\n",
211 				    waittime.tv_sec, waittime.tv_usec);
212 			tvp = &waittime;
213 		} else
214 			tvp = (struct timeval *)NULL;
215 		n = select(nfd, &ibits, 0, 0, tvp);
216 		if (n <= 0) {
217 			/*
218 			 * Need delayed dynamic update if select returned
219 			 * nothing and we timed out.  Otherwise, ignore
220 			 * errors (e.g. EINTR).
221 			 */
222 			if (n < 0) {
223 				if (errno == EINTR)
224 					continue;
225 				syslog(LOG_ERR, "select: %m");
226 			}
227 			omask = sigblock(sigmask(SIGALRM));
228 			if (n == 0 && needupdate) {
229 				if (traceactions)
230 					fprintf(ftrace,
231 					    "send delayed dynamic update\n");
232 				(void) gettimeofday(&now,
233 					    (struct timezone *)NULL);
234 				toall(supply, RTS_CHANGED,
235 				    (struct interface *)NULL);
236 				lastbcast = now;
237 				needupdate = 0;
238 				nextbcast.tv_sec = 0;
239 			}
240 			sigsetmask(omask);
241 			continue;
242 		}
243 		(void) gettimeofday(&now, (struct timezone *)NULL);
244 		omask = sigblock(sigmask(SIGALRM));
245 #ifdef doesntwork
246 /*
247 printf("s %d, ibits %x index %d, mod %d, sh %x, or %x &ibits %x\n",
248 	s,
249 	ibits.fds_bits[0],
250 	(s)/(sizeof(fd_mask) * 8),
251 	((s) % (sizeof(fd_mask) * 8)),
252 	(1 << ((s) % (sizeof(fd_mask) * 8))),
253 	ibits.fds_bits[(s)/(sizeof(fd_mask) * 8)] & (1 << ((s) % (sizeof(fd_mask) * 8))),
254 	&ibits
255 	);
256 */
257 		if (FD_ISSET(s, &ibits))
258 #else
259 		if (ibits.fds_bits[s/32] & (1 << s))
260 #endif
261 			process(s);
262 		/* handle ICMP redirects */
263 		sigsetmask(omask);
264 	}
265 }
266 
267 void
268 timevaladd(t1, t2)
269 	struct timeval *t1, *t2;
270 {
271 
272 	t1->tv_sec += t2->tv_sec;
273 	if ((t1->tv_usec += t2->tv_usec) > 1000000) {
274 		t1->tv_sec++;
275 		t1->tv_usec -= 1000000;
276 	}
277 }
278 
279 void
280 timevalsub(t1, t2)
281 	struct timeval *t1, *t2;
282 {
283 
284 	t1->tv_sec -= t2->tv_sec;
285 	if ((t1->tv_usec -= t2->tv_usec) < 0) {
286 		t1->tv_sec--;
287 		t1->tv_usec += 1000000;
288 	}
289 }
290 
291 void
292 process(fd)
293 	int fd;
294 {
295 	struct sockaddr from;
296 	int fromlen, cc;
297 	union {
298 		char	buf[MAXPACKETSIZE+1];
299 		struct	rip rip;
300 	} inbuf;
301 
302 	for (;;) {
303 		fromlen = sizeof (from);
304 		cc = recvfrom(fd, &inbuf, sizeof (inbuf), 0, &from, &fromlen);
305 		if (cc <= 0) {
306 			if (cc < 0 && errno != EWOULDBLOCK)
307 				perror("recvfrom");
308 			break;
309 		}
310 		if (fromlen != sizeof (struct sockaddr_in))
311 			break;
312 		rip_input(&from, &inbuf.rip, cc);
313 	}
314 }
315 
316 int
317 getsocket(domain, type, sin)
318 	int domain, type;
319 	struct sockaddr_in *sin;
320 {
321 	int sock, on = 1;
322 
323 	if ((sock = socket(domain, type, 0)) < 0) {
324 		perror("socket");
325 		syslog(LOG_ERR, "socket: %m");
326 		return (-1);
327 	}
328 #ifdef SO_BROADCAST
329 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
330 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
331 		close(sock);
332 		return (-1);
333 	}
334 #endif
335 #ifdef SO_RCVBUF
336 	for (on = bufspace; ; on -= 1024) {
337 		if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
338 		    &on, sizeof (on)) == 0)
339 			break;
340 		if (on <= 8*1024) {
341 			syslog(LOG_ERR, "setsockopt SO_RCVBUF: %m");
342 			break;
343 		}
344 	}
345 	if (traceactions)
346 		fprintf(ftrace, "recv buf %d\n", on);
347 #endif
348 	if (bind(sock, (struct sockaddr *)sin, sizeof (*sin)) < 0) {
349 		perror("bind");
350 		syslog(LOG_ERR, "bind: %m");
351 		close(sock);
352 		return (-1);
353 	}
354 	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
355 		syslog(LOG_ERR, "fcntl O_NONBLOCK: %m\n");
356 	return (sock);
357 }
358