main.c revision 27244
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 1997/02/22 16:00:57 peter 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[MAXRXPACKETSIZE+1]; 83 84char **argv0; 85 86int supplier = -1; /* process should supply updates */ 87int dosap = 1; /* By default do SAP services. */ 88int dobcast = 1; /* A RIP/SAP broadcast is needed. */ 89time_t lastbcast; /* Time of last RIP/SAP broadcast */ 90 91struct rip *msg = (struct rip *) &packet[sizeof (struct ipx)]; 92struct sap_packet *sap_msg = 93 (struct sap_packet *) &packet[sizeof (struct ipx)]; 94void hup(), fkexit(), timer(); 95void process(int fd, int pkt_type); 96int getsocket(int type, int proto, struct sockaddr_ipx *sipx); 97void getinfo(); 98void catchtimer(); 99 100int 101main(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 fprintf(stderr, 151 "usage: ipxrouted [ -s ] [ -q ] [ -t ] [ -g ] [ -l ]\n"); 152 exit(1); 153 } 154 155 156#ifndef DEBUG 157 if (!tracepackets) 158 daemon(0, 0); 159#endif 160 openlog("IPXrouted", LOG_PID, LOG_DAEMON); 161 162 addr.sipx_family = AF_IPX; 163 addr.sipx_len = sizeof(addr); 164 addr.sipx_port = htons(IPXPORT_RIP); 165 ipx_anynet.s_net[0] = ipx_anynet.s_net[1] = -1; 166 ipx_netmask.sipx_addr.x_net = ipx_anynet; 167 ipx_netmask.sipx_len = 6; 168 ipx_netmask.sipx_family = AF_IPX; 169 r = socket(AF_ROUTE, SOCK_RAW, 0); 170 /* later, get smart about lookingforinterfaces */ 171 if (r) 172 shutdown(r, 0); /* for now, don't want reponses */ 173 else { 174 fprintf(stderr, "IPXrouted: no routing socket\n"); 175 exit(1); 176 } 177 ripsock = getsocket(SOCK_DGRAM, 0, &addr); 178 if (ripsock < 0) 179 exit(1); 180 181 if (dosap) { 182 addr.sipx_port = htons(IPXPORT_SAP); 183 sapsock = getsocket(SOCK_DGRAM, 0, &addr); 184 if (sapsock < 0) 185 exit(1); 186 } else 187 sapsock = -1; 188 189 /* 190 * Any extra argument is considered 191 * a tracing log file. 192 */ 193 if (argc > 0) 194 traceon(*argv); 195 /* 196 * Collect an initial view of the world by 197 * snooping in the kernel. Then, send a request packet on all 198 * directly connected networks to find out what 199 * everyone else thinks. 200 */ 201 rtinit(); 202 sapinit(); 203 ifinit(); 204 if (supplier < 0) 205 supplier = 0; 206 /* request the state of the world */ 207 msg->rip_cmd = htons(RIPCMD_REQUEST); 208 msg->rip_nets[0].rip_dst = ipx_anynet; 209 msg->rip_nets[0].rip_metric = htons(HOPCNT_INFINITY); 210 msg->rip_nets[0].rip_ticks = htons(-1); 211 toall(sndmsg, NULL, 0); 212 213 if (dosap) { 214 sap_msg->sap_cmd = htons(SAP_REQ); 215 sap_msg->sap[0].ServType = htons(SAP_WILDCARD); 216 toall(sapsndmsg, NULL, 0); 217 } 218 219 signal(SIGALRM, catchtimer); 220 signal(SIGHUP, hup); 221 signal(SIGINT, hup); 222 signal(SIGEMT, fkexit); 223 signal(SIGINFO, getinfo); 224 225 tval.it_interval.tv_sec = TIMER_RATE; 226 tval.it_interval.tv_usec = 0; 227 tval.it_value.tv_sec = TIMER_RATE; 228 tval.it_value.tv_usec = 0; 229 setitimer(ITIMER_REAL, &tval, NULL); 230 231 nfds = 1 + max(sapsock, ripsock); 232 233 for (;;) { 234 if (dobcast) { 235 dobcast = 0; 236 lastbcast = time(NULL); 237 timer(); 238 } 239 240 FD_ZERO(&fdvar); 241 if (dosap) { 242 FD_SET(sapsock, &fdvar); 243 } 244 FD_SET(ripsock, &fdvar); 245 246 if(select(nfds, &fdvar, (fd_set *)NULL, (fd_set *)NULL, 247 (struct timeval *)NULL) < 0) { 248 if(errno == EINTR) 249 continue; 250 perror("during select"); 251 exit(1); 252 } 253 254 if(FD_ISSET(ripsock, &fdvar)) 255 process(ripsock, RIP_PKT); 256 257 if(dosap && FD_ISSET(sapsock, &fdvar)) 258 process(sapsock, SAP_PKT); 259 260 ttime = time(NULL); 261 if (ttime > (lastbcast + TIMER_RATE + (TIMER_RATE * 2 / 3))) { 262 dobcast = 1; 263 syslog(LOG_ERR, "Missed alarm"); 264 } 265 } 266} 267 268void 269process(fd, pkt_type) 270 int fd; 271 int pkt_type; 272{ 273 struct sockaddr from; 274 int fromlen = sizeof (from), cc, omask; 275 struct ipx *ipxdp = (struct ipx *)packet; 276 277 cc = recvfrom(fd, packet, sizeof (packet), 0, &from, &fromlen); 278 if (cc <= 0) { 279 if (cc < 0 && errno != EINTR) 280 syslog(LOG_ERR, "recvfrom: %m"); 281 return; 282 } 283 if (tracepackets > 1 && ftrace) { 284 fprintf(ftrace,"rcv %d bytes on %s ", 285 cc, ipxdp_ntoa(&ipxdp->ipx_dna)); 286 fprintf(ftrace," from %s\n", ipxdp_ntoa(&ipxdp->ipx_sna)); 287 } 288 289 if (noteremoterequests && 290 !ipx_neteqnn(ipxdp->ipx_sna.x_net, ipx_zeronet) && 291 !ipx_neteq(ipxdp->ipx_sna, ipxdp->ipx_dna)) 292 { 293 syslog(LOG_ERR, 294 "net of interface (%s) != net on ether (%s)!\n", 295 ipxdp_nettoa(ipxdp->ipx_dna.x_net), 296 ipxdp_nettoa(ipxdp->ipx_sna.x_net)); 297 } 298 299 /* We get the IPX header in front of the RIF packet*/ 300 cc -= sizeof (struct ipx); 301#define mask(s) (1<<((s)-1)) 302 omask = sigblock(mask(SIGALRM)); 303 switch(pkt_type) { 304 case SAP_PKT: sap_input(&from, cc); 305 break; 306 case RIP_PKT: rip_input(&from, cc); 307 break; 308 } 309 sigsetmask(omask); 310} 311 312int 313getsocket(type, proto, sipx) 314 int type, proto; 315 struct sockaddr_ipx *sipx; 316{ 317 int domain = sipx->sipx_family; 318 int retry, s, on = 1; 319 320 retry = 1; 321 while ((s = socket(domain, type, proto)) < 0 && retry) { 322 syslog(LOG_ERR, "socket: %m"); 323 sleep(5 * retry); 324 retry <<= 1; 325 } 326 if (retry == 0) 327 return (-1); 328 while (bind(s, (struct sockaddr *)sipx, sizeof (*sipx)) < 0 && retry) { 329 syslog(LOG_ERR, "bind: %m"); 330 sleep(5 * retry); 331 retry <<= 1; 332 } 333 if (retry == 0) 334 return (-1); 335 if (domain==AF_IPX) { 336 struct ipx ipxdp; 337 if (setsockopt(s, 0, SO_HEADERS_ON_INPUT, &on, sizeof(on))) { 338 syslog(LOG_ERR, "setsockopt SEE HEADERS: %m"); 339 exit(1); 340 } 341 if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP) 342 ipxdp.ipx_pt = IPXPROTO_RI; 343 else if (ntohs(sipx->sipx_addr.x_port) == IPXPORT_SAP) 344#ifdef IPXPROTO_SAP 345 ipxdp.ipx_pt = IPXPROTO_SAP; 346#else 347 ipxdp.ipx_pt = IPXPROTO_PXP; 348#endif 349 else { 350 syslog(LOG_ERR, "port should be either RIP or SAP"); 351 exit(1); 352 } 353 if (setsockopt(s, 0, SO_DEFAULT_HEADERS, &ipxdp, sizeof(ipxdp))) { 354 syslog(LOG_ERR, "setsockopt SET HEADER: %m"); 355 exit(1); 356 } 357 } 358 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 359 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 360 exit(1); 361 } 362 return (s); 363} 364 365/* 366 * Fork and exit on EMT-- for profiling. 367 */ 368void 369fkexit() 370{ 371 if (fork() == 0) 372 exit(0); 373} 374 375void 376catchtimer() 377{ 378 dobcast = 1; 379} 380 381void 382getinfo() 383{ 384 FILE *fh; 385 386 fh = fopen("/tmp/ipxrouted.dmp", "a"); 387 if(fh == NULL) 388 return; 389 390 dumpriptable(fh); 391 dumpsaptable(fh, sap_head); 392 393 fclose(fh); 394} 395 396