xref: /original-bsd/sbin/routed/main.c (revision 6160dbd1)
1b145e70aSdist /*
2*6160dbd1Sbostic  * Copyright (c) 1983, 1988, 1993
3*6160dbd1Sbostic  *	The Regents of the University of California.  All rights reserved.
4cf60b4c8Sbostic  *
54839fc90Sbostic  * %sccs.include.redist.c%
6b145e70aSdist  */
7b145e70aSdist 
8b32f961cSsam #ifndef lint
9*6160dbd1Sbostic static char copyright[] =
10*6160dbd1Sbostic "@(#) Copyright (c) 1983, 1988, 1993\n\
11*6160dbd1Sbostic 	The Regents of the University of California.  All rights reserved.\n";
12cf60b4c8Sbostic #endif /* not lint */
13b145e70aSdist 
14b145e70aSdist #ifndef lint
15*6160dbd1Sbostic static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 06/05/93";
16cf60b4c8Sbostic #endif /* not lint */
17b32f961cSsam 
18b32f961cSsam /*
19b32f961cSsam  * Routing Table Management Daemon
20b32f961cSsam  */
21ed1accc4Ssam #include "defs.h"
22b32f961cSsam #include <sys/ioctl.h>
23a3f24c4fSkarels #include <sys/file.h>
24d42ef91bSsam 
25b32f961cSsam #include <net/if.h>
26d42ef91bSsam 
27f14ff292Skarels #include <sys/errno.h>
28f14ff292Skarels #include <sys/signal.h>
29f14ff292Skarels #include <sys/syslog.h>
3021220a3bSbostic #include "pathnames.h"
31b32f961cSsam 
32b32f961cSsam int	supplier = -1;		/* process should supply updates */
33ff81b6eeSkarels int	gateway = 0;		/* 1 if we are a gateway to parts beyond */
3475b2916aSkarels int	debug = 0;
35a3f24c4fSkarels int	bufspace = 127*1024;	/* max. input buffer size to request */
36b32f961cSsam 
37b32f961cSsam struct	rip *msg = (struct rip *)packet;
38bf89ec77Sbostic void	hup(), rtdeleteall(), sigtrace(), timer();
39b32f961cSsam 
main(argc,argv)40b32f961cSsam main(argc, argv)
41b32f961cSsam 	int argc;
42b32f961cSsam 	char *argv[];
43b32f961cSsam {
44a3f24c4fSkarels 	int n, cc, nfd, omask, tflags = 0;
45b32f961cSsam 	struct sockaddr from;
46a3f24c4fSkarels 	struct timeval *tvp, waittime;
47a3f24c4fSkarels 	struct itimerval itval;
4826c6a036Skarels 	register struct rip *query = msg;
49a3f24c4fSkarels 	fd_set ibits;
50ed1accc4Ssam 	u_char retry;
51b32f961cSsam 
52b32f961cSsam 	argv0 = argv;
532a9f6210Skarels #if BSD >= 43
54372bf17dSeric 	openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
55e24d5e3eSkarels 	setlogmask(LOG_UPTO(LOG_WARNING));
562a9f6210Skarels #else
572a9f6210Skarels 	openlog("routed", LOG_PID);
582a9f6210Skarels #define LOG_UPTO(x) (x)
592a9f6210Skarels #define setlogmask(x) (x)
602a9f6210Skarels #endif
61b32f961cSsam 	sp = getservbyname("router", "udp");
62ed1accc4Ssam 	if (sp == NULL) {
63ed1accc4Ssam 		fprintf(stderr, "routed: router/udp: unknown service\n");
64b32f961cSsam 		exit(1);
65b32f961cSsam 	}
66ed1accc4Ssam 	addr.sin_family = AF_INET;
67ed1accc4Ssam 	addr.sin_port = sp->s_port;
68204c22bdSsklower 	r = socket(AF_ROUTE, SOCK_RAW, 0);
69204c22bdSsklower 	/* later, get smart about lookingforinterfaces */
70204c22bdSsklower 	if (r)
71204c22bdSsklower 		shutdown(r, 0); /* for now, don't want reponses */
72204c22bdSsklower 	else {
73204c22bdSsklower 		fprintf(stderr, "routed: no routing socket\n");
74204c22bdSsklower 		exit(1);
75204c22bdSsklower 	}
76ed1accc4Ssam 	s = getsocket(AF_INET, SOCK_DGRAM, &addr);
77ed1accc4Ssam 	if (s < 0)
78ed1accc4Ssam 		exit(1);
79b32f961cSsam 	argv++, argc--;
80b32f961cSsam 	while (argc > 0 && **argv == '-') {
814ec9493eSsam 		if (strcmp(*argv, "-s") == 0) {
82b32f961cSsam 			supplier = 1;
83b32f961cSsam 			argv++, argc--;
84b32f961cSsam 			continue;
85b32f961cSsam 		}
864ec9493eSsam 		if (strcmp(*argv, "-q") == 0) {
87b32f961cSsam 			supplier = 0;
88b32f961cSsam 			argv++, argc--;
89b32f961cSsam 			continue;
90b32f961cSsam 		}
914ec9493eSsam 		if (strcmp(*argv, "-t") == 0) {
92a3f24c4fSkarels 			tflags++;
93e24d5e3eSkarels 			setlogmask(LOG_UPTO(LOG_DEBUG));
94c20362dfSkarels 			argv++, argc--;
95c20362dfSkarels 			continue;
96c20362dfSkarels 		}
97c20362dfSkarels 		if (strcmp(*argv, "-d") == 0) {
9875b2916aSkarels 			debug++;
99e24d5e3eSkarels 			setlogmask(LOG_UPTO(LOG_DEBUG));
1004ec9493eSsam 			argv++, argc--;
1014ec9493eSsam 			continue;
1024ec9493eSsam 		}
1031e60868bSkarels 		if (strcmp(*argv, "-g") == 0) {
1041e60868bSkarels 			gateway = 1;
1051e60868bSkarels 			argv++, argc--;
1061e60868bSkarels 			continue;
1071e60868bSkarels 		}
1081e60868bSkarels 		fprintf(stderr,
109ff81b6eeSkarels 			"usage: routed [ -s ] [ -q ] [ -t ] [ -g ]\n");
110b32f961cSsam 		exit(1);
111b32f961cSsam 	}
112a3f24c4fSkarels 
11324ac670aSsklower 	if (debug == 0 && tflags == 0)
114b87ed4a6Skarels 		daemon(0, 0);
115b32f961cSsam 	/*
116ed1accc4Ssam 	 * Any extra argument is considered
117ed1accc4Ssam 	 * a tracing log file.
118ed1accc4Ssam 	 */
119ed1accc4Ssam 	if (argc > 0)
120ed1accc4Ssam 		traceon(*argv);
121a3f24c4fSkarels 	while (tflags-- > 0)
122a3f24c4fSkarels 		bumploglevel();
123a3f24c4fSkarels 
124a3f24c4fSkarels 	(void) gettimeofday(&now, (struct timezone *)NULL);
125ed1accc4Ssam 	/*
126b32f961cSsam 	 * Collect an initial view of the world by
127ff81b6eeSkarels 	 * checking the interface configuration and the gateway kludge
128b32f961cSsam 	 * file.  Then, send a request packet on all
129b32f961cSsam 	 * directly connected networks to find out what
130b32f961cSsam 	 * everyone else thinks.
131b32f961cSsam 	 */
132b32f961cSsam 	rtinit();
133b32f961cSsam 	ifinit();
134a1e3e55aSkarels 	gwkludge();
135ff81b6eeSkarels 	if (gateway > 0)
136ff81b6eeSkarels 		rtdefault();
137b32f961cSsam 	if (supplier < 0)
138b32f961cSsam 		supplier = 0;
13926c6a036Skarels 	query->rip_cmd = RIPCMD_REQUEST;
14026c6a036Skarels 	query->rip_vers = RIPVERSION;
14126c6a036Skarels 	if (sizeof(query->rip_nets[0].rip_dst.sa_family) > 1)	/* XXX */
14226c6a036Skarels 		query->rip_nets[0].rip_dst.sa_family = htons((u_short)AF_UNSPEC);
143a3f24c4fSkarels 	else
14426c6a036Skarels 		query->rip_nets[0].rip_dst.sa_family = AF_UNSPEC;
14526c6a036Skarels 	query->rip_nets[0].rip_metric = htonl((u_long)HOPCNT_INFINITY);
146bf89ec77Sbostic 	toall(sndmsg);
147c3040880Ssam 	signal(SIGALRM, timer);
1484dd93130Skarels 	signal(SIGHUP, hup);
149ca8660ddSkarels 	signal(SIGTERM, hup);
15075b2916aSkarels 	signal(SIGINT, rtdeleteall);
151f14ff292Skarels 	signal(SIGUSR1, sigtrace);
152f14ff292Skarels 	signal(SIGUSR2, sigtrace);
153a3f24c4fSkarels 	itval.it_interval.tv_sec = TIMER_RATE;
154a3f24c4fSkarels 	itval.it_value.tv_sec = TIMER_RATE;
155a3f24c4fSkarels 	itval.it_interval.tv_usec = 0;
156a3f24c4fSkarels 	itval.it_value.tv_usec = 0;
157a3f24c4fSkarels 	srandom(getpid());
158a3f24c4fSkarels 	if (setitimer(ITIMER_REAL, &itval, (struct itimerval *)NULL) < 0)
159a3f24c4fSkarels 		syslog(LOG_ERR, "setitimer: %m\n");
160b32f961cSsam 
161a3f24c4fSkarels 	FD_ZERO(&ibits);
162a3f24c4fSkarels 	nfd = s + 1;			/* 1 + max(fd's) */
163b32f961cSsam 	for (;;) {
164a3f24c4fSkarels 		FD_SET(s, &ibits);
165a3f24c4fSkarels 		/*
166a3f24c4fSkarels 		 * If we need a dynamic update that was held off,
167a3f24c4fSkarels 		 * needupdate will be set, and nextbcast is the time
168a3f24c4fSkarels 		 * by which we want select to return.  Compute time
169a3f24c4fSkarels 		 * until dynamic update should be sent, and select only
170a3f24c4fSkarels 		 * until then.  If we have already passed nextbcast,
171a3f24c4fSkarels 		 * just poll.
172a3f24c4fSkarels 		 */
173a3f24c4fSkarels 		if (needupdate) {
174a3f24c4fSkarels 			waittime = nextbcast;
175a3f24c4fSkarels 			timevalsub(&waittime, &now);
176a3f24c4fSkarels 			if (waittime.tv_sec < 0) {
177a3f24c4fSkarels 				waittime.tv_sec = 0;
178a3f24c4fSkarels 				waittime.tv_usec = 0;
179a3f24c4fSkarels 			}
180a3f24c4fSkarels 			if (traceactions)
181a3f24c4fSkarels 				fprintf(ftrace,
182a3f24c4fSkarels 				 "select until dynamic update %d/%d sec/usec\n",
183a3f24c4fSkarels 				    waittime.tv_sec, waittime.tv_usec);
184a3f24c4fSkarels 			tvp = &waittime;
185a3f24c4fSkarels 		} else
186a3f24c4fSkarels 			tvp = (struct timeval *)NULL;
187a3f24c4fSkarels 		n = select(nfd, &ibits, 0, 0, tvp);
188a3f24c4fSkarels 		if (n <= 0) {
189a3f24c4fSkarels 			/*
190a3f24c4fSkarels 			 * Need delayed dynamic update if select returned
191a3f24c4fSkarels 			 * nothing and we timed out.  Otherwise, ignore
192a3f24c4fSkarels 			 * errors (e.g. EINTR).
193a3f24c4fSkarels 			 */
194a3f24c4fSkarels 			if (n < 0) {
195a3f24c4fSkarels 				if (errno == EINTR)
196b32f961cSsam 					continue;
197a3f24c4fSkarels 				syslog(LOG_ERR, "select: %m");
198a3f24c4fSkarels 			}
199a3f24c4fSkarels 			omask = sigblock(sigmask(SIGALRM));
200a3f24c4fSkarels 			if (n == 0 && needupdate) {
201a3f24c4fSkarels 				if (traceactions)
202a3f24c4fSkarels 					fprintf(ftrace,
203a3f24c4fSkarels 					    "send delayed dynamic update\n");
204a3f24c4fSkarels 				(void) gettimeofday(&now,
205a3f24c4fSkarels 					    (struct timezone *)NULL);
206a3f24c4fSkarels 				toall(supply, RTS_CHANGED,
207a3f24c4fSkarels 				    (struct interface *)NULL);
208a3f24c4fSkarels 				lastbcast = now;
209a3f24c4fSkarels 				needupdate = 0;
210a3f24c4fSkarels 				nextbcast.tv_sec = 0;
211a3f24c4fSkarels 			}
212a3f24c4fSkarels 			sigsetmask(omask);
213a3f24c4fSkarels 			continue;
214a3f24c4fSkarels 		}
215a3f24c4fSkarels 		(void) gettimeofday(&now, (struct timezone *)NULL);
216554657d9Skarels 		omask = sigblock(sigmask(SIGALRM));
217a3f24c4fSkarels #ifdef doesntwork
218a3f24c4fSkarels /*
219a3f24c4fSkarels printf("s %d, ibits %x index %d, mod %d, sh %x, or %x &ibits %x\n",
220a3f24c4fSkarels 	s,
221a3f24c4fSkarels 	ibits.fds_bits[0],
222a3f24c4fSkarels 	(s)/(sizeof(fd_mask) * 8),
223a3f24c4fSkarels 	((s) % (sizeof(fd_mask) * 8)),
224a3f24c4fSkarels 	(1 << ((s) % (sizeof(fd_mask) * 8))),
225a3f24c4fSkarels 	ibits.fds_bits[(s)/(sizeof(fd_mask) * 8)] & (1 << ((s) % (sizeof(fd_mask) * 8))),
226a3f24c4fSkarels 	&ibits
227a3f24c4fSkarels 	);
228a3f24c4fSkarels */
229a3f24c4fSkarels 		if (FD_ISSET(s, &ibits))
230a3f24c4fSkarels #else
231a3f24c4fSkarels 		if (ibits.fds_bits[s/32] & (1 << s))
232a3f24c4fSkarels #endif
233b32f961cSsam 			process(s);
234ed1accc4Ssam 		/* handle ICMP redirects */
235554657d9Skarels 		sigsetmask(omask);
236b32f961cSsam 	}
237b32f961cSsam }
238b32f961cSsam 
239a3f24c4fSkarels timevaladd(t1, t2)
240a3f24c4fSkarels 	struct timeval *t1, *t2;
241a3f24c4fSkarels {
242a3f24c4fSkarels 
243a3f24c4fSkarels 	t1->tv_sec += t2->tv_sec;
244a3f24c4fSkarels 	if ((t1->tv_usec += t2->tv_usec) > 1000000) {
245a3f24c4fSkarels 		t1->tv_sec++;
246a3f24c4fSkarels 		t1->tv_usec -= 1000000;
247a3f24c4fSkarels 	}
248a3f24c4fSkarels }
249a3f24c4fSkarels 
250a3f24c4fSkarels timevalsub(t1, t2)
251a3f24c4fSkarels 	struct timeval *t1, *t2;
252a3f24c4fSkarels {
253a3f24c4fSkarels 
254a3f24c4fSkarels 	t1->tv_sec -= t2->tv_sec;
255a3f24c4fSkarels 	if ((t1->tv_usec -= t2->tv_usec) < 0) {
256a3f24c4fSkarels 		t1->tv_sec--;
257a3f24c4fSkarels 		t1->tv_usec += 1000000;
258a3f24c4fSkarels 	}
259a3f24c4fSkarels }
260a3f24c4fSkarels 
process(fd)261b32f961cSsam process(fd)
262b32f961cSsam 	int fd;
263b32f961cSsam {
264b32f961cSsam 	struct sockaddr from;
265554657d9Skarels 	int fromlen, cc;
266554657d9Skarels 	union {
267554657d9Skarels 		char	buf[MAXPACKETSIZE+1];
268554657d9Skarels 		struct	rip rip;
269554657d9Skarels 	} inbuf;
270b32f961cSsam 
271a3f24c4fSkarels 	for (;;) {
272a3f24c4fSkarels 		fromlen = sizeof (from);
273554657d9Skarels 		cc = recvfrom(fd, &inbuf, sizeof (inbuf), 0, &from, &fromlen);
274b32f961cSsam 		if (cc <= 0) {
275a3f24c4fSkarels 			if (cc < 0 && errno != EWOULDBLOCK)
276ed1accc4Ssam 				perror("recvfrom");
277a3f24c4fSkarels 			break;
278b32f961cSsam 		}
279ed1accc4Ssam 		if (fromlen != sizeof (struct sockaddr_in))
280a3f24c4fSkarels 			break;
281554657d9Skarels 		rip_input(&from, &inbuf.rip, cc);
282b32f961cSsam 	}
283a3f24c4fSkarels }
284ed1accc4Ssam 
getsocket(domain,type,sin)285ed1accc4Ssam getsocket(domain, type, sin)
286ed1accc4Ssam 	int domain, type;
287ed1accc4Ssam 	struct sockaddr_in *sin;
288ed1accc4Ssam {
289a3f24c4fSkarels 	int sock, on = 1;
290ed1accc4Ssam 
291a3f24c4fSkarels 	if ((sock = socket(domain, type, 0)) < 0) {
292ed1accc4Ssam 		perror("socket");
293ca8660ddSkarels 		syslog(LOG_ERR, "socket: %m");
294ed1accc4Ssam 		return (-1);
295ca8660ddSkarels 	}
2962a9f6210Skarels #ifdef SO_BROADCAST
297a3f24c4fSkarels 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
298ca8660ddSkarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
299a3f24c4fSkarels 		close(sock);
30076c72697Skarels 		return (-1);
301ca8660ddSkarels 	}
3022a9f6210Skarels #endif
3032a9f6210Skarels #ifdef SO_RCVBUF
304a3f24c4fSkarels 	for (on = bufspace; ; on -= 1024) {
305a3f24c4fSkarels 		if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
306a3f24c4fSkarels 		    &on, sizeof (on)) == 0)
307a3f24c4fSkarels 			break;
308a3f24c4fSkarels 		if (on <= 8*1024) {
309a1e3e55aSkarels 			syslog(LOG_ERR, "setsockopt SO_RCVBUF: %m");
310a3f24c4fSkarels 			break;
311a3f24c4fSkarels 		}
312a3f24c4fSkarels 	}
313a3f24c4fSkarels 	if (traceactions)
314a3f24c4fSkarels 		fprintf(ftrace, "recv buf %d\n", on);
3152a9f6210Skarels #endif
316bf89ec77Sbostic 	if (bind(sock, (struct sockaddr *)sin, sizeof (*sin)) < 0) {
317ed1accc4Ssam 		perror("bind");
318ca8660ddSkarels 		syslog(LOG_ERR, "bind: %m");
319a3f24c4fSkarels 		close(sock);
320ed1accc4Ssam 		return (-1);
321ca8660ddSkarels 	}
3228bd0397eSbostic 	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
3238bd0397eSbostic 		syslog(LOG_ERR, "fcntl O_NONBLOCK: %m\n");
324a3f24c4fSkarels 	return (sock);
325ed1accc4Ssam }
326