main.c revision 21673
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1985, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Copyright (c) 1995 John Hay.  All rights reserved.
61573Srgrimes *
71573Srgrimes * This file includes significant work done at Cornell University by
81573Srgrimes * Bill Nesheim.  That work included by permission.
91573Srgrimes *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
181573Srgrimes * 3. All advertising materials mentioning features or use of this software
191573Srgrimes *    must display the following acknowledgement:
201573Srgrimes *	This product includes software developed by the University of
211573Srgrimes *	California, Berkeley and its contributors.
221573Srgrimes * 4. Neither the name of the University nor the names of its contributors
231573Srgrimes *    may be used to endorse or promote products derived from this software
241573Srgrimes *    without specific prior written permission.
251573Srgrimes *
261573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361573Srgrimes * SUCH DAMAGE.
3792986Sobrien *
3892986Sobrien *	$FreeBSD: head/usr.sbin/IPXrouted/main.c 21673 1997-01-14 07:20:47Z jkh $
391573Srgrimes */
401573Srgrimes
411573Srgrimes#ifndef lint
421573Srgrimesstatic char copyright[] =
431573Srgrimes"@(#) Copyright (c) 1985, 1993\n\
441573Srgrimes	The Regents of the University of California.  All rights reserved.\n";
451573Srgrimes#endif /* not lint */
461573Srgrimes
471573Srgrimes#ifndef lint
481573Srgrimesstatic char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/5/93";
491573Srgrimes#endif /* not lint */
501573Srgrimes
511573Srgrimes/*
521573Srgrimes * IPX Routing Information Protocol Daemon
531573Srgrimes */
541573Srgrimes#include "defs.h"
551573Srgrimes#include <sys/time.h>
561573Srgrimes
571573Srgrimes#include <net/if.h>
581573Srgrimes
591573Srgrimes#include <errno.h>
601573Srgrimes#include <nlist.h>
611573Srgrimes#include <signal.h>
621573Srgrimes#include <paths.h>
631573Srgrimes#include <stdlib.h>
641573Srgrimes#include <unistd.h>
651573Srgrimes
661573Srgrimes#define SAP_PKT		0
671573Srgrimes#define RIP_PKT		1
681573Srgrimes
691573Srgrimesstruct	sockaddr_ipx addr;	/* Daemon's Address */
701573Srgrimesint	ripsock;		/* RIP Socket to listen on */
711573Srgrimesint	sapsock;		/* SAP Socket to listen on */
721573Srgrimesint	kmem;
731573Srgrimesint	install;		/* if 1 call kernel */
741573Srgrimesint	lookforinterfaces;	/* if 1 probe kernel for new up interfaces */
751573Srgrimesint	performnlist;		/* if 1 check if /kernel has changed */
761573Srgrimesint	externalinterfaces;	/* # of remote and local interfaces */
771573Srgrimesint	timeval;		/* local idea of time */
781573Srgrimesint	noteremoterequests;	/* squawk on requests from non-local nets */
791573Srgrimesint	r;			/* Routing socket to install updates with */
801573Srgrimesstruct	sockaddr_ipx ipx_netmask;	/* Used in installing routes */
811573Srgrimes
821573Srgrimeschar	packet[MAXRXPACKETSIZE+1];
831573Srgrimes
841573Srgrimeschar	**argv0;
851573Srgrimes
861573Srgrimesint	supplier = -1;		/* process should supply updates */
871573Srgrimesint	dosap = 1;		/* By default do SAP services. */
881573Srgrimes
891573Srgrimesstruct	rip *msg = (struct rip *) &packet[sizeof (struct ipx)];
901573Srgrimesstruct	sap_packet *sap_msg =
911573Srgrimes		(struct sap_packet *) &packet[sizeof (struct ipx)];
921573Srgrimesvoid	hup(), fkexit(), timer();
931573Srgrimesvoid	process(int fd, int pkt_type);
941573Srgrimesint	getsocket(int type, int proto, struct sockaddr_ipx *sipx);
951573Srgrimesvoid	getinfo();
961573Srgrimes
971573Srgrimesint
981573Srgrimesmain(argc, argv)
99	int argc;
100	char *argv[];
101{
102	int nfds;
103	fd_set fdvar;
104
105	argv0 = argv;
106	argv++, argc--;
107	while (argc > 0 && **argv == '-') {
108		if (strcmp(*argv, "-s") == 0) {
109			supplier = 1;
110			argv++, argc--;
111			continue;
112		}
113		if (strcmp(*argv, "-q") == 0) {
114			supplier = 0;
115			argv++, argc--;
116			continue;
117		}
118		if (strcmp(*argv, "-R") == 0) {
119			noteremoterequests++;
120			argv++, argc--;
121			continue;
122		}
123		if (strcmp(*argv, "-S") == 0) {
124			dosap = 0;
125			argv++, argc--;
126			continue;
127		}
128		if (strcmp(*argv, "-t") == 0) {
129			tracepackets++;
130			argv++, argc--;
131			ftrace = stderr;
132			tracing = 1;
133			continue;
134		}
135		if (strcmp(*argv, "-g") == 0) {
136			gateway = 1;
137			argv++, argc--;
138			continue;
139		}
140		if (strcmp(*argv, "-l") == 0) {
141			gateway = -1;
142			argv++, argc--;
143			continue;
144		}
145		fprintf(stderr,
146			"usage: ipxrouted [ -s ] [ -q ] [ -t ] [ -g ] [ -l ]\n");
147		exit(1);
148	}
149
150
151#ifndef DEBUG
152	if (!tracepackets)
153		daemon(0, 0);
154#endif
155	openlog("IPXrouted", LOG_PID, LOG_DAEMON);
156
157	addr.sipx_family = AF_IPX;
158	addr.sipx_len = sizeof(addr);
159	addr.sipx_port = htons(IPXPORT_RIP);
160	ipx_anynet.s_net[0] = ipx_anynet.s_net[1] = -1;
161	ipx_netmask.sipx_addr.x_net = ipx_anynet;
162	ipx_netmask.sipx_len = 6;
163	ipx_netmask.sipx_family = AF_IPX;
164	r = socket(AF_ROUTE, SOCK_RAW, 0);
165	/* later, get smart about lookingforinterfaces */
166	if (r)
167		shutdown(r, 0); /* for now, don't want reponses */
168	else {
169		fprintf(stderr, "IPXrouted: no routing socket\n");
170		exit(1);
171	}
172	ripsock = getsocket(SOCK_DGRAM, 0, &addr);
173	if (ripsock < 0)
174		exit(1);
175
176	if (dosap) {
177		addr.sipx_port = htons(IPXPORT_SAP);
178		sapsock = getsocket(SOCK_DGRAM, 0, &addr);
179		if (sapsock < 0)
180			exit(1);
181	} else
182		sapsock = -1;
183
184	/*
185	 * Any extra argument is considered
186	 * a tracing log file.
187	 */
188	if (argc > 0)
189		traceon(*argv);
190	/*
191	 * Collect an initial view of the world by
192	 * snooping in the kernel.  Then, send a request packet on all
193	 * directly connected networks to find out what
194	 * everyone else thinks.
195	 */
196	rtinit();
197	sapinit();
198	ifinit();
199	if (supplier < 0)
200		supplier = 0;
201	/* request the state of the world */
202	msg->rip_cmd = htons(RIPCMD_REQUEST);
203	msg->rip_nets[0].rip_dst = ipx_anynet;
204	msg->rip_nets[0].rip_metric =  htons(HOPCNT_INFINITY);
205	msg->rip_nets[0].rip_ticks =  htons(-1);
206	toall(sndmsg, NULL);
207
208	if (dosap) {
209		sap_msg->sap_cmd = htons(SAP_REQ);
210		sap_msg->sap[0].ServType = htons(SAP_WILDCARD);
211		toall(sapsndmsg, NULL);
212	}
213
214	signal(SIGALRM, timer);
215	signal(SIGHUP, hup);
216	signal(SIGINT, hup);
217	signal(SIGEMT, fkexit);
218	signal(SIGINFO, getinfo);
219	timer();
220
221	nfds = 1 + max(sapsock, ripsock);
222
223	for (;;) {
224		FD_ZERO(&fdvar);
225		if (dosap) {
226			FD_SET(sapsock, &fdvar);
227		}
228		FD_SET(ripsock, &fdvar);
229
230		if(select(nfds, &fdvar, (fd_set *)NULL, (fd_set *)NULL,
231		   (struct timeval *)NULL) < 0) {
232			if(errno == EINTR)
233				continue;
234			perror("during select");
235			exit(1);
236		}
237
238		if(FD_ISSET(ripsock, &fdvar))
239			process(ripsock, RIP_PKT);
240
241		if(dosap && FD_ISSET(sapsock, &fdvar))
242			process(sapsock, SAP_PKT);
243	}
244}
245
246void
247process(fd, pkt_type)
248	int fd;
249	int pkt_type;
250{
251	struct sockaddr from;
252	int fromlen = sizeof (from), cc, omask;
253	struct ipx *ipxdp = (struct ipx *)packet;
254
255	cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen);
256	if (cc <= 0) {
257		if (cc < 0 && errno != EINTR)
258			syslog(LOG_ERR, "recvfrom: %m");
259		return;
260	}
261	if (tracepackets > 1 && ftrace) {
262	    fprintf(ftrace,"rcv %d bytes on %s ",
263		    cc, ipxdp_ntoa(&ipxdp->ipx_dna));
264	    fprintf(ftrace," from %s\n", ipxdp_ntoa(&ipxdp->ipx_sna));
265	}
266
267	if (noteremoterequests &&
268	    !ipx_neteqnn(ipxdp->ipx_sna.x_net, ipx_zeronet) &&
269	    !ipx_neteq(ipxdp->ipx_sna, ipxdp->ipx_dna))
270	{
271		syslog(LOG_ERR,
272		       "net of interface (%s) != net on ether (%s)!\n",
273		       ipxdp_nettoa(ipxdp->ipx_dna.x_net),
274		       ipxdp_nettoa(ipxdp->ipx_sna.x_net));
275	}
276
277	/* We get the IPX header in front of the RIF packet*/
278	cc -= sizeof (struct ipx);
279#define	mask(s)	(1<<((s)-1))
280	omask = sigblock(mask(SIGALRM));
281	switch(pkt_type) {
282		case SAP_PKT: sap_input(&from, cc);
283				break;
284		case RIP_PKT: rip_input(&from, cc);
285				break;
286	}
287	sigsetmask(omask);
288}
289
290int
291getsocket(type, proto, sipx)
292	int type, proto;
293	struct sockaddr_ipx *sipx;
294{
295	int domain = sipx->sipx_family;
296	int retry, s, on = 1;
297
298	retry = 1;
299	while ((s = socket(domain, type, proto)) < 0 && retry) {
300		syslog(LOG_ERR, "socket: %m");
301		sleep(5 * retry);
302		retry <<= 1;
303	}
304	if (retry == 0)
305		return (-1);
306	while (bind(s, (struct sockaddr *)sipx, sizeof (*sipx)) < 0 && retry) {
307		syslog(LOG_ERR, "bind: %m");
308		sleep(5 * retry);
309		retry <<= 1;
310	}
311	if (retry == 0)
312		return (-1);
313	if (domain==AF_IPX) {
314		struct ipx ipxdp;
315		if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) {
316			syslog(LOG_ERR, "setsockopt SEE HEADERS: %m");
317			exit(1);
318		}
319		if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP)
320			ipxdp.ipx_pt = IPXPROTO_RI;
321		else if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_SAP)
322			ipxdp.ipx_pt = IPXPROTO_SAP;
323		else {
324			syslog(LOG_ERR, "port should be either RIP or SAP");
325			exit(1);
326		}
327		if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &ipxdp, sizeof(ipxdp))) {
328			syslog(LOG_ERR, "setsockopt SET HEADER: %m");
329			exit(1);
330		}
331	}
332	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
333		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
334		exit(1);
335	}
336	return (s);
337}
338
339/*
340 * Fork and exit on EMT-- for profiling.
341 */
342void
343fkexit()
344{
345	if (fork() == 0)
346		exit(0);
347}
348
349void
350getinfo()
351{
352	FILE *fh;
353
354	fh = fopen("/tmp/ipxrouted.dmp", "a");
355	if(fh == NULL)
356		return;
357
358	dumpriptable(fh);
359	dumpsaptable(fh, sap_head);
360
361	fclose(fh);
362}
363
364