xref: /titanic_52/usr/src/cmd/cmd-inet/usr.sbin/in.routed/if.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  *
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgment:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/sbin/routed/if.c,v 1.8 2000/08/11 08:24:38 sheldonh Exp $
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 #include "defs.h"
42 #include "pathnames.h"
43 #include <sys/sockio.h>
44 #include <inet/ip.h>
45 #include <kstat.h>
46 #include <stropts.h>
47 #include <fcntl.h>
48 #include <stddef.h>
49 #include <assert.h>
50 
51 /* linked list of all interfaces */
52 struct interface *ifnet;
53 
54 /*
55  * Acceptable sizes (in number of interfaces) for the interface hash
56  * tables.  These must all be prime.  The interface hash tables all
57  * start with a size of hash_table_sizes[0], and increase as needed.
58  */
59 size_t hash_table_sizes[] = { 67, 131, 257, 521, 1031, 2053, 4099, 0 };
60 
61 struct htbl {
62 	void		**htbl_ptrs;
63 	uint_t		(*htbl_hash)(const void *, size_t);
64 	size_t		htbl_link_off;	/* offset of the linkage structure */
65 	size_t		htbl_key_off;	/* offset of the key value (rehash) */
66 	size_t		htbl_size;	/* size of the hash */
67 	uint_t		htbl_size_index;
68 	uint_t		htbl_ifcount;	/* count of entries */
69 	boolean_t	htbl_grow;	/* growth allowed */
70 };
71 
72 /* Get first element -- for iteration */
73 #define	HFIRST(htbl, arg) \
74 	((htbl)->htbl_ptrs[(htbl)->htbl_hash((arg), 0) % (htbl)->htbl_size])
75 
76 /* Add an element to a hash */
77 #define	HADD(htbl, strp) \
78 	hash_link((htbl), (htbl)->htbl_hash((strp), (htbl)->htbl_key_off), \
79 	    (strp))
80 
81 uint_t	tot_interfaces;			/* # of remote and local interfaces */
82 uint_t	rip_interfaces;			/* # of interfaces doing RIP */
83 uint_t	ripout_interfaces;		/* # of interfaces advertising RIP */
84 uint_t	fwd_interfaces;			/* # of interfaces ip_forwarding=1 */
85 static boolean_t foundloopback;		/* valid flag for loopaddr */
86 in_addr_t	loopaddr;		/* our address on loopback */
87 static struct	rt_spare loop_rts;
88 
89 struct timeval ifscan_timer;
90 static struct timeval last_ifscan;
91 #define	IF_RESCAN_DELAY() \
92 	(last_ifscan.tv_sec == now.tv_sec && \
93 	    last_ifscan.tv_usec == now.tv_usec && \
94 	    timercmp(&ifscan_timer, &now, > /* */))
95 
96 boolean_t		have_ripv1_out;	/* have a RIPv1 interface */
97 static boolean_t	have_ripv1_in;
98 
99 static void		if_bad(struct interface *, boolean_t);
100 static boolean_t	addrouteforif(struct interface *);
101 static int	get_if_kstats(struct interface *, struct phyi_data *);
102 static uint_t	ahash(const void *, uint_t);
103 static uint_t	ihash(const void *, uint_t);
104 static uint_t	nhash(const void *, uint_t);
105 static void	htbl_grow(struct htbl *);
106 
107 /*
108  * Table of all interfaces, hashed by interface address.  For remote
109  * interfaces, the gateway address is used.
110  */
111 static struct htbl ahash_tbl = {
112     NULL, ahash, offsetof(struct interface, int_ahash),
113     offsetof(struct interface, int_addr),
114     0, 0, 0, _B_TRUE };
115 /*
116  * Table of broadcast capable interfaces, hashed by interface broadcast
117  * address.
118  */
119 static struct htbl bhash_tbl = {
120     NULL, ahash, offsetof(struct interface, int_bhash),
121     offsetof(struct interface, int_brdaddr),
122     0, 0, 0, _B_TRUE };
123 /*
124  * Table of physical_interface structures (lists of interfaces by ifIndex),
125  * hashed by interface index.
126  */
127 static struct htbl ihash_tbl = {
128     NULL, ihash, offsetof(struct physical_interface, phyi_link),
129     offsetof(struct physical_interface, phyi_index),
130     0, 0, 0, _B_TRUE };
131 /*
132  * Table of all interfaces, hashed by interface name.
133  */
134 static struct htbl nhash_tbl = {
135     NULL, nhash, offsetof(struct interface, int_nhash),
136     offsetof(struct interface, int_name),
137     0, 0, 0, _B_TRUE };
138 
139 static struct physical_interface dummy_phyi;
140 struct interface dummy_ifp;
141 
142 /* Hash based on an IP address. */
143 static uint_t
144 ahash(const void *arg, size_t voffs)
145 {
146 	/* LINTED */
147 	return ((uint_t)*(const in_addr_t *)((const char *)arg + voffs));
148 }
149 
150 static uint_t
151 ihash(const void *arg, size_t voffs)
152 {
153 	/* LINTED */
154 	return ((uint_t)*(const uint32_t *)((const char *)arg + voffs));
155 }
156 
157 static uint_t
158 nhash(const void *arg, size_t voffs)
159 {
160 	const char *cp = (const char *)arg + voffs;
161 	uint_t i;
162 
163 	for (i = 0; *cp != '\0'; cp++) {
164 		i = ((i<<1) & 0x7fffffff) | ((i>>30) & 0x00000003);
165 		i ^= *cp;
166 	}
167 	return (i);
168 }
169 
170 /*
171  * Add an element to the head of the list.
172  */
173 static void
174 link_in(void **head, void *strp, size_t loffs)
175 {
176 	struct hlinkage *hlp;
177 
178 	/* LINTED: alignment known to be good. */
179 	hlp = (struct hlinkage *)((char *)strp + loffs);
180 	hlp->hl_prev = head;
181 	if ((hlp->hl_next = *head) != NULL) {
182 		/* LINTED */
183 		((struct hlinkage *)((char *)*head + loffs))->hl_prev =
184 		    &hlp->hl_next;
185 	}
186 	*head = strp;
187 }
188 
189 /* Remove from a list */
190 static void
191 link_out(void *strp, size_t loffs)
192 {
193 	struct hlinkage *hlp;
194 
195 	/* LINTED: alignment known to be good. */
196 	hlp = (struct hlinkage *)((char *)strp + loffs);
197 	if ((*hlp->hl_prev = hlp->hl_next) != NULL) {
198 		/* LINTED */
199 		((struct hlinkage *)((char *)hlp->hl_next + loffs))->hl_prev =
200 		    hlp->hl_prev;
201 	}
202 }
203 
204 /* Add to a hash */
205 static void
206 hash_link(struct htbl *htbl, uint_t hval, void *strp)
207 {
208 	void **hep;
209 
210 	if (htbl->htbl_grow && htbl->htbl_ifcount >= htbl->htbl_size * 5)
211 		htbl_grow(htbl);
212 
213 	hep = &htbl->htbl_ptrs[hval % htbl->htbl_size];
214 	link_in(hep, strp, htbl->htbl_link_off);
215 	htbl->htbl_ifcount++;
216 }
217 
218 /* Remove from a hash */
219 static void
220 hash_unlink(struct htbl *htbl, void *strp)
221 {
222 	link_out(strp, htbl->htbl_link_off);
223 	htbl->htbl_ifcount--;
224 }
225 
226 static void
227 dummy_ifp_init(void)
228 {
229 	dummy_phyi.phyi_interface = &dummy_ifp;
230 	dummy_ifp.int_phys = &dummy_phyi;
231 	(void) strcpy(dummy_phyi.phyi_name, "wildcard");
232 	(void) strcpy(dummy_ifp.int_name, "wildcard");
233 	dummy_ifp.int_dstaddr = dummy_ifp.int_addr = INADDR_NONE;
234 	dummy_ifp.int_mask = IP_HOST_MASK;
235 	dummy_ifp.int_metric = HOPCNT_INFINITY;
236 	dummy_ifp.int_state = (IS_BROKE|IS_PASSIVE|IS_NO_RIP|IS_NO_RDISC);
237 	dummy_ifp.int_std_mask = std_mask(dummy_ifp.int_addr);
238 	dummy_ifp.int_std_net = dummy_ifp.int_net & dummy_ifp.int_std_mask;
239 	dummy_ifp.int_std_addr = htonl(dummy_ifp.int_std_net);
240 }
241 
242 /* allocate the interface hash tables */
243 void
244 iftbl_alloc(void)
245 {
246 	size_t initial_size = hash_table_sizes[0];
247 
248 	errno = 0;
249 	ahash_tbl.htbl_ptrs = calloc(initial_size, sizeof (void *));
250 	bhash_tbl.htbl_ptrs = calloc(initial_size, sizeof (void *));
251 	ihash_tbl.htbl_ptrs = calloc(initial_size, sizeof (void *));
252 	nhash_tbl.htbl_ptrs = calloc(initial_size, sizeof (void *));
253 
254 	if (errno != 0)
255 		BADERR(_B_FALSE, "Unable to allocate interface tables");
256 
257 	ahash_tbl.htbl_size = initial_size;
258 	bhash_tbl.htbl_size = initial_size;
259 	ihash_tbl.htbl_size = initial_size;
260 	nhash_tbl.htbl_size = initial_size;
261 
262 	dummy_ifp_init();
263 }
264 
265 
266 static void
267 htbl_grow(struct htbl *htbl)
268 {
269 	void *strp;
270 	void **new_ptrs, **saved_old_ptrs, **old_ptrs;
271 	size_t new_size, old_size;
272 	static uint_t failed_count;
273 
274 	if ((new_size = hash_table_sizes[htbl->htbl_size_index + 1]) == 0)
275 		return;
276 
277 	if ((new_ptrs = calloc(new_size, sizeof (void *))) == NULL) {
278 		/*
279 		 * This is not fatal since we already have a
280 		 * functional, yet crowded, interface table.
281 		 */
282 		if (++failed_count % 100 == 1)
283 			msglog("%sunable to grow interface hash table: %s",
284 			    failed_count > 1 ? "Still " : "",
285 			    rip_strerror(errno));
286 		return;
287 	}
288 
289 	failed_count = 0;
290 
291 	saved_old_ptrs = old_ptrs = htbl->htbl_ptrs;
292 	old_size = htbl->htbl_size;
293 	htbl->htbl_ptrs = new_ptrs;
294 	htbl->htbl_size = new_size;
295 	htbl->htbl_size_index++;
296 	htbl->htbl_ifcount = 0;
297 
298 	/*
299 	 * Go through the list of structures, and re-link each into
300 	 * this new table.
301 	 */
302 	htbl->htbl_grow = _B_FALSE;
303 	while (old_size-- > 0) {
304 		strp = *old_ptrs++;
305 		HADD(htbl, strp);
306 	}
307 
308 	htbl->htbl_grow = _B_TRUE;
309 	free(saved_old_ptrs);
310 }
311 
312 /* Link a new interface into the lists and hash tables. */
313 void
314 if_link(struct interface *ifp, uint32_t ifindex)
315 {
316 	struct physical_interface *phyi;
317 
318 	link_in((void **)&ifnet, ifp, offsetof(struct interface, int_link));
319 
320 	HADD(&ahash_tbl, ifp);
321 	HADD(&nhash_tbl, ifp);
322 
323 	if (ifp->int_if_flags & IFF_BROADCAST)
324 		HADD(&bhash_tbl, ifp);
325 
326 	if (ifindex != 0) {
327 		for (phyi = HFIRST(&ihash_tbl, &ifindex);
328 		    phyi != NULL; phyi = phyi->phyi_link.hl_next) {
329 			if (phyi->phyi_index == ifindex)
330 				break;
331 		}
332 		if (phyi == NULL) {
333 			size_t size;
334 
335 			phyi = rtmalloc(sizeof (*phyi), "physical_interface");
336 			(void) memset(phyi, 0, sizeof (*phyi));
337 			phyi->phyi_index = ifindex;
338 			/* LINTED */
339 			assert(IF_NAME_LEN >= IF_NAMESIZE);
340 
341 			size = strcspn(ifp->int_name, ":");
342 			(void) strncpy(phyi->phyi_name, ifp->int_name,
343 			    size);
344 			phyi->phyi_name[size] = '\0';
345 			HADD(&ihash_tbl, phyi);
346 		}
347 		link_in((void **)&phyi->phyi_interface, ifp,
348 		    offsetof(struct interface, int_ilist));
349 		ifp->int_phys = phyi;
350 	}
351 }
352 
353 /* Find the interface with an address */
354 struct interface *
355 ifwithaddr(in_addr_t addr,
356     boolean_t bcast,	/* notice IFF_BROADCAST address */
357     boolean_t remote)	/* include IS_REMOTE interfaces */
358 {
359 	struct interface *ifp, *possible = NULL;
360 	uint32_t remote_state;
361 
362 	remote_state = (!remote ? IS_REMOTE : 0);
363 
364 	for (ifp = HFIRST(&ahash_tbl, &addr); ifp != NULL;
365 	    ifp = ifp->int_ahash.hl_next) {
366 		if (ifp->int_addr != addr)
367 			continue;
368 		if (ifp->int_state & remote_state)
369 			continue;
370 		if (!(ifp->int_state & (IS_BROKE | IS_PASSIVE)))
371 			return (ifp);
372 		possible = ifp;
373 	}
374 
375 	if (possible != NULL || !bcast)
376 		return (possible);
377 
378 	for (ifp = HFIRST(&bhash_tbl, &addr); ifp != NULL;
379 	    ifp = ifp->int_bhash.hl_next) {
380 		if (ifp->int_brdaddr != addr)
381 			continue;
382 		if (ifp->int_state & remote_state)
383 			continue;
384 		if (!(ifp->int_state & (IS_BROKE | IS_PASSIVE)))
385 			return (ifp);
386 		possible = ifp;
387 	}
388 
389 	return (possible);
390 }
391 
392 
393 /* find the interface with the specified name ("hme0" for example) */
394 struct interface *
395 ifwithname(const char *name)
396 {
397 	struct interface *ifp;
398 
399 	for (;;) {
400 		for (ifp = HFIRST(&nhash_tbl, name); ifp != NULL;
401 		    ifp = ifp->int_nhash.hl_next) {
402 			if (strcmp(ifp->int_name, name) == 0)
403 				return (ifp);
404 		}
405 
406 		/*
407 		 * If there is no known interface, maybe there is a
408 		 * new interface.  So just once look for new interfaces.
409 		 */
410 		if (IF_RESCAN_DELAY())
411 			return (NULL);
412 		ifscan();
413 	}
414 }
415 
416 struct interface *
417 findremoteif(in_addr_t addr)
418 {
419 	struct interface *ifp;
420 
421 	for (ifp = HFIRST(&ahash_tbl, &addr); ifp != NULL;
422 	    ifp = ifp->int_ahash.hl_next) {
423 		if ((ifp->int_state & IS_REMOTE) && ifp->int_addr == addr)
424 			return (ifp);
425 	}
426 
427 	return (NULL);
428 }
429 
430 struct interface *
431 findifaddr(in_addr_t addr)
432 {
433 	struct interface *ifp;
434 
435 	for (ifp = HFIRST(&ahash_tbl, &addr); ifp != NULL;
436 	    ifp = ifp->int_ahash.hl_next) {
437 		if (ifp->int_addr == addr)
438 			return (ifp);
439 	}
440 
441 	return (NULL);
442 }
443 
444 /*
445  * Return the first interface with the given index.
446  */
447 struct interface *
448 ifwithindex(ulong_t index,
449     boolean_t rescan_ok)
450 {
451 	struct physical_interface *phyi;
452 
453 	for (;;) {
454 		for (phyi = HFIRST(&ihash_tbl, &index); phyi != NULL;
455 		    phyi = phyi->phyi_link.hl_next) {
456 			if (phyi->phyi_index == index)
457 				return (phyi->phyi_interface);
458 		}
459 
460 		/*
461 		 * If there is no known interface, maybe there is a
462 		 * new interface.  So just once look for new interfaces.
463 		 */
464 		if (!rescan_ok || IF_RESCAN_DELAY())
465 			return (NULL);
466 		rescan_ok = _B_FALSE;
467 		ifscan();
468 	}
469 }
470 
471 
472 /*
473  * Find an interface which should be receiving packets sent from the
474  * given address.  Used as a last ditch effort for figuring out which
475  * interface a packet came in on.  Also used for finding out which
476  * interface points towards the gateway of static routes learned from
477  * the kernel.
478  */
479 struct interface *
480 iflookup(in_addr_t addr)
481 {
482 	struct interface *ifp, *maybe;
483 
484 	maybe = NULL;
485 	for (;;) {
486 		for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
487 			/*
488 			 * Don't return a duplicate interface since
489 			 * it is unusable for output.
490 			 */
491 			if (ifp->int_state & IS_DUP)
492 				continue;
493 
494 			if (ifp->int_if_flags & IFF_POINTOPOINT) {
495 				/* finished with a match */
496 				if (ifp->int_dstaddr == addr)
497 					return (ifp);
498 			} else {
499 				/* finished with an exact match */
500 				if (ifp->int_addr == addr) {
501 					if (IS_PASSIVE_IFP(ifp))
502 						trace_misc("iflookup "
503 						    "returning passive intf %s",
504 						    ifp->int_name);
505 					return (ifp);
506 				}
507 
508 				/* Look for the longest approximate match. */
509 				if (on_net(addr, ifp->int_net, ifp->int_mask) &&
510 				    (maybe == NULL ||
511 					ifp->int_mask > maybe->int_mask))
512 					maybe = ifp;
513 			}
514 		}
515 
516 		/*
517 		 * If there is no known interface, maybe there is a
518 		 * new interface.  So just once look for new interfaces.
519 		 */
520 		if (maybe == NULL && !IF_RESCAN_DELAY())
521 			ifscan();
522 		else
523 			break;
524 	}
525 
526 	if (maybe != NULL && IS_PASSIVE_IFP(maybe)) {
527 		trace_misc("iflookup returning passive intf %s",
528 		    maybe->int_name);
529 	}
530 	return (maybe);
531 }
532 
533 /*
534  * Find the netmask that would be inferred by RIPv1 listeners
535  *	on the given interface for a given network.
536  *	If no interface is specified, look for the best fitting	interface.
537  */
538 in_addr_t
539 ripv1_mask_net(in_addr_t addr,	/* in network byte order */
540     const struct interface *ifp)	/* as seen on this interface */
541 {
542 	const struct r1net *r1p;
543 	in_addr_t mask = 0;
544 
545 	if (addr == 0)			/* default always has 0 mask */
546 		return (mask);
547 
548 	if (ifp != NULL && ifp->int_ripv1_mask != HOST_MASK) {
549 		/*
550 		 * If the target network is that of the associated interface
551 		 * on which it arrived, then use the netmask of the interface.
552 		 */
553 		if (on_net(addr, ifp->int_net, ifp->int_std_mask))
554 			mask = ifp->int_ripv1_mask;
555 
556 	} else {
557 		/*
558 		 * Examine all interfaces, and if it the target seems
559 		 * to have the same network number of an interface, use the
560 		 * netmask of that interface.  If there is more than one
561 		 * such interface, prefer the interface with the longest
562 		 * match.
563 		 */
564 		for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
565 			if (on_net(addr, ifp->int_std_net, ifp->int_std_mask) &&
566 			    ifp->int_ripv1_mask > mask &&
567 			    ifp->int_ripv1_mask != HOST_MASK)
568 				mask = ifp->int_ripv1_mask;
569 		}
570 
571 	}
572 
573 	if (mask == 0) {
574 		/*
575 		 * Check to see if the user has supplied an applicable
576 		 * netmask as a ripv1_mask option in /etc/gateways.
577 		 */
578 		for (r1p = r1nets; r1p != NULL; r1p = r1p->r1net_next) {
579 			/*
580 			 * If the address is is on a matching network
581 			 * and we haven't already found a longer match,
582 			 * use the matching netmask.
583 			 */
584 			if (on_net(addr, r1p->r1net_net, r1p->r1net_match) &&
585 			    r1p->r1net_mask > mask)
586 				mask = r1p->r1net_mask;
587 		}
588 
589 		/* Otherwise, make the classic A/B/C guess. */
590 		if (mask == 0)
591 			mask = std_mask(addr);
592 	}
593 
594 	return (mask);
595 }
596 
597 
598 in_addr_t
599 ripv1_mask_host(in_addr_t addr,		/* in network byte order */
600     const struct interface *ifp)	/* as seen on this interface */
601 {
602 	in_addr_t mask = ripv1_mask_net(addr, ifp);
603 
604 
605 	/*
606 	 * If the computed netmask does not mask all of the set bits
607 	 * in the address, then assume it is a host address
608 	 */
609 	if ((ntohl(addr) & ~mask) != 0)
610 		mask = HOST_MASK;
611 	return (mask);
612 }
613 
614 
615 /* See if a IP address looks reasonable as a destination */
616 boolean_t			/* _B_FALSE=bad _B_TRUE=good */
617 check_dst(in_addr_t addr)
618 {
619 	addr = ntohl(addr);
620 
621 	if (IN_CLASSA(addr)) {
622 		if (addr == 0)
623 			return (_B_TRUE);	/* default */
624 
625 		addr >>= IN_CLASSA_NSHIFT;
626 		return (addr != 0 && addr != IN_LOOPBACKNET);
627 	}
628 
629 	return (IN_CLASSB(addr) || IN_CLASSC(addr));
630 }
631 
632 /*
633  * Find an existing interface which has the given parameters, but don't
634  * return the interface with name "name" if "name" is specified.
635  */
636 struct interface *
637 check_dup(const char *name,	/* Don't return this interface */
638     in_addr_t addr,	/* IP address, so network byte order */
639     in_addr_t dstaddr,	/* ditto */
640     in_addr_t mask,	/* mask, so host byte order */
641     uint64_t if_flags,	/* set IFF_POINTOPOINT to ignore local int_addr */
642     boolean_t allowdups)	/* set true to include duplicates */
643 {
644 	struct interface *best_ifp = NULL;
645 	struct interface *ifp;
646 	in_addr_t dstaddr_h = ntohl(dstaddr);
647 	int best_pref = 0;
648 	int pref;
649 
650 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
651 		/* This interface, not a duplicate. */
652 		if (name != NULL && strcmp(name, ifp->int_name) == 0)
653 			continue;
654 
655 		/*
656 		 * Find an interface which isn't already a duplicate to
657 		 * avoid cyclical duplication.  (i.e. qfe0:1 is a duplicate
658 		 * of qfe0, and qfe0 is a duplicate of qfe0:1.  That would
659 		 * be bad)
660 		 */
661 		if (!allowdups && (ifp->int_state & IS_DUP))
662 			continue;
663 
664 		if (ifp->int_mask != mask)
665 			continue;
666 
667 		if (!IS_IFF_UP(ifp->int_if_flags))
668 			continue;
669 
670 		/*
671 		 * The local address can only be shared with a point-to-point
672 		 * link.
673 		 */
674 		if ((ifp->int_addr == addr &&
675 		    ((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0) ||
676 		    on_net(ifp->int_dstaddr, dstaddr_h, mask)) {
677 			pref = 0;
678 			if (!(ifp->int_state & IS_ALIAS))
679 				pref++;
680 			if (!IS_RIP_OUT_OFF(ifp->int_state))
681 				pref += 2;
682 			if (IS_IFF_ROUTING(ifp->int_if_flags))
683 				pref += 4;
684 			if (pref > best_pref) {
685 				best_pref = pref;
686 				best_ifp = ifp;
687 			}
688 		}
689 	}
690 	return (best_ifp);
691 }
692 
693 
694 /*
695  * See that a remote gateway is reachable.
696  *	Note that the answer can change as real interfaces come and go.
697  */
698 boolean_t			/* _B_FALSE=bad _B_TRUE=good */
699 check_remote(struct interface *ifp)
700 {
701 	struct rt_entry *rt;
702 
703 	/* do not worry about other kinds */
704 	if (!(ifp->int_state & IS_REMOTE))
705 	    return (_B_TRUE);
706 
707 	rt = rtfind(ifp->int_addr);
708 	if (rt != NULL &&
709 	    rt->rt_ifp != NULL &&
710 	    on_net(ifp->int_addr,
711 		rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
712 		return (_B_TRUE);
713 
714 	/*
715 	 * the gateway cannot be reached directly from one of our
716 	 * interfaces
717 	 */
718 	if (!(ifp->int_state & IS_BROKE)) {
719 		msglog("unreachable gateway %s in "PATH_GATEWAYS,
720 		    naddr_ntoa(ifp->int_addr));
721 		if_bad(ifp, _B_FALSE);
722 	}
723 	return (_B_FALSE);
724 }
725 
726 /* Delete an interface. */
727 static void
728 ifdel(struct interface *ifp)
729 {
730 	struct rewire_data wire;
731 	boolean_t resurrected;
732 	struct physical_interface *phyi;
733 
734 	trace_if("Del", ifp);
735 
736 	ifp->int_state |= IS_BROKE;
737 
738 	/* unlink the interface */
739 	link_out(ifp, offsetof(struct interface, int_link));
740 	hash_unlink(&ahash_tbl, ifp);
741 	hash_unlink(&nhash_tbl, ifp);
742 	if (ifp->int_if_flags & IFF_BROADCAST)
743 		hash_unlink(&bhash_tbl, ifp);
744 
745 	/* Remove from list of interfaces with this ifIndex */
746 	if ((phyi = ifp->int_phys) != NULL) {
747 		link_out(ifp, offsetof(struct interface, int_ilist));
748 		if (phyi->phyi_interface == NULL) {
749 			hash_unlink(&ihash_tbl, phyi);
750 			free(phyi);
751 		}
752 	}
753 
754 	/*
755 	 * If this is a lead interface, then check first for
756 	 * duplicates of this interface with an eye towards promoting
757 	 * one of them.
758 	 */
759 	resurrected = _B_FALSE;
760 	if (!(ifp->int_state & IS_DUP) &&
761 	    (wire.if_new = check_dup(ifp->int_name, ifp->int_addr,
762 		ifp->int_dstaddr, ifp->int_mask, ifp->int_if_flags,
763 		_B_TRUE)) != NULL &&
764 	    !IS_IFF_QUIET(wire.if_new->int_if_flags)) {
765 
766 		trace_act("promoting duplicate %s in place of %s",
767 		    wire.if_new->int_name, ifp->int_name);
768 
769 		/* Rewire routes with the replacement interface */
770 		wire.if_old = ifp;
771 		wire.metric_delta = wire.if_new->int_metric - ifp->int_metric;
772 		(void) rn_walktree(rhead, walk_rewire, &wire);
773 		kern_rewire_ifp(wire.if_old, wire.if_new);
774 		if_rewire_rdisc(wire.if_old, wire.if_new);
775 
776 		/* Mark the replacement as being no longer a duplicate */
777 		wire.if_new->int_state &= ~IS_DUP;
778 		tot_interfaces++;
779 		if (!IS_RIP_OFF(wire.if_new->int_state))
780 			rip_interfaces++;
781 		if (!IS_RIP_OUT_OFF(wire.if_new->int_state))
782 			ripout_interfaces++;
783 		if (IS_IFF_ROUTING(wire.if_new->int_if_flags))
784 			fwd_interfaces++;
785 
786 		set_rdisc_mg(wire.if_new, 1);
787 		rip_mcast_on(wire.if_new);
788 
789 		/* We came out ok; no need to clobber routes over this. */
790 		resurrected = _B_TRUE;
791 	}
792 
793 	rip_mcast_off(ifp);
794 	if (rip_sock_interface == ifp)
795 		rip_sock_interface = NULL;
796 
797 	set_rdisc_mg(ifp, 0);
798 
799 	/*
800 	 * Note that duplicates are not counted in the total number of
801 	 * interfaces.
802 	 */
803 	if (!(ifp->int_state & IS_DUP) && !IS_IFF_QUIET(ifp->int_if_flags)) {
804 		tot_interfaces--;
805 		if (!IS_RIP_OFF(ifp->int_state))
806 			rip_interfaces--;
807 		if (!IS_RIP_OUT_OFF(ifp->int_state))
808 			ripout_interfaces--;
809 		if (IS_IFF_ROUTING(ifp->int_if_flags))
810 			fwd_interfaces--;
811 	}
812 
813 	if (!resurrected) {
814 		/*
815 		 * Zap all routes associated with this interface.
816 		 * Assume routes just using gateways beyond this interface
817 		 * will timeout naturally, and have probably already died.
818 		 */
819 		(void) rn_walktree(rhead, walk_bad, ifp);
820 		kern_flush_ifp(ifp);
821 
822 		if_bad_rdisc(ifp);
823 	}
824 
825 	free(ifp);
826 }
827 
828 
829 /* Mark an interface ill. */
830 void
831 if_sick(struct interface *ifp, boolean_t recurse)
832 {
833 	struct interface *ifp1;
834 
835 	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
836 		ifp->int_state |= IS_SICK;
837 		ifp->int_act_time = NEVER;
838 		trace_if("Chg", ifp);
839 
840 		LIM_SEC(ifscan_timer, now.tv_sec+CHECK_BAD_INTERVAL);
841 		if (recurse && ifp->int_phys != NULL) {
842 			/* If an interface is sick, so are its aliases. */
843 			for (ifp1 = ifp->int_phys->phyi_interface;
844 			    ifp1 != NULL; ifp1 = ifp1->int_ilist.hl_next) {
845 				if (ifp1 != ifp)
846 					if_sick(ifp1, _B_FALSE);
847 			}
848 		}
849 	}
850 }
851 
852 
853 /* Mark an interface dead. */
854 static void
855 if_bad(struct interface *ifp, boolean_t recurse)
856 {
857 	struct interface *ifp1;
858 	struct rewire_data wire;
859 
860 	if (ifp->int_state & IS_BROKE)
861 		return;
862 
863 	LIM_SEC(ifscan_timer, now.tv_sec+CHECK_BAD_INTERVAL);
864 
865 	ifp->int_state |= (IS_BROKE | IS_SICK);
866 	ifp->int_act_time = NEVER;
867 	ifp->int_query_time = NEVER;
868 	/* Note: don't reset the stats timestamp here */
869 
870 	trace_if("Chg", ifp);
871 
872 	if (recurse && ifp->int_phys != NULL) {
873 		/* If an interface is bad, so are its aliases. */
874 		for (ifp1 = ifp->int_phys->phyi_interface;
875 		    ifp1 != NULL; ifp1 = ifp1->int_ilist.hl_next) {
876 			if (ifp1 != ifp)
877 				if_bad(ifp1, _B_FALSE);
878 		}
879 	}
880 
881 	/* If we can find a replacement, then pick it up. */
882 	if (!(ifp->int_state & IS_DUP) &&
883 	    (wire.if_new = check_dup(ifp->int_name, ifp->int_addr,
884 		ifp->int_dstaddr, ifp->int_mask, ifp->int_if_flags,
885 		_B_TRUE)) != NULL &&
886 	    !IS_IFF_QUIET(wire.if_new->int_if_flags)) {
887 		trace_act("promoting duplicate %s in place of %s",
888 		    wire.if_new->int_name, ifp->int_name);
889 		wire.if_old = ifp;
890 		wire.metric_delta = wire.if_new->int_metric - ifp->int_metric;
891 		(void) rn_walktree(rhead, walk_rewire, &wire);
892 		if_rewire_rdisc(wire.if_old, wire.if_new);
893 
894 		/* The broken guy becomes the duplicate */
895 		wire.if_new->int_state &= ~IS_DUP;
896 		set_rdisc_mg(ifp, 0);
897 		rip_mcast_off(ifp);
898 		ifp->int_state |= IS_DUP;
899 
900 		/* join the mcast groups for the replacement */
901 		set_rdisc_mg(wire.if_new, 1);
902 		rip_mcast_on(wire.if_new);
903 
904 		if (rip_sock_interface == ifp)
905 			rip_sock_interface = NULL;
906 	} else {
907 		(void) rn_walktree(rhead, walk_bad, ifp);
908 		if_bad_rdisc(ifp);
909 	}
910 }
911 
912 
913 /* Mark an interface alive */
914 void
915 if_ok(struct interface *ifp, const char *type, boolean_t recurse)
916 {
917 	struct interface *ifp1;
918 	boolean_t wasbroken = _B_FALSE;
919 
920 	if (ifp->int_state & IS_BROKE) {
921 		writelog(LOG_WARNING, "%sinterface %s to %s restored",
922 		    type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
923 		ifp->int_state &= ~(IS_BROKE | IS_SICK);
924 		wasbroken = _B_TRUE;
925 	} else if (ifp->int_state & IS_SICK) {
926 		trace_act("%sinterface %s to %s working better",
927 		    type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
928 		ifp->int_state &= ~IS_SICK;
929 	}
930 
931 	if (recurse && ifp->int_phys != NULL && IS_IFF_UP(ifp->int_if_flags)) {
932 		ifp->int_phys->phyi_data.ts = 0;
933 
934 		/* Also mark all aliases of this interface as ok */
935 		for (ifp1 = ifp->int_phys->phyi_interface;
936 		    ifp1 != NULL; ifp1 = ifp1->int_ilist.hl_next) {
937 			if (ifp1 != ifp)
938 				if_ok(ifp1, type, _B_FALSE);
939 		}
940 	}
941 
942 	if (wasbroken) {
943 		if (!(ifp->int_state & IS_DUP))
944 			if_ok_rdisc(ifp);
945 
946 		if (ifp->int_state & IS_REMOTE)
947 			(void) addrouteforif(ifp);
948 	}
949 }
950 
951 boolean_t
952 remote_address_ok(struct interface *ifp, in_addr_t addr)
953 {
954 	if (ifp->int_if_flags & IFF_POINTOPOINT) {
955 		if (addr == ifp->int_dstaddr)
956 			return (_B_TRUE);
957 	} else if (on_net(addr, ifp->int_net, ifp->int_mask)) {
958 		return (_B_TRUE);
959 	}
960 	return (_B_FALSE);
961 }
962 
963 /*
964  * Find the network interfaces which have configured themselves.
965  *	This must be done regularly, if only for extra addresses
966  *	that come and go on interfaces.
967  */
968 void
969 ifscan(void)
970 {
971 	uint_t complaints = 0;
972 	static uint_t prev_complaints = 0;
973 #define	COMP_BADADDR	0x001
974 #define	COMP_NODST	0x002
975 #define	COMP_NOBADDR	0x004
976 #define	COMP_NOMASK	0x008
977 #define	COMP_BAD_METRIC	0x010
978 #define	COMP_NETMASK	0x020
979 #define	COMP_NO_INDEX	0x040
980 #define	COMP_BAD_FLAGS	0x080
981 #define	COMP_NO_KSTATS	0x100
982 #define	COMP_IPFORWARD	0x200
983 
984 	struct interface ifs, *ifp, *ifp1;
985 	struct rt_entry *rt;
986 	size_t needed;
987 	static size_t lastneeded = 0;
988 	char *buf;
989 	static char *lastbuf = NULL;
990 	int32_t in, ierr, out, oerr;
991 	struct intnet *intnetp;
992 	int sock;
993 	struct lifnum lifn;
994 	struct lifconf lifc;
995 	struct lifreq *lifrp, *lifrp_lim;
996 	struct sockaddr_in *sinp;
997 	in_addr_t haddr;
998 	static in_addr_t myaddr = 0;
999 	uint32_t ifindex;
1000 	struct phyi_data newstats;
1001 	struct physical_interface *phyi;
1002 
1003 	last_ifscan = now;
1004 	ifscan_timer.tv_sec = now.tv_sec +
1005 	    (supplier || tot_interfaces != 1 ?
1006 		CHECK_ACT_INTERVAL : CHECK_QUIET_INTERVAL);
1007 
1008 	/* mark all interfaces so we can get rid of those that disappear */
1009 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next)
1010 		ifp->int_state &= ~IS_CHECKED;
1011 
1012 	/* Fetch the size of the current interface list */
1013 	if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
1014 		BADERR(_B_TRUE, "ifscan: socket(SOCK_DGRAM)");
1015 	lifn.lifn_family = AF_INET;	/* Only count IPv4 interfaces */
1016 	/*
1017 	 * Include IFF_NOXMIT interfaces.  Such interfaces are exluded
1018 	 * from protocol operations, but their inclusion in the
1019 	 * internal table enables us to know when packets arrive on
1020 	 * such interfaces.
1021 	 */
1022 	lifn.lifn_flags = LIFC_NOXMIT;
1023 calculate_lifc_len:
1024 	if (ioctl(sock, SIOCGLIFNUM, &lifn) == -1) {
1025 		BADERR(_B_TRUE, "ifscan: ioctl(SIOCGLIFNUM)");
1026 	}
1027 
1028 	/*
1029 	 * When calculating the buffer size needed, add a small number
1030 	 * of interfaces to those we counted.  We do this to capture
1031 	 * the interface status of potential interfaces which may have
1032 	 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
1033 	 * Try to reuse the buffer we already have to avoid heap
1034 	 * thrash.
1035 	 */
1036 	needed = (lifn.lifn_count + 4) * sizeof (struct lifreq);
1037 	if (needed > lastneeded || needed < lastneeded/2) {
1038 		if (lastbuf != NULL)
1039 			free(lastbuf);
1040 		if ((buf = malloc(needed)) == NULL) {
1041 			lastbuf = NULL;
1042 			msglog("ifscan: malloc: %s", rip_strerror(errno));
1043 			return;
1044 		}
1045 	} else {
1046 		buf = lastbuf;
1047 	}
1048 	lastbuf = buf;
1049 	lastneeded = needed;
1050 
1051 	/* Get the list */
1052 	lifc.lifc_family = AF_INET;	/* We only need IPv4 interfaces */
1053 	lifc.lifc_flags = LIFC_NOXMIT;
1054 	lifc.lifc_len = needed;
1055 	lifc.lifc_buf = buf;
1056 	if (ioctl(sock, SIOCGLIFCONF, &lifc) == -1) {
1057 		/*
1058 		 * IP returns EINVAL if the lifc_len we passed in is
1059 		 * too small.  If that's the case, we need to go back
1060 		 * and recalculate it.
1061 		 */
1062 		if (errno == EINVAL)
1063 			goto calculate_lifc_len;
1064 		BADERR(_B_TRUE, "ifscan: ioctl(SIOCGLIFCONF)");
1065 	}
1066 
1067 	/*
1068 	 * If the returned lifc_len is within one lifreq of the
1069 	 * requested ammount, we may have used a buffer which
1070 	 * was too small to hold all of the interfaces.  In that
1071 	 * case go back and recalculate needed.
1072 	 */
1073 	if (lifc.lifc_len >= needed - sizeof (struct lifreq))
1074 		goto calculate_lifc_len;
1075 
1076 	lifrp = lifc.lifc_req;
1077 	lifrp_lim = lifrp + lifc.lifc_len / sizeof (*lifrp);
1078 	for (; lifrp < lifrp_lim; lifrp++) {
1079 
1080 		(void) memset(&ifs, 0, sizeof (ifs));
1081 
1082 		(void) strlcpy(ifs.int_name, lifrp->lifr_name,
1083 		    sizeof (ifs.int_name));
1084 
1085 		/* SIOCGLIFCONF fills in the lifr_addr of each lifreq */
1086 		ifs.int_addr = ((struct sockaddr_in *)&lifrp->lifr_addr)->
1087 		    sin_addr.s_addr;
1088 
1089 		if (ioctl(sock, SIOCGLIFFLAGS, lifrp) == -1) {
1090 			if (!(prev_complaints & COMP_BAD_FLAGS))
1091 				writelog(LOG_NOTICE,
1092 				    "unable to get interface flags for %s: %s",
1093 				    ifs.int_name, rip_strerror(errno));
1094 			complaints |= COMP_BAD_FLAGS;
1095 			ifs.int_if_flags = 0;
1096 		} else {
1097 			ifs.int_if_flags = lifrp->lifr_flags;
1098 		}
1099 
1100 		if (IN_EXPERIMENTAL(ntohl(ifs.int_addr)) ||
1101 		    (ntohl(ifs.int_addr) & IN_CLASSA_NET) == 0) {
1102 			if (IS_IFF_UP(ifs.int_if_flags)) {
1103 				if (!(prev_complaints & COMP_BADADDR))
1104 					writelog(LOG_NOTICE,
1105 					    "%s has a bad address %s",
1106 					    ifs.int_name,
1107 					    naddr_ntoa(ifs.int_addr));
1108 				complaints |= COMP_BADADDR;
1109 			}
1110 			continue;
1111 		}
1112 
1113 		/* Get the interface index. */
1114 		if (ioctl(sock, SIOCGLIFINDEX, lifrp) == -1) {
1115 			ifindex = 0;
1116 			ifs.int_if_flags &= ~IFF_UP;
1117 			if (!(prev_complaints & COMP_NO_INDEX))
1118 				writelog(LOG_NOTICE, "%s has no ifIndex: %s",
1119 				    ifs.int_name, rip_strerror(errno));
1120 			complaints |= COMP_NO_INDEX;
1121 		} else {
1122 			ifindex = lifrp->lifr_index;
1123 		}
1124 
1125 		/*
1126 		 * Get the destination address for point-to-point
1127 		 * interfaces.
1128 		 */
1129 		if (ifs.int_if_flags & IFF_POINTOPOINT) {
1130 			sinp = (struct sockaddr_in *)&lifrp->lifr_dstaddr;
1131 			if (ioctl(sock, SIOCGLIFDSTADDR, lifrp) == -1) {
1132 				if (IS_IFF_UP(ifs.int_if_flags)) {
1133 					if (!(prev_complaints & COMP_NODST))
1134 						writelog(LOG_NOTICE,
1135 						    "%s has no destination "
1136 						    "address : %s",
1137 						    ifs.int_name,
1138 						    rip_strerror(errno));
1139 					complaints |= COMP_NODST;
1140 				}
1141 				continue;
1142 			}
1143 			ifs.int_net = ntohl(sinp->sin_addr.s_addr);
1144 			if (IN_EXPERIMENTAL(ifs.int_net) ||
1145 			    (ifs.int_net != 0 &&
1146 			    (ifs.int_net & IN_CLASSA_NET) == 0)) {
1147 				if (IS_IFF_UP(ifs.int_if_flags)) {
1148 					if (!(prev_complaints & COMP_NODST))
1149 						writelog(LOG_NOTICE,
1150 						    "%s has a bad "
1151 						    "destination address %s",
1152 						    ifs.int_name,
1153 						    naddr_ntoa(ifs.int_net));
1154 					complaints |= COMP_NODST;
1155 				}
1156 				continue;
1157 			}
1158 			ifs.int_dstaddr = sinp->sin_addr.s_addr;
1159 		}
1160 
1161 		/* Get the subnet mask */
1162 		sinp = (struct sockaddr_in *)&lifrp->lifr_addr;
1163 		if (ioctl(sock, SIOCGLIFNETMASK, lifrp) == -1) {
1164 			if (IS_IFF_UP(ifs.int_if_flags)) {
1165 				if (!(prev_complaints & COMP_NOMASK))
1166 					writelog(LOG_NOTICE,
1167 					    "%s has no netmask: %s",
1168 					    ifs.int_name, rip_strerror(errno));
1169 				complaints |= COMP_NOMASK;
1170 			}
1171 			continue;
1172 		}
1173 		if (sinp->sin_addr.s_addr == INADDR_ANY) {
1174 			if (!(ifs.int_if_flags &
1175 			    (IFF_POINTOPOINT|IFF_LOOPBACK))) {
1176 				if (IS_IFF_UP(ifs.int_if_flags)) {
1177 					if (!(prev_complaints & COMP_NOMASK))
1178 						writelog(LOG_NOTICE,
1179 						    "%s has all-zero netmask",
1180 						    ifs.int_name);
1181 					complaints |= COMP_NOMASK;
1182 				}
1183 				continue;
1184 			}
1185 			ifs.int_mask = IP_HOST_MASK;
1186 		} else {
1187 			ifs.int_mask = ntohl(sinp->sin_addr.s_addr);
1188 		}
1189 
1190 		/*
1191 		 * Get the broadcast address on broadcast capable
1192 		 * interfaces.
1193 		 */
1194 		if (ifs.int_if_flags & IFF_BROADCAST) {
1195 			if (ioctl(sock, SIOCGLIFBRDADDR, lifrp) == -1) {
1196 				if (IS_IFF_UP(ifs.int_if_flags)) {
1197 					if (!(prev_complaints & COMP_NOBADDR))
1198 						writelog(LOG_NOTICE,
1199 						    "%s has no broadcast "
1200 						    "address: %s",
1201 						    ifs.int_name,
1202 						    rip_strerror(errno));
1203 					complaints |= COMP_NOBADDR;
1204 				}
1205 				continue;
1206 			}
1207 			haddr = ntohl(sinp->sin_addr.s_addr);
1208 			if (IN_EXPERIMENTAL(haddr) ||
1209 			    (haddr & IN_CLASSA_NET) == 0) {
1210 				if (IS_IFF_UP(ifs.int_if_flags)) {
1211 					if (!(prev_complaints & COMP_NOBADDR))
1212 						writelog(LOG_NOTICE,
1213 						    "%s has a bad broadcast "
1214 						    "address %s",
1215 						    ifs.int_name,
1216 						    naddr_ntoa(haddr));
1217 					complaints |= COMP_NOBADDR;
1218 				}
1219 				continue;
1220 			}
1221 		}
1222 		ifs.int_brdaddr = sinp->sin_addr.s_addr;
1223 
1224 		/* Get interface metric, if possible. */
1225 		if (ioctl(sock, SIOCGLIFMETRIC, lifrp) == -1) {
1226 			if (IS_IFF_UP(ifs.int_if_flags)) {
1227 				if (!(prev_complaints & COMP_BAD_METRIC))
1228 					writelog(LOG_NOTICE,
1229 					    "%s has no metric: %s",
1230 					    ifs.int_name, rip_strerror(errno));
1231 				complaints |= COMP_BAD_METRIC;
1232 			}
1233 		} else {
1234 			ifs.int_metric = lifrp->lifr_metric;
1235 			if (ifs.int_metric > HOPCNT_INFINITY) {
1236 				if (IS_IFF_UP(ifs.int_if_flags)) {
1237 					if (!(prev_complaints &
1238 					    COMP_BAD_METRIC))
1239 						writelog(LOG_NOTICE,
1240 						    "%s has a metric of %d, "
1241 						    "defaulting to %d",
1242 						    ifs.int_name,
1243 						    ifs.int_metric,
1244 						    HOPCNT_INFINITY);
1245 					complaints |= COMP_BAD_METRIC;
1246 				}
1247 				ifs.int_metric = HOPCNT_INFINITY;
1248 			}
1249 		}
1250 
1251 		ifs.int_state |= IS_CHECKED;
1252 		ifs.int_query_time = NEVER;
1253 
1254 		/*
1255 		 * If this is an alias, then mark it appropriately.
1256 		 * Do not output RIP or Router-Discovery packets via
1257 		 * aliases.
1258 		 */
1259 		if (strchr(ifs.int_name, ':') != NULL)
1260 			ifs.int_state |= IS_ALIAS;
1261 
1262 		if (ifs.int_if_flags & IFF_LOOPBACK) {
1263 			ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
1264 			ifs.int_dstaddr = ifs.int_addr;
1265 			ifs.int_mask = HOST_MASK;
1266 			ifs.int_ripv1_mask = HOST_MASK;
1267 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
1268 			ifs.int_net = ntohl(ifs.int_dstaddr);
1269 			if (!foundloopback) {
1270 				foundloopback = _B_TRUE;
1271 				loopaddr = ifs.int_addr;
1272 				loop_rts.rts_gate = loopaddr;
1273 				loop_rts.rts_router = loopaddr;
1274 			}
1275 
1276 		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
1277 			ifs.int_ripv1_mask = ifs.int_mask;
1278 			ifs.int_mask = HOST_MASK;
1279 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
1280 
1281 		} else {
1282 			ifs.int_dstaddr = ifs.int_addr;
1283 			ifs.int_ripv1_mask = ifs.int_mask;
1284 			ifs.int_std_mask = std_mask(ifs.int_addr);
1285 			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
1286 			if (ifs.int_mask != ifs.int_std_mask)
1287 				ifs.int_state |= IS_SUBNET;
1288 		}
1289 		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
1290 		ifs.int_std_addr = htonl(ifs.int_std_net);
1291 
1292 		/*
1293 		 * If this interface duplicates another, mark it
1294 		 * appropriately so that we don't generate duplicate
1295 		 * packets.
1296 		 */
1297 		ifp = check_dup(ifs.int_name, ifs.int_addr, ifs.int_dstaddr,
1298 		    ifs.int_mask, ifs.int_if_flags, _B_FALSE);
1299 		if (ifp != NULL) {
1300 			trace_misc("%s (%s%s%s) is a duplicate of %s (%s%s%s)",
1301 			    ifs.int_name,
1302 			    addrname(ifs.int_addr, ifs.int_mask, 1),
1303 			    ((ifs.int_if_flags & IFF_POINTOPOINT) ?
1304 				"-->" : ""),
1305 			    ((ifs.int_if_flags & IFF_POINTOPOINT) ?
1306 				naddr_ntoa(ifs.int_dstaddr) : ""),
1307 			    ifp->int_name,
1308 			    addrname(ifp->int_addr, ifp->int_mask, 1),
1309 			    ((ifp->int_if_flags & IFF_POINTOPOINT) ?
1310 				"-->" : ""),
1311 			    ((ifp->int_if_flags & IFF_POINTOPOINT) ?
1312 				naddr_ntoa(ifp->int_dstaddr) : ""));
1313 			ifs.int_state |= IS_DUP;
1314 		} else {
1315 			ifs.int_state &= ~IS_DUP;
1316 		}
1317 
1318 		/*
1319 		 * See if this is a familiar interface.
1320 		 * If so, stop worrying about it if it is the same.
1321 		 * Start it over if it now is to somewhere else, as happens
1322 		 * frequently with PPP and SLIP, or if its forwarding
1323 		 * status has changed.
1324 		 */
1325 		ifp = ifwithname(ifs.int_name);
1326 		if (ifp != NULL) {
1327 			ifp->int_state |= IS_CHECKED;
1328 			ifp->int_state = (ifp->int_state & ~IS_DUP) |
1329 			    (ifs.int_state & IS_DUP);
1330 
1331 			if ((ifp->int_phys == NULL && ifindex != 0) ||
1332 			    (ifp->int_phys != NULL &&
1333 				ifp->int_phys->phyi_index != ifindex) ||
1334 			    0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
1335 				& (IFF_BROADCAST | IFF_LOOPBACK |
1336 				IFF_POINTOPOINT | IFF_MULTICAST |
1337 				IFF_ROUTER | IFF_NORTEXCH | IFF_NOXMIT)) ||
1338 			    ifp->int_addr != ifs.int_addr ||
1339 			    ifp->int_brdaddr != ifs.int_brdaddr ||
1340 			    ifp->int_dstaddr != ifs.int_dstaddr ||
1341 			    ifp->int_mask != ifs.int_mask ||
1342 			    ifp->int_metric != ifs.int_metric) {
1343 				/*
1344 				 * Forget old information about
1345 				 * a changed interface.
1346 				 */
1347 				trace_act("interface %s has changed",
1348 				    ifp->int_name);
1349 				ifdel(ifp);
1350 				ifp = NULL;
1351 			}
1352 		}
1353 
1354 		if (ifp != NULL) {
1355 			/* note interfaces that have been turned off */
1356 			if (!IS_IFF_UP(ifs.int_if_flags)) {
1357 				if (IS_IFF_UP(ifp->int_if_flags)) {
1358 					writelog(LOG_WARNING,
1359 					    "interface %s to %s turned off",
1360 					    ifp->int_name,
1361 					    naddr_ntoa(ifp->int_dstaddr));
1362 					if_bad(ifp, _B_FALSE);
1363 					ifp->int_if_flags &= ~IFF_UP;
1364 				} else if (ifp->int_phys != NULL &&
1365 				    now.tv_sec > (ifp->int_phys->phyi_data.ts +
1366 				    CHECK_BAD_INTERVAL)) {
1367 					trace_act("interface %s has been off"
1368 					    " %ld seconds; forget it",
1369 					    ifp->int_name,
1370 					    now.tv_sec -
1371 					    ifp->int_phys->phyi_data.ts);
1372 					ifdel(ifp);
1373 				}
1374 				continue;
1375 			}
1376 			/* or that were off and are now ok */
1377 			if (!IS_IFF_UP(ifp->int_if_flags)) {
1378 				ifp->int_if_flags |= IFF_UP;
1379 				if_ok(ifp, "", _B_FALSE);
1380 			}
1381 
1382 			/*
1383 			 * If it has been long enough,
1384 			 * see if the interface is broken.
1385 			 */
1386 			if ((phyi = ifp->int_phys) == NULL ||
1387 			    now.tv_sec < phyi->phyi_data.ts +
1388 			    CHECK_BAD_INTERVAL)
1389 				continue;
1390 
1391 			(void) memset(&newstats, 0, sizeof (newstats));
1392 			if (get_if_kstats(ifp, &newstats) == -1) {
1393 				if (!(prev_complaints & COMP_NO_KSTATS))
1394 					writelog(LOG_WARNING,
1395 					    "unable to obtain kstats for %s",
1396 					    phyi->phyi_name);
1397 				complaints |= COMP_NO_KSTATS;
1398 			}
1399 
1400 			/*
1401 			 * If the interface just awoke, restart the counters.
1402 			 */
1403 			if (phyi->phyi_data.ts == 0) {
1404 				phyi->phyi_data = newstats;
1405 				continue;
1406 			}
1407 
1408 			in = newstats.ipackets - phyi->phyi_data.ipackets;
1409 			ierr = newstats.ierrors - phyi->phyi_data.ierrors;
1410 			out = newstats.opackets - phyi->phyi_data.opackets;
1411 			oerr = newstats.oerrors - phyi->phyi_data.oerrors;
1412 			phyi->phyi_data = newstats;
1413 
1414 			/*
1415 			 * Withhold judgment when the short error counters
1416 			 * wrap, the interface is reset, or if there are
1417 			 * no kstats.
1418 			 */
1419 			if (ierr < 0 || in < 0 || oerr < 0 || out < 0 ||
1420 			    newstats.ts == 0) {
1421 				LIM_SEC(ifscan_timer,
1422 				    now.tv_sec + CHECK_BAD_INTERVAL);
1423 				continue;
1424 			}
1425 
1426 			/* Withhold judgement when there is no traffic */
1427 			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1428 				continue;
1429 
1430 			/*
1431 			 * It is bad if at least 25% of input or output on
1432 			 * an interface results in errors.  Require
1433 			 * presistent problems before marking it dead.
1434 			 */
1435 			if ((ierr > 0 && ierr >= in/4) ||
1436 			    (oerr > 0 && oerr >= out/4)) {
1437 				if (!(ifp->int_state & IS_SICK)) {
1438 					trace_act("interface %s to %s"
1439 					    " sick: in=%d ierr=%d"
1440 					    " out=%d oerr=%d",
1441 					    ifp->int_name,
1442 					    naddr_ntoa(ifp->int_dstaddr),
1443 					    in, ierr, out, oerr);
1444 					if_sick(ifp, _B_TRUE);
1445 					continue;
1446 				}
1447 				if (!(ifp->int_state & IS_BROKE)) {
1448 					writelog(LOG_WARNING,
1449 					    "interface %s to %s broken:"
1450 					    " in=%d ierr=%d out=%d oerr=%d",
1451 					    ifp->int_name,
1452 					    naddr_ntoa(ifp->int_dstaddr),
1453 					    in, ierr, out, oerr);
1454 					if_bad(ifp, _B_TRUE);
1455 				}
1456 				continue;
1457 			}
1458 
1459 			/* otherwise, it is active and healthy */
1460 			ifp->int_act_time = now.tv_sec;
1461 			if_ok(ifp, "", _B_TRUE);
1462 			continue;
1463 		}
1464 
1465 		/*
1466 		 * This is a new interface.
1467 		 * If it is dead, forget it.
1468 		 */
1469 		if (!IS_IFF_UP(ifs.int_if_flags))
1470 			continue;
1471 
1472 		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT |
1473 		    IFF_BROADCAST | IFF_LOOPBACK)) &&
1474 		    !(ifs.int_state & IS_PASSIVE)) {
1475 			if (!(prev_complaints & COMP_BAD_FLAGS))
1476 				trace_act("%s is neither broadcast, "
1477 				    "point-to-point, nor loopback",
1478 				    ifs.int_name);
1479 			complaints |= COMP_BAD_FLAGS;
1480 			if (!(ifs.int_if_flags & IFF_MULTICAST))
1481 				ifs.int_state |= IS_NO_RDISC;
1482 		}
1483 
1484 
1485 		/*
1486 		 * It is new and ok.   Add it to the list of interfaces
1487 		 */
1488 		ifp = rtmalloc(sizeof (*ifp), "ifscan ifp");
1489 		(void) memcpy(ifp, &ifs, sizeof (*ifp));
1490 		get_parms(ifp);
1491 		if_link(ifp, ifindex);
1492 		trace_if("Add", ifp);
1493 
1494 		if (ifp->int_phys != NULL &&
1495 		    get_if_kstats(ifp, &ifp->int_phys->phyi_data) == -1) {
1496 			if (!(prev_complaints & COMP_NO_KSTATS))
1497 				writelog(LOG_NOTICE,
1498 				    "unable to obtain kstats for %s",
1499 				    ifp->int_phys->phyi_name);
1500 			complaints |= COMP_NO_KSTATS;
1501 		}
1502 
1503 		/* Detect interfaces that have conflicting netmasks. */
1504 		if (!(ifp->int_if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK))) {
1505 			for (ifp1 = ifnet; ifp1 != NULL;
1506 			    ifp1 = ifp1->int_next) {
1507 				if (ifp1->int_mask == ifp->int_mask)
1508 					continue;
1509 
1510 				/*
1511 				 * we don't care about point-to-point
1512 				 * or loopback aliases
1513 				 */
1514 				if (ifp1->int_if_flags &
1515 				    (IFF_POINTOPOINT|IFF_LOOPBACK)) {
1516 					continue;
1517 				}
1518 
1519 				/* ignore aliases on the same network */
1520 				if (ifp->int_phys == ifp1->int_phys)
1521 					continue;
1522 
1523 				if (on_net(ifp->int_addr,
1524 				    ifp1->int_net, ifp1->int_mask) ||
1525 				    on_net(ifp1->int_addr,
1526 					ifp->int_net, ifp->int_mask)) {
1527 					writelog(LOG_INFO,
1528 					    "possible netmask problem"
1529 					    " between %s:%s and %s:%s",
1530 					    ifp->int_name,
1531 					    addrname(htonl(ifp->int_net),
1532 						ifp->int_mask, 1),
1533 					    ifp1->int_name,
1534 					    addrname(htonl(ifp1->int_net),
1535 						ifp1->int_mask, 1));
1536 					complaints |= COMP_NETMASK;
1537 				}
1538 			}
1539 		}
1540 
1541 		if (!(ifp->int_state & IS_DUP) &&
1542 		    !IS_IFF_QUIET(ifp->int_if_flags)) {
1543 			/* Count the # of directly connected networks. */
1544 			tot_interfaces++;
1545 			if (!IS_RIP_OFF(ifp->int_state))
1546 				rip_interfaces++;
1547 			if (!IS_RIP_OUT_OFF(ifp->int_state))
1548 				ripout_interfaces++;
1549 			if (IS_IFF_ROUTING(ifp->int_if_flags))
1550 				fwd_interfaces++;
1551 
1552 			if_ok_rdisc(ifp);
1553 			rip_on(ifp);
1554 		}
1555 	}
1556 
1557 	(void) close(sock);
1558 
1559 	/*
1560 	 * If we are multi-homed and have at least two interfaces that
1561 	 * are able to forward, then output RIP by default.
1562 	 */
1563 	if (!supplier_set)
1564 		set_supplier();
1565 
1566 	/*
1567 	 * If we are multi-homed, optionally advertise a route to
1568 	 * our main address.
1569 	 */
1570 	if (advertise_mhome || (tot_interfaces > 1 && mhome)) {
1571 		/* lookup myaddr if we haven't done so already */
1572 		if (myaddr == 0) {
1573 			char myname[MAXHOSTNAMELEN+1];
1574 
1575 			/*
1576 			 * If we are unable to resolve our hostname, don't
1577 			 * bother trying again.
1578 			 */
1579 			if (gethostname(myname, MAXHOSTNAMELEN) == -1) {
1580 				msglog("gethostname: %s", rip_strerror(errno));
1581 				advertise_mhome = _B_FALSE;
1582 				mhome = _B_FALSE;
1583 			} else if (gethost(myname, &myaddr) == 0) {
1584 				writelog(LOG_WARNING,
1585 				    "unable to resolve local hostname %s",
1586 				    myname);
1587 				advertise_mhome = _B_FALSE;
1588 				mhome = _B_FALSE;
1589 			}
1590 		}
1591 		if (myaddr != 0 &&
1592 		    (ifp = ifwithaddr(myaddr, _B_FALSE, _B_FALSE)) != NULL &&
1593 		    foundloopback) {
1594 			advertise_mhome = _B_TRUE;
1595 			rt = rtget(myaddr, HOST_MASK);
1596 			if (rt != NULL) {
1597 				if (rt->rt_ifp != ifp ||
1598 				    rt->rt_router != loopaddr) {
1599 					rtdelete(rt);
1600 					rt = NULL;
1601 				} else {
1602 					loop_rts.rts_ifp = ifp;
1603 					loop_rts.rts_metric = 0;
1604 					loop_rts.rts_time = rt->rt_time;
1605 					loop_rts.rts_origin = RO_LOOPBCK;
1606 					rtchange(rt, rt->rt_state | RS_MHOME,
1607 					    &loop_rts, NULL);
1608 				}
1609 			}
1610 			if (rt == NULL) {
1611 				loop_rts.rts_ifp = ifp;
1612 				loop_rts.rts_metric = 0;
1613 				loop_rts.rts_origin = RO_LOOPBCK;
1614 				rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1615 			}
1616 		}
1617 	}
1618 
1619 	for (ifp = ifnet; ifp != NULL; ifp = ifp1) {
1620 		ifp1 = ifp->int_next;	/* because we may delete it */
1621 
1622 		/* Forget any interfaces that have disappeared. */
1623 		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1624 			trace_act("interface %s has disappeared",
1625 			    ifp->int_name);
1626 			ifdel(ifp);
1627 			continue;
1628 		}
1629 
1630 		if ((ifp->int_state & IS_BROKE) &&
1631 		    !(ifp->int_state & IS_PASSIVE))
1632 			LIM_SEC(ifscan_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1633 
1634 		/*
1635 		 * If we ever have a RIPv1 interface, assume we always will.
1636 		 * It might come back if it ever goes away.
1637 		 */
1638 		if (!(ifp->int_state & (IS_NO_RIPV1_OUT | IS_DUP)) &&
1639 		    should_supply(ifp))
1640 			have_ripv1_out = _B_TRUE;
1641 		if (!(ifp->int_state & IS_NO_RIPV1_IN))
1642 			have_ripv1_in = _B_TRUE;
1643 	}
1644 
1645 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
1646 		/*
1647 		 * Ensure there is always a network route for interfaces,
1648 		 * after any dead interfaces have been deleted, which
1649 		 * might affect routes for point-to-point links.
1650 		 */
1651 		if (addrouteforif(ifp) == 0)
1652 			continue;
1653 
1654 		/*
1655 		 * Add routes to the local end of point-to-point interfaces
1656 		 * using loopback.
1657 		 */
1658 		if ((ifp->int_if_flags & IFF_POINTOPOINT) &&
1659 		    !(ifp->int_state & IS_REMOTE) && foundloopback) {
1660 			/*
1661 			 * Delete any routes to the network address through
1662 			 * foreign routers. Remove even static routes.
1663 			 */
1664 			del_static(ifp->int_addr, HOST_MASK, 0, ifp, 0);
1665 			rt = rtget(ifp->int_addr, HOST_MASK);
1666 			if (rt != NULL && rt->rt_router != loopaddr) {
1667 				rtdelete(rt);
1668 				rt = NULL;
1669 			}
1670 			if (rt != NULL) {
1671 				if (!(rt->rt_state & RS_LOCAL) ||
1672 				    rt->rt_metric > ifp->int_metric) {
1673 					ifp1 = ifp;
1674 				} else {
1675 					ifp1 = rt->rt_ifp;
1676 				}
1677 				loop_rts.rts_ifp = ifp1;
1678 				loop_rts.rts_metric = 0;
1679 				loop_rts.rts_time = rt->rt_time;
1680 				loop_rts.rts_origin = RO_LOOPBCK;
1681 				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN) |
1682 				    (RS_IF|RS_LOCAL)), &loop_rts, 0);
1683 			} else {
1684 				loop_rts.rts_ifp = ifp;
1685 				loop_rts.rts_metric = 0;
1686 				loop_rts.rts_origin = RO_LOOPBCK;
1687 				rtadd(ifp->int_addr, HOST_MASK,
1688 				    (RS_IF | RS_LOCAL), &loop_rts);
1689 			}
1690 		}
1691 	}
1692 
1693 	/* add the authority routes */
1694 	for (intnetp = intnets; intnetp != NULL;
1695 	    intnetp = intnetp->intnet_next) {
1696 		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1697 		if (rt != NULL &&
1698 		    !(rt->rt_state & RS_NO_NET_SYN) &&
1699 		    !(rt->rt_state & RS_NET_INT)) {
1700 			rtdelete(rt);
1701 			rt = NULL;
1702 		}
1703 		if (rt == NULL) {
1704 			loop_rts.rts_ifp = NULL;
1705 			loop_rts.rts_metric = intnetp->intnet_metric-1;
1706 			loop_rts.rts_origin = RO_LOOPBCK;
1707 			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1708 			    RS_NET_SYN | RS_NET_INT, &loop_rts);
1709 		}
1710 	}
1711 
1712 	prev_complaints = complaints;
1713 }
1714 
1715 
1716 static void
1717 check_net_syn(struct interface *ifp)
1718 {
1719 	struct rt_entry *rt;
1720 	struct rt_spare new;
1721 
1722 	/*
1723 	 * Turn on the need to automatically synthesize a network route
1724 	 * for this interface only if we are running RIPv1 on some other
1725 	 * interface that is on a different class-A,B,or C network.
1726 	 */
1727 	if (have_ripv1_out || have_ripv1_in) {
1728 		ifp->int_state |= IS_NEED_NET_SYN;
1729 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1730 		if (rt != NULL &&
1731 		    0 == (rt->rt_state & RS_NO_NET_SYN) &&
1732 		    (!(rt->rt_state & RS_NET_SYN) ||
1733 			rt->rt_metric > ifp->int_metric)) {
1734 			rtdelete(rt);
1735 			rt = NULL;
1736 		}
1737 		if (rt == NULL) {
1738 			(void) memset(&new, 0, sizeof (new));
1739 			new.rts_ifp = ifp;
1740 			new.rts_gate = ifp->int_addr;
1741 			new.rts_router = ifp->int_addr;
1742 			new.rts_metric = ifp->int_metric;
1743 			new.rts_origin = RO_NET_SYN;
1744 			rtadd(ifp->int_std_addr, ifp->int_std_mask,
1745 			    RS_NET_SYN, &new);
1746 		}
1747 
1748 	} else {
1749 		ifp->int_state &= ~IS_NEED_NET_SYN;
1750 
1751 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1752 		if (rt != NULL &&
1753 		    (rt->rt_state & RS_NET_SYN) &&
1754 		    rt->rt_ifp == ifp)
1755 			rtbad_sub(rt, NULL);
1756 	}
1757 }
1758 
1759 
1760 /*
1761  * Add route for interface if not currently installed.
1762  * Create route to other end if a point-to-point link,
1763  * otherwise a route to this (sub)network.
1764  */
1765 static boolean_t			/* _B_FALSE=bad interface */
1766 addrouteforif(struct interface *ifp)
1767 {
1768 	struct rt_entry *rt;
1769 	struct rt_spare new;
1770 	in_addr_t dst;
1771 	uint16_t rt_newstate = RS_IF;
1772 
1773 
1774 	/* skip sick interfaces */
1775 	if (ifp->int_state & IS_BROKE)
1776 		return (_B_FALSE);
1777 
1778 	/*
1779 	 * don't install routes for duplicate interfaces, or
1780 	 * unnumbered point-to-point interfaces.
1781 	 */
1782 	if ((ifp->int_state & IS_DUP) ||
1783 	    ((ifp->int_if_flags & IFF_POINTOPOINT) && ifp->int_dstaddr == 0))
1784 		return (_B_TRUE);
1785 
1786 	/*
1787 	 * If the interface on a subnet, then install a RIPv1 route to
1788 	 * the network as well (unless it is sick).
1789 	 */
1790 	if (ifp->int_state & IS_SUBNET)
1791 		check_net_syn(ifp);
1792 
1793 	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) ?
1794 	    ifp->int_dstaddr : htonl(ifp->int_net));
1795 
1796 	(void) memset(&new, 0, sizeof (new));
1797 	new.rts_ifp = ifp;
1798 	new.rts_router = ifp->int_addr;
1799 	new.rts_gate = ifp->int_addr;
1800 	new.rts_metric = ifp->int_metric;
1801 	new.rts_time = now.tv_sec;
1802 	if (ifp->int_if_flags & IFF_POINTOPOINT)
1803 		new.rts_origin = RO_PTOPT;
1804 	else if (ifp->int_if_flags & IFF_LOOPBACK)
1805 		new.rts_origin = RO_LOOPBCK;
1806 	else
1807 		new.rts_origin = RO_IF;
1808 
1809 	/*
1810 	 * If we are going to send packets to the gateway,
1811 	 * it must be reachable using our physical interfaces
1812 	 */
1813 	if ((ifp->int_state & IS_REMOTE) &&
1814 	    !(ifp->int_state & IS_EXTERNAL) &&
1815 	    !check_remote(ifp))
1816 		return (_B_FALSE);
1817 
1818 	/*
1819 	 * We are finished if the correct main interface route exists.
1820 	 * The right route must be for the right interface, not synthesized
1821 	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1822 	 */
1823 	del_static(dst, ifp->int_mask, 0, ifp, 0);
1824 	rt = rtget(dst, ifp->int_mask);
1825 	if (!IS_IFF_ROUTING(ifp->int_if_flags))
1826 		rt_newstate |= RS_NOPROPAGATE;
1827 	if (rt != NULL) {
1828 		if ((rt->rt_ifp != ifp || rt->rt_router != ifp->int_addr) &&
1829 		    (rt->rt_ifp == NULL ||
1830 		    (rt->rt_ifp->int_state & IS_BROKE))) {
1831 			rtdelete(rt);
1832 			rt = NULL;
1833 		} else {
1834 			rtchange(rt, ((rt->rt_state | rt_newstate) &
1835 			    ~(RS_NET_SYN | RS_LOCAL)), &new, 0);
1836 		}
1837 	}
1838 	if (rt == NULL) {
1839 		if (ifp->int_transitions++ > 0)
1840 			trace_act("re-installing interface %s;"
1841 			    " went up %d times",
1842 			    ifp->int_name, ifp->int_transitions);
1843 
1844 		rtadd(dst, ifp->int_mask, rt_newstate, &new);
1845 	}
1846 
1847 	return (_B_TRUE);
1848 }
1849 
1850 /*
1851  * Obtains the named kstat, and places its value in *value.  It
1852  * returns 0 for success, -1 for failure.
1853  */
1854 static int
1855 kstat_named_value(kstat_t *ksp, char *name, uint32_t *value)
1856 {
1857 	kstat_named_t *knp;
1858 
1859 	if (ksp == NULL)
1860 		return (-1);
1861 
1862 	if ((knp = kstat_data_lookup(ksp, name)) == NULL) {
1863 		return (-1);
1864 	} else if (knp->data_type != KSTAT_DATA_UINT32) {
1865 		return (-1);
1866 	} else {
1867 		*value = knp->value.ui32;
1868 		return (0);
1869 	}
1870 }
1871 
1872 static int
1873 get_if_kstats(struct interface *ifp, struct phyi_data *newdata)
1874 {
1875 	struct physical_interface *phyi = ifp->int_phys;
1876 	kstat_ctl_t *kc;
1877 	kstat_t *ksp;
1878 
1879 	/* We did this recently; don't do it again. */
1880 	if (phyi->phyi_data.ts == now.tv_sec) {
1881 		if (newdata != &phyi->phyi_data)
1882 			*newdata = phyi->phyi_data;
1883 		return (0);
1884 	}
1885 
1886 	if ((kc = kstat_open()) == NULL)
1887 		return (-1);
1888 
1889 	if ((ksp = kstat_lookup(kc, NULL, -1, phyi->phyi_name)) == NULL) {
1890 		(void) kstat_close(kc);
1891 		return (-1);
1892 	}
1893 
1894 	if (kstat_read(kc, ksp, NULL) == -1) {
1895 		(void) kstat_close(kc);
1896 		return (-1);
1897 	}
1898 
1899 	if ((kstat_named_value(ksp, "ipackets", &newdata->ipackets) == -1) ||
1900 	    (kstat_named_value(ksp, "opackets",	&newdata->opackets) == -1)) {
1901 		newdata->ts = 0;
1902 		(void) kstat_close(kc);
1903 		return (-1);
1904 	}
1905 
1906 	/* The loopback interface does not keep track of errors */
1907 	if (!(ifp->int_if_flags & IFF_LOOPBACK)) {
1908 		if ((kstat_named_value(ksp, "ierrors",
1909 		    &newdata->ierrors) == -1) ||
1910 		    (kstat_named_value(ksp, "oerrors",
1911 		    &newdata->oerrors) == -1)) {
1912 			newdata->ts = 0;
1913 			(void) kstat_close(kc);
1914 			return (-1);
1915 		}
1916 	}
1917 
1918 	newdata->ts = now.tv_sec;
1919 	(void) kstat_close(kc);
1920 	return (0);
1921 }
1922 
1923 /*
1924  * Returns true if we should supply routes to other systems.  If the
1925  * user has forced us to be a supplier (by the command line) or if we
1926  * have more than one forwarding interface and this is one of the
1927  * forwarding interfaces, then behave as a RIP supplier (supply rdisc
1928  * advertisements and RIP responses).
1929  */
1930 boolean_t
1931 should_supply(struct interface *ifp)
1932 {
1933 	if (ifp != NULL && !IS_IFF_ROUTING(ifp->int_if_flags))
1934 		return (_B_FALSE);
1935 	return ((supplier_set && supplier) ||
1936 	    (!supplier_set && fwd_interfaces > 1));
1937 }
1938