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