startup.c revision 21673
1214571Sdim/*
2214571Sdim * Copyright (c) 1985, 1993
3214571Sdim *	The Regents of the University of California.  All rights reserved.
4214571Sdim *
5214571Sdim * Copyright (c) 1995 John Hay.  All rights reserved.
6214571Sdim *
7214571Sdim * This file includes significant work done at Cornell University by
8214571Sdim * Bill Nesheim.  That work included by permission.
9214571Sdim *
10214571Sdim * Redistribution and use in source and binary forms, with or without
11214571Sdim * modification, are permitted provided that the following conditions
12214571Sdim * are met:
13214571Sdim * 1. Redistributions of source code must retain the above copyright
14214571Sdim *    notice, this list of conditions and the following disclaimer.
15214571Sdim * 2. Redistributions in binary form must reproduce the above copyright
16214571Sdim *    notice, this list of conditions and the following disclaimer in the
17214571Sdim *    documentation and/or other materials provided with the distribution.
18214571Sdim * 3. All advertising materials mentioning features or use of this software
19214571Sdim *    must display the following acknowledgement:
20214571Sdim *	This product includes software developed by the University of
21214571Sdim *	California, Berkeley and its contributors.
22214571Sdim * 4. Neither the name of the University nor the names of its contributors
23214571Sdim *    may be used to endorse or promote products derived from this software
24214571Sdim *    without specific prior written permission.
25214571Sdim *
26214571Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27214571Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28214571Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29214571Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30214571Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31214571Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32214571Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33214571Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34214571Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35214571Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36214571Sdim * SUCH DAMAGE.
37214571Sdim *
38214571Sdim *	$FreeBSD: head/usr.sbin/IPXrouted/startup.c 21673 1997-01-14 07:20:47Z jkh $
39214571Sdim */
40214571Sdim
41214571Sdim#ifndef lint
42214571Sdimstatic char sccsid[] = "@(#)startup.c	8.1 (Berkeley) 6/5/93";
43214571Sdim#endif /* not lint */
44214571Sdim
45214571Sdim/*
46214571Sdim * Routing Table Management Daemon
47214571Sdim */
48214571Sdim#include "defs.h"
49214571Sdim
50214571Sdim#include <sys/param.h>
51214571Sdim#include <sys/ioctl.h>
52214571Sdim#include <sys/sysctl.h>
53214571Sdim#include <sys/time.h>
54214571Sdim
55214571Sdim#include <net/if.h>
56214571Sdim#include <net/if_dl.h>
57214571Sdim
58214571Sdim#include <nlist.h>
59214571Sdim#include <stdlib.h>
60214571Sdim
61214571Sdimstruct	interface *ifnet;
62214571Sdimint	lookforinterfaces = 1;
63214571Sdimint	performnlist = 1;
64214571Sdimint	gateway = 0;
65214571Sdimint	externalinterfaces = 0;		/* # of remote and local interfaces */
66214571Sdim
67214571Sdimvoid
68214571Sdimquit(s)
69214571Sdim	char *s;
70214571Sdim{
71214571Sdim	extern int errno;
72214571Sdim	int sverrno = errno;
73214571Sdim
74214571Sdim	(void) fprintf(stderr, "IPXroute: ");
75214571Sdim	if (s)
76214571Sdim		(void) fprintf(stderr, "%s: ", s);
77214571Sdim	(void) fprintf(stderr, "%s\n", strerror(sverrno));
78214571Sdim	exit(1);
79214571Sdim	/* NOTREACHED */
80214571Sdim}
81214571Sdim
82214571Sdimstruct rt_addrinfo info;
83214571Sdim/* Sleazy use of local variables throughout file, warning!!!! */
84214571Sdim#define netmask	info.rti_info[RTAX_NETMASK]
85214571Sdim#define ifaaddr	info.rti_info[RTAX_IFA]
86214571Sdim#define brdaddr	info.rti_info[RTAX_BRD]
87214571Sdim
88214571Sdim#define ROUNDUP(a) \
89214571Sdim	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
90214571Sdim#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
91214571Sdim
92214571Sdimvoid
93214571Sdimrt_xaddrs(cp, cplim, rtinfo)
94214571Sdim	register caddr_t cp, cplim;
95214571Sdim	register struct rt_addrinfo *rtinfo;
96214571Sdim{
97214571Sdim	register struct sockaddr *sa;
98214571Sdim	register int i;
99214571Sdim
100214571Sdim	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
101214571Sdim	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
102214571Sdim		if ((rtinfo->rti_addrs & (1 << i)) == 0)
103214571Sdim			continue;
104214571Sdim		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
105214571Sdim		ADVANCE(cp, sa);
106214571Sdim	}
107214571Sdim}
108214571Sdim
109214571Sdim/*
110214571Sdim * Find the network interfaces which have configured themselves.
111214571Sdim * If the interface is present but not yet up (for example an
112214571Sdim * ARPANET IMP), set the lookforinterfaces flag so we'll
113214571Sdim * come back later and look again.
114214571Sdim */
115214571Sdimvoid
116214571Sdimifinit(void)
117214571Sdim{
118214571Sdim	struct interface ifs, *ifp;
119214571Sdim	size_t needed;
120214571Sdim	int mib[6], no_ipxaddr = 0, flags = 0;
121214571Sdim	char *buf, *cplim, *cp;
122214571Sdim	register struct if_msghdr *ifm;
123214571Sdim	register struct ifa_msghdr *ifam;
124214571Sdim	struct sockaddr_dl *sdl = 0;
125214571Sdim
126214571Sdim        mib[0] = CTL_NET;
127214571Sdim        mib[1] = PF_ROUTE;
128214571Sdim        mib[2] = 0;
129214571Sdim        mib[3] = AF_IPX;
130214571Sdim        mib[4] = NET_RT_IFLIST;
131214571Sdim        mib[5] = 0;
132214571Sdim        if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
133214571Sdim                quit("route-sysctl-estimate");
134214571Sdim	if ((buf = malloc(needed)) == NULL)
135214571Sdim		quit("malloc");
136214571Sdim        if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
137214571Sdim	lookforinterfaces = 0;
138214571Sdim	cplim = buf + needed;
139214571Sdim	for (cp = buf; cp < cplim; cp += ifm->ifm_msglen) {
140214571Sdim		ifm = (struct if_msghdr *)cp;
141214571Sdim		if (ifm->ifm_type == RTM_IFINFO) {
142214571Sdim			bzero(&ifs, sizeof(ifs));
143214571Sdim			ifs.int_flags = flags = ifm->ifm_flags | IFF_INTERFACE;
144214571Sdim			if ((flags & IFF_UP) == 0 || no_ipxaddr)
145214571Sdim				lookforinterfaces = 1;
146214571Sdim			sdl = (struct sockaddr_dl *) (ifm + 1);
147214571Sdim			sdl->sdl_data[sdl->sdl_nlen] = 0;
148214571Sdim			no_ipxaddr = 1;
149214571Sdim			continue;
150214571Sdim		}
151214571Sdim		if (ifm->ifm_type != RTM_NEWADDR)
152214571Sdim			quit("ifinit: out of sync");
153214571Sdim		if ((flags & IFF_UP) == 0)
154214571Sdim			continue;
155214571Sdim		ifam = (struct ifa_msghdr *)ifm;
156214571Sdim		info.rti_addrs = ifam->ifam_addrs;
157214571Sdim		rt_xaddrs((char *)(ifam + 1), cp + ifam->ifam_msglen, &info);
158214571Sdim		if (ifaaddr == 0) {
159214571Sdim			syslog(LOG_ERR, "%s: (get addr)", sdl->sdl_data);
160214571Sdim			continue;
161214571Sdim		}
162214571Sdim		ifs.int_addr = *ifaaddr;
163214571Sdim		if (ifs.int_addr.sa_family != AF_IPX)
164214571Sdim			continue;
165214571Sdim		no_ipxaddr = 0;
166214571Sdim		if (ifs.int_flags & IFF_POINTOPOINT) {
167214571Sdim			if (brdaddr == 0) {
168214571Sdim				syslog(LOG_ERR, "%s: (get dstaddr)",
169214571Sdim					sdl->sdl_data);
170214571Sdim				continue;
171214571Sdim			}
172214571Sdim			if (brdaddr->sa_family == AF_UNSPEC) {
173214571Sdim				lookforinterfaces = 1;
174214571Sdim				continue;
175214571Sdim			}
176214571Sdim			ifs.int_dstaddr = *brdaddr;
177214571Sdim		}
178214571Sdim		if (ifs.int_flags & IFF_BROADCAST) {
179214571Sdim			if (brdaddr == 0) {
180214571Sdim				syslog(LOG_ERR, "%s: (get broadaddr)",
181214571Sdim					sdl->sdl_data);
182214571Sdim				continue;
183214571Sdim			}
184214571Sdim			ifs.int_dstaddr = *brdaddr;
185214571Sdim		}
186214571Sdim		/*
187214571Sdim		 * already known to us?
188214571Sdim		 * what makes a POINTOPOINT if unique is its dst addr,
189214571Sdim		 * NOT its source address
190214571Sdim		 */
191214571Sdim		if ( ((ifs.int_flags & IFF_POINTOPOINT) &&
192214571Sdim			if_ifwithdstaddr(&ifs.int_dstaddr)) ||
193214571Sdim			( ((ifs.int_flags & IFF_POINTOPOINT) == 0) &&
194214571Sdim			if_ifwithaddr(&ifs.int_addr)))
195214571Sdim			continue;
196214571Sdim		/* no one cares about software loopback interfaces */
197214571Sdim		if (ifs.int_flags & IFF_LOOPBACK)
198214571Sdim			continue;
199214571Sdim		ifp = (struct interface *)
200214571Sdim			malloc(sdl->sdl_nlen + 1 + sizeof(ifs));
201214571Sdim		if (ifp == 0) {
202214571Sdim			syslog(LOG_ERR, "IPXrouted: out of memory\n");
203214571Sdim			lookforinterfaces = 1;
204214571Sdim			break;
205214571Sdim		}
206214571Sdim		*ifp = ifs;
207214571Sdim		/*
208214571Sdim		 * Count the # of directly connected networks
209214571Sdim		 * and point to point links which aren't looped
210214571Sdim		 * back to ourself.  This is used below to
211214571Sdim		 * decide if we should be a routing ``supplier''.
212214571Sdim		 */
213214571Sdim		if ((ifs.int_flags & IFF_POINTOPOINT) == 0 ||
214214571Sdim		    if_ifwithaddr(&ifs.int_dstaddr) == 0)
215214571Sdim			externalinterfaces++;
216214571Sdim		/*
217214571Sdim		 * If we have a point-to-point link, we want to act
218214571Sdim		 * as a supplier even if it's our only interface,
219214571Sdim		 * as that's the only way our peer on the other end
220214571Sdim		 * can tell that the link is up.
221214571Sdim		 */
222214571Sdim		if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0)
223214571Sdim			supplier = 1;
224214571Sdim		ifp->int_name = (char *)(ifp + 1);
225214571Sdim		strcpy(ifp->int_name, sdl->sdl_data);
226214571Sdim
227214571Sdim		ifp->int_metric = ifam->ifam_metric;
228214571Sdim		ifp->int_next = ifnet;
229214571Sdim		ifnet = ifp;
230214571Sdim		traceinit(ifp);
231214571Sdim		addrouteforif(ifp);
232214571Sdim	}
233214571Sdim	if (externalinterfaces > 1 && supplier < 0)
234214571Sdim		supplier = 1;
235214571Sdim	free(buf);
236214571Sdim}
237214571Sdim
238214571Sdimvoid
239214571Sdimaddrouteforif(ifp)
240214571Sdim	struct interface *ifp;
241214571Sdim{
242214571Sdim	struct sockaddr_ipx net;
243214571Sdim	struct sockaddr *dst;
244214571Sdim	struct rt_entry *rt;
245214571Sdim
246214571Sdim	if (ifp->int_flags & IFF_POINTOPOINT) {
247214571Sdim		int (*match)();
248214571Sdim		register struct interface *ifp2 = ifnet;
249214571Sdim
250214571Sdim		dst = &ifp->int_dstaddr;
251214571Sdim
252214571Sdim		/* Search for interfaces with the same net */
253214571Sdim		ifp->int_sq.n = ifp->int_sq.p = &(ifp->int_sq);
254214571Sdim		match = afswitch[dst->sa_family].af_netmatch;
255214571Sdim		if (match)
256214571Sdim		for (ifp2 = ifnet; ifp2; ifp2 =ifp2->int_next) {
257214571Sdim			if (ifp->int_flags & IFF_POINTOPOINT == 0)
258214571Sdim				continue;
259214571Sdim			if ((*match)(&ifp2->int_dstaddr,&ifp->int_dstaddr)) {
260214571Sdim				insque(&ifp2->int_sq,&ifp->int_sq);
261214571Sdim				break;
262214571Sdim			}
263214571Sdim		}
264214571Sdim	} else {
265214571Sdim		bzero(&net, sizeof(net));
266214571Sdim		net.sipx_family = AF_IPX;
267214571Sdim		net.sipx_len = sizeof (net);
268214571Sdim		net.sipx_addr.x_net = satoipx_addr(ifp->int_broadaddr).x_net;
269214571Sdim		dst = (struct sockaddr *)&net;
270214571Sdim	}
271214571Sdim	rt = rtlookup(dst);
272214571Sdim	if (rt)
273214571Sdim		rtdelete(rt);
274214571Sdim	if (tracing)
275214571Sdim		fprintf(stderr, "Adding route to interface %s\n", ifp->int_name);
276214571Sdim	if (ifp->int_transitions++ > 0)
277214571Sdim		syslog(LOG_ERR, "re-installing interface %s", ifp->int_name);
278214571Sdim	rtadd(dst, &ifp->int_addr, ifp->int_metric, 0,
279214571Sdim		ifp->int_flags & (IFF_INTERFACE|IFF_PASSIVE|IFF_REMOTE));
280214571Sdim}
281214571Sdim
282214571Sdim