if.c revision 1.1.1.6 1 1.1 cgd /*
2 1.1.1.2 mycroft * Copyright (c) 1983, 1993
3 1.1.1.2 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1.1.4 christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
35 1.1.1.2 mycroft static char sccsid[] = "@(#)if.c 8.1 (Berkeley) 6/5/93";
36 1.1.1.4 christos #elif defined(__NetBSD__)
37 1.1.1.4 christos static char rcsid[] = "$NetBSD: if.c,v 1.1.1.6 1998/06/02 17:41:24 thorpej Exp $";
38 1.1.1.4 christos #endif
39 1.1.1.6 thorpej #ident "$Revision: 1.1.1.6 $"
40 1.1.1.3 thorpej
41 1.1 cgd #include "defs.h"
42 1.1.1.3 thorpej #include "pathnames.h"
43 1.1.1.3 thorpej
44 1.1.1.5 christos struct interface *ifnet; /* all interfaces */
45 1.1.1.5 christos
46 1.1.1.5 christos /* hash table for all interfaces, big enough to tolerate ridiculous
47 1.1.1.5 christos * numbers of IP aliases. Crazy numbers of aliases such as 7000
48 1.1.1.5 christos * still will not do well, but not just in looking up interfaces
49 1.1.1.5 christos * by name or address.
50 1.1.1.5 christos */
51 1.1.1.5 christos #define AHASH_LEN 211 /* must be prime */
52 1.1.1.5 christos #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
53 1.1.1.5 christos struct interface *ahash_tbl[AHASH_LEN];
54 1.1.1.5 christos
55 1.1.1.5 christos #define BHASH_LEN 211 /* must be prime */
56 1.1.1.5 christos #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
57 1.1.1.5 christos struct interface *bhash_tbl[BHASH_LEN];
58 1.1.1.5 christos
59 1.1.1.5 christos struct interface *remote_if; /* remote interfaces */
60 1.1.1.5 christos
61 1.1.1.5 christos /* hash for physical interface names.
62 1.1.1.5 christos * Assume there are never more 100 or 200 real interfaces, and that
63 1.1.1.5 christos * aliases are put on the end of the hash chains.
64 1.1.1.5 christos */
65 1.1.1.5 christos #define NHASH_LEN 97
66 1.1.1.5 christos struct interface *nhash_tbl[NHASH_LEN];
67 1.1.1.5 christos
68 1.1.1.3 thorpej int tot_interfaces; /* # of remote and local interfaces */
69 1.1.1.3 thorpej int rip_interfaces; /* # of interfaces doing RIP */
70 1.1.1.3 thorpej int foundloopback; /* valid flag for loopaddr */
71 1.1.1.3 thorpej naddr loopaddr; /* our address on loopback */
72 1.1.1.6 thorpej struct rt_spare loop_rts;
73 1.1.1.3 thorpej
74 1.1.1.3 thorpej struct timeval ifinit_timer;
75 1.1.1.5 christos static struct timeval last_ifinit;
76 1.1.1.6 thorpej #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
77 1.1.1.6 thorpej && last_ifinit.tv_usec == now.tv_usec \
78 1.1.1.6 thorpej && timercmp(&ifinit_timer, &now, >))
79 1.1 cgd
80 1.1.1.3 thorpej int have_ripv1_out; /* have a RIPv1 interface */
81 1.1.1.3 thorpej int have_ripv1_in;
82 1.1 cgd
83 1.1.1.3 thorpej
84 1.1.1.5 christos static struct interface**
85 1.1.1.5 christos nhash(register char *p)
86 1.1.1.5 christos {
87 1.1.1.5 christos register u_int i;
88 1.1.1.5 christos
89 1.1.1.5 christos for (i = 0; *p != '\0'; p++) {
90 1.1.1.5 christos i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
91 1.1.1.5 christos i ^= *p;
92 1.1.1.5 christos }
93 1.1.1.5 christos return &nhash_tbl[i % NHASH_LEN];
94 1.1.1.5 christos }
95 1.1.1.5 christos
96 1.1.1.5 christos
97 1.1.1.5 christos /* Link a new interface into the lists and hash tables.
98 1.1.1.5 christos */
99 1.1.1.5 christos void
100 1.1.1.5 christos if_link(struct interface *ifp)
101 1.1.1.5 christos {
102 1.1.1.5 christos struct interface **hifp;
103 1.1.1.5 christos
104 1.1.1.5 christos ifp->int_prev = &ifnet;
105 1.1.1.5 christos ifp->int_next = ifnet;
106 1.1.1.5 christos if (ifnet != 0)
107 1.1.1.5 christos ifnet->int_prev = &ifp->int_next;
108 1.1.1.5 christos ifnet = ifp;
109 1.1.1.5 christos
110 1.1.1.5 christos hifp = AHASH(ifp->int_addr);
111 1.1.1.5 christos ifp->int_ahash_prev = hifp;
112 1.1.1.5 christos if ((ifp->int_ahash = *hifp) != 0)
113 1.1.1.5 christos (*hifp)->int_ahash_prev = &ifp->int_ahash;
114 1.1.1.5 christos *hifp = ifp;
115 1.1.1.5 christos
116 1.1.1.5 christos if (ifp->int_if_flags & IFF_BROADCAST) {
117 1.1.1.5 christos hifp = BHASH(ifp->int_brdaddr);
118 1.1.1.5 christos ifp->int_bhash_prev = hifp;
119 1.1.1.5 christos if ((ifp->int_bhash = *hifp) != 0)
120 1.1.1.5 christos (*hifp)->int_bhash_prev = &ifp->int_bhash;
121 1.1.1.5 christos *hifp = ifp;
122 1.1.1.5 christos }
123 1.1.1.5 christos
124 1.1.1.5 christos if (ifp->int_state & IS_REMOTE) {
125 1.1.1.5 christos ifp->int_rlink_prev = &remote_if;
126 1.1.1.5 christos ifp->int_rlink = remote_if;
127 1.1.1.5 christos if (remote_if != 0)
128 1.1.1.5 christos remote_if->int_rlink_prev = &ifp->int_rlink;
129 1.1.1.5 christos remote_if = ifp;
130 1.1.1.5 christos }
131 1.1.1.5 christos
132 1.1.1.5 christos hifp = nhash(ifp->int_name);
133 1.1.1.5 christos if (ifp->int_state & IS_ALIAS) {
134 1.1.1.5 christos /* put aliases on the end of the hash chain */
135 1.1.1.5 christos while (*hifp != 0)
136 1.1.1.5 christos hifp = &(*hifp)->int_nhash;
137 1.1.1.5 christos }
138 1.1.1.5 christos ifp->int_nhash_prev = hifp;
139 1.1.1.5 christos if ((ifp->int_nhash = *hifp) != 0)
140 1.1.1.5 christos (*hifp)->int_nhash_prev = &ifp->int_nhash;
141 1.1.1.5 christos *hifp = ifp;
142 1.1.1.5 christos }
143 1.1.1.5 christos
144 1.1.1.5 christos
145 1.1.1.3 thorpej /* Find the interface with an address
146 1.1 cgd */
147 1.1 cgd struct interface *
148 1.1.1.3 thorpej ifwithaddr(naddr addr,
149 1.1.1.3 thorpej int bcast, /* notice IFF_BROADCAST address */
150 1.1.1.3 thorpej int remote) /* include IS_REMOTE interfaces */
151 1.1 cgd {
152 1.1.1.3 thorpej struct interface *ifp, *possible = 0;
153 1.1 cgd
154 1.1.1.5 christos remote = (remote == 0) ? IS_REMOTE : 0;
155 1.1.1.3 thorpej
156 1.1.1.5 christos for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
157 1.1.1.5 christos if (ifp->int_addr != addr)
158 1.1.1.5 christos continue;
159 1.1.1.5 christos if ((ifp->int_state & remote) != 0)
160 1.1.1.5 christos continue;
161 1.1.1.5 christos if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
162 1.1.1.5 christos return ifp;
163 1.1.1.5 christos possible = ifp;
164 1.1.1.5 christos }
165 1.1.1.3 thorpej
166 1.1.1.5 christos if (possible || !bcast)
167 1.1.1.5 christos return possible;
168 1.1.1.5 christos
169 1.1.1.5 christos for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
170 1.1.1.5 christos if (ifp->int_brdaddr != addr)
171 1.1.1.5 christos continue;
172 1.1.1.5 christos if ((ifp->int_state & remote) != 0)
173 1.1.1.5 christos continue;
174 1.1.1.5 christos if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
175 1.1.1.5 christos return ifp;
176 1.1.1.5 christos possible = ifp;
177 1.1 cgd }
178 1.1.1.3 thorpej
179 1.1.1.3 thorpej return possible;
180 1.1 cgd }
181 1.1 cgd
182 1.1.1.3 thorpej
183 1.1.1.3 thorpej /* find the interface with a name
184 1.1 cgd */
185 1.1 cgd struct interface *
186 1.1.1.3 thorpej ifwithname(char *name, /* "ec0" or whatever */
187 1.1.1.3 thorpej naddr addr) /* 0 or network address */
188 1.1 cgd {
189 1.1.1.3 thorpej struct interface *ifp;
190 1.1 cgd
191 1.1.1.5 christos for (;;) {
192 1.1.1.5 christos for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
193 1.1.1.5 christos /* If the network address is not specified,
194 1.1.1.5 christos * ignore any alias interfaces. Otherwise, look
195 1.1.1.5 christos * for the interface with the target name and address.
196 1.1.1.5 christos */
197 1.1.1.5 christos if (!strcmp(ifp->int_name, name)
198 1.1.1.5 christos && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
199 1.1.1.5 christos || (ifp->int_addr == addr)))
200 1.1.1.5 christos return ifp;
201 1.1.1.5 christos }
202 1.1.1.3 thorpej
203 1.1.1.5 christos /* If there is no known interface, maybe there is a
204 1.1.1.5 christos * new interface. So just once look for new interfaces.
205 1.1.1.5 christos */
206 1.1.1.6 thorpej if (IF_RESCAN_DELAY())
207 1.1.1.5 christos return 0;
208 1.1.1.5 christos ifinit();
209 1.1 cgd }
210 1.1 cgd }
211 1.1 cgd
212 1.1.1.3 thorpej
213 1.1 cgd struct interface *
214 1.1.1.5 christos ifwithindex(u_short index,
215 1.1.1.5 christos int rescan_ok)
216 1.1 cgd {
217 1.1.1.3 thorpej struct interface *ifp;
218 1.1.1.3 thorpej
219 1.1.1.5 christos for (;;) {
220 1.1.1.5 christos for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
221 1.1.1.5 christos if (ifp->int_index == index)
222 1.1.1.5 christos return ifp;
223 1.1.1.5 christos }
224 1.1.1.3 thorpej
225 1.1.1.5 christos /* If there is no known interface, maybe there is a
226 1.1.1.5 christos * new interface. So just once look for new interfaces.
227 1.1.1.5 christos */
228 1.1.1.5 christos if (!rescan_ok
229 1.1.1.6 thorpej || IF_RESCAN_DELAY())
230 1.1.1.5 christos return 0;
231 1.1.1.5 christos ifinit();
232 1.1 cgd }
233 1.1 cgd }
234 1.1 cgd
235 1.1.1.3 thorpej
236 1.1.1.3 thorpej /* Find an interface from which the specified address
237 1.1 cgd * should have come from. Used for figuring out which
238 1.1.1.5 christos * interface a packet came in on.
239 1.1 cgd */
240 1.1 cgd struct interface *
241 1.1.1.3 thorpej iflookup(naddr addr)
242 1.1 cgd {
243 1.1.1.3 thorpej struct interface *ifp, *maybe;
244 1.1 cgd
245 1.1 cgd maybe = 0;
246 1.1.1.5 christos for (;;) {
247 1.1.1.5 christos for (ifp = ifnet; ifp; ifp = ifp->int_next) {
248 1.1.1.5 christos if (ifp->int_if_flags & IFF_POINTOPOINT) {
249 1.1.1.3 thorpej /* finished with a match */
250 1.1.1.5 christos if (ifp->int_dstaddr == addr)
251 1.1.1.5 christos return ifp;
252 1.1.1.3 thorpej
253 1.1.1.5 christos } else {
254 1.1.1.5 christos /* finished with an exact match */
255 1.1.1.5 christos if (ifp->int_addr == addr)
256 1.1.1.5 christos return ifp;
257 1.1.1.3 thorpej
258 1.1.1.5 christos /* Look for the longest approximate match.
259 1.1.1.5 christos */
260 1.1.1.5 christos if (on_net(addr, ifp->int_net, ifp->int_mask)
261 1.1.1.5 christos && (maybe == 0
262 1.1.1.5 christos || ifp->int_mask > maybe->int_mask))
263 1.1.1.5 christos maybe = ifp;
264 1.1.1.5 christos }
265 1.1.1.3 thorpej }
266 1.1.1.3 thorpej
267 1.1.1.5 christos if (maybe != 0
268 1.1.1.6 thorpej || IF_RESCAN_DELAY())
269 1.1.1.5 christos return maybe;
270 1.1.1.5 christos
271 1.1.1.5 christos /* If there is no known interface, maybe there is a
272 1.1.1.5 christos * new interface. So just once look for new interfaces.
273 1.1.1.5 christos */
274 1.1.1.5 christos ifinit();
275 1.1.1.5 christos }
276 1.1.1.3 thorpej }
277 1.1.1.3 thorpej
278 1.1.1.3 thorpej
279 1.1.1.3 thorpej /* Return the classical netmask for an IP address.
280 1.1.1.3 thorpej */
281 1.1.1.5 christos naddr /* host byte order */
282 1.1.1.5 christos std_mask(naddr addr) /* network byte order */
283 1.1.1.3 thorpej {
284 1.1.1.3 thorpej NTOHL(addr); /* was a host, not a network */
285 1.1.1.3 thorpej
286 1.1.1.3 thorpej if (addr == 0) /* default route has mask 0 */
287 1.1.1.3 thorpej return 0;
288 1.1.1.3 thorpej if (IN_CLASSA(addr))
289 1.1.1.3 thorpej return IN_CLASSA_NET;
290 1.1.1.3 thorpej if (IN_CLASSB(addr))
291 1.1.1.3 thorpej return IN_CLASSB_NET;
292 1.1.1.3 thorpej return IN_CLASSC_NET;
293 1.1.1.3 thorpej }
294 1.1.1.3 thorpej
295 1.1.1.3 thorpej
296 1.1.1.4 christos /* Find the netmask that would be inferred by RIPv1 listeners
297 1.1.1.3 thorpej * on the given interface for a given network.
298 1.1.1.3 thorpej * If no interface is specified, look for the best fitting interface.
299 1.1.1.3 thorpej */
300 1.1.1.3 thorpej naddr
301 1.1.1.3 thorpej ripv1_mask_net(naddr addr, /* in network byte order */
302 1.1.1.3 thorpej struct interface *ifp) /* as seen on this interface */
303 1.1.1.3 thorpej {
304 1.1.1.6 thorpej struct r1net *r1p;
305 1.1.1.3 thorpej naddr mask = 0;
306 1.1.1.3 thorpej
307 1.1.1.3 thorpej if (addr == 0) /* default always has 0 mask */
308 1.1.1.3 thorpej return mask;
309 1.1.1.3 thorpej
310 1.1.1.6 thorpej if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
311 1.1.1.3 thorpej /* If the target network is that of the associated interface
312 1.1.1.3 thorpej * on which it arrived, then use the netmask of the interface.
313 1.1.1.3 thorpej */
314 1.1.1.3 thorpej if (on_net(addr, ifp->int_net, ifp->int_std_mask))
315 1.1.1.3 thorpej mask = ifp->int_ripv1_mask;
316 1.1.1.3 thorpej
317 1.1.1.3 thorpej } else {
318 1.1.1.3 thorpej /* Examine all interfaces, and if it the target seems
319 1.1.1.3 thorpej * to have the same network number of an interface, use the
320 1.1.1.3 thorpej * netmask of that interface. If there is more than one
321 1.1.1.3 thorpej * such interface, prefer the interface with the longest
322 1.1.1.3 thorpej * match.
323 1.1.1.3 thorpej */
324 1.1.1.3 thorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
325 1.1.1.3 thorpej if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
326 1.1.1.6 thorpej && ifp->int_ripv1_mask > mask
327 1.1.1.6 thorpej && ifp->int_ripv1_mask != HOST_MASK)
328 1.1.1.3 thorpej mask = ifp->int_ripv1_mask;
329 1.1.1.3 thorpej }
330 1.1.1.6 thorpej
331 1.1.1.3 thorpej }
332 1.1.1.3 thorpej
333 1.1.1.6 thorpej /* check special definitions */
334 1.1.1.6 thorpej if (mask == 0) {
335 1.1.1.6 thorpej for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
336 1.1.1.6 thorpej if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
337 1.1.1.6 thorpej && r1p->r1net_mask > mask)
338 1.1.1.6 thorpej r1p->r1net_mask = mask;
339 1.1.1.6 thorpej }
340 1.1.1.6 thorpej
341 1.1.1.6 thorpej /* Otherwise, make the classic A/B/C guess.
342 1.1.1.6 thorpej */
343 1.1.1.6 thorpej if (mask == 0)
344 1.1.1.6 thorpej mask = std_mask(addr);
345 1.1.1.6 thorpej }
346 1.1.1.3 thorpej
347 1.1.1.3 thorpej return mask;
348 1.1.1.3 thorpej }
349 1.1.1.3 thorpej
350 1.1.1.3 thorpej
351 1.1.1.3 thorpej naddr
352 1.1.1.3 thorpej ripv1_mask_host(naddr addr, /* in network byte order */
353 1.1.1.3 thorpej struct interface *ifp) /* as seen on this interface */
354 1.1.1.3 thorpej {
355 1.1.1.3 thorpej naddr mask = ripv1_mask_net(addr, ifp);
356 1.1.1.3 thorpej
357 1.1.1.3 thorpej
358 1.1.1.3 thorpej /* If the computed netmask does not mask the address,
359 1.1.1.3 thorpej * then assume it is a host address
360 1.1.1.3 thorpej */
361 1.1.1.3 thorpej if ((ntohl(addr) & ~mask) != 0)
362 1.1.1.3 thorpej mask = HOST_MASK;
363 1.1.1.3 thorpej return mask;
364 1.1.1.3 thorpej }
365 1.1.1.3 thorpej
366 1.1.1.3 thorpej
367 1.1.1.3 thorpej /* See if a IP address looks reasonable as a destination
368 1.1.1.3 thorpej */
369 1.1.1.3 thorpej int /* 0=bad */
370 1.1.1.3 thorpej check_dst(naddr addr)
371 1.1.1.3 thorpej {
372 1.1.1.3 thorpej NTOHL(addr);
373 1.1.1.3 thorpej
374 1.1.1.3 thorpej if (IN_CLASSA(addr)) {
375 1.1.1.3 thorpej if (addr == 0)
376 1.1.1.3 thorpej return 1; /* default */
377 1.1.1.3 thorpej
378 1.1.1.3 thorpej addr >>= IN_CLASSA_NSHIFT;
379 1.1.1.3 thorpej return (addr != 0 && addr != IN_LOOPBACKNET);
380 1.1.1.3 thorpej }
381 1.1.1.3 thorpej
382 1.1.1.3 thorpej return (IN_CLASSB(addr) || IN_CLASSC(addr));
383 1.1.1.3 thorpej }
384 1.1.1.3 thorpej
385 1.1.1.3 thorpej
386 1.1.1.5 christos /* See a new interface duplicates an existing interface.
387 1.1.1.5 christos */
388 1.1.1.5 christos struct interface *
389 1.1.1.5 christos check_dup(naddr addr, /* IP address, so network byte order */
390 1.1.1.5 christos naddr dstaddr, /* ditto */
391 1.1.1.5 christos naddr mask, /* mask, so host byte order */
392 1.1.1.5 christos int if_flags)
393 1.1.1.5 christos {
394 1.1.1.5 christos struct interface *ifp;
395 1.1.1.5 christos
396 1.1.1.5 christos for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
397 1.1.1.5 christos if (ifp->int_mask != mask)
398 1.1.1.5 christos continue;
399 1.1.1.5 christos
400 1.1.1.6 thorpej if (!iff_up(ifp->int_if_flags))
401 1.1.1.5 christos continue;
402 1.1.1.5 christos
403 1.1.1.5 christos /* The local address can only be shared with a point-to-point
404 1.1.1.5 christos * link.
405 1.1.1.5 christos */
406 1.1.1.5 christos if (ifp->int_addr == addr
407 1.1.1.5 christos && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
408 1.1.1.5 christos return ifp;
409 1.1.1.5 christos
410 1.1.1.5 christos if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
411 1.1.1.5 christos return ifp;
412 1.1.1.5 christos }
413 1.1.1.5 christos return 0;
414 1.1.1.5 christos }
415 1.1.1.5 christos
416 1.1.1.5 christos
417 1.1.1.5 christos /* See that a remote gateway is reachable.
418 1.1.1.5 christos * Note that the answer can change as real interfaces come and go.
419 1.1.1.5 christos */
420 1.1.1.5 christos int /* 0=bad */
421 1.1.1.5 christos check_remote(struct interface *ifp)
422 1.1.1.5 christos {
423 1.1.1.5 christos struct rt_entry *rt;
424 1.1.1.5 christos
425 1.1.1.5 christos /* do not worry about other kinds */
426 1.1.1.5 christos if (!(ifp->int_state & IS_REMOTE))
427 1.1.1.5 christos return 1;
428 1.1.1.5 christos
429 1.1.1.5 christos rt = rtfind(ifp->int_addr);
430 1.1.1.5 christos if (rt != 0
431 1.1.1.5 christos && rt->rt_ifp != 0
432 1.1.1.5 christos &&on_net(ifp->int_addr,
433 1.1.1.5 christos rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
434 1.1.1.5 christos return 1;
435 1.1.1.5 christos
436 1.1.1.5 christos /* the gateway cannot be reached directly from one of our
437 1.1.1.5 christos * interfaces
438 1.1.1.5 christos */
439 1.1.1.5 christos if (!(ifp->int_state & IS_BROKE)) {
440 1.1.1.5 christos msglog("unreachable gateway %s in "_PATH_GATEWAYS,
441 1.1.1.5 christos naddr_ntoa(ifp->int_addr));
442 1.1.1.5 christos if_bad(ifp);
443 1.1.1.5 christos }
444 1.1.1.5 christos return 0;
445 1.1.1.5 christos }
446 1.1.1.5 christos
447 1.1.1.5 christos
448 1.1.1.3 thorpej /* Delete an interface.
449 1.1.1.3 thorpej */
450 1.1.1.3 thorpej static void
451 1.1.1.3 thorpej ifdel(struct interface *ifp)
452 1.1.1.3 thorpej {
453 1.1.1.3 thorpej struct ip_mreq m;
454 1.1.1.3 thorpej struct interface *ifp1;
455 1.1.1.3 thorpej
456 1.1.1.3 thorpej
457 1.1.1.3 thorpej trace_if("Del", ifp);
458 1.1.1.3 thorpej
459 1.1.1.3 thorpej ifp->int_state |= IS_BROKE;
460 1.1.1.3 thorpej
461 1.1.1.3 thorpej /* unlink the interface
462 1.1.1.3 thorpej */
463 1.1.1.5 christos *ifp->int_prev = ifp->int_next;
464 1.1.1.3 thorpej if (ifp->int_next != 0)
465 1.1.1.3 thorpej ifp->int_next->int_prev = ifp->int_prev;
466 1.1.1.5 christos *ifp->int_ahash_prev = ifp->int_ahash;
467 1.1.1.5 christos if (ifp->int_ahash != 0)
468 1.1.1.5 christos ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
469 1.1.1.5 christos *ifp->int_nhash_prev = ifp->int_nhash;
470 1.1.1.5 christos if (ifp->int_nhash != 0)
471 1.1.1.5 christos ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
472 1.1.1.5 christos if (ifp->int_if_flags & IFF_BROADCAST) {
473 1.1.1.5 christos *ifp->int_bhash_prev = ifp->int_bhash;
474 1.1.1.5 christos if (ifp->int_bhash != 0)
475 1.1.1.5 christos ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
476 1.1.1.5 christos }
477 1.1.1.5 christos if (ifp->int_state & IS_REMOTE) {
478 1.1.1.5 christos *ifp->int_rlink_prev = ifp->int_rlink;
479 1.1.1.5 christos if (ifp->int_rlink != 0)
480 1.1.1.5 christos ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
481 1.1.1.5 christos }
482 1.1.1.3 thorpej
483 1.1.1.3 thorpej if (!(ifp->int_state & IS_ALIAS)) {
484 1.1.1.5 christos /* delete aliases when the main interface dies
485 1.1.1.3 thorpej */
486 1.1.1.3 thorpej for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
487 1.1.1.3 thorpej if (ifp1 != ifp
488 1.1.1.3 thorpej && !strcmp(ifp->int_name, ifp1->int_name))
489 1.1.1.3 thorpej ifdel(ifp1);
490 1.1.1.3 thorpej }
491 1.1.1.3 thorpej
492 1.1.1.3 thorpej if ((ifp->int_if_flags & IFF_MULTICAST)
493 1.1.1.3 thorpej #ifdef MCAST_PPP_BUG
494 1.1.1.3 thorpej && !(ifp->int_if_flags & IFF_POINTOPOINT)
495 1.1.1.3 thorpej #endif
496 1.1.1.3 thorpej && rip_sock >= 0) {
497 1.1.1.3 thorpej m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
498 1.1.1.3 thorpej m.imr_interface.s_addr = ((ifp->int_if_flags
499 1.1.1.3 thorpej & IFF_POINTOPOINT)
500 1.1.1.3 thorpej ? ifp->int_dstaddr
501 1.1.1.3 thorpej : ifp->int_addr);
502 1.1.1.3 thorpej if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
503 1.1.1.3 thorpej &m, sizeof(m)) < 0
504 1.1.1.3 thorpej && errno != EADDRNOTAVAIL
505 1.1.1.3 thorpej && !TRACEACTIONS)
506 1.1.1.3 thorpej LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
507 1.1.1.5 christos if (rip_sock_mcast == ifp)
508 1.1.1.5 christos rip_sock_mcast = 0;
509 1.1.1.3 thorpej }
510 1.1.1.3 thorpej if (ifp->int_rip_sock >= 0) {
511 1.1.1.3 thorpej (void)close(ifp->int_rip_sock);
512 1.1.1.3 thorpej ifp->int_rip_sock = -1;
513 1.1.1.3 thorpej fix_select();
514 1.1.1.3 thorpej }
515 1.1.1.3 thorpej
516 1.1.1.3 thorpej tot_interfaces--;
517 1.1.1.3 thorpej if (!IS_RIP_OFF(ifp->int_state))
518 1.1.1.3 thorpej rip_interfaces--;
519 1.1.1.3 thorpej
520 1.1.1.3 thorpej /* Zap all routes associated with this interface.
521 1.1.1.6 thorpej * Assume routes just using gateways beyond this interface
522 1.1.1.6 thorpej * will timeout naturally, and have probably already died.
523 1.1.1.3 thorpej */
524 1.1.1.3 thorpej (void)rn_walktree(rhead, walk_bad, 0);
525 1.1.1.3 thorpej
526 1.1.1.3 thorpej set_rdisc_mg(ifp, 0);
527 1.1.1.3 thorpej if_bad_rdisc(ifp);
528 1.1.1.3 thorpej }
529 1.1.1.3 thorpej
530 1.1.1.3 thorpej free(ifp);
531 1.1.1.3 thorpej }
532 1.1.1.3 thorpej
533 1.1.1.3 thorpej
534 1.1.1.3 thorpej /* Mark an interface ill.
535 1.1.1.3 thorpej */
536 1.1.1.3 thorpej void
537 1.1.1.3 thorpej if_sick(struct interface *ifp)
538 1.1.1.3 thorpej {
539 1.1.1.3 thorpej if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
540 1.1.1.3 thorpej ifp->int_state |= IS_SICK;
541 1.1.1.5 christos ifp->int_act_time = NEVER;
542 1.1.1.3 thorpej trace_if("Chg", ifp);
543 1.1.1.3 thorpej
544 1.1.1.3 thorpej LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
545 1.1.1.3 thorpej }
546 1.1.1.3 thorpej }
547 1.1.1.3 thorpej
548 1.1.1.3 thorpej
549 1.1.1.3 thorpej /* Mark an interface dead.
550 1.1.1.3 thorpej */
551 1.1.1.3 thorpej void
552 1.1.1.3 thorpej if_bad(struct interface *ifp)
553 1.1.1.3 thorpej {
554 1.1.1.3 thorpej struct interface *ifp1;
555 1.1.1.3 thorpej
556 1.1.1.3 thorpej
557 1.1.1.3 thorpej if (ifp->int_state & IS_BROKE)
558 1.1.1.3 thorpej return;
559 1.1.1.3 thorpej
560 1.1.1.3 thorpej LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
561 1.1.1.3 thorpej
562 1.1.1.3 thorpej ifp->int_state |= (IS_BROKE | IS_SICK);
563 1.1.1.5 christos ifp->int_act_time = NEVER;
564 1.1.1.5 christos ifp->int_query_time = NEVER;
565 1.1.1.6 thorpej ifp->int_data.ts = now.tv_sec;
566 1.1.1.3 thorpej
567 1.1.1.3 thorpej trace_if("Chg", ifp);
568 1.1.1.3 thorpej
569 1.1.1.3 thorpej if (!(ifp->int_state & IS_ALIAS)) {
570 1.1.1.3 thorpej for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
571 1.1.1.3 thorpej if (ifp1 != ifp
572 1.1.1.3 thorpej && !strcmp(ifp->int_name, ifp1->int_name))
573 1.1.1.3 thorpej if_bad(ifp1);
574 1.1.1.3 thorpej }
575 1.1.1.3 thorpej (void)rn_walktree(rhead, walk_bad, 0);
576 1.1.1.3 thorpej if_bad_rdisc(ifp);
577 1.1.1.3 thorpej }
578 1.1.1.3 thorpej }
579 1.1.1.3 thorpej
580 1.1.1.3 thorpej
581 1.1.1.3 thorpej /* Mark an interface alive
582 1.1.1.3 thorpej */
583 1.1.1.3 thorpej int /* 1=it was dead */
584 1.1.1.3 thorpej if_ok(struct interface *ifp,
585 1.1.1.3 thorpej char *type)
586 1.1.1.3 thorpej {
587 1.1.1.3 thorpej struct interface *ifp1;
588 1.1.1.3 thorpej
589 1.1.1.3 thorpej
590 1.1.1.3 thorpej if (!(ifp->int_state & IS_BROKE)) {
591 1.1.1.3 thorpej if (ifp->int_state & IS_SICK) {
592 1.1.1.5 christos trace_act("%sinterface %s to %s working better",
593 1.1.1.3 thorpej type,
594 1.1.1.5 christos ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
595 1.1.1.3 thorpej ifp->int_state &= ~IS_SICK;
596 1.1.1.3 thorpej }
597 1.1.1.3 thorpej return 0;
598 1.1.1.3 thorpej }
599 1.1.1.3 thorpej
600 1.1.1.3 thorpej msglog("%sinterface %s to %s restored",
601 1.1.1.5 christos type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
602 1.1.1.3 thorpej ifp->int_state &= ~(IS_BROKE | IS_SICK);
603 1.1.1.3 thorpej ifp->int_data.ts = 0;
604 1.1.1.3 thorpej
605 1.1.1.3 thorpej if (!(ifp->int_state & IS_ALIAS)) {
606 1.1.1.3 thorpej for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
607 1.1.1.3 thorpej if (ifp1 != ifp
608 1.1.1.3 thorpej && !strcmp(ifp->int_name, ifp1->int_name))
609 1.1.1.3 thorpej if_ok(ifp1, type);
610 1.1.1.3 thorpej }
611 1.1.1.3 thorpej if_ok_rdisc(ifp);
612 1.1.1.3 thorpej }
613 1.1.1.5 christos
614 1.1.1.5 christos if (ifp->int_state & IS_REMOTE) {
615 1.1.1.5 christos if (!addrouteforif(ifp))
616 1.1.1.5 christos return 0;
617 1.1.1.5 christos }
618 1.1.1.3 thorpej return 1;
619 1.1.1.3 thorpej }
620 1.1.1.3 thorpej
621 1.1.1.3 thorpej
622 1.1.1.3 thorpej /* disassemble routing message
623 1.1.1.3 thorpej */
624 1.1.1.3 thorpej void
625 1.1.1.3 thorpej rt_xaddrs(struct rt_addrinfo *info,
626 1.1.1.3 thorpej struct sockaddr *sa,
627 1.1.1.3 thorpej struct sockaddr *lim,
628 1.1.1.3 thorpej int addrs)
629 1.1.1.3 thorpej {
630 1.1.1.3 thorpej int i;
631 1.1.1.3 thorpej #ifdef _HAVE_SA_LEN
632 1.1.1.3 thorpej static struct sockaddr sa_zero;
633 1.1.1.3 thorpej #endif
634 1.1.1.3 thorpej #ifdef sgi
635 1.1.1.3 thorpej #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
636 1.1.1.3 thorpej : sizeof(__uint64_t))
637 1.1.1.3 thorpej #else
638 1.1.1.3 thorpej #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
639 1.1.1.3 thorpej : sizeof(long))
640 1.1.1.3 thorpej #endif
641 1.1.1.3 thorpej
642 1.1.1.3 thorpej
643 1.1.1.3 thorpej bzero(info, sizeof(*info));
644 1.1.1.3 thorpej info->rti_addrs = addrs;
645 1.1.1.3 thorpej for (i = 0; i < RTAX_MAX && sa < lim; i++) {
646 1.1.1.3 thorpej if ((addrs & (1 << i)) == 0)
647 1.1 cgd continue;
648 1.1.1.3 thorpej #ifdef _HAVE_SA_LEN
649 1.1.1.3 thorpej info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
650 1.1.1.3 thorpej sa = (struct sockaddr *)((char*)(sa)
651 1.1.1.3 thorpej + ROUNDUP(sa->sa_len));
652 1.1.1.3 thorpej #else
653 1.1.1.3 thorpej info->rti_info[i] = sa;
654 1.1.1.3 thorpej sa = (struct sockaddr *)((char*)(sa)
655 1.1.1.3 thorpej + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
656 1.1.1.3 thorpej #endif
657 1.1.1.3 thorpej }
658 1.1.1.3 thorpej }
659 1.1.1.3 thorpej
660 1.1.1.3 thorpej
661 1.1.1.3 thorpej /* Find the network interfaces which have configured themselves.
662 1.1.1.3 thorpej * This must be done regularly, if only for extra addresses
663 1.1.1.3 thorpej * that come and go on interfaces.
664 1.1.1.3 thorpej */
665 1.1.1.3 thorpej void
666 1.1.1.3 thorpej ifinit(void)
667 1.1.1.3 thorpej {
668 1.1.1.3 thorpej static char *sysctl_buf;
669 1.1.1.3 thorpej static size_t sysctl_buf_size = 0;
670 1.1.1.3 thorpej uint complaints = 0;
671 1.1.1.3 thorpej static u_int prev_complaints = 0;
672 1.1.1.4 christos # define COMP_NOT_INET 0x001
673 1.1.1.5 christos # define COMP_NOADDR 0x002
674 1.1.1.5 christos # define COMP_BADADDR 0x004
675 1.1.1.5 christos # define COMP_NODST 0x008
676 1.1.1.5 christos # define COMP_NOBADR 0x010
677 1.1.1.5 christos # define COMP_NOMASK 0x020
678 1.1.1.5 christos # define COMP_DUP 0x040
679 1.1.1.5 christos # define COMP_BAD_METRIC 0x080
680 1.1.1.5 christos # define COMP_NETMASK 0x100
681 1.1.1.3 thorpej
682 1.1.1.3 thorpej struct interface ifs, ifs0, *ifp, *ifp1;
683 1.1.1.3 thorpej struct rt_entry *rt;
684 1.1.1.3 thorpej size_t needed;
685 1.1.1.3 thorpej int mib[6];
686 1.1.1.3 thorpej struct if_msghdr *ifm;
687 1.1.1.3 thorpej struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
688 1.1.1.3 thorpej int in, ierr, out, oerr;
689 1.1.1.3 thorpej struct intnet *intnetp;
690 1.1.1.3 thorpej struct rt_addrinfo info;
691 1.1.1.3 thorpej #ifdef SIOCGIFMETRIC
692 1.1.1.3 thorpej struct ifreq ifr;
693 1.1.1.3 thorpej #endif
694 1.1.1.3 thorpej
695 1.1.1.3 thorpej
696 1.1.1.5 christos last_ifinit = now;
697 1.1.1.3 thorpej ifinit_timer.tv_sec = now.tv_sec + (supplier
698 1.1.1.3 thorpej ? CHECK_ACT_INTERVAL
699 1.1.1.3 thorpej : CHECK_QUIET_INTERVAL);
700 1.1.1.3 thorpej
701 1.1.1.3 thorpej /* mark all interfaces so we can get rid of thost that disappear */
702 1.1.1.3 thorpej for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next)
703 1.1.1.3 thorpej ifp->int_state &= ~(IS_CHECKED | IS_DUP);
704 1.1.1.3 thorpej
705 1.1.1.3 thorpej /* Fetch the interface list, without too many system calls
706 1.1.1.3 thorpej * since we do it repeatedly.
707 1.1.1.3 thorpej */
708 1.1.1.3 thorpej mib[0] = CTL_NET;
709 1.1.1.3 thorpej mib[1] = PF_ROUTE;
710 1.1.1.3 thorpej mib[2] = 0;
711 1.1.1.3 thorpej mib[3] = AF_INET;
712 1.1.1.3 thorpej mib[4] = NET_RT_IFLIST;
713 1.1.1.3 thorpej mib[5] = 0;
714 1.1.1.3 thorpej for (;;) {
715 1.1.1.3 thorpej if ((needed = sysctl_buf_size) != 0) {
716 1.1.1.3 thorpej if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
717 1.1.1.3 thorpej break;
718 1.1.1.6 thorpej /* retry if the table grew */
719 1.1.1.3 thorpej if (errno != ENOMEM && errno != EFAULT)
720 1.1.1.6 thorpej BADERR(1, "ifinit: sysctl(RT_IFLIST)");
721 1.1.1.3 thorpej free(sysctl_buf);
722 1.1.1.3 thorpej needed = 0;
723 1.1.1.3 thorpej }
724 1.1.1.3 thorpej if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
725 1.1.1.6 thorpej BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
726 1.1.1.6 thorpej sysctl_buf = rtmalloc(sysctl_buf_size = needed,
727 1.1.1.6 thorpej "ifinit sysctl");
728 1.1.1.3 thorpej }
729 1.1.1.3 thorpej
730 1.1.1.3 thorpej ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
731 1.1.1.3 thorpej for (ifam = (struct ifa_msghdr *)sysctl_buf;
732 1.1.1.3 thorpej ifam < ifam_lim;
733 1.1.1.3 thorpej ifam = ifam2) {
734 1.1.1.3 thorpej
735 1.1.1.3 thorpej ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
736 1.1.1.3 thorpej
737 1.1.1.3 thorpej if (ifam->ifam_type == RTM_IFINFO) {
738 1.1.1.5 christos struct sockaddr_dl *sdl;
739 1.1.1.5 christos
740 1.1.1.3 thorpej ifm = (struct if_msghdr *)ifam;
741 1.1.1.3 thorpej /* make prototype structure for the IP aliases
742 1.1.1.3 thorpej */
743 1.1.1.3 thorpej bzero(&ifs0, sizeof(ifs0));
744 1.1.1.3 thorpej ifs0.int_rip_sock = -1;
745 1.1.1.3 thorpej ifs0.int_index = ifm->ifm_index;
746 1.1.1.3 thorpej ifs0.int_if_flags = ifm->ifm_flags;
747 1.1.1.3 thorpej ifs0.int_state = IS_CHECKED;
748 1.1.1.5 christos ifs0.int_query_time = NEVER;
749 1.1.1.3 thorpej ifs0.int_act_time = now.tv_sec;
750 1.1.1.3 thorpej ifs0.int_data.ts = now.tv_sec;
751 1.1.1.3 thorpej ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
752 1.1.1.3 thorpej ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
753 1.1.1.3 thorpej ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
754 1.1.1.3 thorpej ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
755 1.1.1.3 thorpej #ifdef sgi
756 1.1.1.3 thorpej ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
757 1.1.1.3 thorpej #endif
758 1.1.1.3 thorpej sdl = (struct sockaddr_dl *)(ifm + 1);
759 1.1.1.3 thorpej sdl->sdl_data[sdl->sdl_nlen] = 0;
760 1.1.1.5 christos strncpy(ifs0.int_name, sdl->sdl_data,
761 1.1.1.5 christos MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
762 1.1.1.3 thorpej continue;
763 1.1.1.3 thorpej }
764 1.1.1.3 thorpej if (ifam->ifam_type != RTM_NEWADDR) {
765 1.1.1.4 christos logbad(1,"ifinit: out of sync");
766 1.1.1.3 thorpej continue;
767 1.1.1.3 thorpej }
768 1.1.1.3 thorpej rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
769 1.1.1.3 thorpej (struct sockaddr *)ifam2,
770 1.1.1.3 thorpej ifam->ifam_addrs);
771 1.1.1.3 thorpej
772 1.1.1.5 christos /* Prepare for the next address of this interface, which
773 1.1.1.5 christos * will be an alias.
774 1.1.1.5 christos * Do not output RIP or Router-Discovery packets via aliases.
775 1.1.1.5 christos */
776 1.1.1.5 christos bcopy(&ifs0, &ifs, sizeof(ifs));
777 1.1.1.6 thorpej ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
778 1.1.1.5 christos
779 1.1.1.3 thorpej if (INFO_IFA(&info) == 0) {
780 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
781 1.1.1.3 thorpej if (!(prev_complaints & COMP_NOADDR))
782 1.1.1.4 christos msglog("%s has no address",
783 1.1.1.5 christos ifs.int_name);
784 1.1.1.3 thorpej complaints |= COMP_NOADDR;
785 1.1.1.3 thorpej }
786 1.1.1.3 thorpej continue;
787 1.1.1.3 thorpej }
788 1.1.1.3 thorpej if (INFO_IFA(&info)->sa_family != AF_INET) {
789 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
790 1.1.1.3 thorpej if (!(prev_complaints & COMP_NOT_INET))
791 1.1.1.5 christos trace_act("%s: not AF_INET",
792 1.1.1.5 christos ifs.int_name);
793 1.1.1.3 thorpej complaints |= COMP_NOT_INET;
794 1.1.1.3 thorpej }
795 1.1.1.3 thorpej continue;
796 1.1.1.3 thorpej }
797 1.1.1.3 thorpej
798 1.1.1.3 thorpej ifs.int_addr = S_ADDR(INFO_IFA(&info));
799 1.1.1.3 thorpej
800 1.1.1.4 christos if (ntohl(ifs.int_addr)>>24 == 0
801 1.1.1.4 christos || ntohl(ifs.int_addr)>>24 == 0xff) {
802 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
803 1.1.1.4 christos if (!(prev_complaints & COMP_BADADDR))
804 1.1.1.4 christos msglog("%s has a bad address",
805 1.1.1.5 christos ifs.int_name);
806 1.1.1.4 christos complaints |= COMP_BADADDR;
807 1.1.1.4 christos }
808 1.1.1.4 christos continue;
809 1.1.1.4 christos }
810 1.1.1.4 christos
811 1.1.1.5 christos if (ifs.int_if_flags & IFF_LOOPBACK) {
812 1.1.1.5 christos ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
813 1.1.1.3 thorpej ifs.int_dstaddr = ifs.int_addr;
814 1.1.1.5 christos ifs.int_mask = HOST_MASK;
815 1.1.1.5 christos ifs.int_ripv1_mask = HOST_MASK;
816 1.1.1.5 christos ifs.int_std_mask = std_mask(ifs.int_dstaddr);
817 1.1.1.5 christos ifs.int_net = ntohl(ifs.int_dstaddr);
818 1.1.1.5 christos if (!foundloopback) {
819 1.1.1.5 christos foundloopback = 1;
820 1.1.1.5 christos loopaddr = ifs.int_addr;
821 1.1.1.6 thorpej loop_rts.rts_gate = loopaddr;
822 1.1.1.6 thorpej loop_rts.rts_router = loopaddr;
823 1.1.1.3 thorpej }
824 1.1.1.3 thorpej
825 1.1.1.3 thorpej } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
826 1.1.1.3 thorpej if (INFO_BRD(&info) == 0
827 1.1.1.3 thorpej || INFO_BRD(&info)->sa_family != AF_INET) {
828 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
829 1.1.1.3 thorpej if (!(prev_complaints & COMP_NODST))
830 1.1.1.3 thorpej msglog("%s has a bad"
831 1.1.1.3 thorpej " destination address",
832 1.1.1.5 christos ifs.int_name);
833 1.1.1.3 thorpej complaints |= COMP_NODST;
834 1.1.1.3 thorpej }
835 1.1.1.3 thorpej continue;
836 1.1.1.3 thorpej }
837 1.1.1.3 thorpej ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
838 1.1.1.4 christos if (ntohl(ifs.int_dstaddr)>>24 == 0
839 1.1.1.4 christos || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
840 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
841 1.1.1.4 christos if (!(prev_complaints & COMP_NODST))
842 1.1.1.4 christos msglog("%s has a bad"
843 1.1.1.4 christos " destination address",
844 1.1.1.5 christos ifs.int_name);
845 1.1.1.4 christos complaints |= COMP_NODST;
846 1.1.1.4 christos }
847 1.1.1.4 christos continue;
848 1.1.1.4 christos }
849 1.1.1.3 thorpej ifs.int_mask = HOST_MASK;
850 1.1.1.3 thorpej ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
851 1.1.1.3 thorpej ifs.int_std_mask = std_mask(ifs.int_dstaddr);
852 1.1.1.3 thorpej ifs.int_net = ntohl(ifs.int_dstaddr);
853 1.1.1.5 christos
854 1.1.1.5 christos } else {
855 1.1.1.5 christos if (INFO_MASK(&info) == 0) {
856 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
857 1.1.1.5 christos if (!(prev_complaints & COMP_NOMASK))
858 1.1.1.5 christos msglog("%s has no netmask",
859 1.1.1.5 christos ifs.int_name);
860 1.1.1.5 christos complaints |= COMP_NOMASK;
861 1.1.1.5 christos }
862 1.1.1.5 christos continue;
863 1.1.1.3 thorpej }
864 1.1.1.5 christos ifs.int_dstaddr = ifs.int_addr;
865 1.1.1.5 christos ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
866 1.1.1.5 christos ifs.int_ripv1_mask = ifs.int_mask;
867 1.1.1.5 christos ifs.int_std_mask = std_mask(ifs.int_addr);
868 1.1.1.5 christos ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
869 1.1.1.5 christos if (ifs.int_mask != ifs.int_std_mask)
870 1.1.1.5 christos ifs.int_state |= IS_SUBNET;
871 1.1.1.3 thorpej
872 1.1.1.5 christos if (ifs.int_if_flags & IFF_BROADCAST) {
873 1.1.1.5 christos if (INFO_BRD(&info) == 0) {
874 1.1.1.6 thorpej if (iff_up(ifs.int_if_flags)) {
875 1.1.1.5 christos if (!(prev_complaints
876 1.1.1.5 christos & COMP_NOBADR))
877 1.1.1.5 christos msglog("%s has"
878 1.1.1.5 christos "no broadcast address",
879 1.1.1.5 christos ifs.int_name);
880 1.1.1.5 christos complaints |= COMP_NOBADR;
881 1.1.1.5 christos }
882 1.1.1.5 christos continue;
883 1.1.1.5 christos }
884 1.1.1.5 christos ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
885 1.1.1.5 christos }
886 1.1.1.3 thorpej }
887 1.1.1.3 thorpej ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
888 1.1.1.3 thorpej ifs.int_std_addr = htonl(ifs.int_std_net);
889 1.1.1.3 thorpej
890 1.1.1.3 thorpej /* Use a minimum metric of one. Treat the interface metric
891 1.1.1.3 thorpej * (default 0) as an increment to the hop count of one.
892 1.1.1.3 thorpej *
893 1.1.1.3 thorpej * The metric obtained from the routing socket dump of
894 1.1.1.3 thorpej * interface addresses is wrong. It is not set by the
895 1.1.1.3 thorpej * SIOCSIFMETRIC ioctl.
896 1.1.1.3 thorpej */
897 1.1.1.3 thorpej #ifdef SIOCGIFMETRIC
898 1.1.1.5 christos strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
899 1.1.1.3 thorpej if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
900 1.1.1.3 thorpej DBGERR(1, "ioctl(SIOCGIFMETRIC)");
901 1.1.1.3 thorpej ifs.int_metric = 0;
902 1.1.1.3 thorpej } else {
903 1.1.1.3 thorpej ifs.int_metric = ifr.ifr_metric;
904 1.1.1.3 thorpej }
905 1.1.1.3 thorpej #else
906 1.1.1.3 thorpej ifs.int_metric = ifam->ifam_metric;
907 1.1.1.3 thorpej #endif
908 1.1.1.3 thorpej if (ifs.int_metric > HOPCNT_INFINITY) {
909 1.1.1.3 thorpej ifs.int_metric = 0;
910 1.1.1.3 thorpej if (!(prev_complaints & COMP_BAD_METRIC)
911 1.1.1.6 thorpej && iff_up(ifs.int_if_flags)) {
912 1.1.1.3 thorpej complaints |= COMP_BAD_METRIC;
913 1.1.1.3 thorpej msglog("%s has a metric of %d",
914 1.1.1.5 christos ifs.int_name, ifs.int_metric);
915 1.1.1.3 thorpej }
916 1.1.1.3 thorpej }
917 1.1.1.3 thorpej
918 1.1.1.3 thorpej /* See if this is a familiar interface.
919 1.1.1.3 thorpej * If so, stop worrying about it if it is the same.
920 1.1.1.3 thorpej * Start it over if it now is to somewhere else, as happens
921 1.1.1.3 thorpej * frequently with PPP and SLIP.
922 1.1.1.3 thorpej */
923 1.1.1.5 christos ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
924 1.1.1.5 christos ? ifs.int_addr
925 1.1.1.5 christos : 0));
926 1.1.1.3 thorpej if (ifp != 0) {
927 1.1.1.3 thorpej ifp->int_state |= IS_CHECKED;
928 1.1.1.3 thorpej
929 1.1.1.3 thorpej if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
930 1.1.1.3 thorpej & (IFF_BROADCAST
931 1.1.1.3 thorpej | IFF_LOOPBACK
932 1.1.1.3 thorpej | IFF_POINTOPOINT
933 1.1.1.3 thorpej | IFF_MULTICAST))
934 1.1.1.3 thorpej || 0 != ((ifp->int_state ^ ifs.int_state)
935 1.1.1.3 thorpej & IS_ALIAS)
936 1.1.1.3 thorpej || ifp->int_addr != ifs.int_addr
937 1.1.1.3 thorpej || ifp->int_brdaddr != ifs.int_brdaddr
938 1.1.1.3 thorpej || ifp->int_dstaddr != ifs.int_dstaddr
939 1.1.1.3 thorpej || ifp->int_mask != ifs.int_mask
940 1.1.1.3 thorpej || ifp->int_metric != ifs.int_metric) {
941 1.1.1.3 thorpej /* Forget old information about
942 1.1.1.3 thorpej * a changed interface.
943 1.1.1.3 thorpej */
944 1.1.1.5 christos trace_act("interface %s has changed",
945 1.1.1.3 thorpej ifp->int_name);
946 1.1.1.3 thorpej ifdel(ifp);
947 1.1.1.3 thorpej ifp = 0;
948 1.1.1.3 thorpej }
949 1.1.1.3 thorpej }
950 1.1.1.3 thorpej
951 1.1.1.3 thorpej if (ifp != 0) {
952 1.1.1.4 christos /* The primary representative of an alias worries
953 1.1.1.4 christos * about how things are working.
954 1.1.1.4 christos */
955 1.1.1.3 thorpej if (ifp->int_state & IS_ALIAS)
956 1.1.1.3 thorpej continue;
957 1.1.1.3 thorpej
958 1.1.1.3 thorpej /* note interfaces that have been turned off
959 1.1.1.3 thorpej */
960 1.1.1.6 thorpej if (!iff_up(ifs.int_if_flags)) {
961 1.1.1.6 thorpej if (iff_up(ifp->int_if_flags)) {
962 1.1.1.3 thorpej msglog("interface %s to %s turned off",
963 1.1.1.3 thorpej ifp->int_name,
964 1.1.1.5 christos naddr_ntoa(ifp->int_dstaddr));
965 1.1.1.3 thorpej if_bad(ifp);
966 1.1.1.6 thorpej ifp->int_if_flags &= ~IFF_UP;
967 1.1.1.6 thorpej } else if (now.tv_sec>(ifp->int_data.ts
968 1.1.1.6 thorpej + CHECK_BAD_INTERVAL)) {
969 1.1.1.6 thorpej trace_act("interface %s has been off"
970 1.1.1.6 thorpej " %d seconds; forget it",
971 1.1.1.6 thorpej ifp->int_name,
972 1.1.1.6 thorpej now.tv_sec-ifp->int_data.ts);
973 1.1.1.6 thorpej ifdel(ifp);
974 1.1.1.3 thorpej }
975 1.1.1.3 thorpej continue;
976 1.1.1.3 thorpej }
977 1.1.1.3 thorpej /* or that were off and are now ok */
978 1.1.1.6 thorpej if (!iff_up(ifp->int_if_flags)) {
979 1.1.1.6 thorpej ifp->int_if_flags |= IFF_UP;
980 1.1.1.3 thorpej (void)if_ok(ifp, "");
981 1.1.1.3 thorpej }
982 1.1.1.3 thorpej
983 1.1.1.3 thorpej /* If it has been long enough,
984 1.1.1.3 thorpej * see if the interface is broken.
985 1.1.1.3 thorpej */
986 1.1.1.3 thorpej if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
987 1.1.1.3 thorpej continue;
988 1.1.1.3 thorpej
989 1.1.1.3 thorpej in = ifs.int_data.ipackets - ifp->int_data.ipackets;
990 1.1.1.3 thorpej ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
991 1.1.1.3 thorpej out = ifs.int_data.opackets - ifp->int_data.opackets;
992 1.1.1.3 thorpej oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
993 1.1.1.3 thorpej #ifdef sgi
994 1.1.1.3 thorpej /* Through at least IRIX 6.2, PPP and SLIP
995 1.1.1.5 christos * count packets dropped by the filters.
996 1.1.1.3 thorpej * But FDDI rings stuck non-operational count
997 1.1.1.3 thorpej * dropped packets as they wait for improvement.
998 1.1.1.3 thorpej */
999 1.1.1.3 thorpej if (!(ifp->int_if_flags & IFF_POINTOPOINT))
1000 1.1.1.3 thorpej oerr += (ifs.int_data.odrops
1001 1.1.1.3 thorpej - ifp->int_data.odrops);
1002 1.1.1.3 thorpej #endif
1003 1.1.1.3 thorpej /* If the interface just awoke, restart the counters.
1004 1.1.1.3 thorpej */
1005 1.1.1.3 thorpej if (ifp->int_data.ts == 0) {
1006 1.1.1.3 thorpej ifp->int_data = ifs.int_data;
1007 1.1.1.3 thorpej continue;
1008 1.1.1.3 thorpej }
1009 1.1.1.3 thorpej ifp->int_data = ifs.int_data;
1010 1.1.1.3 thorpej
1011 1.1.1.3 thorpej /* Withhold judgement when the short error
1012 1.1.1.3 thorpej * counters wrap or the interface is reset.
1013 1.1.1.3 thorpej */
1014 1.1.1.3 thorpej if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1015 1.1.1.3 thorpej LIM_SEC(ifinit_timer,
1016 1.1.1.3 thorpej now.tv_sec+CHECK_BAD_INTERVAL);
1017 1.1.1.3 thorpej continue;
1018 1.1.1.3 thorpej }
1019 1.1.1.3 thorpej
1020 1.1.1.3 thorpej /* Withhold judgement when there is no traffic
1021 1.1.1.3 thorpej */
1022 1.1.1.3 thorpej if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1023 1.1.1.3 thorpej continue;
1024 1.1.1.3 thorpej
1025 1.1.1.3 thorpej /* It is bad if input or output is not working.
1026 1.1.1.3 thorpej * Require presistent problems before marking it dead.
1027 1.1.1.3 thorpej */
1028 1.1.1.3 thorpej if ((in <= ierr && ierr > 0)
1029 1.1.1.3 thorpej || (out <= oerr && oerr > 0)) {
1030 1.1.1.3 thorpej if (!(ifp->int_state & IS_SICK)) {
1031 1.1.1.3 thorpej trace_act("interface %s to %s"
1032 1.1.1.3 thorpej " sick: in=%d ierr=%d"
1033 1.1.1.5 christos " out=%d oerr=%d",
1034 1.1.1.3 thorpej ifp->int_name,
1035 1.1.1.5 christos naddr_ntoa(ifp->int_dstaddr),
1036 1.1.1.3 thorpej in, ierr, out, oerr);
1037 1.1.1.3 thorpej if_sick(ifp);
1038 1.1.1.3 thorpej continue;
1039 1.1.1.3 thorpej }
1040 1.1.1.3 thorpej if (!(ifp->int_state & IS_BROKE)) {
1041 1.1.1.5 christos msglog("interface %s to %s broken:"
1042 1.1.1.3 thorpej " in=%d ierr=%d out=%d oerr=%d",
1043 1.1.1.3 thorpej ifp->int_name,
1044 1.1.1.5 christos naddr_ntoa(ifp->int_dstaddr),
1045 1.1.1.3 thorpej in, ierr, out, oerr);
1046 1.1.1.3 thorpej if_bad(ifp);
1047 1.1.1.3 thorpej }
1048 1.1.1.3 thorpej continue;
1049 1.1.1.3 thorpej }
1050 1.1.1.3 thorpej
1051 1.1.1.3 thorpej /* otherwise, it is active and healthy
1052 1.1.1.3 thorpej */
1053 1.1.1.3 thorpej ifp->int_act_time = now.tv_sec;
1054 1.1.1.3 thorpej (void)if_ok(ifp, "");
1055 1.1.1.3 thorpej continue;
1056 1.1.1.3 thorpej }
1057 1.1.1.3 thorpej
1058 1.1.1.3 thorpej /* This is a new interface.
1059 1.1.1.3 thorpej * If it is dead, forget it.
1060 1.1.1.3 thorpej */
1061 1.1.1.6 thorpej if (!iff_up(ifs.int_if_flags))
1062 1.1.1.3 thorpej continue;
1063 1.1.1.3 thorpej
1064 1.1.1.5 christos /* If it duplicates an existing interface,
1065 1.1.1.5 christos * complain about it, mark the other one
1066 1.1.1.5 christos * duplicated, and forget this one.
1067 1.1.1.3 thorpej */
1068 1.1.1.5 christos ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1069 1.1.1.5 christos ifs.int_if_flags);
1070 1.1.1.5 christos if (ifp != 0) {
1071 1.1.1.5 christos /* Ignore duplicates of itself, caused by having
1072 1.1.1.5 christos * IP aliases on the same network.
1073 1.1.1.3 thorpej */
1074 1.1.1.5 christos if (!strcmp(ifp->int_name, ifs.int_name))
1075 1.1.1.3 thorpej continue;
1076 1.1.1.3 thorpej
1077 1.1.1.3 thorpej if (!(prev_complaints & COMP_DUP)) {
1078 1.1.1.3 thorpej complaints |= COMP_DUP;
1079 1.1.1.5 christos msglog("%s (%s%s%s) is duplicated by"
1080 1.1.1.5 christos " %s (%s%s%s)",
1081 1.1.1.5 christos ifs.int_name,
1082 1.1.1.5 christos addrname(ifs.int_addr,ifs.int_mask,1),
1083 1.1.1.5 christos ((ifs.int_if_flags & IFF_POINTOPOINT)
1084 1.1.1.5 christos ? "-->" : ""),
1085 1.1.1.5 christos ((ifs.int_if_flags & IFF_POINTOPOINT)
1086 1.1.1.5 christos ? naddr_ntoa(ifs.int_dstaddr) : ""),
1087 1.1.1.5 christos ifp->int_name,
1088 1.1.1.5 christos addrname(ifp->int_addr,ifp->int_mask,1),
1089 1.1.1.5 christos ((ifp->int_if_flags & IFF_POINTOPOINT)
1090 1.1.1.5 christos ? "-->" : ""),
1091 1.1.1.5 christos ((ifp->int_if_flags & IFF_POINTOPOINT)
1092 1.1.1.5 christos ? naddr_ntoa(ifp->int_dstaddr) : ""));
1093 1.1.1.3 thorpej }
1094 1.1.1.3 thorpej ifp->int_state |= IS_DUP;
1095 1.1.1.3 thorpej continue;
1096 1.1.1.5 christos }
1097 1.1.1.5 christos
1098 1.1.1.5 christos if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST))
1099 1.1.1.5 christos && !(ifs.int_state & IS_PASSIVE)) {
1100 1.1.1.5 christos trace_act("%s is neither broadcast, point-to-point,"
1101 1.1.1.5 christos " nor loopback",
1102 1.1.1.5 christos ifs.int_name);
1103 1.1.1.5 christos if (!(ifs.int_state & IFF_MULTICAST))
1104 1.1.1.5 christos ifs.int_state |= IS_NO_RDISC;
1105 1.1.1.5 christos }
1106 1.1.1.3 thorpej
1107 1.1.1.3 thorpej
1108 1.1.1.5 christos /* It is new and ok. Add it to the list of interfaces
1109 1.1.1.3 thorpej */
1110 1.1.1.6 thorpej ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1111 1.1.1.3 thorpej bcopy(&ifs, ifp, sizeof(*ifp));
1112 1.1.1.5 christos get_parms(ifp);
1113 1.1.1.5 christos if_link(ifp);
1114 1.1.1.3 thorpej trace_if("Add", ifp);
1115 1.1.1.3 thorpej
1116 1.1.1.4 christos /* Notice likely bad netmask.
1117 1.1.1.4 christos */
1118 1.1.1.4 christos if (!(prev_complaints & COMP_NETMASK)
1119 1.1.1.5 christos && !(ifp->int_if_flags & IFF_POINTOPOINT)
1120 1.1.1.5 christos && ifp->int_addr != RIP_DEFAULT) {
1121 1.1.1.4 christos for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
1122 1.1.1.4 christos if (ifp1->int_mask == ifp->int_mask)
1123 1.1.1.4 christos continue;
1124 1.1.1.4 christos if (ifp1->int_if_flags & IFF_POINTOPOINT)
1125 1.1.1.4 christos continue;
1126 1.1.1.5 christos if (ifp1->int_dstaddr == RIP_DEFAULT)
1127 1.1.1.5 christos continue;
1128 1.1.1.5 christos if (on_net(ifp->int_dstaddr,
1129 1.1.1.4 christos ifp1->int_net, ifp1->int_mask)
1130 1.1.1.5 christos || on_net(ifp1->int_dstaddr,
1131 1.1.1.4 christos ifp->int_net, ifp->int_mask)) {
1132 1.1.1.4 christos msglog("possible netmask problem"
1133 1.1.1.5 christos " between %s:%s and %s:%s",
1134 1.1.1.4 christos ifp->int_name,
1135 1.1.1.4 christos addrname(htonl(ifp->int_net),
1136 1.1.1.4 christos ifp->int_mask, 1),
1137 1.1.1.4 christos ifp1->int_name,
1138 1.1.1.4 christos addrname(htonl(ifp1->int_net),
1139 1.1.1.4 christos ifp1->int_mask, 1));
1140 1.1.1.4 christos complaints |= COMP_NETMASK;
1141 1.1.1.4 christos }
1142 1.1.1.4 christos }
1143 1.1.1.4 christos }
1144 1.1.1.4 christos
1145 1.1.1.3 thorpej if (!(ifp->int_state & IS_ALIAS)) {
1146 1.1.1.5 christos /* Count the # of directly connected networks.
1147 1.1.1.5 christos */
1148 1.1.1.3 thorpej if (!(ifp->int_if_flags & IFF_LOOPBACK))
1149 1.1.1.3 thorpej tot_interfaces++;
1150 1.1.1.3 thorpej if (!IS_RIP_OFF(ifp->int_state))
1151 1.1.1.3 thorpej rip_interfaces++;
1152 1.1.1.3 thorpej
1153 1.1.1.5 christos /* turn on router discovery and RIP If needed */
1154 1.1.1.5 christos if_ok_rdisc(ifp);
1155 1.1.1.5 christos rip_on(ifp);
1156 1.1.1.5 christos }
1157 1.1.1.3 thorpej }
1158 1.1.1.3 thorpej
1159 1.1.1.5 christos /* If we are multi-homed and have at least two interfaces
1160 1.1.1.3 thorpej * listening to RIP, then output by default.
1161 1.1.1.3 thorpej */
1162 1.1.1.3 thorpej if (!supplier_set && rip_interfaces > 1)
1163 1.1.1.3 thorpej set_supplier();
1164 1.1.1.3 thorpej
1165 1.1.1.3 thorpej /* If we are multi-homed, optionally advertise a route to
1166 1.1.1.3 thorpej * our main address.
1167 1.1.1.3 thorpej */
1168 1.1.1.3 thorpej if (advertise_mhome
1169 1.1.1.3 thorpej || (tot_interfaces > 1
1170 1.1.1.3 thorpej && mhome
1171 1.1.1.3 thorpej && (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1172 1.1.1.3 thorpej && foundloopback)) {
1173 1.1.1.3 thorpej advertise_mhome = 1;
1174 1.1.1.3 thorpej rt = rtget(myaddr, HOST_MASK);
1175 1.1.1.3 thorpej if (rt != 0) {
1176 1.1.1.3 thorpej if (rt->rt_ifp != ifp
1177 1.1.1.3 thorpej || rt->rt_router != loopaddr) {
1178 1.1.1.3 thorpej rtdelete(rt);
1179 1.1.1.3 thorpej rt = 0;
1180 1.1.1.3 thorpej } else {
1181 1.1.1.6 thorpej loop_rts.rts_ifp = ifp;
1182 1.1.1.6 thorpej loop_rts.rts_metric = 0;
1183 1.1.1.6 thorpej loop_rts.rts_time = rt->rt_time;
1184 1.1.1.3 thorpej rtchange(rt, rt->rt_state | RS_MHOME,
1185 1.1.1.6 thorpej &loop_rts, 0);
1186 1.1.1.3 thorpej }
1187 1.1.1.3 thorpej }
1188 1.1.1.6 thorpej if (rt == 0) {
1189 1.1.1.6 thorpej loop_rts.rts_ifp = ifp;
1190 1.1.1.6 thorpej loop_rts.rts_metric = 0;
1191 1.1.1.6 thorpej rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1192 1.1.1.6 thorpej }
1193 1.1.1.3 thorpej }
1194 1.1.1.3 thorpej
1195 1.1.1.3 thorpej for (ifp = ifnet; ifp != 0; ifp = ifp1) {
1196 1.1.1.3 thorpej ifp1 = ifp->int_next; /* because we may delete it */
1197 1.1.1.3 thorpej
1198 1.1.1.3 thorpej /* Forget any interfaces that have disappeared.
1199 1.1.1.3 thorpej */
1200 1.1.1.3 thorpej if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1201 1.1.1.5 christos trace_act("interface %s has disappeared",
1202 1.1.1.3 thorpej ifp->int_name);
1203 1.1.1.3 thorpej ifdel(ifp);
1204 1.1.1.3 thorpej continue;
1205 1.1.1.3 thorpej }
1206 1.1.1.3 thorpej
1207 1.1.1.3 thorpej if ((ifp->int_state & IS_BROKE)
1208 1.1.1.3 thorpej && !(ifp->int_state & IS_PASSIVE))
1209 1.1.1.3 thorpej LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1210 1.1.1.3 thorpej
1211 1.1.1.3 thorpej /* If we ever have a RIPv1 interface, assume we always will.
1212 1.1.1.3 thorpej * It might come back if it ever goes away.
1213 1.1.1.3 thorpej */
1214 1.1.1.4 christos if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1215 1.1.1.4 christos have_ripv1_out = 1;
1216 1.1.1.4 christos if (!(ifp->int_state & IS_NO_RIPV1_IN))
1217 1.1.1.4 christos have_ripv1_in = 1;
1218 1.1.1.3 thorpej }
1219 1.1.1.3 thorpej
1220 1.1.1.3 thorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1221 1.1.1.3 thorpej /* Ensure there is always a network route for interfaces,
1222 1.1.1.3 thorpej * after any dead interfaces have been deleted, which
1223 1.1.1.3 thorpej * might affect routes for point-to-point links.
1224 1.1.1.3 thorpej */
1225 1.1.1.5 christos if (!addrouteforif(ifp))
1226 1.1.1.5 christos continue;
1227 1.1.1.3 thorpej
1228 1.1.1.3 thorpej /* Add routes to the local end of point-to-point interfaces
1229 1.1.1.3 thorpej * using loopback.
1230 1.1.1.3 thorpej */
1231 1.1.1.3 thorpej if ((ifp->int_if_flags & IFF_POINTOPOINT)
1232 1.1.1.3 thorpej && !(ifp->int_state & IS_REMOTE)
1233 1.1.1.3 thorpej && foundloopback) {
1234 1.1.1.3 thorpej /* Delete any routes to the network address through
1235 1.1.1.3 thorpej * foreign routers. Remove even static routes.
1236 1.1.1.3 thorpej */
1237 1.1.1.6 thorpej del_static(ifp->int_addr, HOST_MASK, 0, 0);
1238 1.1.1.3 thorpej rt = rtget(ifp->int_addr, HOST_MASK);
1239 1.1.1.3 thorpej if (rt != 0 && rt->rt_router != loopaddr) {
1240 1.1.1.3 thorpej rtdelete(rt);
1241 1.1.1.3 thorpej rt = 0;
1242 1.1.1.3 thorpej }
1243 1.1.1.3 thorpej if (rt != 0) {
1244 1.1.1.3 thorpej if (!(rt->rt_state & RS_LOCAL)
1245 1.1.1.3 thorpej || rt->rt_metric > ifp->int_metric) {
1246 1.1.1.3 thorpej ifp1 = ifp;
1247 1.1.1.3 thorpej } else {
1248 1.1.1.3 thorpej ifp1 = rt->rt_ifp;
1249 1.1.1.3 thorpej }
1250 1.1.1.6 thorpej loop_rts.rts_ifp = ifp1;
1251 1.1.1.6 thorpej loop_rts.rts_metric = 0;
1252 1.1.1.6 thorpej loop_rts.rts_time = rt->rt_time;
1253 1.1.1.6 thorpej rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1254 1.1.1.6 thorpej | (RS_IF|RS_LOCAL)),
1255 1.1.1.6 thorpej &loop_rts, 0);
1256 1.1.1.3 thorpej } else {
1257 1.1.1.6 thorpej loop_rts.rts_ifp = ifp;
1258 1.1.1.6 thorpej loop_rts.rts_metric = 0;
1259 1.1.1.3 thorpej rtadd(ifp->int_addr, HOST_MASK,
1260 1.1.1.6 thorpej (RS_IF | RS_LOCAL), &loop_rts);
1261 1.1.1.3 thorpej }
1262 1.1.1.3 thorpej }
1263 1.1.1.3 thorpej }
1264 1.1.1.3 thorpej
1265 1.1.1.3 thorpej /* add the authority routes */
1266 1.1.1.3 thorpej for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1267 1.1.1.3 thorpej rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1268 1.1.1.3 thorpej if (rt != 0
1269 1.1.1.3 thorpej && !(rt->rt_state & RS_NO_NET_SYN)
1270 1.1.1.3 thorpej && !(rt->rt_state & RS_NET_INT)) {
1271 1.1.1.3 thorpej rtdelete(rt);
1272 1.1.1.3 thorpej rt = 0;
1273 1.1.1.3 thorpej }
1274 1.1.1.6 thorpej if (rt == 0) {
1275 1.1.1.6 thorpej loop_rts.rts_ifp = 0;
1276 1.1.1.6 thorpej loop_rts.rts_metric = intnetp->intnet_metric-1;
1277 1.1.1.3 thorpej rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1278 1.1.1.6 thorpej RS_NET_SYN | RS_NET_INT, &loop_rts);
1279 1.1.1.6 thorpej }
1280 1.1.1.3 thorpej }
1281 1.1.1.3 thorpej
1282 1.1.1.3 thorpej prev_complaints = complaints;
1283 1.1.1.3 thorpej }
1284 1.1.1.3 thorpej
1285 1.1.1.3 thorpej
1286 1.1.1.3 thorpej static void
1287 1.1.1.3 thorpej check_net_syn(struct interface *ifp)
1288 1.1.1.3 thorpej {
1289 1.1.1.3 thorpej struct rt_entry *rt;
1290 1.1.1.6 thorpej static struct rt_spare new;
1291 1.1.1.3 thorpej
1292 1.1.1.3 thorpej
1293 1.1.1.3 thorpej /* Turn on the need to automatically synthesize a network route
1294 1.1.1.3 thorpej * for this interface only if we are running RIPv1 on some other
1295 1.1.1.3 thorpej * interface that is on a different class-A,B,or C network.
1296 1.1.1.3 thorpej */
1297 1.1.1.3 thorpej if (have_ripv1_out || have_ripv1_in) {
1298 1.1.1.3 thorpej ifp->int_state |= IS_NEED_NET_SYN;
1299 1.1.1.3 thorpej rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1300 1.1.1.3 thorpej if (rt != 0
1301 1.1.1.3 thorpej && 0 == (rt->rt_state & RS_NO_NET_SYN)
1302 1.1.1.3 thorpej && (!(rt->rt_state & RS_NET_SYN)
1303 1.1.1.3 thorpej || rt->rt_metric > ifp->int_metric)) {
1304 1.1.1.3 thorpej rtdelete(rt);
1305 1.1.1.3 thorpej rt = 0;
1306 1.1.1.3 thorpej }
1307 1.1.1.6 thorpej if (rt == 0) {
1308 1.1.1.6 thorpej new.rts_ifp = ifp;
1309 1.1.1.6 thorpej new.rts_gate = ifp->int_addr;
1310 1.1.1.6 thorpej new.rts_router = ifp->int_addr;
1311 1.1.1.6 thorpej new.rts_metric = ifp->int_metric;
1312 1.1.1.3 thorpej rtadd(ifp->int_std_addr, ifp->int_std_mask,
1313 1.1.1.6 thorpej RS_NET_SYN, &new);
1314 1.1.1.6 thorpej }
1315 1.1.1.3 thorpej
1316 1.1.1.3 thorpej } else {
1317 1.1.1.3 thorpej ifp->int_state &= ~IS_NEED_NET_SYN;
1318 1.1.1.3 thorpej
1319 1.1.1.3 thorpej rt = rtget(ifp->int_std_addr,
1320 1.1.1.3 thorpej ifp->int_std_mask);
1321 1.1.1.3 thorpej if (rt != 0
1322 1.1.1.3 thorpej && (rt->rt_state & RS_NET_SYN)
1323 1.1.1.3 thorpej && rt->rt_ifp == ifp)
1324 1.1.1.3 thorpej rtbad_sub(rt);
1325 1.1.1.3 thorpej }
1326 1.1.1.3 thorpej }
1327 1.1.1.3 thorpej
1328 1.1.1.3 thorpej
1329 1.1.1.3 thorpej /* Add route for interface if not currently installed.
1330 1.1.1.3 thorpej * Create route to other end if a point-to-point link,
1331 1.1.1.3 thorpej * otherwise a route to this (sub)network.
1332 1.1.1.3 thorpej */
1333 1.1.1.5 christos int /* 0=bad interface */
1334 1.1.1.3 thorpej addrouteforif(struct interface *ifp)
1335 1.1.1.3 thorpej {
1336 1.1.1.3 thorpej struct rt_entry *rt;
1337 1.1.1.6 thorpej static struct rt_spare new;
1338 1.1.1.6 thorpej naddr dst;
1339 1.1.1.3 thorpej
1340 1.1.1.3 thorpej
1341 1.1.1.3 thorpej /* skip sick interfaces
1342 1.1.1.3 thorpej */
1343 1.1.1.3 thorpej if (ifp->int_state & IS_BROKE)
1344 1.1.1.5 christos return 0;
1345 1.1.1.3 thorpej
1346 1.1.1.3 thorpej /* If the interface on a subnet, then install a RIPv1 route to
1347 1.1.1.3 thorpej * the network as well (unless it is sick).
1348 1.1.1.3 thorpej */
1349 1.1.1.3 thorpej if (ifp->int_state & IS_SUBNET)
1350 1.1.1.3 thorpej check_net_syn(ifp);
1351 1.1.1.3 thorpej
1352 1.1.1.5 christos dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1353 1.1.1.5 christos ? ifp->int_dstaddr
1354 1.1.1.5 christos : htonl(ifp->int_net));
1355 1.1.1.3 thorpej
1356 1.1.1.6 thorpej new.rts_ifp = ifp;
1357 1.1.1.6 thorpej new.rts_router = ifp->int_addr;
1358 1.1.1.6 thorpej new.rts_gate = ifp->int_addr;
1359 1.1.1.6 thorpej new.rts_metric = ifp->int_metric;
1360 1.1.1.6 thorpej new.rts_time = now.tv_sec;
1361 1.1.1.6 thorpej
1362 1.1.1.5 christos /* If we are going to send packets to the gateway,
1363 1.1.1.5 christos * it must be reachable using our physical interfaces
1364 1.1.1.5 christos */
1365 1.1.1.5 christos if ((ifp->int_state & IS_REMOTE)
1366 1.1.1.5 christos && !(ifp->int_state & IS_EXTERNAL)
1367 1.1.1.5 christos && !check_remote(ifp))
1368 1.1.1.5 christos return 0;
1369 1.1.1.3 thorpej
1370 1.1.1.3 thorpej /* We are finished if the correct main interface route exists.
1371 1.1.1.3 thorpej * The right route must be for the right interface, not synthesized
1372 1.1.1.3 thorpej * from a subnet, be a "gateway" or not as appropriate, and so forth.
1373 1.1.1.3 thorpej */
1374 1.1.1.6 thorpej del_static(dst, ifp->int_mask, 0, 0);
1375 1.1.1.3 thorpej rt = rtget(dst, ifp->int_mask);
1376 1.1.1.3 thorpej if (rt != 0) {
1377 1.1.1.3 thorpej if ((rt->rt_ifp != ifp
1378 1.1.1.3 thorpej || rt->rt_router != ifp->int_addr)
1379 1.1.1.3 thorpej && (!(ifp->int_state & IS_DUP)
1380 1.1.1.3 thorpej || rt->rt_ifp == 0
1381 1.1.1.3 thorpej || (rt->rt_ifp->int_state & IS_BROKE))) {
1382 1.1.1.3 thorpej rtdelete(rt);
1383 1.1.1.3 thorpej rt = 0;
1384 1.1.1.3 thorpej } else {
1385 1.1.1.3 thorpej rtchange(rt, ((rt->rt_state | RS_IF)
1386 1.1.1.3 thorpej & ~(RS_NET_SYN | RS_LOCAL)),
1387 1.1.1.6 thorpej &new, 0);
1388 1.1.1.3 thorpej }
1389 1.1.1.3 thorpej }
1390 1.1.1.3 thorpej if (rt == 0) {
1391 1.1.1.3 thorpej if (ifp->int_transitions++ > 0)
1392 1.1.1.5 christos trace_act("re-install interface %s",
1393 1.1.1.3 thorpej ifp->int_name);
1394 1.1.1.3 thorpej
1395 1.1.1.6 thorpej rtadd(dst, ifp->int_mask, RS_IF, &new);
1396 1.1 cgd }
1397 1.1.1.5 christos
1398 1.1.1.5 christos return 1;
1399 1.1 cgd }
1400