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