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