File:  [DragonFly] / src / usr.sbin / IPXrouted / startup.c
Revision 1.2: download - view: text, annotated - select for diffs
Tue Jun 17 04:29:52 2003 UTC (11 years, 5 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids.  Most
ids have been removed from !lint sections and moved into comment sections.

    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: src/usr.sbin/IPXrouted/startup.c,v 1.8.2.1 2000/07/01 10:46:25 ps Exp $
   39:  * $DragonFly: src/usr.sbin/IPXrouted/startup.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
   40:  *
   41:  * @(#)startup.c	8.1 (Berkeley) 6/5/93
   42:  */
   43: 
   44: /*
   45:  * Routing Table Management Daemon
   46:  */
   47: #include "defs.h"
   48: 
   49: #include <sys/param.h>
   50: #include <sys/ioctl.h>
   51: #include <sys/sysctl.h>
   52: #include <sys/time.h>
   53: 
   54: #include <net/if.h>
   55: #include <net/if_dl.h>
   56: 
   57: #include <errno.h>
   58: #include <nlist.h>
   59: #include <stdlib.h>
   60: 
   61: struct	interface *ifnet;
   62: int	lookforinterfaces = 1;
   63: int	performnlist = 1;
   64: int	gateway = 0;
   65: int	externalinterfaces = 0;		/* # of remote and local interfaces */
   66: 
   67: void
   68: quit(s)
   69: 	char *s;
   70: {
   71: 	int sverrno = errno;
   72: 
   73: 	(void) fprintf(stderr, "IPXroute: ");
   74: 	if (s)
   75: 		(void) fprintf(stderr, "%s: ", s);
   76: 	(void) fprintf(stderr, "%s\n", strerror(sverrno));
   77: 	exit(1);
   78: 	/* NOTREACHED */
   79: }
   80: 
   81: struct rt_addrinfo info;
   82: /* Sleazy use of local variables throughout file, warning!!!! */
   83: #define netmask	info.rti_info[RTAX_NETMASK]
   84: #define ifaaddr	info.rti_info[RTAX_IFA]
   85: #define brdaddr	info.rti_info[RTAX_BRD]
   86: 
   87: #define ROUNDUP(a) \
   88: 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
   89: #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
   90: 
   91: void
   92: rt_xaddrs(cp, cplim, rtinfo)
   93: 	register caddr_t cp, cplim;
   94: 	register struct rt_addrinfo *rtinfo;
   95: {
   96: 	register struct sockaddr *sa;
   97: 	register int i;
   98: 
   99: 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
  100: 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
  101: 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
  102: 			continue;
  103: 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
  104: 		ADVANCE(cp, sa);
  105: 	}
  106: }
  107: 
  108: /*
  109:  * Find the network interfaces which have configured themselves.
  110:  * If the interface is present but not yet up (for example an
  111:  * ARPANET IMP), set the lookforinterfaces flag so we'll
  112:  * come back later and look again.
  113:  */
  114: void
  115: ifinit(void)
  116: {
  117: 	struct interface ifs, *ifp;
  118: 	size_t needed;
  119: 	int mib[6], no_ipxaddr = 0, flags = 0;
  120: 	char *buf, *cplim, *cp;
  121: 	register struct if_msghdr *ifm;
  122: 	register struct ifa_msghdr *ifam;
  123: 	struct sockaddr_dl *sdl = 0;
  124: 
  125:         mib[0] = CTL_NET;
  126:         mib[1] = PF_ROUTE;
  127:         mib[2] = 0;
  128:         mib[3] = AF_IPX;
  129:         mib[4] = NET_RT_IFLIST;
  130:         mib[5] = 0;
  131:         if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
  132:                 quit("route-sysctl-estimate");
  133: 	if ((buf = malloc(needed)) == NULL)
  134: 		quit("malloc");
  135:         if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
  136: 	lookforinterfaces = 0;
  137: 	cplim = buf + needed;
  138: 	for (cp = buf; cp < cplim; cp += ifm->ifm_msglen) {
  139: 		ifm = (struct if_msghdr *)cp;
  140: 		if (ifm->ifm_type == RTM_IFINFO) {
  141: 			bzero(&ifs, sizeof(ifs));
  142: 			ifs.int_flags = flags = ifm->ifm_flags | IFF_INTERFACE;
  143: 			if ((flags & IFF_UP) == 0 || no_ipxaddr)
  144: 				lookforinterfaces = 1;
  145: 			sdl = (struct sockaddr_dl *) (ifm + 1);
  146: 			sdl->sdl_data[sdl->sdl_nlen] = 0;
  147: 			no_ipxaddr = 1;
  148: 			continue;
  149: 		}
  150: 		if (ifm->ifm_type != RTM_NEWADDR)
  151: 			quit("ifinit: out of sync");
  152: 		if ((flags & IFF_UP) == 0)
  153: 			continue;
  154: 		ifam = (struct ifa_msghdr *)ifm;
  155: 		info.rti_addrs = ifam->ifam_addrs;
  156: 		rt_xaddrs((char *)(ifam + 1), cp + ifam->ifam_msglen, &info);
  157: 		if (ifaaddr == 0) {
  158: 			syslog(LOG_ERR, "%s: (get addr)", sdl->sdl_data);
  159: 			continue;
  160: 		}
  161: 		ifs.int_addr = *ifaaddr;
  162: 		if (ifs.int_addr.sa_family != AF_IPX)
  163: 			continue;
  164: 		no_ipxaddr = 0;
  165: 		if (ifs.int_flags & IFF_POINTOPOINT) {
  166: 			if (brdaddr == 0) {
  167: 				syslog(LOG_ERR, "%s: (get dstaddr)",
  168: 					sdl->sdl_data);
  169: 				continue;
  170: 			}
  171: 			if (brdaddr->sa_family == AF_UNSPEC) {
  172: 				lookforinterfaces = 1;
  173: 				continue;
  174: 			}
  175: 			ifs.int_dstaddr = *brdaddr;
  176: 		}
  177: 		if (ifs.int_flags & IFF_BROADCAST) {
  178: 			if (brdaddr == 0) {
  179: 				syslog(LOG_ERR, "%s: (get broadaddr)",
  180: 					sdl->sdl_data);
  181: 				continue;
  182: 			}
  183: 			ifs.int_dstaddr = *brdaddr;
  184: 		}
  185: 		if (ifs.int_flags & IFF_LOOPBACK) {
  186: 			ifs.int_dstaddr = ifs.int_addr;
  187: 		}
  188: 		/* 
  189: 		 * already known to us? 
  190: 		 * what makes a POINTOPOINT if unique is its dst addr,
  191: 		 * NOT its source address 
  192: 		 */
  193: 		if ( ((ifs.int_flags & IFF_POINTOPOINT) &&
  194: 			if_ifwithdstaddr(&ifs.int_dstaddr)) ||
  195: 			( ((ifs.int_flags & IFF_POINTOPOINT) == 0) &&
  196: 			if_ifwithaddr(&ifs.int_addr)))
  197: 			continue;
  198: 		ifp = (struct interface *)
  199: 			malloc(sdl->sdl_nlen + 1 + sizeof(ifs));
  200: 		if (ifp == 0) {
  201: 			syslog(LOG_ERR, "IPXrouted: out of memory\n");
  202: 			lookforinterfaces = 1;
  203: 			break;
  204: 		}
  205: 		*ifp = ifs;
  206: 		/*
  207: 		 * Count the # of directly connected networks
  208: 		 * and point to point links which aren't looped
  209: 		 * back to ourself.  This is used below to
  210: 		 * decide if we should be a routing ``supplier''.
  211: 		 */
  212: 		if ((ifs.int_flags & IFF_POINTOPOINT) == 0 ||
  213: 		    if_ifwithaddr(&ifs.int_dstaddr) == 0)
  214: 			externalinterfaces++;
  215: 		/*
  216: 		 * If we have a point-to-point link, we want to act
  217: 		 * as a supplier even if it's our only interface,
  218: 		 * as that's the only way our peer on the other end
  219: 		 * can tell that the link is up.
  220: 		 */
  221: 		if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0)
  222: 			supplier = 1;
  223: 		ifp->int_name = (char *)(ifp + 1);
  224: 		strcpy(ifp->int_name, sdl->sdl_data);
  225: 
  226: 		ifp->int_metric = ifam->ifam_metric;
  227: 		ifp->int_next = ifnet;
  228: 		ifnet = ifp;
  229: 		traceinit(ifp);
  230: 		addrouteforif(ifp);
  231: 	}
  232: 	if (externalinterfaces > 1 && supplier < 0)
  233: 		supplier = 1;
  234: 	free(buf);
  235: }
  236: 
  237: void
  238: addrouteforif(ifp)
  239: 	struct interface *ifp;
  240: {
  241: 	struct sockaddr_ipx net;
  242: 	struct sockaddr *dst;
  243: 	struct rt_entry *rt;
  244: 
  245: 	if (ifp->int_flags & IFF_POINTOPOINT) {
  246: 		int (*match)();
  247: 		register struct interface *ifp2 = ifnet;
  248: 		
  249: 		dst = &ifp->int_dstaddr;
  250: 
  251: 		/* Search for interfaces with the same net */
  252: 		ifp->int_sq.n = ifp->int_sq.p = &(ifp->int_sq);
  253: 		match = afswitch[dst->sa_family].af_netmatch;
  254: 		if (match)
  255: 		for (ifp2 = ifnet; ifp2; ifp2 =ifp2->int_next) {
  256: 			if (ifp->int_flags & IFF_POINTOPOINT == 0)
  257: 				continue;
  258: 			if ((*match)(&ifp2->int_dstaddr,&ifp->int_dstaddr)) {
  259: 				insque(&ifp2->int_sq,&ifp->int_sq);
  260: 				break;
  261: 			}
  262: 		}
  263: 	} else {
  264: 		bzero(&net, sizeof(net));
  265: 		net.sipx_family = AF_IPX;
  266: 		net.sipx_len = sizeof (net);
  267: 		net.sipx_addr.x_net = satoipx_addr(ifp->int_broadaddr).x_net;
  268: 		dst = (struct sockaddr *)&net;
  269: 	}
  270: 	rt = rtlookup(dst);
  271: 	if (rt)
  272: 		rtdelete(rt);
  273: 	if (tracing)
  274: 		fprintf(stderr, "Adding route to interface %s\n", ifp->int_name);
  275: 	if (ifp->int_transitions++ > 0)
  276: 		syslog(LOG_ERR, "re-installing interface %s", ifp->int_name);
  277: 	rtadd(dst, &ifp->int_addr, ifp->int_metric, 0,
  278: 		ifp->int_flags & (IFF_INTERFACE|IFF_PASSIVE|IFF_REMOTE));
  279: }
  280: