1 /*
2  * Copyright 2007 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 		/*
518 		 * If there is no known interface, maybe there is a
519 		 * new interface.  So just once look for new interfaces.
520 		 */
521 		if (maybe == NULL && !IF_RESCAN_DELAY())
522 			ifscan();
523 		else
524 			break;
525 	}
526 
527 	if (maybe != NULL && IS_PASSIVE_IFP(maybe)) {
528 		trace_misc("iflookup returning passive intf %s",
529 		    maybe->int_name);
530 	}
531 	return (maybe);
532 }
533 
534 /*
535  * Find the netmask that would be inferred by RIPv1 listeners
536  *	on the given interface for a given network.
537  *	If no interface is specified, look for the best fitting	interface.
538  */
539 in_addr_t
540 ripv1_mask_net(in_addr_t addr,	/* in network byte order */
541     const struct interface *ifp)	/* as seen on this interface */
542 {
543 	const struct r1net *r1p;
544 	in_addr_t mask = 0;
545 
546 	if (addr == 0)			/* default always has 0 mask */
547 		return (mask);
548 
549 	if (ifp != NULL && ifp->int_ripv1_mask != HOST_MASK) {
550 		/*
551 		 * If the target network is that of the associated interface
552 		 * on which it arrived, then use the netmask of the interface.
553 		 */
554 		if (on_net(addr, ifp->int_net, ifp->int_std_mask))
555 			mask = ifp->int_ripv1_mask;
556 
557 	} else {
558 		/*
559 		 * Examine all interfaces, and if it the target seems
560 		 * to have the same network number of an interface, use the
561 		 * netmask of that interface.  If there is more than one
562 		 * such interface, prefer the interface with the longest
563 		 * match.
564 		 */
565 		for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
566 			if (on_net(addr, ifp->int_std_net, ifp->int_std_mask) &&
567 			    ifp->int_ripv1_mask > mask &&
568 			    ifp->int_ripv1_mask != HOST_MASK)
569 				mask = ifp->int_ripv1_mask;
570 		}
571 
572 	}
573 
574 	if (mask == 0) {
575 		/*
576 		 * Check to see if the user has supplied an applicable
577 		 * netmask as a ripv1_mask option in /etc/gateways.
578 		 */
579 		for (r1p = r1nets; r1p != NULL; r1p = r1p->r1net_next) {
580 			/*
581 			 * If the address is is on a matching network
582 			 * and we haven't already found a longer match,
583 			 * use the matching netmask.
584 			 */
585 			if (on_net(addr, r1p->r1net_net, r1p->r1net_match) &&
586 			    r1p->r1net_mask > mask)
587 				mask = r1p->r1net_mask;
588 		}
589 
590 		/* Otherwise, make the classic A/B/C guess. */
591 		if (mask == 0)
592 			mask = std_mask(addr);
593 	}
594 
595 	return (mask);
596 }
597 
598 
599 in_addr_t
600 ripv1_mask_host(in_addr_t addr,		/* in network byte order */
601     const struct interface *ifp)	/* as seen on this interface */
602 {
603 	in_addr_t mask = ripv1_mask_net(addr, ifp);
604 
605 
606 	/*
607 	 * If the computed netmask does not mask all of the set bits
608 	 * in the address, then assume it is a host address
609 	 */
610 	if ((ntohl(addr) & ~mask) != 0)
611 		mask = HOST_MASK;
612 	return (mask);
613 }
614 
615 
616 /* See if a IP address looks reasonable as a destination */
617 boolean_t			/* _B_FALSE=bad _B_TRUE=good */
618 check_dst(in_addr_t addr)
619 {
620 	addr = ntohl(addr);
621 
622 	if (IN_CLASSA(addr)) {
623 		if (addr == 0)
624 			return (_B_TRUE);	/* default */
625 
626 		addr >>= IN_CLASSA_NSHIFT;
627 		return (addr != 0 && addr != IN_LOOPBACKNET);
628 	}
629 
630 	/* Must not allow destination to be link local address. */
631 	if (IN_LINKLOCAL(addr))
632 		return (_B_FALSE);
633 
634 	return (IN_CLASSB(addr) || IN_CLASSC(addr));
635 }
636 
637 /*
638  * Find an existing interface which has the given parameters, but don't
639  * return the interface with name "name" if "name" is specified.
640  */
641 struct interface *
642 check_dup(const char *name,	/* Don't return this interface */
643     in_addr_t addr,	/* IP address, so network byte order */
644     in_addr_t dstaddr,	/* ditto */
645     in_addr_t mask,	/* mask, so host byte order */
646     uint64_t if_flags,	/* set IFF_POINTOPOINT to ignore local int_addr */
647     boolean_t allowdups)	/* set true to include duplicates */
648 {
649 	struct interface *best_ifp = NULL;
650 	struct interface *ifp;
651 	in_addr_t dstaddr_h = ntohl(dstaddr);
652 	int best_pref = 0;
653 	int pref;
654 
655 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
656 		/* This interface, not a duplicate. */
657 		if (name != NULL && strcmp(name, ifp->int_name) == 0)
658 			continue;
659 
660 		/*
661 		 * Find an interface which isn't already a duplicate to
662 		 * avoid cyclical duplication.  (i.e. qfe0:1 is a duplicate
663 		 * of qfe0, and qfe0 is a duplicate of qfe0:1.  That would
664 		 * be bad)
665 		 */
666 		if (!allowdups && (ifp->int_state & IS_DUP))
667 			continue;
668 
669 		if (ifp->int_mask != mask)
670 			continue;
671 
672 		if (!IS_IFF_UP(ifp->int_if_flags))
673 			continue;
674 
675 		/*
676 		 * The local address can only be shared with a point-to-point
677 		 * link.
678 		 */
679 		if ((ifp->int_addr == addr &&
680 		    ((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0) ||
681 		    on_net(ifp->int_dstaddr, dstaddr_h, mask)) {
682 			pref = 0;
683 			if (!(ifp->int_state & IS_ALIAS))
684 				pref++;
685 			if (!IS_RIP_OUT_OFF(ifp->int_state))
686 				pref += 2;
687 			if (IS_IFF_ROUTING(ifp->int_if_flags))
688 				pref += 4;
689 			if (pref > best_pref) {
690 				best_pref = pref;
691 				best_ifp = ifp;
692 			}
693 		}
694 	}
695 	return (best_ifp);
696 }
697 
698 
699 /*
700  * See that a remote gateway is reachable.
701  *	Note that the answer can change as real interfaces come and go.
702  */
703 boolean_t			/* _B_FALSE=bad _B_TRUE=good */
704 check_remote(struct interface *ifp)
705 {
706 	struct rt_entry *rt;
707 
708 	/* do not worry about other kinds */
709 	if (!(ifp->int_state & IS_REMOTE))
710 		return (_B_TRUE);
711 
712 	rt = rtfind(ifp->int_addr);
713 	if (rt != NULL &&
714 	    rt->rt_ifp != NULL &&
715 	    on_net(ifp->int_addr, rt->rt_ifp->int_net, rt->rt_ifp->int_mask)) {
716 		return (_B_TRUE);
717 	}
718 
719 	/*
720 	 * the gateway cannot be reached directly from one of our
721 	 * interfaces
722 	 */
723 	if (!(ifp->int_state & IS_BROKE)) {
724 		msglog("unreachable gateway %s in "PATH_GATEWAYS,
725 		    naddr_ntoa(ifp->int_addr));
726 		if_bad(ifp, _B_FALSE);
727 	}
728 	return (_B_FALSE);
729 }
730 
731 /* Delete an interface. */
732 static void
733 ifdel(struct interface *ifp)
734 {
735 	struct rewire_data wire;
736 	boolean_t resurrected;
737 	struct physical_interface *phyi;
738 
739 	trace_if("Del", ifp);
740 
741 	ifp->int_state |= IS_BROKE;
742 
743 	/* unlink the interface */
744 	link_out(ifp, offsetof(struct interface, int_link));
745 	hash_unlink(&ahash_tbl, ifp);
746 	hash_unlink(&nhash_tbl, ifp);
747 	if (ifp->int_if_flags & IFF_BROADCAST)
748 		hash_unlink(&bhash_tbl, ifp);
749 
750 	/* Remove from list of interfaces with this ifIndex */
751 	if ((phyi = ifp->int_phys) != NULL) {
752 		link_out(ifp, offsetof(struct interface, int_ilist));
753 		if (phyi->phyi_interface == NULL) {
754 			hash_unlink(&ihash_tbl, phyi);
755 			free(phyi);
756 		}
757 	}
758 
759 	/*
760 	 * If this is a lead interface, then check first for
761 	 * duplicates of this interface with an eye towards promoting
762 	 * one of them.
763 	 */
764 	resurrected = _B_FALSE;
765 	if (!(ifp->int_state & IS_DUP) &&
766 	    (wire.if_new = check_dup(ifp->int_name, ifp->int_addr,
767 	    ifp->int_dstaddr, ifp->int_mask, ifp->int_if_flags,
768 	    _B_TRUE)) != NULL &&
769 	    !IS_IFF_QUIET(wire.if_new->int_if_flags)) {
770 
771 		trace_act("promoting duplicate %s in place of %s",
772 		    wire.if_new->int_name, ifp->int_name);
773 
774 		/* Rewire routes with the replacement interface */
775 		wire.if_old = ifp;
776 		wire.metric_delta = wire.if_new->int_metric - ifp->int_metric;
777 		(void) rn_walktree(rhead, walk_rewire, &wire);
778 		kern_rewire_ifp(wire.if_old, wire.if_new);
779 		if_rewire_rdisc(wire.if_old, wire.if_new);
780 
781 		/* Mark the replacement as being no longer a duplicate */
782 		wire.if_new->int_state &= ~IS_DUP;
783 		tot_interfaces++;
784 		if (!IS_RIP_OFF(wire.if_new->int_state))
785 			rip_interfaces++;
786 		if (!IS_RIP_OUT_OFF(wire.if_new->int_state))
787 			ripout_interfaces++;
788 		if (IS_IFF_ROUTING(wire.if_new->int_if_flags))
789 			fwd_interfaces++;
790 
791 		set_rdisc_mg(wire.if_new, 1);
792 		rip_mcast_on(wire.if_new);
793 
794 		/* We came out ok; no need to clobber routes over this. */
795 		resurrected = _B_TRUE;
796 	}
797 
798 	rip_mcast_off(ifp);
799 	if (rip_sock_interface == ifp)
800 		rip_sock_interface = NULL;
801 
802 	set_rdisc_mg(ifp, 0);
803 
804 	/*
805 	 * Note that duplicates are not counted in the total number of
806 	 * interfaces.
807 	 */
808 	if (!(ifp->int_state & IS_DUP) && !IS_IFF_QUIET(ifp->int_if_flags)) {
809 		tot_interfaces--;
810 		if (!IS_RIP_OFF(ifp->int_state))
811 			rip_interfaces--;
812 		if (!IS_RIP_OUT_OFF(ifp->int_state))
813 			ripout_interfaces--;
814 		if (IS_IFF_ROUTING(ifp->int_if_flags))
815 			fwd_interfaces--;
816 	}
817 
818 	if (!resurrected) {
819 		/*
820 		 * Zap all routes associated with this interface.
821 		 * Assume routes just using gateways beyond this interface
822 		 * will timeout naturally, and have probably already died.
823 		 */
824 		(void) rn_walktree(rhead, walk_bad, ifp);
825 		kern_flush_ifp(ifp);
826 
827 		if_bad_rdisc(ifp);
828 	}
829 
830 	free(ifp);
831 }
832 
833 
834 /* Mark an interface ill. */
835 void
836 if_sick(struct interface *ifp, boolean_t recurse)
837 {
838 	struct interface *ifp1;
839 
840 	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
841 		ifp->int_state |= IS_SICK;
842 		ifp->int_act_time = NEVER;
843 		trace_if("Chg", ifp);
844 
845 		LIM_SEC(ifscan_timer, now.tv_sec+CHECK_BAD_INTERVAL);
846 		if (recurse && ifp->int_phys != NULL) {
847 			/* If an interface is sick, so are its aliases. */
848 			for (ifp1 = ifp->int_phys->phyi_interface;
849 			    ifp1 != NULL; ifp1 = ifp1->int_ilist.hl_next) {
850 				if (ifp1 != ifp)
851 					if_sick(ifp1, _B_FALSE);
852 			}
853 		}
854 	}
855 }
856 
857 
858 /* Mark an interface dead. */
859 static void
860 if_bad(struct interface *ifp, boolean_t recurse)
861 {
862 	struct interface *ifp1;
863 	struct rewire_data wire;
864 
865 	if (ifp->int_state & IS_BROKE)
866 		return;
867 
868 	LIM_SEC(ifscan_timer, now.tv_sec+CHECK_BAD_INTERVAL);
869 
870 	ifp->int_state |= (IS_BROKE | IS_SICK);
871 	ifp->int_act_time = NEVER;
872 	ifp->int_query_time = NEVER;
873 	/* Note: don't reset the stats timestamp here */
874 
875 	trace_if("Chg", ifp);
876 
877 	if (recurse && ifp->int_phys != NULL) {
878 		/* If an interface is bad, so are its aliases. */
879 		for (ifp1 = ifp->int_phys->phyi_interface;
880 		    ifp1 != NULL; ifp1 = ifp1->int_ilist.hl_next) {
881 			if (ifp1 != ifp)
882 				if_bad(ifp1, _B_FALSE);
883 		}
884 	}
885 
886 	/* If we can find a replacement, then pick it up. */
887 	if (!(ifp->int_state & IS_DUP) &&
888 	    (wire.if_new = check_dup(ifp->int_name, ifp->int_addr,
889 	    ifp->int_dstaddr, ifp->int_mask, ifp->int_if_flags,
890 	    _B_TRUE)) != NULL &&
891 	    !IS_IFF_QUIET(wire.if_new->int_if_flags)) {
892 		trace_act("promoting duplicate %s in place of %s",
893 		    wire.if_new->int_name, ifp->int_name);
894 		wire.if_old = ifp;
895 		wire.metric_delta = wire.if_new->int_metric - ifp->int_metric;
896 		(void) rn_walktree(rhead, walk_rewire, &wire);
897 		if_rewire_rdisc(wire.if_old, wire.if_new);
898 
899 		/* The broken guy becomes the duplicate */
900 		wire.if_new->int_state &= ~IS_DUP;
901 		set_rdisc_mg(ifp, 0);
902 		rip_mcast_off(ifp);
903 		ifp->int_state |= IS_DUP;
904 
905 		/* join the mcast groups for the replacement */
906 		set_rdisc_mg(wire.if_new, 1);
907 		rip_mcast_on(wire.if_new);
908 
909 		if (rip_sock_interface == ifp)
910 			rip_sock_interface = NULL;
911 	} else {
912 		(void) rn_walktree(rhead, walk_bad, ifp);
913 		if_bad_rdisc(ifp);
914 	}
915 }
916 
917 
918 /* Mark an interface alive */
919 void
920 if_ok(struct interface *ifp, const char *type, boolean_t recurse)
921 {
922 	struct interface *ifp1;
923 	boolean_t wasbroken = _B_FALSE;
924 
925 	if (ifp->int_state & IS_BROKE) {
926 		writelog(LOG_WARNING, "%sinterface %s to %s restored",
927 		    type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
928 		ifp->int_state &= ~(IS_BROKE | IS_SICK);
929 		wasbroken = _B_TRUE;
930 	} else if (ifp->int_state & IS_SICK) {
931 		trace_act("%sinterface %s to %s working better",
932 		    type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
933 		ifp->int_state &= ~IS_SICK;
934 	}
935 
936 	if (recurse && ifp->int_phys != NULL && IS_IFF_UP(ifp->int_if_flags)) {
937 		ifp->int_phys->phyi_data.ts = 0;
938 
939 		/* Also mark all aliases of this interface as ok */
940 		for (ifp1 = ifp->int_phys->phyi_interface;
941 		    ifp1 != NULL; ifp1 = ifp1->int_ilist.hl_next) {
942 			if (ifp1 != ifp)
943 				if_ok(ifp1, type, _B_FALSE);
944 		}
945 	}
946 
947 	if (wasbroken) {
948 		if (!(ifp->int_state & IS_DUP))
949 			if_ok_rdisc(ifp);
950 
951 		if (ifp->int_state & IS_REMOTE)
952 			(void) addrouteforif(ifp);
953 	}
954 }
955 
956 boolean_t
957 remote_address_ok(struct interface *ifp, in_addr_t addr)
958 {
959 	if (ifp->int_if_flags & IFF_POINTOPOINT) {
960 		if (addr == ifp->int_dstaddr)
961 			return (_B_TRUE);
962 	} else if (on_net(addr, ifp->int_net, ifp->int_mask)) {
963 		return (_B_TRUE);
964 	}
965 	return (_B_FALSE);
966 }
967 
968 /*
969  * Find the network interfaces which have configured themselves.
970  *	This must be done regularly, if only for extra addresses
971  *	that come and go on interfaces.
972  */
973 void
974 ifscan(void)
975 {
976 	uint_t complaints = 0;
977 	static uint_t prev_complaints = 0;
978 #define	COMP_BADADDR	0x001
979 #define	COMP_NODST	0x002
980 #define	COMP_NOBADDR	0x004
981 #define	COMP_NOMASK	0x008
982 #define	COMP_BAD_METRIC	0x010
983 #define	COMP_NETMASK	0x020
984 #define	COMP_NO_INDEX	0x040
985 #define	COMP_BAD_FLAGS	0x080
986 #define	COMP_NO_KSTATS	0x100
987 #define	COMP_IPFORWARD	0x200
988 
989 	struct interface ifs, *ifp, *ifp1;
990 	struct rt_entry *rt;
991 	size_t needed;
992 	static size_t lastneeded = 0;
993 	char *buf;
994 	static char *lastbuf = NULL;
995 	int32_t in, ierr, out, oerr;
996 	struct intnet *intnetp;
997 	int sock;
998 	struct lifnum lifn;
999 	struct lifconf lifc;
1000 	struct lifreq *lifrp, *lifrp_lim;
1001 	struct sockaddr_in *sinp;
1002 	in_addr_t haddr;
1003 	static in_addr_t myaddr = 0;
1004 	uint32_t ifindex;
1005 	struct phyi_data newstats;
1006 	struct physical_interface *phyi;
1007 
1008 	last_ifscan = now;
1009 	ifscan_timer.tv_sec = now.tv_sec +
1010 	    (supplier || tot_interfaces != 1 ?
1011 	    CHECK_ACT_INTERVAL : CHECK_QUIET_INTERVAL);
1012 
1013 	/* mark all interfaces so we can get rid of those that disappear */
1014 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next)
1015 		ifp->int_state &= ~IS_CHECKED;
1016 
1017 	/* Fetch the size of the current interface list */
1018 	if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
1019 		BADERR(_B_TRUE, "ifscan: socket(SOCK_DGRAM)");
1020 	lifn.lifn_family = AF_INET;	/* Only count IPv4 interfaces */
1021 	/*
1022 	 * Include IFF_NOXMIT interfaces.  Such interfaces are exluded
1023 	 * from protocol operations, but their inclusion in the
1024 	 * internal table enables us to know when packets arrive on
1025 	 * such interfaces.
1026 	 */
1027 	lifn.lifn_flags = LIFC_NOXMIT;
1028 calculate_lifc_len:
1029 	if (ioctl(sock, SIOCGLIFNUM, &lifn) == -1) {
1030 		BADERR(_B_TRUE, "ifscan: ioctl(SIOCGLIFNUM)");
1031 	}
1032 
1033 	/*
1034 	 * When calculating the buffer size needed, add a small number
1035 	 * of interfaces to those we counted.  We do this to capture
1036 	 * the interface status of potential interfaces which may have
1037 	 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
1038 	 * Try to reuse the buffer we already have to avoid heap
1039 	 * thrash.
1040 	 */
1041 	needed = (lifn.lifn_count + 4) * sizeof (struct lifreq);
1042 	if (needed > lastneeded || needed < lastneeded/2) {
1043 		if (lastbuf != NULL)
1044 			free(lastbuf);
1045 		if ((buf = malloc(needed)) == NULL) {
1046 			lastbuf = NULL;
1047 			msglog("ifscan: malloc: %s", rip_strerror(errno));
1048 			return;
1049 		}
1050 	} else {
1051 		buf = lastbuf;
1052 	}
1053 	lastbuf = buf;
1054 	lastneeded = needed;
1055 
1056 	/* Get the list */
1057 	lifc.lifc_family = AF_INET;	/* We only need IPv4 interfaces */
1058 	lifc.lifc_flags = LIFC_NOXMIT;
1059 	lifc.lifc_len = needed;
1060 	lifc.lifc_buf = buf;
1061 	if (ioctl(sock, SIOCGLIFCONF, &lifc) == -1) {
1062 		/*
1063 		 * IP returns EINVAL if the lifc_len we passed in is
1064 		 * too small.  If that's the case, we need to go back
1065 		 * and recalculate it.
1066 		 */
1067 		if (errno == EINVAL)
1068 			goto calculate_lifc_len;
1069 		BADERR(_B_TRUE, "ifscan: ioctl(SIOCGLIFCONF)");
1070 	}
1071 
1072 	/*
1073 	 * If the returned lifc_len is within one lifreq of the
1074 	 * requested ammount, we may have used a buffer which
1075 	 * was too small to hold all of the interfaces.  In that
1076 	 * case go back and recalculate needed.
1077 	 */
1078 	if (lifc.lifc_len >= needed - sizeof (struct lifreq))
1079 		goto calculate_lifc_len;
1080 
1081 	lifrp = lifc.lifc_req;
1082 	lifrp_lim = lifrp + lifc.lifc_len / sizeof (*lifrp);
1083 	for (; lifrp < lifrp_lim; lifrp++) {
1084 
1085 		(void) memset(&ifs, 0, sizeof (ifs));
1086 
1087 		(void) strlcpy(ifs.int_name, lifrp->lifr_name,
1088 		    sizeof (ifs.int_name));
1089 
1090 		/* SIOCGLIFCONF fills in the lifr_addr of each lifreq */
1091 		ifs.int_addr = ((struct sockaddr_in *)&lifrp->lifr_addr)->
1092 		    sin_addr.s_addr;
1093 
1094 		if (ioctl(sock, SIOCGLIFFLAGS, lifrp) == -1) {
1095 			if (!(prev_complaints & COMP_BAD_FLAGS))
1096 				writelog(LOG_NOTICE,
1097 				    "unable to get interface flags for %s: %s",
1098 				    ifs.int_name, rip_strerror(errno));
1099 			complaints |= COMP_BAD_FLAGS;
1100 			ifs.int_if_flags = 0;
1101 		} else {
1102 			ifs.int_if_flags = lifrp->lifr_flags;
1103 		}
1104 
1105 		if (IN_EXPERIMENTAL(ntohl(ifs.int_addr)) ||
1106 		    (ntohl(ifs.int_addr) & IN_CLASSA_NET) == 0) {
1107 			if (IS_IFF_UP(ifs.int_if_flags)) {
1108 				if (!(prev_complaints & COMP_BADADDR))
1109 					writelog(LOG_NOTICE,
1110 					    "%s has a bad address %s",
1111 					    ifs.int_name,
1112 					    naddr_ntoa(ifs.int_addr));
1113 				complaints |= COMP_BADADDR;
1114 			}
1115 			continue;
1116 		}
1117 
1118 		/* Ignore interface with IPv4 link local address. */
1119 		if (IN_LINKLOCAL(ntohl(ifs.int_addr)))
1120 			continue;
1121 
1122 		/* Get the interface index. */
1123 		if (ioctl(sock, SIOCGLIFINDEX, lifrp) == -1) {
1124 			ifindex = 0;
1125 			ifs.int_if_flags &= ~IFF_UP;
1126 			if (!(prev_complaints & COMP_NO_INDEX))
1127 				writelog(LOG_NOTICE, "%s has no ifIndex: %s",
1128 				    ifs.int_name, rip_strerror(errno));
1129 			complaints |= COMP_NO_INDEX;
1130 		} else {
1131 			ifindex = lifrp->lifr_index;
1132 		}
1133 
1134 		/*
1135 		 * Get the destination address for point-to-point
1136 		 * interfaces.
1137 		 */
1138 		if (ifs.int_if_flags & IFF_POINTOPOINT) {
1139 			sinp = (struct sockaddr_in *)&lifrp->lifr_dstaddr;
1140 			if (ioctl(sock, SIOCGLIFDSTADDR, lifrp) == -1) {
1141 				if (IS_IFF_UP(ifs.int_if_flags)) {
1142 					if (!(prev_complaints & COMP_NODST))
1143 						writelog(LOG_NOTICE,
1144 						    "%s has no destination "
1145 						    "address : %s",
1146 						    ifs.int_name,
1147 						    rip_strerror(errno));
1148 					complaints |= COMP_NODST;
1149 				}
1150 				continue;
1151 			}
1152 			ifs.int_net = ntohl(sinp->sin_addr.s_addr);
1153 			if (IN_EXPERIMENTAL(ifs.int_net) ||
1154 			    (ifs.int_net != 0 &&
1155 			    (ifs.int_net & IN_CLASSA_NET) == 0)) {
1156 				if (IS_IFF_UP(ifs.int_if_flags)) {
1157 					if (!(prev_complaints & COMP_NODST))
1158 						writelog(LOG_NOTICE,
1159 						    "%s has a bad "
1160 						    "destination address %s",
1161 						    ifs.int_name,
1162 						    naddr_ntoa(ifs.int_net));
1163 					complaints |= COMP_NODST;
1164 				}
1165 				continue;
1166 			}
1167 			ifs.int_dstaddr = sinp->sin_addr.s_addr;
1168 		}
1169 
1170 		/* Get the subnet mask */
1171 		sinp = (struct sockaddr_in *)&lifrp->lifr_addr;
1172 		if (ioctl(sock, SIOCGLIFNETMASK, lifrp) == -1) {
1173 			if (IS_IFF_UP(ifs.int_if_flags)) {
1174 				if (!(prev_complaints & COMP_NOMASK))
1175 					writelog(LOG_NOTICE,
1176 					    "%s has no netmask: %s",
1177 					    ifs.int_name, rip_strerror(errno));
1178 				complaints |= COMP_NOMASK;
1179 			}
1180 			continue;
1181 		}
1182 		if (sinp->sin_addr.s_addr == INADDR_ANY) {
1183 			if (!(ifs.int_if_flags &
1184 			    (IFF_POINTOPOINT|IFF_LOOPBACK))) {
1185 				if (IS_IFF_UP(ifs.int_if_flags)) {
1186 					if (!(prev_complaints & COMP_NOMASK))
1187 						writelog(LOG_NOTICE,
1188 						    "%s has all-zero netmask",
1189 						    ifs.int_name);
1190 					complaints |= COMP_NOMASK;
1191 				}
1192 				continue;
1193 			}
1194 			ifs.int_mask = IP_HOST_MASK;
1195 		} else {
1196 			ifs.int_mask = ntohl(sinp->sin_addr.s_addr);
1197 		}
1198 
1199 		/*
1200 		 * Get the broadcast address on broadcast capable
1201 		 * interfaces.
1202 		 */
1203 		if (ifs.int_if_flags & IFF_BROADCAST) {
1204 			if (ioctl(sock, SIOCGLIFBRDADDR, lifrp) == -1) {
1205 				if (IS_IFF_UP(ifs.int_if_flags)) {
1206 					if (!(prev_complaints & COMP_NOBADDR))
1207 						writelog(LOG_NOTICE,
1208 						    "%s has no broadcast "
1209 						    "address: %s",
1210 						    ifs.int_name,
1211 						    rip_strerror(errno));
1212 					complaints |= COMP_NOBADDR;
1213 				}
1214 				continue;
1215 			}
1216 			haddr = ntohl(sinp->sin_addr.s_addr);
1217 			if (IN_EXPERIMENTAL(haddr) ||
1218 			    (haddr & IN_CLASSA_NET) == 0) {
1219 				if (IS_IFF_UP(ifs.int_if_flags)) {
1220 					if (!(prev_complaints & COMP_NOBADDR))
1221 						writelog(LOG_NOTICE,
1222 						    "%s has a bad broadcast "
1223 						    "address %s",
1224 						    ifs.int_name,
1225 						    naddr_ntoa(haddr));
1226 					complaints |= COMP_NOBADDR;
1227 				}
1228 				continue;
1229 			}
1230 		}
1231 		ifs.int_brdaddr = sinp->sin_addr.s_addr;
1232 
1233 		/* Get interface metric, if possible. */
1234 		if (ioctl(sock, SIOCGLIFMETRIC, lifrp) == -1) {
1235 			if (IS_IFF_UP(ifs.int_if_flags)) {
1236 				if (!(prev_complaints & COMP_BAD_METRIC))
1237 					writelog(LOG_NOTICE,
1238 					    "%s has no metric: %s",
1239 					    ifs.int_name, rip_strerror(errno));
1240 				complaints |= COMP_BAD_METRIC;
1241 			}
1242 		} else {
1243 			ifs.int_metric = lifrp->lifr_metric;
1244 			if (ifs.int_metric > HOPCNT_INFINITY) {
1245 				if (IS_IFF_UP(ifs.int_if_flags)) {
1246 					if (!(prev_complaints &
1247 					    COMP_BAD_METRIC))
1248 						writelog(LOG_NOTICE,
1249 						    "%s has a metric of %d, "
1250 						    "defaulting to %d",
1251 						    ifs.int_name,
1252 						    ifs.int_metric,
1253 						    HOPCNT_INFINITY);
1254 					complaints |= COMP_BAD_METRIC;
1255 				}
1256 				ifs.int_metric = HOPCNT_INFINITY;
1257 			}
1258 		}
1259 
1260 		ifs.int_state |= IS_CHECKED;
1261 		ifs.int_query_time = NEVER;
1262 
1263 		/*
1264 		 * If this is an alias, then mark it appropriately.
1265 		 * Do not output RIP or Router-Discovery packets via
1266 		 * aliases.
1267 		 */
1268 		if (strchr(ifs.int_name, ':') != NULL)
1269 			ifs.int_state |= IS_ALIAS;
1270 
1271 		if (ifs.int_if_flags & IFF_LOOPBACK) {
1272 			ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
1273 			ifs.int_dstaddr = ifs.int_addr;
1274 			ifs.int_mask = HOST_MASK;
1275 			ifs.int_ripv1_mask = HOST_MASK;
1276 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
1277 			ifs.int_net = ntohl(ifs.int_dstaddr);
1278 			if (!foundloopback) {
1279 				foundloopback = _B_TRUE;
1280 				loopaddr = ifs.int_addr;
1281 				loop_rts.rts_gate = loopaddr;
1282 				loop_rts.rts_router = loopaddr;
1283 			}
1284 
1285 		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
1286 			ifs.int_ripv1_mask = ifs.int_mask;
1287 			ifs.int_mask = HOST_MASK;
1288 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
1289 
1290 		} else {
1291 			ifs.int_dstaddr = ifs.int_addr;
1292 			ifs.int_ripv1_mask = ifs.int_mask;
1293 			ifs.int_std_mask = std_mask(ifs.int_addr);
1294 			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
1295 			if (ifs.int_mask != ifs.int_std_mask)
1296 				ifs.int_state |= IS_SUBNET;
1297 		}
1298 		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
1299 		ifs.int_std_addr = htonl(ifs.int_std_net);
1300 
1301 		/*
1302 		 * If this interface duplicates another, mark it
1303 		 * appropriately so that we don't generate duplicate
1304 		 * packets.
1305 		 */
1306 		ifp = check_dup(ifs.int_name, ifs.int_addr, ifs.int_dstaddr,
1307 		    ifs.int_mask, ifs.int_if_flags, _B_FALSE);
1308 		if (ifp != NULL) {
1309 			trace_misc("%s (%s%s%s) is a duplicate of %s (%s%s%s)",
1310 			    ifs.int_name,
1311 			    addrname(ifs.int_addr, ifs.int_mask, 1),
1312 			    ((ifs.int_if_flags & IFF_POINTOPOINT) ?
1313 			    "-->" : ""),
1314 			    ((ifs.int_if_flags & IFF_POINTOPOINT) ?
1315 			    naddr_ntoa(ifs.int_dstaddr) : ""),
1316 			    ifp->int_name,
1317 			    addrname(ifp->int_addr, ifp->int_mask, 1),
1318 			    ((ifp->int_if_flags & IFF_POINTOPOINT) ?
1319 			    "-->" : ""),
1320 			    ((ifp->int_if_flags & IFF_POINTOPOINT) ?
1321 			    naddr_ntoa(ifp->int_dstaddr) : ""));
1322 			ifs.int_state |= IS_DUP;
1323 		} else {
1324 			ifs.int_state &= ~IS_DUP;
1325 		}
1326 
1327 		/*
1328 		 * See if this is a familiar interface.
1329 		 * If so, stop worrying about it if it is the same.
1330 		 * Start it over if it now is to somewhere else, as happens
1331 		 * frequently with PPP and SLIP, or if its forwarding
1332 		 * status has changed.
1333 		 */
1334 		ifp = ifwithname(ifs.int_name);
1335 		if (ifp != NULL) {
1336 			ifp->int_state |= IS_CHECKED;
1337 			ifp->int_state = (ifp->int_state & ~IS_DUP) |
1338 			    (ifs.int_state & IS_DUP);
1339 
1340 			if ((ifp->int_phys == NULL && ifindex != 0) ||
1341 			    (ifp->int_phys != NULL &&
1342 			    ifp->int_phys->phyi_index != ifindex) ||
1343 			    0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
1344 			    & (IFF_BROADCAST | IFF_LOOPBACK |
1345 			    IFF_POINTOPOINT | IFF_MULTICAST |
1346 			    IFF_ROUTER | IFF_NORTEXCH | IFF_NOXMIT)) ||
1347 			    ifp->int_addr != ifs.int_addr ||
1348 			    ifp->int_brdaddr != ifs.int_brdaddr ||
1349 			    ifp->int_dstaddr != ifs.int_dstaddr ||
1350 			    ifp->int_mask != ifs.int_mask ||
1351 			    ifp->int_metric != ifs.int_metric) {
1352 				/*
1353 				 * Forget old information about
1354 				 * a changed interface.
1355 				 */
1356 				trace_act("interface %s has changed",
1357 				    ifp->int_name);
1358 				ifdel(ifp);
1359 				ifp = NULL;
1360 			}
1361 		}
1362 
1363 		if (ifp != NULL) {
1364 			/* note interfaces that have been turned off */
1365 			if (!IS_IFF_UP(ifs.int_if_flags)) {
1366 				if (IS_IFF_UP(ifp->int_if_flags)) {
1367 					writelog(LOG_WARNING,
1368 					    "interface %s to %s turned off",
1369 					    ifp->int_name,
1370 					    naddr_ntoa(ifp->int_dstaddr));
1371 					if_bad(ifp, _B_FALSE);
1372 					ifp->int_if_flags &= ~IFF_UP;
1373 				} else if (ifp->int_phys != NULL &&
1374 				    now.tv_sec > (ifp->int_phys->phyi_data.ts +
1375 				    CHECK_BAD_INTERVAL)) {
1376 					trace_act("interface %s has been off"
1377 					    " %ld seconds; forget it",
1378 					    ifp->int_name,
1379 					    now.tv_sec -
1380 					    ifp->int_phys->phyi_data.ts);
1381 					ifdel(ifp);
1382 				}
1383 				continue;
1384 			}
1385 			/* or that were off and are now ok */
1386 			if (!IS_IFF_UP(ifp->int_if_flags)) {
1387 				ifp->int_if_flags |= IFF_UP;
1388 				if_ok(ifp, "", _B_FALSE);
1389 			}
1390 
1391 			/*
1392 			 * If it has been long enough,
1393 			 * see if the interface is broken.
1394 			 */
1395 			if ((phyi = ifp->int_phys) == NULL ||
1396 			    now.tv_sec < phyi->phyi_data.ts +
1397 			    CHECK_BAD_INTERVAL)
1398 				continue;
1399 
1400 			(void) memset(&newstats, 0, sizeof (newstats));
1401 			if (get_if_kstats(ifp, &newstats) == -1) {
1402 				if (!(prev_complaints & COMP_NO_KSTATS))
1403 					writelog(LOG_WARNING,
1404 					    "unable to obtain kstats for %s",
1405 					    phyi->phyi_name);
1406 				complaints |= COMP_NO_KSTATS;
1407 			}
1408 
1409 			/*
1410 			 * If the interface just awoke, restart the counters.
1411 			 */
1412 			if (phyi->phyi_data.ts == 0) {
1413 				phyi->phyi_data = newstats;
1414 				continue;
1415 			}
1416 
1417 			in = newstats.ipackets - phyi->phyi_data.ipackets;
1418 			ierr = newstats.ierrors - phyi->phyi_data.ierrors;
1419 			out = newstats.opackets - phyi->phyi_data.opackets;
1420 			oerr = newstats.oerrors - phyi->phyi_data.oerrors;
1421 			phyi->phyi_data = newstats;
1422 
1423 			/*
1424 			 * Withhold judgment when the short error counters
1425 			 * wrap, the interface is reset, or if there are
1426 			 * no kstats.
1427 			 */
1428 			if (ierr < 0 || in < 0 || oerr < 0 || out < 0 ||
1429 			    newstats.ts == 0) {
1430 				LIM_SEC(ifscan_timer,
1431 				    now.tv_sec + CHECK_BAD_INTERVAL);
1432 				continue;
1433 			}
1434 
1435 			/* Withhold judgement when there is no traffic */
1436 			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1437 				continue;
1438 
1439 			/*
1440 			 * It is bad if at least 25% of input or output on
1441 			 * an interface results in errors.  Require
1442 			 * presistent problems before marking it dead.
1443 			 */
1444 			if ((ierr > 0 && ierr >= in/4) ||
1445 			    (oerr > 0 && oerr >= out/4)) {
1446 				if (!(ifp->int_state & IS_SICK)) {
1447 					trace_act("interface %s to %s"
1448 					    " sick: in=%d ierr=%d"
1449 					    " out=%d oerr=%d",
1450 					    ifp->int_name,
1451 					    naddr_ntoa(ifp->int_dstaddr),
1452 					    in, ierr, out, oerr);
1453 					if_sick(ifp, _B_TRUE);
1454 					continue;
1455 				}
1456 				if (!(ifp->int_state & IS_BROKE)) {
1457 					writelog(LOG_WARNING,
1458 					    "interface %s to %s broken:"
1459 					    " in=%d ierr=%d out=%d oerr=%d",
1460 					    ifp->int_name,
1461 					    naddr_ntoa(ifp->int_dstaddr),
1462 					    in, ierr, out, oerr);
1463 					if_bad(ifp, _B_TRUE);
1464 				}
1465 				continue;
1466 			}
1467 
1468 			/* otherwise, it is active and healthy */
1469 			ifp->int_act_time = now.tv_sec;
1470 			if_ok(ifp, "", _B_TRUE);
1471 			continue;
1472 		}
1473 
1474 		/*
1475 		 * This is a new interface.
1476 		 * If it is dead, forget it.
1477 		 */
1478 		if (!IS_IFF_UP(ifs.int_if_flags))
1479 			continue;
1480 
1481 		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT |
1482 		    IFF_BROADCAST | IFF_LOOPBACK)) &&
1483 		    !(ifs.int_state & IS_PASSIVE)) {
1484 			if (!(prev_complaints & COMP_BAD_FLAGS))
1485 				trace_act("%s is neither broadcast, "
1486 				    "point-to-point, nor loopback",
1487 				    ifs.int_name);
1488 			complaints |= COMP_BAD_FLAGS;
1489 			if (!(ifs.int_if_flags & IFF_MULTICAST))
1490 				ifs.int_state |= IS_NO_RDISC;
1491 		}
1492 
1493 
1494 		/*
1495 		 * It is new and ok.   Add it to the list of interfaces
1496 		 */
1497 		ifp = rtmalloc(sizeof (*ifp), "ifscan ifp");
1498 		(void) memcpy(ifp, &ifs, sizeof (*ifp));
1499 		get_parms(ifp);
1500 		if_link(ifp, ifindex);
1501 		trace_if("Add", ifp);
1502 
1503 		if (ifp->int_phys != NULL &&
1504 		    get_if_kstats(ifp, &ifp->int_phys->phyi_data) == -1) {
1505 			if (!(prev_complaints & COMP_NO_KSTATS))
1506 				writelog(LOG_NOTICE,
1507 				    "unable to obtain kstats for %s",
1508 				    ifp->int_phys->phyi_name);
1509 			complaints |= COMP_NO_KSTATS;
1510 		}
1511 
1512 		/* Detect interfaces that have conflicting netmasks. */
1513 		if (!(ifp->int_if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK))) {
1514 			for (ifp1 = ifnet; ifp1 != NULL;
1515 			    ifp1 = ifp1->int_next) {
1516 				if (ifp1->int_mask == ifp->int_mask)
1517 					continue;
1518 
1519 				/*
1520 				 * we don't care about point-to-point
1521 				 * or loopback aliases
1522 				 */
1523 				if (ifp1->int_if_flags &
1524 				    (IFF_POINTOPOINT|IFF_LOOPBACK)) {
1525 					continue;
1526 				}
1527 
1528 				/* ignore aliases on the same network */
1529 				if (ifp->int_phys == ifp1->int_phys)
1530 					continue;
1531 
1532 				if (on_net(ifp->int_addr,
1533 				    ifp1->int_net, ifp1->int_mask) ||
1534 				    on_net(ifp1->int_addr,
1535 				    ifp->int_net, ifp->int_mask)) {
1536 					writelog(LOG_INFO,
1537 					    "possible netmask problem"
1538 					    " between %s:%s and %s:%s",
1539 					    ifp->int_name,
1540 					    addrname(htonl(ifp->int_net),
1541 					    ifp->int_mask, 1),
1542 					    ifp1->int_name,
1543 					    addrname(htonl(ifp1->int_net),
1544 					    ifp1->int_mask, 1));
1545 					complaints |= COMP_NETMASK;
1546 				}
1547 			}
1548 		}
1549 
1550 		if (!(ifp->int_state & IS_DUP) &&
1551 		    !IS_IFF_QUIET(ifp->int_if_flags)) {
1552 			/* Count the # of directly connected networks. */
1553 			tot_interfaces++;
1554 			if (!IS_RIP_OFF(ifp->int_state))
1555 				rip_interfaces++;
1556 			if (!IS_RIP_OUT_OFF(ifp->int_state))
1557 				ripout_interfaces++;
1558 			if (IS_IFF_ROUTING(ifp->int_if_flags))
1559 				fwd_interfaces++;
1560 
1561 			if_ok_rdisc(ifp);
1562 			rip_on(ifp);
1563 		}
1564 	}
1565 
1566 	(void) close(sock);
1567 
1568 	/*
1569 	 * If we are multi-homed and have at least two interfaces that
1570 	 * are able to forward, then output RIP by default.
1571 	 */
1572 	if (!supplier_set)
1573 		set_supplier();
1574 
1575 	/*
1576 	 * If we are multi-homed, optionally advertise a route to
1577 	 * our main address.
1578 	 */
1579 	if (advertise_mhome || (tot_interfaces > 1 && mhome)) {
1580 		/* lookup myaddr if we haven't done so already */
1581 		if (myaddr == 0) {
1582 			char myname[MAXHOSTNAMELEN+1];
1583 
1584 			/*
1585 			 * If we are unable to resolve our hostname, don't
1586 			 * bother trying again.
1587 			 */
1588 			if (gethostname(myname, MAXHOSTNAMELEN) == -1) {
1589 				msglog("gethostname: %s", rip_strerror(errno));
1590 				advertise_mhome = _B_FALSE;
1591 				mhome = _B_FALSE;
1592 			} else if (gethost(myname, &myaddr) == 0) {
1593 				writelog(LOG_WARNING,
1594 				    "unable to resolve local hostname %s",
1595 				    myname);
1596 				advertise_mhome = _B_FALSE;
1597 				mhome = _B_FALSE;
1598 			}
1599 		}
1600 		if (myaddr != 0 &&
1601 		    (ifp = ifwithaddr(myaddr, _B_FALSE, _B_FALSE)) != NULL &&
1602 		    foundloopback) {
1603 			advertise_mhome = _B_TRUE;
1604 			rt = rtget(myaddr, HOST_MASK);
1605 			if (rt != NULL) {
1606 				if (rt->rt_ifp != ifp ||
1607 				    rt->rt_router != loopaddr) {
1608 					rtdelete(rt);
1609 					rt = NULL;
1610 				} else {
1611 					loop_rts.rts_ifp = ifp;
1612 					loop_rts.rts_metric = 0;
1613 					loop_rts.rts_time = rt->rt_time;
1614 					loop_rts.rts_origin = RO_LOOPBCK;
1615 					rtchange(rt, rt->rt_state | RS_MHOME,
1616 					    &loop_rts, NULL);
1617 				}
1618 			}
1619 			if (rt == NULL) {
1620 				loop_rts.rts_ifp = ifp;
1621 				loop_rts.rts_metric = 0;
1622 				loop_rts.rts_origin = RO_LOOPBCK;
1623 				rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1624 			}
1625 		}
1626 	}
1627 
1628 	for (ifp = ifnet; ifp != NULL; ifp = ifp1) {
1629 		ifp1 = ifp->int_next;	/* because we may delete it */
1630 
1631 		/* Forget any interfaces that have disappeared. */
1632 		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1633 			trace_act("interface %s has disappeared",
1634 			    ifp->int_name);
1635 			ifdel(ifp);
1636 			continue;
1637 		}
1638 
1639 		if ((ifp->int_state & IS_BROKE) &&
1640 		    !(ifp->int_state & IS_PASSIVE))
1641 			LIM_SEC(ifscan_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1642 
1643 		/*
1644 		 * If we ever have a RIPv1 interface, assume we always will.
1645 		 * It might come back if it ever goes away.
1646 		 */
1647 		if (!(ifp->int_state & (IS_NO_RIPV1_OUT | IS_DUP)) &&
1648 		    should_supply(ifp))
1649 			have_ripv1_out = _B_TRUE;
1650 		if (!(ifp->int_state & IS_NO_RIPV1_IN))
1651 			have_ripv1_in = _B_TRUE;
1652 	}
1653 
1654 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
1655 		/*
1656 		 * Ensure there is always a network route for interfaces,
1657 		 * after any dead interfaces have been deleted, which
1658 		 * might affect routes for point-to-point links.
1659 		 */
1660 		if (addrouteforif(ifp) == 0)
1661 			continue;
1662 
1663 		/*
1664 		 * Add routes to the local end of point-to-point interfaces
1665 		 * using loopback.
1666 		 */
1667 		if ((ifp->int_if_flags & IFF_POINTOPOINT) &&
1668 		    !(ifp->int_state & IS_REMOTE) && foundloopback) {
1669 			/*
1670 			 * Delete any routes to the network address through
1671 			 * foreign routers. Remove even static routes.
1672 			 */
1673 			del_static(ifp->int_addr, HOST_MASK, 0, ifp, 0);
1674 			rt = rtget(ifp->int_addr, HOST_MASK);
1675 			if (rt != NULL && rt->rt_router != loopaddr) {
1676 				rtdelete(rt);
1677 				rt = NULL;
1678 			}
1679 			if (rt != NULL) {
1680 				if (!(rt->rt_state & RS_LOCAL) ||
1681 				    rt->rt_metric > ifp->int_metric) {
1682 					ifp1 = ifp;
1683 				} else {
1684 					ifp1 = rt->rt_ifp;
1685 				}
1686 				loop_rts.rts_ifp = ifp1;
1687 				loop_rts.rts_metric = 0;
1688 				loop_rts.rts_time = rt->rt_time;
1689 				loop_rts.rts_origin = RO_LOOPBCK;
1690 				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN) |
1691 				    (RS_IF|RS_LOCAL)), &loop_rts, 0);
1692 			} else {
1693 				loop_rts.rts_ifp = ifp;
1694 				loop_rts.rts_metric = 0;
1695 				loop_rts.rts_origin = RO_LOOPBCK;
1696 				rtadd(ifp->int_addr, HOST_MASK,
1697 				    (RS_IF | RS_LOCAL), &loop_rts);
1698 			}
1699 		}
1700 	}
1701 
1702 	/* add the authority routes */
1703 	for (intnetp = intnets; intnetp != NULL;
1704 	    intnetp = intnetp->intnet_next) {
1705 		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1706 		if (rt != NULL &&
1707 		    !(rt->rt_state & RS_NO_NET_SYN) &&
1708 		    !(rt->rt_state & RS_NET_INT)) {
1709 			rtdelete(rt);
1710 			rt = NULL;
1711 		}
1712 		if (rt == NULL) {
1713 			loop_rts.rts_ifp = NULL;
1714 			loop_rts.rts_metric = intnetp->intnet_metric-1;
1715 			loop_rts.rts_origin = RO_LOOPBCK;
1716 			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1717 			    RS_NET_SYN | RS_NET_INT, &loop_rts);
1718 		}
1719 	}
1720 
1721 	prev_complaints = complaints;
1722 }
1723 
1724 
1725 static void
1726 check_net_syn(struct interface *ifp)
1727 {
1728 	struct rt_entry *rt;
1729 	struct rt_spare new;
1730 
1731 	/*
1732 	 * Turn on the need to automatically synthesize a network route
1733 	 * for this interface only if we are running RIPv1 on some other
1734 	 * interface that is on a different class-A,B,or C network.
1735 	 */
1736 	if (have_ripv1_out || have_ripv1_in) {
1737 		ifp->int_state |= IS_NEED_NET_SYN;
1738 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1739 		if (rt != NULL &&
1740 		    0 == (rt->rt_state & RS_NO_NET_SYN) &&
1741 		    (!(rt->rt_state & RS_NET_SYN) ||
1742 		    rt->rt_metric > ifp->int_metric)) {
1743 			rtdelete(rt);
1744 			rt = NULL;
1745 		}
1746 		if (rt == NULL) {
1747 			(void) memset(&new, 0, sizeof (new));
1748 			new.rts_ifp = ifp;
1749 			new.rts_gate = ifp->int_addr;
1750 			new.rts_router = ifp->int_addr;
1751 			new.rts_metric = ifp->int_metric;
1752 			new.rts_origin = RO_NET_SYN;
1753 			rtadd(ifp->int_std_addr, ifp->int_std_mask,
1754 			    RS_NET_SYN, &new);
1755 		}
1756 
1757 	} else {
1758 		ifp->int_state &= ~IS_NEED_NET_SYN;
1759 
1760 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1761 		if (rt != NULL &&
1762 		    (rt->rt_state & RS_NET_SYN) &&
1763 		    rt->rt_ifp == ifp)
1764 			rtbad_sub(rt, NULL);
1765 	}
1766 }
1767 
1768 
1769 /*
1770  * Add route for interface if not currently installed.
1771  * Create route to other end if a point-to-point link,
1772  * otherwise a route to this (sub)network.
1773  */
1774 static boolean_t			/* _B_FALSE=bad interface */
1775 addrouteforif(struct interface *ifp)
1776 {
1777 	struct rt_entry *rt;
1778 	struct rt_spare new;
1779 	in_addr_t dst;
1780 	uint16_t rt_newstate = RS_IF;
1781 
1782 
1783 	/* skip sick interfaces */
1784 	if (ifp->int_state & IS_BROKE)
1785 		return (_B_FALSE);
1786 
1787 	/*
1788 	 * don't install routes for duplicate interfaces, or
1789 	 * unnumbered point-to-point interfaces.
1790 	 */
1791 	if ((ifp->int_state & IS_DUP) ||
1792 	    ((ifp->int_if_flags & IFF_POINTOPOINT) && ifp->int_dstaddr == 0))
1793 		return (_B_TRUE);
1794 
1795 	/*
1796 	 * If the interface on a subnet, then install a RIPv1 route to
1797 	 * the network as well (unless it is sick).
1798 	 */
1799 	if (ifp->int_state & IS_SUBNET)
1800 		check_net_syn(ifp);
1801 
1802 	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) ?
1803 	    ifp->int_dstaddr : htonl(ifp->int_net));
1804 
1805 	(void) memset(&new, 0, sizeof (new));
1806 	new.rts_ifp = ifp;
1807 	new.rts_router = ifp->int_addr;
1808 	new.rts_gate = ifp->int_addr;
1809 	new.rts_metric = ifp->int_metric;
1810 	new.rts_time = now.tv_sec;
1811 	if (ifp->int_if_flags & IFF_POINTOPOINT)
1812 		new.rts_origin = RO_PTOPT;
1813 	else if (ifp->int_if_flags & IFF_LOOPBACK)
1814 		new.rts_origin = RO_LOOPBCK;
1815 	else
1816 		new.rts_origin = RO_IF;
1817 
1818 	/*
1819 	 * If we are going to send packets to the gateway,
1820 	 * it must be reachable using our physical interfaces
1821 	 */
1822 	if ((ifp->int_state & IS_REMOTE) &&
1823 	    !(ifp->int_state & IS_EXTERNAL) &&
1824 	    !check_remote(ifp))
1825 		return (_B_FALSE);
1826 
1827 	/*
1828 	 * We are finished if the correct main interface route exists.
1829 	 * The right route must be for the right interface, not synthesized
1830 	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1831 	 */
1832 	del_static(dst, ifp->int_mask, 0, ifp, 0);
1833 	rt = rtget(dst, ifp->int_mask);
1834 	if (!IS_IFF_ROUTING(ifp->int_if_flags))
1835 		rt_newstate |= RS_NOPROPAGATE;
1836 	if (rt != NULL) {
1837 		if ((rt->rt_ifp != ifp || rt->rt_router != ifp->int_addr) &&
1838 		    (rt->rt_ifp == NULL ||
1839 		    (rt->rt_ifp->int_state & IS_BROKE))) {
1840 			rtdelete(rt);
1841 			rt = NULL;
1842 		} else {
1843 			rtchange(rt, ((rt->rt_state | rt_newstate) &
1844 			    ~(RS_NET_SYN | RS_LOCAL)), &new, 0);
1845 		}
1846 	}
1847 	if (rt == NULL) {
1848 		if (ifp->int_transitions++ > 0)
1849 			trace_act("re-installing interface %s;"
1850 			    " went up %d times",
1851 			    ifp->int_name, ifp->int_transitions);
1852 
1853 		rtadd(dst, ifp->int_mask, rt_newstate, &new);
1854 	}
1855 
1856 	return (_B_TRUE);
1857 }
1858 
1859 /*
1860  * Obtains the named kstat, and places its value in *value.  It
1861  * returns 0 for success, -1 for failure.
1862  */
1863 static int
1864 kstat_named_value(kstat_t *ksp, char *name, uint32_t *value)
1865 {
1866 	kstat_named_t *knp;
1867 
1868 	if (ksp == NULL)
1869 		return (-1);
1870 
1871 	if ((knp = kstat_data_lookup(ksp, name)) == NULL) {
1872 		return (-1);
1873 	} else if (knp->data_type != KSTAT_DATA_UINT32) {
1874 		return (-1);
1875 	} else {
1876 		*value = knp->value.ui32;
1877 		return (0);
1878 	}
1879 }
1880 
1881 static int
1882 get_if_kstats(struct interface *ifp, struct phyi_data *newdata)
1883 {
1884 	struct physical_interface *phyi = ifp->int_phys;
1885 	kstat_ctl_t *kc;
1886 	kstat_t *ksp;
1887 
1888 	/* We did this recently; don't do it again. */
1889 	if (phyi->phyi_data.ts == now.tv_sec) {
1890 		if (newdata != &phyi->phyi_data)
1891 			*newdata = phyi->phyi_data;
1892 		return (0);
1893 	}
1894 
1895 	if ((kc = kstat_open()) == NULL)
1896 		return (-1);
1897 
1898 	if ((ksp = kstat_lookup(kc, NULL, -1, phyi->phyi_name)) == NULL) {
1899 		(void) kstat_close(kc);
1900 		return (-1);
1901 	}
1902 
1903 	if (kstat_read(kc, ksp, NULL) == -1) {
1904 		(void) kstat_close(kc);
1905 		return (-1);
1906 	}
1907 
1908 	if ((kstat_named_value(ksp, "ipackets", &newdata->ipackets) == -1) ||
1909 	    (kstat_named_value(ksp, "opackets",	&newdata->opackets) == -1)) {
1910 		newdata->ts = 0;
1911 		(void) kstat_close(kc);
1912 		return (-1);
1913 	}
1914 
1915 	/* The loopback interface does not keep track of errors */
1916 	if (!(ifp->int_if_flags & IFF_LOOPBACK)) {
1917 		if ((kstat_named_value(ksp, "ierrors",
1918 		    &newdata->ierrors) == -1) ||
1919 		    (kstat_named_value(ksp, "oerrors",
1920 		    &newdata->oerrors) == -1)) {
1921 			newdata->ts = 0;
1922 			(void) kstat_close(kc);
1923 			return (-1);
1924 		}
1925 	}
1926 
1927 	newdata->ts = now.tv_sec;
1928 	(void) kstat_close(kc);
1929 	return (0);
1930 }
1931 
1932 /*
1933  * Returns true if we should supply routes to other systems.  If the
1934  * user has forced us to be a supplier (by the command line) or if we
1935  * have more than one forwarding interface and this is one of the
1936  * forwarding interfaces, then behave as a RIP supplier (supply rdisc
1937  * advertisements and RIP responses).
1938  */
1939 boolean_t
1940 should_supply(struct interface *ifp)
1941 {
1942 	if (ifp != NULL && !IS_IFF_ROUTING(ifp->int_if_flags))
1943 		return (_B_FALSE);
1944 	return ((supplier_set && supplier) ||
1945 	    (!supplier_set && fwd_interfaces > 1));
1946 }
1947