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