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