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