main.c revision 21673
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: head/usr.sbin/IPXrouted/main.c 21673 1997-01-14 07:20:47Z jkh $ 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. */ 88 89struct rip *msg = (struct rip *) &packet[sizeof (struct ipx)]; 90struct sap_packet *sap_msg = 91 (struct sap_packet *) &packet[sizeof (struct ipx)]; 92void hup(), fkexit(), timer(); 93void process(int fd, int pkt_type); 94int getsocket(int type, int proto, struct sockaddr_ipx *sipx); 95void getinfo(); 96 97int 98main(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