Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / IPXrouted / main.c
1 /*
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (c) 1995 John Hay.  All rights reserved.
6  *
7  * This file includes significant work done at Cornell University by
8  * Bill Nesheim.  That work included by permission.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * $FreeBSD: src/usr.sbin/IPXrouted/main.c,v 1.10 1999/08/28 01:15:03 peter Exp $
39  */
40
41 #ifndef lint
42 static char copyright[] =
43 "@(#) Copyright (c) 1985, 1993\n\
44         The Regents of the University of California.  All rights reserved.\n";
45 #endif /* not lint */
46
47 #ifndef lint
48 static char sccsid[] = "@(#)main.c      8.1 (Berkeley) 6/5/93";
49 #endif /* not lint */
50
51 /*
52  * IPX Routing Information Protocol Daemon
53  */
54 #include "defs.h"
55 #include <sys/time.h>
56
57 #include <net/if.h>
58
59 #include <errno.h>
60 #include <nlist.h>
61 #include <signal.h>
62 #include <paths.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65
66 #define SAP_PKT         0
67 #define RIP_PKT         1
68
69 struct  sockaddr_ipx addr;      /* Daemon's Address */
70 int     ripsock;                /* RIP Socket to listen on */
71 int     sapsock;                /* SAP Socket to listen on */
72 int     kmem;
73 int     install;                /* if 1 call kernel */
74 int     lookforinterfaces;      /* if 1 probe kernel for new up interfaces */
75 int     performnlist;           /* if 1 check if /kernel has changed */
76 int     externalinterfaces;     /* # of remote and local interfaces */
77 int     timeval;                /* local idea of time */
78 int     noteremoterequests;     /* squawk on requests from non-local nets */
79 int     r;                      /* Routing socket to install updates with */
80 struct  sockaddr_ipx ipx_netmask;       /* Used in installing routes */
81
82 char    packet[MAXRXPACKETSIZE+1];
83
84 char    **argv0;
85
86 int     supplier = -1;          /* process should supply updates */
87 int     dosap = 1;              /* By default do SAP services. */
88 int     dobcast = 1;            /* A RIP/SAP broadcast is needed. */
89 time_t  lastbcast;              /* Time of last RIP/SAP broadcast */
90
91 struct  rip *msg = (struct rip *) &packet[sizeof (struct ipx)]; 
92 struct  sap_packet *sap_msg = 
93                 (struct sap_packet *) &packet[sizeof (struct ipx)]; 
94 void    hup(), fkexit(), timer();
95 void    process(int fd, int pkt_type);
96 int     getsocket(int type, int proto, struct sockaddr_ipx *sipx);
97 void    getinfo();
98 void    catchtimer();
99
100 int
101 main(argc, argv)
102         int argc;
103         char *argv[];
104 {
105         int nfds;
106         fd_set fdvar;
107         time_t ttime;
108         struct itimerval tval;
109
110         argv0 = argv;
111         argv++, argc--;
112         while (argc > 0 && **argv == '-') {
113                 if (strcmp(*argv, "-s") == 0) {
114                         supplier = 1;
115                         argv++, argc--;
116                         continue;
117                 }
118                 if (strcmp(*argv, "-q") == 0) {
119                         supplier = 0;
120                         argv++, argc--;
121                         continue;
122                 }
123                 if (strcmp(*argv, "-R") == 0) {
124                         noteremoterequests++;
125                         argv++, argc--;
126                         continue;
127                 }
128                 if (strcmp(*argv, "-S") == 0) {
129                         dosap = 0;
130                         argv++, argc--;
131                         continue;
132                 }
133                 if (strcmp(*argv, "-t") == 0) {
134                         tracepackets++;
135                         argv++, argc--;
136                         ftrace = stderr;
137                         tracing = 1; 
138                         continue;
139                 }
140                 if (strcmp(*argv, "-g") == 0) {
141                         gateway = 1;
142                         argv++, argc--;
143                         continue;
144                 }
145                 if (strcmp(*argv, "-l") == 0) {
146                         gateway = -1;
147                         argv++, argc--;
148                         continue;
149                 }
150                 if (strcmp(*argv, "-N") == 0) {
151                         dognreply = 0;
152                         argv++, argc--;
153                         continue;
154                 }
155                 fprintf(stderr,
156                         "usage: ipxrouted [ -s ] [ -q ] [ -t ] [ -g ] [ -l ] [ -N ]\n");
157                 exit(1);
158         }
159         
160         
161 #ifndef DEBUG
162         if (!tracepackets)
163                 daemon(0, 0);
164 #endif
165         openlog("IPXrouted", LOG_PID, LOG_DAEMON);
166
167         addr.sipx_family = AF_IPX;
168         addr.sipx_len = sizeof(addr);
169         addr.sipx_port = htons(IPXPORT_RIP);
170         ipx_anynet.s_net[0] = ipx_anynet.s_net[1] = -1;
171         ipx_netmask.sipx_addr.x_net = ipx_anynet;
172         ipx_netmask.sipx_len = 6;
173         ipx_netmask.sipx_family = AF_IPX;
174         r = socket(AF_ROUTE, SOCK_RAW, 0);
175         /* later, get smart about lookingforinterfaces */
176         if (r)
177                 shutdown(r, 0); /* for now, don't want reponses */
178         else {
179                 fprintf(stderr, "IPXrouted: no routing socket\n");
180                 exit(1);
181         }
182         ripsock = getsocket(SOCK_DGRAM, 0, &addr);
183         if (ripsock < 0)
184                 exit(1);
185
186         if (dosap) {
187                 addr.sipx_port = htons(IPXPORT_SAP);
188                 sapsock = getsocket(SOCK_DGRAM, 0, &addr);
189                 if (sapsock < 0)
190                         exit(1);
191         } else
192                 sapsock = -1;
193
194         /*
195          * Any extra argument is considered
196          * a tracing log file.
197          */
198         if (argc > 0)
199                 traceon(*argv);
200         /*
201          * Collect an initial view of the world by
202          * snooping in the kernel.  Then, send a request packet on all
203          * directly connected networks to find out what
204          * everyone else thinks.
205          */
206         rtinit();
207         sapinit();
208         ifinit();
209         if (supplier < 0)
210                 supplier = 0;
211         /* request the state of the world */
212         msg->rip_cmd = htons(RIPCMD_REQUEST);
213         msg->rip_nets[0].rip_dst = ipx_anynet;
214         msg->rip_nets[0].rip_metric =  htons(HOPCNT_INFINITY);
215         msg->rip_nets[0].rip_ticks =  htons(-1);
216         toall(sndmsg, NULL, 0);
217
218         if (dosap) {
219                 sap_msg->sap_cmd = htons(SAP_REQ);
220                 sap_msg->sap[0].ServType = htons(SAP_WILDCARD);
221                 toall(sapsndmsg, NULL, 0);
222         }
223
224         signal(SIGALRM, catchtimer);
225         signal(SIGHUP, hup);
226         signal(SIGINT, hup);
227         signal(SIGEMT, fkexit);
228         signal(SIGINFO, getinfo);
229  
230         tval.it_interval.tv_sec = TIMER_RATE;
231         tval.it_interval.tv_usec = 0;
232         tval.it_value.tv_sec = TIMER_RATE;
233         tval.it_value.tv_usec = 0;
234         setitimer(ITIMER_REAL, &tval, NULL);
235
236         nfds = 1 + max(sapsock, ripsock);
237
238         for (;;) {
239                 if (dobcast) {
240                         dobcast = 0;
241                         lastbcast = time(NULL);
242                         timer();
243                 }
244
245                 FD_ZERO(&fdvar);
246                 if (dosap) {
247                         FD_SET(sapsock, &fdvar);
248                 }
249                 FD_SET(ripsock, &fdvar);
250
251                 if(select(nfds, &fdvar, (fd_set *)NULL, (fd_set *)NULL, 
252                    (struct timeval *)NULL) < 0) {
253                         if(errno == EINTR)
254                                 continue;
255                         perror("during select");
256                         exit(1);
257                 }
258
259                 if(FD_ISSET(ripsock, &fdvar))
260                         process(ripsock, RIP_PKT);
261
262                 if(dosap && FD_ISSET(sapsock, &fdvar))
263                         process(sapsock, SAP_PKT);
264
265                 ttime = time(NULL);
266                 if (ttime > (lastbcast + TIMER_RATE + (TIMER_RATE * 2 / 3))) {
267                         dobcast = 1;
268                         syslog(LOG_ERR, "Missed alarm");
269                 }
270         }
271 }
272
273 void
274 process(fd, pkt_type)
275         int fd;
276         int pkt_type;
277 {
278         struct sockaddr from;
279         int fromlen = sizeof (from), cc, omask;
280         struct ipx *ipxdp = (struct ipx *)packet;
281
282         cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen);
283         if (cc <= 0) {
284                 if (cc < 0 && errno != EINTR)
285                         syslog(LOG_ERR, "recvfrom: %m");
286                 return;
287         }
288         if (tracepackets > 1 && ftrace) {
289             fprintf(ftrace,"rcv %d bytes on %s ", 
290                     cc, ipxdp_ntoa(&ipxdp->ipx_dna));
291             fprintf(ftrace," from %s\n", ipxdp_ntoa(&ipxdp->ipx_sna));
292         }
293         
294         if (noteremoterequests && 
295             !ipx_neteqnn(ipxdp->ipx_sna.x_net, ipx_zeronet) &&
296             !ipx_neteq(ipxdp->ipx_sna, ipxdp->ipx_dna))
297         {
298                 syslog(LOG_ERR,
299                        "net of interface (%s) != net on ether (%s)!\n",
300                        ipxdp_nettoa(ipxdp->ipx_dna.x_net),
301                        ipxdp_nettoa(ipxdp->ipx_sna.x_net));
302         }
303                         
304         /* We get the IPX header in front of the RIF packet*/
305         cc -= sizeof (struct ipx);
306 #define mask(s) (1<<((s)-1))
307         omask = sigblock(mask(SIGALRM));
308         switch(pkt_type) {
309                 case SAP_PKT: sap_input(&from, cc);
310                                 break;
311                 case RIP_PKT: rip_input(&from, cc);
312                                 break;
313         }
314         sigsetmask(omask);
315 }
316
317 int
318 getsocket(type, proto, sipx)
319         int type, proto; 
320         struct sockaddr_ipx *sipx;
321 {
322         int domain = sipx->sipx_family;
323         int retry, s, on = 1;
324
325         retry = 1;
326         while ((s = socket(domain, type, proto)) < 0 && retry) {
327                 syslog(LOG_ERR, "socket: %m");
328                 sleep(5 * retry);
329                 retry <<= 1;
330         }
331         if (retry == 0)
332                 return (-1);
333         while (bind(s, (struct sockaddr *)sipx, sizeof (*sipx)) < 0 && retry) {
334                 syslog(LOG_ERR, "bind: %m");
335                 sleep(5 * retry);
336                 retry <<= 1;
337         }
338         if (retry == 0)
339                 return (-1);
340         if (domain==AF_IPX) {
341                 struct ipx ipxdp;
342                 if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) {
343                         syslog(LOG_ERR, "setsockopt SEE HEADERS: %m");
344                         exit(1);
345                 }
346                 if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP)
347                         ipxdp.ipx_pt = IPXPROTO_RI;
348                 else if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_SAP)
349 #ifdef IPXPROTO_SAP
350                         ipxdp.ipx_pt = IPXPROTO_SAP;
351 #else
352                         ipxdp.ipx_pt = IPXPROTO_PXP;
353 #endif
354                 else {
355                         syslog(LOG_ERR, "port should be either RIP or SAP");
356                         exit(1);
357                 }
358                 if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &ipxdp, sizeof(ipxdp))) {
359                         syslog(LOG_ERR, "setsockopt SET HEADER: %m");
360                         exit(1);
361                 }
362         }
363         if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
364                 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
365                 exit(1);
366         }
367         return (s);
368 }
369
370 /*
371  * Fork and exit on EMT-- for profiling.
372  */
373 void
374 fkexit()
375 {
376         if (fork() == 0)
377                 exit(0);
378 }
379
380 void
381 catchtimer()
382 {
383         dobcast = 1;
384 }
385
386 void
387 getinfo()
388 {
389         FILE *fh;
390
391         fh = fopen("/var/log/ipxrouted.dmp", "a");
392         if(fh == NULL)
393                 return;
394
395         dumpriptable(fh);
396         dumpsaptable(fh, sap_head);
397
398         fclose(fh);
399 }
400