2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1995 John Hay. All rights reserved.
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim. That work included by permission.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
42 static const char copyright[] =
43 "@(#) Copyright (c) 1985, 1993\n\
44 The Regents of the University of California. All rights reserved.\n";
48 static const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/5/93";
52 * IPX Routing Information Protocol Daemon
69 struct sockaddr_ipx addr; /* Daemon's Address */
70 int ripsock; /* RIP Socket to listen on */
71 int sapsock; /* SAP Socket to listen on */
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 */
82 char packet[MAXRXPACKETSIZE+1];
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 */
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);
108 struct itimerval tval;
112 while (argc > 0 && **argv == '-') {
113 if (strcmp(*argv, "-s") == 0) {
118 if (strcmp(*argv, "-q") == 0) {
123 if (strcmp(*argv, "-R") == 0) {
124 noteremoterequests++;
128 if (strcmp(*argv, "-S") == 0) {
133 if (strcmp(*argv, "-t") == 0) {
140 if (strcmp(*argv, "-g") == 0) {
145 if (strcmp(*argv, "-l") == 0) {
150 if (strcmp(*argv, "-N") == 0) {
156 "usage: ipxrouted [ -s ] [ -q ] [ -t ] [ -g ] [ -l ] [ -N ]\n");
165 openlog("IPXrouted", LOG_PID, LOG_DAEMON);
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 */
177 shutdown(r, SHUT_RD); /* for now, don't want reponses */
179 fprintf(stderr, "IPXrouted: no routing socket\n");
182 ripsock = getsocket(SOCK_DGRAM, 0, &addr);
187 addr.sipx_port = htons(IPXPORT_SAP);
188 sapsock = getsocket(SOCK_DGRAM, 0, &addr);
195 * Any extra argument is considered
196 * a tracing log file.
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.
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);
219 sap_msg->sap_cmd = htons(SAP_REQ);
220 sap_msg->sap[0].ServType = htons(SAP_WILDCARD);
221 toall(sapsndmsg, NULL, 0);
224 signal(SIGALRM, catchtimer);
227 signal(SIGEMT, fkexit);
228 signal(SIGINFO, getinfo);
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);
236 nfds = 1 + max(sapsock, ripsock);
241 lastbcast = time(NULL);
247 FD_SET(sapsock, &fdvar);
249 FD_SET(ripsock, &fdvar);
251 if(select(nfds, &fdvar, (fd_set *)NULL, (fd_set *)NULL,
252 (struct timeval *)NULL) < 0) {
255 perror("during select");
259 if(FD_ISSET(ripsock, &fdvar))
260 process(ripsock, RIP_PKT);
262 if(dosap && FD_ISSET(sapsock, &fdvar))
263 process(sapsock, SAP_PKT);
266 if (ttime > (lastbcast + TIMER_RATE + (TIMER_RATE * 2 / 3))) {
268 syslog(LOG_ERR, "Missed alarm");
274 process(fd, pkt_type)
278 struct sockaddr from;
280 socklen_t fromlen = sizeof (from);
281 struct ipx *ipxdp = (struct ipx *)packet;
283 cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen);
285 if (cc < 0 && errno != EINTR)
286 syslog(LOG_ERR, "recvfrom: %m");
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));
295 if (noteremoterequests &&
296 !ipx_neteqnn(ipxdp->ipx_sna.x_net, ipx_zeronet) &&
297 !ipx_neteq(ipxdp->ipx_sna, ipxdp->ipx_dna))
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));
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));
310 case SAP_PKT: sap_input(&from, cc);
312 case RIP_PKT: rip_input(&from, cc);
319 getsocket(type, proto, sipx)
321 struct sockaddr_ipx *sipx;
323 int domain = sipx->sipx_family;
324 int retry, s, on = 1;
327 while ((s = socket(domain, type, proto)) < 0 && retry) {
328 syslog(LOG_ERR, "socket: %m");
334 while (bind(s, (struct sockaddr *)sipx, sizeof (*sipx)) < 0 && retry) {
335 syslog(LOG_ERR, "bind: %m");
341 if (domain==AF_IPX) {
343 if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) {
344 syslog(LOG_ERR, "setsockopt SEE HEADERS: %m");
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)
351 ipxdp.ipx_pt = IPXPROTO_SAP;
353 ipxdp.ipx_pt = IPXPROTO_PXP;
356 syslog(LOG_ERR, "port should be either RIP or SAP");
359 if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &ipxdp, sizeof(ipxdp))) {
360 syslog(LOG_ERR, "setsockopt SET HEADER: %m");
364 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
365 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
372 * Fork and exit on EMT-- for profiling.
392 fh = fopen("/var/log/ipxrouted.dmp", "a");
397 dumpsaptable(fh, sap_head);