]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/IPXrouted/main.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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$
39  */
40
41 #ifndef lint
42 static const 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 const 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, SHUT_RD); /* 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  cc, omask;
280         socklen_t fromlen = sizeof (from);
281         struct ipx *ipxdp = (struct ipx *)packet;
282
283         cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen);
284         if (cc <= 0) {
285                 if (cc < 0 && errno != EINTR)
286                         syslog(LOG_ERR, "recvfrom: %m");
287                 return;
288         }
289         if (tracepackets > 1 && ftrace) {
290             fprintf(ftrace,"rcv %d bytes on %s ", 
291                     cc, ipxdp_ntoa(&ipxdp->ipx_dna));
292             fprintf(ftrace," from %s\n", ipxdp_ntoa(&ipxdp->ipx_sna));
293         }
294         
295         if (noteremoterequests && 
296             !ipx_neteqnn(ipxdp->ipx_sna.x_net, ipx_zeronet) &&
297             !ipx_neteq(ipxdp->ipx_sna, ipxdp->ipx_dna))
298         {
299                 syslog(LOG_ERR,
300                        "net of interface (%s) != net on ether (%s)!\n",
301                        ipxdp_nettoa(ipxdp->ipx_dna.x_net),
302                        ipxdp_nettoa(ipxdp->ipx_sna.x_net));
303         }
304                         
305         /* We get the IPX header in front of the RIF packet*/
306         cc -= sizeof (struct ipx);
307 #define mask(s) (1<<((s)-1))
308         omask = sigblock(mask(SIGALRM));
309         switch(pkt_type) {
310                 case SAP_PKT: sap_input(&from, cc);
311                                 break;
312                 case RIP_PKT: rip_input(&from, cc);
313                                 break;
314         }
315         sigsetmask(omask);
316 }
317
318 int
319 getsocket(type, proto, sipx)
320         int type, proto; 
321         struct sockaddr_ipx *sipx;
322 {
323         int domain = sipx->sipx_family;
324         int retry, s, on = 1;
325
326         retry = 1;
327         while ((s = socket(domain, type, proto)) < 0 && retry) {
328                 syslog(LOG_ERR, "socket: %m");
329                 sleep(5 * retry);
330                 retry <<= 1;
331         }
332         if (retry == 0)
333                 return (-1);
334         while (bind(s, (struct sockaddr *)sipx, sizeof (*sipx)) < 0 && retry) {
335                 syslog(LOG_ERR, "bind: %m");
336                 sleep(5 * retry);
337                 retry <<= 1;
338         }
339         if (retry == 0)
340                 return (-1);
341         if (domain==AF_IPX) {
342                 struct ipx ipxdp;
343                 if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) {
344                         syslog(LOG_ERR, "setsockopt SEE HEADERS: %m");
345                         exit(1);
346                 }
347                 if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP)
348                         ipxdp.ipx_pt = IPXPROTO_RI;
349                 else if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_SAP)
350 #ifdef IPXPROTO_SAP
351                         ipxdp.ipx_pt = IPXPROTO_SAP;
352 #else
353                         ipxdp.ipx_pt = IPXPROTO_PXP;
354 #endif
355                 else {
356                         syslog(LOG_ERR, "port should be either RIP or SAP");
357                         exit(1);
358                 }
359                 if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &ipxdp, sizeof(ipxdp))) {
360                         syslog(LOG_ERR, "setsockopt SET HEADER: %m");
361                         exit(1);
362                 }
363         }
364         if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
365                 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
366                 exit(1);
367         }
368         return (s);
369 }
370
371 /*
372  * Fork and exit on EMT-- for profiling.
373  */
374 void
375 fkexit()
376 {
377         if (fork() == 0)
378                 exit(0);
379 }
380
381 void
382 catchtimer()
383 {
384         dobcast = 1;
385 }
386
387 void
388 getinfo()
389 {
390         FILE *fh;
391
392         fh = fopen("/var/log/ipxrouted.dmp", "a");
393         if(fh == NULL)
394                 return;
395
396         dumpriptable(fh);
397         dumpsaptable(fh, sap_head);
398
399         fclose(fh);
400 }
401