main.c revision 11820
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 * $Id: main.c,v 1.6 1995/10/11 18:57:21 jhay Exp $ 39 */ 40 41#ifndef lint 42static 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 48static 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 69struct sockaddr_ipx addr; /* Daemon's Address */ 70int ripsock; /* RIP Socket to listen on */ 71int sapsock; /* SAP Socket to listen on */ 72int kmem; 73int install; /* if 1 call kernel */ 74int lookforinterfaces; /* if 1 probe kernel for new up interfaces */ 75int performnlist; /* if 1 check if /kernel has changed */ 76int externalinterfaces; /* # of remote and local interfaces */ 77int timeval; /* local idea of time */ 78int noteremoterequests; /* squawk on requests from non-local nets */ 79int r; /* Routing socket to install updates with */ 80struct sockaddr_ipx ipx_netmask; /* Used in installing routes */ 81 82char packet[MAXPACKETSIZE+sizeof(struct ipxdp)+1]; 83 84char **argv0; 85 86int supplier = -1; /* process should supply updates */ 87int dosap = 1; /* By default do SAP services. */ 88 89struct rip *msg = (struct rip *) &packet[sizeof (struct ipxdp)]; 90struct sap_packet *sap_msg = 91 (struct sap_packet *) &packet[sizeof (struct ipxdp)]; 92void hup(), fkexit(), timer(); 93void process(int fd, int pkt_type); 94int getsocket(int type, int proto, struct sockaddr_ipx *sipx); 95 96int 97main(argc, argv) 98 int argc; 99 char *argv[]; 100{ 101 int nfds; 102 fd_set fdvar; 103 104 argv0 = argv; 105 argv++, argc--; 106 while (argc > 0 && **argv == '-') { 107 if (strcmp(*argv, "-s") == 0) { 108 supplier = 1; 109 argv++, argc--; 110 continue; 111 } 112 if (strcmp(*argv, "-q") == 0) { 113 supplier = 0; 114 argv++, argc--; 115 continue; 116 } 117 if (strcmp(*argv, "-R") == 0) { 118 noteremoterequests++; 119 argv++, argc--; 120 continue; 121 } 122 if (strcmp(*argv, "-S") == 0) { 123 dosap = 0; 124 argv++, argc--; 125 continue; 126 } 127 if (strcmp(*argv, "-t") == 0) { 128 tracepackets++; 129 argv++, argc--; 130 ftrace = stderr; 131 tracing = 1; 132 continue; 133 } 134 if (strcmp(*argv, "-g") == 0) { 135 gateway = 1; 136 argv++, argc--; 137 continue; 138 } 139 if (strcmp(*argv, "-l") == 0) { 140 gateway = -1; 141 argv++, argc--; 142 continue; 143 } 144 fprintf(stderr, 145 "usage: ipxrouted [ -s ] [ -q ] [ -t ] [ -g ] [ -l ]\n"); 146 exit(1); 147 } 148 149 150#ifndef DEBUG 151 if (!tracepackets) 152 daemon(0, 0); 153#endif 154 openlog("IPXrouted", LOG_PID, LOG_DAEMON); 155 156 addr.sipx_family = AF_IPX; 157 addr.sipx_len = sizeof(addr); 158 addr.sipx_port = htons(IPXPORT_RIP); 159 ipx_anynet.s_net[0] = ipx_anynet.s_net[1] = -1; 160 ipx_netmask.sipx_addr.x_net = ipx_anynet; 161 ipx_netmask.sipx_len = 6; 162 ipx_netmask.sipx_family = AF_IPX; 163 r = socket(AF_ROUTE, SOCK_RAW, 0); 164 /* later, get smart about lookingforinterfaces */ 165 if (r) 166 shutdown(r, 0); /* for now, don't want reponses */ 167 else { 168 fprintf(stderr, "IPXrouted: no routing socket\n"); 169 exit(1); 170 } 171 ripsock = getsocket(SOCK_DGRAM, 0, &addr); 172 if (ripsock < 0) 173 exit(1); 174 175 if (dosap) { 176 addr.sipx_port = htons(IPXPORT_SAP); 177 sapsock = getsocket(SOCK_DGRAM, 0, &addr); 178 if (sapsock < 0) 179 exit(1); 180 } else 181 sapsock = -1; 182 183 /* 184 * Any extra argument is considered 185 * a tracing log file. 186 */ 187 if (argc > 0) 188 traceon(*argv); 189 /* 190 * Collect an initial view of the world by 191 * snooping in the kernel. Then, send a request packet on all 192 * directly connected networks to find out what 193 * everyone else thinks. 194 */ 195 rtinit(); 196 sapinit(); 197 ifinit(); 198 if (supplier < 0) 199 supplier = 0; 200 /* request the state of the world */ 201 msg->rip_cmd = htons(RIPCMD_REQUEST); 202 msg->rip_nets[0].rip_dst = ipx_anynet; 203 msg->rip_nets[0].rip_metric = htons(HOPCNT_INFINITY); 204 msg->rip_nets[0].rip_ticks = htons(-1); 205 toall(sndmsg, NULL); 206 207 if (dosap) { 208 sap_msg->sap_cmd = htons(SAP_REQ); 209 sap_msg->sap[0].ServType = htons(SAP_WILDCARD); 210 toall(sapsndmsg, NULL); 211 } 212 213 signal(SIGALRM, timer); 214 signal(SIGHUP, hup); 215 signal(SIGINT, hup); 216 signal(SIGEMT, fkexit); 217 timer(); 218 219 nfds = 1 + max(sapsock, ripsock); 220 221 for (;;) { 222 FD_ZERO(&fdvar); 223 if (dosap) { 224 FD_SET(sapsock, &fdvar); 225 } 226 FD_SET(ripsock, &fdvar); 227 228 if(select(nfds, &fdvar, (fd_set *)NULL, (fd_set *)NULL, 229 (struct timeval *)NULL) < 0) { 230 if(errno != EINTR) { 231 perror("during select"); 232 exit(1); 233 } 234 } 235 236 if(FD_ISSET(ripsock, &fdvar)) 237 process(ripsock, RIP_PKT); 238 239 if(dosap && FD_ISSET(sapsock, &fdvar)) 240 process(sapsock, SAP_PKT); 241 } 242} 243 244void 245process(fd, pkt_type) 246 int fd; 247 int pkt_type; 248{ 249 struct sockaddr from; 250 int fromlen = sizeof (from), cc, omask; 251 struct ipxdp *ipxdp = (struct ipxdp *)packet; 252 253 cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen); 254 if (cc <= 0) { 255 if (cc < 0 && errno != EINTR) 256 syslog(LOG_ERR, "recvfrom: %m"); 257 return; 258 } 259 if (tracepackets > 1 && ftrace) { 260 fprintf(ftrace,"rcv %d bytes on %s ", 261 cc, ipxdp_ntoa(&ipxdp->ipxdp_dna)); 262 fprintf(ftrace," from %s\n", ipxdp_ntoa(&ipxdp->ipxdp_sna)); 263 } 264 265 if (noteremoterequests && 266 !ipx_neteqnn(ipxdp->ipxdp_sna.x_net, ipx_zeronet) && 267 !ipx_neteq(ipxdp->ipxdp_sna, ipxdp->ipxdp_dna)) 268 { 269 syslog(LOG_ERR, 270 "net of interface (%s) != net on ether (%s)!\n", 271 ipxdp_nettoa(ipxdp->ipxdp_dna.x_net), 272 ipxdp_nettoa(ipxdp->ipxdp_sna.x_net)); 273 } 274 275 /* We get the IPX header in front of the RIF packet*/ 276 cc -= sizeof (struct ipxdp); 277#define mask(s) (1<<((s)-1)) 278 omask = sigblock(mask(SIGALRM)); 279 switch(pkt_type) { 280 case SAP_PKT: sap_input(&from, cc); 281 break; 282 case RIP_PKT: rip_input(&from, cc); 283 break; 284 } 285 sigsetmask(omask); 286} 287 288int 289getsocket(type, proto, sipx) 290 int type, proto; 291 struct sockaddr_ipx *sipx; 292{ 293 int domain = sipx->sipx_family; 294 int retry, s, on = 1; 295 296 retry = 1; 297 while ((s = socket(domain, type, proto)) < 0 && retry) { 298 syslog(LOG_ERR, "socket: %m"); 299 sleep(5 * retry); 300 retry <<= 1; 301 } 302 if (retry == 0) 303 return (-1); 304 while (bind(s, (struct sockaddr *)sipx, sizeof (*sipx)) < 0 && retry) { 305 syslog(LOG_ERR, "bind: %m"); 306 sleep(5 * retry); 307 retry <<= 1; 308 } 309 if (retry == 0) 310 return (-1); 311 if (domain==AF_IPX) { 312 struct ipxdp ipxdp; 313 if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) { 314 syslog(LOG_ERR, "setsockopt SEE HEADERS: %m"); 315 exit(1); 316 } 317 if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP) 318 ipxdp.ipxdp_pt = IPXPROTO_RI; 319 else if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_SAP) 320 ipxdp.ipxdp_pt = IPXPROTO_SAP; 321 else { 322 syslog(LOG_ERR, "port should be either RIP or SAP"); 323 exit(1); 324 } 325 if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &ipxdp, sizeof(ipxdp))) { 326 syslog(LOG_ERR, "setsockopt SET HEADER: %m"); 327 exit(1); 328 } 329 } 330 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 331 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 332 exit(1); 333 } 334 return (s); 335} 336 337/* 338 * Fork and exit on EMT-- for profiling. 339 */ 340void 341fkexit() 342{ 343 if (fork() == 0) 344 exit(0); 345} 346