17c478bd9Sstevel@tonic-gate /*
2*e704a8f2Smeem  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1995
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
97c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
107c478bd9Sstevel@tonic-gate  * are met:
117c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
127c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
137c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
147c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
157c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
167c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
177c478bd9Sstevel@tonic-gate  *    must display the following acknowledgment:
187c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
197c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
207c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
217c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
227c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
257c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
267c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
277c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
287c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
297c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
307c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
317c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
327c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
337c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
347c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * $FreeBSD: src/sbin/routed/rdisc.c,v 1.8 2000/08/11 08:24:38 sheldonh Exp $
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "defs.h"
407c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
417c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
427c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
433173664eSapersson #include <fcntl.h>
443173664eSapersson #include <strings.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * The size of the control buffer passed to recvmsg() used to receive
487c478bd9Sstevel@tonic-gate  * ancillary data.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #define	CONTROL_BUFSIZE	1024
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* router advertisement ICMP packet */
537c478bd9Sstevel@tonic-gate struct icmp_ad {
547c478bd9Sstevel@tonic-gate 	uint8_t    icmp_type;		/* type of message */
557c478bd9Sstevel@tonic-gate 	uint8_t    icmp_code;		/* type sub code */
567c478bd9Sstevel@tonic-gate 	uint16_t   icmp_cksum;		/* ones complement cksum of struct */
577c478bd9Sstevel@tonic-gate 	uint8_t    icmp_ad_num;	/* # of following router addresses */
587c478bd9Sstevel@tonic-gate 	uint8_t    icmp_ad_asize;	/* 2--words in each advertisement */
597c478bd9Sstevel@tonic-gate 	uint16_t   icmp_ad_life;	/* seconds of validity */
607c478bd9Sstevel@tonic-gate 	struct icmp_ad_info {
617c478bd9Sstevel@tonic-gate 	    in_addr_t  icmp_ad_addr;
627c478bd9Sstevel@tonic-gate 	    uint32_t  icmp_ad_pref;
637c478bd9Sstevel@tonic-gate 	} icmp_ad_info[1];
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* router solicitation ICMP packet */
677c478bd9Sstevel@tonic-gate struct icmp_so {
687c478bd9Sstevel@tonic-gate 	uint8_t    icmp_type;		/* type of message */
697c478bd9Sstevel@tonic-gate 	uint8_t    icmp_code;		/* type sub code */
707c478bd9Sstevel@tonic-gate 	uint16_t   icmp_cksum;		/* ones complement cksum of struct */
717c478bd9Sstevel@tonic-gate 	uint32_t   icmp_so_rsvd;
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate union ad_u {
757c478bd9Sstevel@tonic-gate 	struct icmp icmp;
767c478bd9Sstevel@tonic-gate 	struct icmp_ad ad;
777c478bd9Sstevel@tonic-gate 	struct icmp_so so;
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int	rdisc_sock = -1;		/* router-discovery raw socket */
823173664eSapersson int	rdisc_mib_sock = -1;		/* AF_UNIX mib info socket */
837c478bd9Sstevel@tonic-gate static struct interface *rdisc_sock_interface; /* current rdisc interface */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct timeval rdisc_timer;
867c478bd9Sstevel@tonic-gate boolean_t rdisc_ok;				/* using solicited route */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	MAX_ADS		16
897c478bd9Sstevel@tonic-gate int max_ads; /* at least one per interface */
907c478bd9Sstevel@tonic-gate /* accumulated advertisements */
913173664eSapersson static struct dr *cur_drp;
923173664eSapersson struct dr *drs;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * adjust unsigned preference by interface metric,
967c478bd9Sstevel@tonic-gate  * without driving it to infinity
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #define	PREF(p, ifp) ((p) <= (uint32_t)(ifp)->int_metric ? ((p) != 0 ? 1 : 0) \
997c478bd9Sstevel@tonic-gate 	: (p) - ((ifp)->int_metric))
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static void rdisc_sort(void);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef enum { unicast, bcast, mcast } dstaddr_t;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* dump an ICMP Router Discovery Advertisement Message */
1067c478bd9Sstevel@tonic-gate static void
trace_rdisc(const char * act,uint32_t from,uint32_t to,struct interface * ifp,union ad_u * p,uint_t len)1077c478bd9Sstevel@tonic-gate trace_rdisc(const char	*act,
1087c478bd9Sstevel@tonic-gate     uint32_t from,
1097c478bd9Sstevel@tonic-gate     uint32_t to,
1107c478bd9Sstevel@tonic-gate     struct interface *ifp,
1117c478bd9Sstevel@tonic-gate     union ad_u	*p,
1127c478bd9Sstevel@tonic-gate     uint_t len)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	int i;
1157c478bd9Sstevel@tonic-gate 	n_long *wp, *lim;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (!TRACEPACKETS || ftrace == 0)
1197c478bd9Sstevel@tonic-gate 		return;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	lastlog();
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
1247c478bd9Sstevel@tonic-gate 		(void) fprintf(ftrace, "%s Router Ad"
1257c478bd9Sstevel@tonic-gate 		    " from %s to %s via %s life=%d\n",
1267c478bd9Sstevel@tonic-gate 		    act, naddr_ntoa(from), naddr_ntoa(to),
1277c478bd9Sstevel@tonic-gate 		    ifp ? ifp->int_name : "?",
1287c478bd9Sstevel@tonic-gate 		    ntohs(p->ad.icmp_ad_life));
1297c478bd9Sstevel@tonic-gate 		if (!TRACECONTENTS)
1307c478bd9Sstevel@tonic-gate 			return;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
1337c478bd9Sstevel@tonic-gate 		lim = &wp[(len - sizeof (p->ad)) / sizeof (*wp)];
1347c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
1357c478bd9Sstevel@tonic-gate 			(void) fprintf(ftrace, "\t%s preference=%ld",
1367c478bd9Sstevel@tonic-gate 			    naddr_ntoa(wp[0]), (long)ntohl(wp[1]));
1377c478bd9Sstevel@tonic-gate 			wp += p->ad.icmp_ad_asize;
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 		(void) fputc('\n', ftrace);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	} else {
1427c478bd9Sstevel@tonic-gate 		trace_act("%s Router Solic. from %s to %s via %s rsvd=%#x",
1437c478bd9Sstevel@tonic-gate 		    act, naddr_ntoa(from), naddr_ntoa(to),
1447c478bd9Sstevel@tonic-gate 		    ifp ? ifp->int_name : "?",
1457c478bd9Sstevel@tonic-gate 		    ntohl(p->so.icmp_so_rsvd));
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * Prepare Router Discovery socket.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate static void
get_rdisc_sock(void)1537c478bd9Sstevel@tonic-gate get_rdisc_sock(void)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	int on = 1;
1567c478bd9Sstevel@tonic-gate 	unsigned char ttl = 1;
1573173664eSapersson 	struct sockaddr_un laddr;
1583173664eSapersson 	int len;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (rdisc_sock < 0) {
1617c478bd9Sstevel@tonic-gate 		max_ads = MAX_ADS;
1627c478bd9Sstevel@tonic-gate 		drs = rtmalloc(max_ads * sizeof (struct dr), "get_rdisc_sock");
1637c478bd9Sstevel@tonic-gate 		(void) memset(drs, 0, max_ads * sizeof (struct dr));
1647c478bd9Sstevel@tonic-gate 		rdisc_sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
1657c478bd9Sstevel@tonic-gate 		if (rdisc_sock < 0)
1667c478bd9Sstevel@tonic-gate 			BADERR(_B_TRUE, "rdisc_sock = socket()");
1677c478bd9Sstevel@tonic-gate 		fix_sock(rdisc_sock, "rdisc_sock");
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		if (setsockopt(rdisc_sock, IPPROTO_IP, IP_RECVIF, &on,
1707c478bd9Sstevel@tonic-gate 		    sizeof (on)))
1717c478bd9Sstevel@tonic-gate 			BADERR(_B_FALSE, "setsockopt(IP_RECVIF)");
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		if (setsockopt(rdisc_sock, IPPROTO_IP, IP_MULTICAST_TTL,
1747c478bd9Sstevel@tonic-gate 		    &ttl, sizeof (ttl)) < 0)
1757c478bd9Sstevel@tonic-gate 			DBGERR(_B_TRUE,
1767c478bd9Sstevel@tonic-gate 			    "rdisc_sock setsockopt(IP_MULTICAST_TTL)");
1777c478bd9Sstevel@tonic-gate 
1783173664eSapersson 		/*
1793173664eSapersson 		 * On Solaris also open an AF_UNIX socket to
1803173664eSapersson 		 * pass default router information to mib agent
1813173664eSapersson 		 */
1823173664eSapersson 
1833173664eSapersson 		rdisc_mib_sock = socket(AF_UNIX, SOCK_DGRAM, 0);
1843173664eSapersson 		if (rdisc_mib_sock < 0) {
1853173664eSapersson 			BADERR(_B_TRUE, "rdisc_mib_sock = socket()");
1863173664eSapersson 		}
1873173664eSapersson 
1883173664eSapersson 		bzero(&laddr, sizeof (laddr));
1893173664eSapersson 		laddr.sun_family = AF_UNIX;
1903173664eSapersson 
1913173664eSapersson 		(void) strncpy(laddr.sun_path, RDISC_SNMP_SOCKET,
1923173664eSapersson 		    sizeof (laddr.sun_path));
1933173664eSapersson 		len = sizeof (struct sockaddr_un);
1943173664eSapersson 
1953173664eSapersson 		(void) unlink(RDISC_SNMP_SOCKET);
1963173664eSapersson 
1973173664eSapersson 		if (bind(rdisc_mib_sock, (struct sockaddr *)&laddr, len) < 0) {
1983173664eSapersson 			BADERR(_B_TRUE, "bind(rdisc_mib_sock)");
1993173664eSapersson 		}
2003173664eSapersson 
2013173664eSapersson 		if (fcntl(rdisc_mib_sock, F_SETFL, O_NONBLOCK) < 0) {
2023173664eSapersson 			BADERR(_B_TRUE, "rdisc_mib_sock fcntl O_NONBLOCK");
2033173664eSapersson 		}
2043173664eSapersson 
2057c478bd9Sstevel@tonic-gate 		fix_select();
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Pick multicast group for router-discovery socket
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate void
set_rdisc_mg(struct interface * ifp,int on)2147c478bd9Sstevel@tonic-gate set_rdisc_mg(struct interface *ifp,
2157c478bd9Sstevel@tonic-gate     int on)	/* 0=turn it off */
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	struct ip_mreq m;
2187c478bd9Sstevel@tonic-gate 	boolean_t dosupply;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (rdisc_sock < 0) {
2217c478bd9Sstevel@tonic-gate 		/*
2227c478bd9Sstevel@tonic-gate 		 * Create the raw socket so that we can hear at least
2237c478bd9Sstevel@tonic-gate 		 * broadcast router discovery packets.
2247c478bd9Sstevel@tonic-gate 		 */
2257c478bd9Sstevel@tonic-gate 		if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC ||
2267c478bd9Sstevel@tonic-gate 		    !on)
2277c478bd9Sstevel@tonic-gate 			return;
2287c478bd9Sstevel@tonic-gate 		get_rdisc_sock();
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (!(ifp->int_if_flags & IFF_MULTICAST)) {
2327c478bd9Sstevel@tonic-gate 		/* Can't multicast, so no groups could have been joined. */
2337c478bd9Sstevel@tonic-gate 		ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
2347c478bd9Sstevel@tonic-gate 		return;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	dosupply = should_supply(ifp);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	(void) memset(&m, 0, sizeof (m));
2407c478bd9Sstevel@tonic-gate 	m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT) &&
2417c478bd9Sstevel@tonic-gate 	    (ifp->int_dstaddr != 0) ? ifp->int_dstaddr : ifp->int_addr);
2427c478bd9Sstevel@tonic-gate 	if (dosupply || (ifp->int_state & IS_NO_ADV_IN) || !on) {
2437c478bd9Sstevel@tonic-gate 		/* stop listening to advertisements */
2447c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_ALL_HOSTS) {
2457c478bd9Sstevel@tonic-gate 			m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
2467c478bd9Sstevel@tonic-gate 			if (setsockopt(rdisc_sock, IPPROTO_IP,
2477c478bd9Sstevel@tonic-gate 			    IP_DROP_MEMBERSHIP, &m, sizeof (m)) < 0 &&
2487c478bd9Sstevel@tonic-gate 			    errno != EADDRNOTAVAIL && errno != ENOENT)
2497c478bd9Sstevel@tonic-gate 				LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
2507c478bd9Sstevel@tonic-gate 			ifp->int_state &= ~IS_ALL_HOSTS;
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	} else if (!(ifp->int_state & IS_ALL_HOSTS)) {
2547c478bd9Sstevel@tonic-gate 		/* start listening to advertisements */
2557c478bd9Sstevel@tonic-gate 		m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
2567c478bd9Sstevel@tonic-gate 		if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
2577c478bd9Sstevel@tonic-gate 		    &m, sizeof (m)) < 0) {
2587c478bd9Sstevel@tonic-gate 			LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
2597c478bd9Sstevel@tonic-gate 		} else {
2607c478bd9Sstevel@tonic-gate 			ifp->int_state |= IS_ALL_HOSTS;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (!dosupply || (ifp->int_state & IS_NO_ADV_OUT) ||
2657c478bd9Sstevel@tonic-gate 	    !IS_IFF_ROUTING(ifp->int_if_flags) || !on) {
2667c478bd9Sstevel@tonic-gate 		/* stop listening to solicitations */
2677c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_ALL_ROUTERS) {
2687c478bd9Sstevel@tonic-gate 			m.imr_multiaddr.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2697c478bd9Sstevel@tonic-gate 			if (setsockopt(rdisc_sock, IPPROTO_IP,
2707c478bd9Sstevel@tonic-gate 			    IP_DROP_MEMBERSHIP, &m, sizeof (m)) < 0 &&
2717c478bd9Sstevel@tonic-gate 			    errno != EADDRNOTAVAIL && errno != ENOENT)
2727c478bd9Sstevel@tonic-gate 				LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
2737c478bd9Sstevel@tonic-gate 			ifp->int_state &= ~IS_ALL_ROUTERS;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	} else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
2777c478bd9Sstevel@tonic-gate 		/* start hearing solicitations */
2787c478bd9Sstevel@tonic-gate 		m.imr_multiaddr.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2797c478bd9Sstevel@tonic-gate 		if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
2807c478bd9Sstevel@tonic-gate 		    &m, sizeof (m)) < 0) {
2817c478bd9Sstevel@tonic-gate 			LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
2827c478bd9Sstevel@tonic-gate 		} else {
2837c478bd9Sstevel@tonic-gate 			ifp->int_state |= IS_ALL_ROUTERS;
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * start or stop supplying routes to other systems.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate void
set_supplier(void)2937c478bd9Sstevel@tonic-gate set_supplier(void)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	struct interface *ifp;
2967c478bd9Sstevel@tonic-gate 	struct dr *drp;
2977c478bd9Sstevel@tonic-gate 	static boolean_t supplystate = _B_FALSE;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (supplystate == (fwd_interfaces > 1))
3007c478bd9Sstevel@tonic-gate 		return;
3017c478bd9Sstevel@tonic-gate 	supplystate = fwd_interfaces > 1;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	trace_act("%d forwarding interfaces present; becoming %ssupplier",
3047c478bd9Sstevel@tonic-gate 	    fwd_interfaces, supplystate ? "" : "non-");
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (supplystate) {
3077c478bd9Sstevel@tonic-gate 		/* Forget discovered routes. */
3087c478bd9Sstevel@tonic-gate 		for (drp = drs; drp < &drs[max_ads]; drp++) {
3097c478bd9Sstevel@tonic-gate 			drp->dr_recv_pref = DEF_PREFERENCELEVEL;
3107c478bd9Sstevel@tonic-gate 			drp->dr_life = 0;
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		rdisc_age(0);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * Do not start advertising until we have heard some
3167c478bd9Sstevel@tonic-gate 		 * RIP routes.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		/* get rid of any redirects */
3217c478bd9Sstevel@tonic-gate 		del_redirects(0, 0);
3227c478bd9Sstevel@tonic-gate 	} else {
3237c478bd9Sstevel@tonic-gate 		/*
3247c478bd9Sstevel@tonic-gate 		 * Flush out all those advertisements we had sent by sending
3257c478bd9Sstevel@tonic-gate 		 * one with lifetime=0.
3267c478bd9Sstevel@tonic-gate 		 */
3277c478bd9Sstevel@tonic-gate 		rdisc_adv(_B_TRUE);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Switch router discovery multicast groups from soliciting
3327c478bd9Sstevel@tonic-gate 	 * to advertising or back.
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
3357c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_BROKE)
3367c478bd9Sstevel@tonic-gate 			continue;
3377c478bd9Sstevel@tonic-gate 		ifp->int_rdisc_cnt = 0;
3387c478bd9Sstevel@tonic-gate 		ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
3397c478bd9Sstevel@tonic-gate 		ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
3407c478bd9Sstevel@tonic-gate 		set_rdisc_mg(ifp, 1);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * Age discovered routes and find the best one
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate void
rdisc_age(in_addr_t bad_gate)3497c478bd9Sstevel@tonic-gate rdisc_age(in_addr_t bad_gate)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	time_t sec;
3527c478bd9Sstevel@tonic-gate 	struct dr *drp;
3537c478bd9Sstevel@tonic-gate 	struct rt_spare new;
3547c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * If we are being told about a bad router,
3587c478bd9Sstevel@tonic-gate 	 * then age the discovered default route, and if there is
3597c478bd9Sstevel@tonic-gate 	 * no alternative, solicit a replacement.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	if (bad_gate != 0) {
3627c478bd9Sstevel@tonic-gate 		/*
3637c478bd9Sstevel@tonic-gate 		 * Look for the bad discovered default route.
3647c478bd9Sstevel@tonic-gate 		 * Age it and note its interface.
3657c478bd9Sstevel@tonic-gate 		 */
3667c478bd9Sstevel@tonic-gate 		for (drp = drs; drp < &drs[max_ads]; drp++) {
3677c478bd9Sstevel@tonic-gate 			if (drp->dr_ts == 0)
3687c478bd9Sstevel@tonic-gate 				continue;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 			/*
3717c478bd9Sstevel@tonic-gate 			 * When we find the bad router, age the route
3727c478bd9Sstevel@tonic-gate 			 * to at most SUPPLY_INTERVAL.
3737c478bd9Sstevel@tonic-gate 			 * This is contrary to RFC 1256, but defends against
3747c478bd9Sstevel@tonic-gate 			 * black holes.
3757c478bd9Sstevel@tonic-gate 			 */
3767c478bd9Sstevel@tonic-gate 			if (drp->dr_gate == bad_gate) {
3777c478bd9Sstevel@tonic-gate 				sec = (now.tv_sec - drp->dr_life +
3787c478bd9Sstevel@tonic-gate 				    SUPPLY_INTERVAL);
3797c478bd9Sstevel@tonic-gate 				if (drp->dr_ts > sec) {
3807c478bd9Sstevel@tonic-gate 					trace_act("age 0.0.0.0 --> %s via %s",
3817c478bd9Sstevel@tonic-gate 					    naddr_ntoa(drp->dr_gate),
3827c478bd9Sstevel@tonic-gate 					    drp->dr_ifp->int_name);
3837c478bd9Sstevel@tonic-gate 					drp->dr_ts = sec;
3847c478bd9Sstevel@tonic-gate 				}
3857c478bd9Sstevel@tonic-gate 				break;
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	} else if (should_supply(NULL)) {
3897c478bd9Sstevel@tonic-gate 		/*
3907c478bd9Sstevel@tonic-gate 		 * If switching from client to server, get rid of old
3917c478bd9Sstevel@tonic-gate 		 * default routes.
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		if (cur_drp != NULL) {
3947c478bd9Sstevel@tonic-gate 			rt = rtget(RIP_DEFAULT, 0);
3957c478bd9Sstevel@tonic-gate 			/*
3967c478bd9Sstevel@tonic-gate 			 * If there is a current default router, and the
3977c478bd9Sstevel@tonic-gate 			 * there is no rt_spare entry, create one
3987c478bd9Sstevel@tonic-gate 			 * for cur_drp to prevent segmentation fault
3997c478bd9Sstevel@tonic-gate 			 * at rdisc_sort.
4007c478bd9Sstevel@tonic-gate 			 */
4017c478bd9Sstevel@tonic-gate 			if (rt == NULL) {
4027c478bd9Sstevel@tonic-gate 				(void) memset(&new, 0, sizeof (new));
4037c478bd9Sstevel@tonic-gate 				new.rts_ifp = cur_drp->dr_ifp;
4047c478bd9Sstevel@tonic-gate 				new.rts_gate = cur_drp->dr_gate;
4057c478bd9Sstevel@tonic-gate 				new.rts_router = cur_drp->dr_gate;
4067c478bd9Sstevel@tonic-gate 				new.rts_metric = HOPCNT_INFINITY-1;
4077c478bd9Sstevel@tonic-gate 				new.rts_time = now.tv_sec;
4087c478bd9Sstevel@tonic-gate 				new.rts_origin = RO_RDISC;
4097c478bd9Sstevel@tonic-gate 				rtadd(RIP_DEFAULT, 0, RS_NOPROPAGATE, &new);
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			rdisc_sort();
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		rdisc_adv(_B_FALSE);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	rdisc_sol();
4187c478bd9Sstevel@tonic-gate 	if (cur_drp != NULL) {
4197c478bd9Sstevel@tonic-gate 		rt = rtget(RIP_DEFAULT, 0);
4207c478bd9Sstevel@tonic-gate 		if (rt == NULL) {
4217c478bd9Sstevel@tonic-gate 			(void) memset(&new, 0, sizeof (new));
4227c478bd9Sstevel@tonic-gate 			new.rts_ifp = cur_drp->dr_ifp;
4237c478bd9Sstevel@tonic-gate 			new.rts_gate = cur_drp->dr_gate;
4247c478bd9Sstevel@tonic-gate 			new.rts_router = cur_drp->dr_gate;
4257c478bd9Sstevel@tonic-gate 			new.rts_metric = HOPCNT_INFINITY-1;
4267c478bd9Sstevel@tonic-gate 			new.rts_time = now.tv_sec;
4277c478bd9Sstevel@tonic-gate 			new.rts_origin = RO_RDISC;
4287c478bd9Sstevel@tonic-gate 			rtadd(RIP_DEFAULT, 0, RS_NOPROPAGATE, &new);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	rdisc_sort();
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * Delete old redirected routes to keep the kernel table small,
4357c478bd9Sstevel@tonic-gate 	 * and to prevent black holes.  Check that the kernel table
4367c478bd9Sstevel@tonic-gate 	 * matches the daemon table (i.e. has the default route).
4377c478bd9Sstevel@tonic-gate 	 * But only if RIP is not running and we are not dealing with
4387c478bd9Sstevel@tonic-gate 	 * a bad gateway, since otherwise age() will be called.
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	if (rip_sock < 0 && bad_gate == 0)
4417c478bd9Sstevel@tonic-gate 		age(0);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate  * Zap all routes discovered via an interface that has gone bad
4477c478bd9Sstevel@tonic-gate  * This should only be called when !(ifp->int_state & IS_DUP)
4487c478bd9Sstevel@tonic-gate  * This is called by if_del and if_bad, and the interface pointer
4497c478bd9Sstevel@tonic-gate  * might not be valid after this.
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate void
if_bad_rdisc(struct interface * ifp)4527c478bd9Sstevel@tonic-gate if_bad_rdisc(struct interface *ifp)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	struct dr *drp;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	for (drp = drs; drp < &drs[max_ads]; drp++) {
4577c478bd9Sstevel@tonic-gate 		if (drp->dr_ifp != ifp)
4587c478bd9Sstevel@tonic-gate 			continue;
4597c478bd9Sstevel@tonic-gate 		(void) memset(drp, 0, sizeof (*drp));
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/* make a note to re-solicit, turn RIP on or off, etc. */
4637c478bd9Sstevel@tonic-gate 	rdisc_timer.tv_sec = 0;
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * Rewire all routes discovered via an interface that has gone bad
4687c478bd9Sstevel@tonic-gate  * This is only called by if_del.
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate void
if_rewire_rdisc(struct interface * oldifp,struct interface * newifp)4717c478bd9Sstevel@tonic-gate if_rewire_rdisc(struct interface *oldifp, struct interface *newifp)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	struct dr *drp;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	for (drp = drs; drp < &drs[max_ads]; drp++) {
4767c478bd9Sstevel@tonic-gate 		if (drp->dr_ifp != oldifp)
4777c478bd9Sstevel@tonic-gate 			continue;
4787c478bd9Sstevel@tonic-gate 		drp->dr_ifp = newifp;
4797c478bd9Sstevel@tonic-gate 		drp->dr_pref += (newifp->int_metric - oldifp->int_metric);
4807c478bd9Sstevel@tonic-gate 		drp->dr_flags |= DR_CHANGED;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/* make a note to re-solicit, turn RIP on or off, etc. */
4847c478bd9Sstevel@tonic-gate 	rdisc_timer.tv_sec = 0;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * Mark an interface ok for router discovering.
4897c478bd9Sstevel@tonic-gate  * This is called by if_ok and ifinit.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate void
if_ok_rdisc(struct interface * ifp)4927c478bd9Sstevel@tonic-gate if_ok_rdisc(struct interface *ifp)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	set_rdisc_mg(ifp, 1);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	ifp->int_rdisc_cnt = 0;
4977c478bd9Sstevel@tonic-gate 	ifp->int_rdisc_timer.tv_sec = now.tv_sec +
4987c478bd9Sstevel@tonic-gate 	    ((ifp->int_state & IS_NO_ADV_OUT) ?
4997c478bd9Sstevel@tonic-gate 	    MAX_SOLICITATION_DELAY : MIN_WAITTIME);
5007c478bd9Sstevel@tonic-gate 	if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, > /* cstyle */))
5017c478bd9Sstevel@tonic-gate 		rdisc_timer = ifp->int_rdisc_timer;
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * Get rid of a dead discovered router
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate static void
del_rdisc(struct dr * drp)5087c478bd9Sstevel@tonic-gate del_rdisc(struct dr *drp)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	struct interface *ifp;
5117c478bd9Sstevel@tonic-gate 	uint32_t gate;
5127c478bd9Sstevel@tonic-gate 	int i;
5137c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
5147c478bd9Sstevel@tonic-gate 	struct rt_spare *rts = NULL;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	del_redirects(gate = drp->dr_gate, 0);
5177c478bd9Sstevel@tonic-gate 	drp->dr_ts = 0;
5187c478bd9Sstevel@tonic-gate 	drp->dr_life = 0;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	rt = rtget(RIP_DEFAULT, 0);
5217c478bd9Sstevel@tonic-gate 	if (rt == NULL) {
5227c478bd9Sstevel@tonic-gate 		trace_act("could not find default route in table");
5237c478bd9Sstevel@tonic-gate 	} else {
5247c478bd9Sstevel@tonic-gate 		for (i = 0; i < rt->rt_num_spares; i++) {
5257c478bd9Sstevel@tonic-gate 			if ((rt->rt_spares[i].rts_gate == drp->dr_gate) &&
5267c478bd9Sstevel@tonic-gate 			    (rt->rt_spares[i].rts_origin == RO_RDISC)) {
5277c478bd9Sstevel@tonic-gate 				rts = &rt->rt_spares[i];
5287c478bd9Sstevel@tonic-gate 				break;
5297c478bd9Sstevel@tonic-gate 			}
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		if (rts != NULL)
5327c478bd9Sstevel@tonic-gate 			rts_delete(rt, rts);
5337c478bd9Sstevel@tonic-gate 		else
5347c478bd9Sstevel@tonic-gate 			trace_act("could not find default route "
5357c478bd9Sstevel@tonic-gate 			    "through %s in table", naddr_ntoa(drp->dr_gate));
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/* Count the other discovered routers on the interface.  */
5397c478bd9Sstevel@tonic-gate 	i = 0;
5407c478bd9Sstevel@tonic-gate 	ifp = drp->dr_ifp;
5417c478bd9Sstevel@tonic-gate 	for (drp = drs; drp < &drs[max_ads]; drp++) {
5427c478bd9Sstevel@tonic-gate 		if (drp->dr_ts != 0 && drp->dr_ifp == ifp)
5437c478bd9Sstevel@tonic-gate 			i++;
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/*
5477c478bd9Sstevel@tonic-gate 	 * If that was the last good discovered router on the interface,
5487c478bd9Sstevel@tonic-gate 	 * then solicit a new one.
5497c478bd9Sstevel@tonic-gate 	 * This is contrary to RFC 1256, but defends against black holes.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	if (i != 0) {
5527c478bd9Sstevel@tonic-gate 		trace_act("discovered router %s via %s"
5537c478bd9Sstevel@tonic-gate 		    " is bad--have %d remaining",
5547c478bd9Sstevel@tonic-gate 		    naddr_ntoa(gate), ifp->int_name, i);
5557c478bd9Sstevel@tonic-gate 	} else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
5567c478bd9Sstevel@tonic-gate 		trace_act("last discovered router %s via %s"
5577c478bd9Sstevel@tonic-gate 		    " is bad--re-solicit",
5587c478bd9Sstevel@tonic-gate 		    naddr_ntoa(gate), ifp->int_name);
5597c478bd9Sstevel@tonic-gate 		ifp->int_rdisc_cnt = 0;
5607c478bd9Sstevel@tonic-gate 		ifp->int_rdisc_timer.tv_sec = 0;
5617c478bd9Sstevel@tonic-gate 		rdisc_sol();
5627c478bd9Sstevel@tonic-gate 	} else {
5637c478bd9Sstevel@tonic-gate 		trace_act("last discovered router %s via %s"
5647c478bd9Sstevel@tonic-gate 		    " is bad--wait to solicit",
5657c478bd9Sstevel@tonic-gate 		    naddr_ntoa(gate), ifp->int_name);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /* Find the best discovered route, and discard stale routers. */
5717c478bd9Sstevel@tonic-gate static void
rdisc_sort(void)5727c478bd9Sstevel@tonic-gate rdisc_sort(void)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	struct dr *drp, *new_drp;
5757c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
5767c478bd9Sstevel@tonic-gate 	struct rt_spare new, *rts;
5777c478bd9Sstevel@tonic-gate 	struct interface *ifp;
5787c478bd9Sstevel@tonic-gate 	uint_t new_st = 0;
5797c478bd9Sstevel@tonic-gate 	uint32_t new_pref = DEF_PREFERENCELEVEL;
5807c478bd9Sstevel@tonic-gate 	int first_rdisc_slot = 0;
5817c478bd9Sstevel@tonic-gate 	int j;
5827c478bd9Sstevel@tonic-gate 	boolean_t spares_avail;
5837c478bd9Sstevel@tonic-gate 	void *ptr;
5847c478bd9Sstevel@tonic-gate 	size_t ptrsize;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	rt = rtget(RIP_DEFAULT, 0);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * If all the rt_spare entries are taken up with with default routes
5907c478bd9Sstevel@tonic-gate 	 * learnt from RIP (ie rts_origin = RO_RIP), bail out.
5917c478bd9Sstevel@tonic-gate 	 * NOTE:
5927c478bd9Sstevel@tonic-gate 	 *	We *always* prefer default routes learned via RIP
5937c478bd9Sstevel@tonic-gate 	 *	(ie RO_RIP) over those learnt via RDISC (ie RO_RDISC).
5947c478bd9Sstevel@tonic-gate 	 *	The rdisc machinery should not modify, replace or
5957c478bd9Sstevel@tonic-gate 	 *	remove any existing default routes with RO_RIP set.
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	if (rt != NULL) {
5987c478bd9Sstevel@tonic-gate 		spares_avail = _B_FALSE;
5997c478bd9Sstevel@tonic-gate 		for (j = 0; j < rt->rt_num_spares; j++)  {
6007c478bd9Sstevel@tonic-gate 			rts = &rt->rt_spares[j];
60109f979dcSsowmini 			if (rts->rts_gate == 0 || rts->rts_origin != RO_RIP ||
60209f979dcSsowmini 			    rts->rts_ifp == &dummy_ifp) {
6037c478bd9Sstevel@tonic-gate 				spares_avail = _B_TRUE;
6047c478bd9Sstevel@tonic-gate 				break;
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		if (!spares_avail) {
6087c478bd9Sstevel@tonic-gate 			ptrsize = (rt->rt_num_spares + SPARE_INC) *
6097c478bd9Sstevel@tonic-gate 			    sizeof (struct rt_spare);
6107c478bd9Sstevel@tonic-gate 			ptr = realloc(rt->rt_spares, ptrsize);
6117c478bd9Sstevel@tonic-gate 			if (ptr != NULL) {
6127c478bd9Sstevel@tonic-gate 				struct rt_spare *tmprts;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 				rt->rt_spares = ptr;
6157c478bd9Sstevel@tonic-gate 				rts = &rt->rt_spares[rt->rt_num_spares];
6167c478bd9Sstevel@tonic-gate 				(void) memset(rts, 0,
6177c478bd9Sstevel@tonic-gate 				    (SPARE_INC * sizeof (struct rt_spare)));
6187c478bd9Sstevel@tonic-gate 				rt->rt_num_spares += SPARE_INC;
6197c478bd9Sstevel@tonic-gate 				for (tmprts = rts, j = SPARE_INC;
6207c478bd9Sstevel@tonic-gate 				    j != 0; j--, tmprts++)
6217c478bd9Sstevel@tonic-gate 					tmprts->rts_metric = HOPCNT_INFINITY;
6227c478bd9Sstevel@tonic-gate 				spares_avail = _B_TRUE;
6237c478bd9Sstevel@tonic-gate 			} else {
6247c478bd9Sstevel@tonic-gate 				return;
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	/* Find the best RDISC advertiser */
6297c478bd9Sstevel@tonic-gate 	rt = NULL;
6307c478bd9Sstevel@tonic-gate 	new_drp = NULL;
6317c478bd9Sstevel@tonic-gate 	for (drp = drs; drp < &drs[max_ads]; drp++) {
6327c478bd9Sstevel@tonic-gate 		if (drp->dr_ts == 0)
6337c478bd9Sstevel@tonic-gate 			continue;
6347c478bd9Sstevel@tonic-gate 		ifp = drp->dr_ifp;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		/* Get rid of expired discovered routers. */
6377c478bd9Sstevel@tonic-gate 		if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
6387c478bd9Sstevel@tonic-gate 			del_rdisc(drp);
6397c478bd9Sstevel@tonic-gate 			continue;
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		/*
6457c478bd9Sstevel@tonic-gate 		 * Update preference with possibly changed interface
6467c478bd9Sstevel@tonic-gate 		 * metric.
6477c478bd9Sstevel@tonic-gate 		 */
6487c478bd9Sstevel@tonic-gate 		drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		/*
6517c478bd9Sstevel@tonic-gate 		 * Prefer the current route to prevent thrashing.
6527c478bd9Sstevel@tonic-gate 		 * Prefer shorter lifetimes to speed the detection of
6537c478bd9Sstevel@tonic-gate 		 * bad routers.
6547c478bd9Sstevel@tonic-gate 		 * Avoid sick interfaces.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		if (new_drp == NULL ||
6577c478bd9Sstevel@tonic-gate 		    (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK) &&
6587c478bd9Sstevel@tonic-gate 		    (new_pref < drp->dr_pref ||
6597c478bd9Sstevel@tonic-gate 		    (new_pref == drp->dr_pref && (drp == cur_drp ||
6607c478bd9Sstevel@tonic-gate 		    (new_drp != cur_drp &&
6617c478bd9Sstevel@tonic-gate 		    new_drp->dr_life > drp->dr_life))))) ||
6627c478bd9Sstevel@tonic-gate 		    ((new_st & IS_SICK) &&
6637c478bd9Sstevel@tonic-gate 		    !(drp->dr_ifp->int_state & IS_SICK))) {
664*e704a8f2Smeem 			new_drp = drp;
665*e704a8f2Smeem 			new_st = drp->dr_ifp->int_state;
666*e704a8f2Smeem 			new_pref = drp->dr_pref;
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * switch to a better RDISC advertiser
6727c478bd9Sstevel@tonic-gate 	 */
6737c478bd9Sstevel@tonic-gate 	if ((new_drp != cur_drp) || (rt == NULL))  {
6747c478bd9Sstevel@tonic-gate 		rt = rtget(RIP_DEFAULT, 0);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		/*
6777c478bd9Sstevel@tonic-gate 		 * Purge the table of all the default routes that were
6787c478bd9Sstevel@tonic-gate 		 * learnt via RDISC, while keeping an eye the first available
6797c478bd9Sstevel@tonic-gate 		 * slot for the spare entry of new_drp
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		if (rt != NULL) {
6827c478bd9Sstevel@tonic-gate 			int i;
6837c478bd9Sstevel@tonic-gate 			for (i = 0; i < rt->rt_num_spares; i++)  {
6847c478bd9Sstevel@tonic-gate 				rts = &rt->rt_spares[i];
68509f979dcSsowmini 				if ((rts->rts_gate == 0 ||
68609f979dcSsowmini 				    rts->rts_ifp == &dummy_ifp) &&
68709f979dcSsowmini 				    first_rdisc_slot == 0)
6887c478bd9Sstevel@tonic-gate 					first_rdisc_slot = i;
6897c478bd9Sstevel@tonic-gate 				if (rts->rts_origin == RO_RDISC) {
6907c478bd9Sstevel@tonic-gate 					rts_delete(rt, rts);
6917c478bd9Sstevel@tonic-gate 					if (first_rdisc_slot == 0) {
6927c478bd9Sstevel@tonic-gate 						first_rdisc_slot = i;
6937c478bd9Sstevel@tonic-gate 					}
6947c478bd9Sstevel@tonic-gate 				}
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		/* Stop using RDISC routes if they are all bad */
6997c478bd9Sstevel@tonic-gate 		if (new_drp == NULL) {
7007c478bd9Sstevel@tonic-gate 			trace_act("turn off Router Discovery client");
7017c478bd9Sstevel@tonic-gate 			rdisc_ok = _B_FALSE;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		} else {
7047c478bd9Sstevel@tonic-gate 			if (cur_drp == NULL) {
7057c478bd9Sstevel@tonic-gate 				trace_act("turn on Router Discovery client"
7067c478bd9Sstevel@tonic-gate 				    " using %s via %s",
7077c478bd9Sstevel@tonic-gate 				    naddr_ntoa(new_drp->dr_gate),
7087c478bd9Sstevel@tonic-gate 				    new_drp->dr_ifp->int_name);
7097c478bd9Sstevel@tonic-gate 				rdisc_ok = _B_TRUE;
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 			/* Prepare a spare entry for the new_drp */
7137c478bd9Sstevel@tonic-gate 			(void) memset(&new, 0, sizeof (new));
7147c478bd9Sstevel@tonic-gate 			new.rts_ifp = new_drp->dr_ifp;
7157c478bd9Sstevel@tonic-gate 			new.rts_gate = new_drp->dr_gate;
7167c478bd9Sstevel@tonic-gate 			new.rts_router = new_drp->dr_gate;
7177c478bd9Sstevel@tonic-gate 			new.rts_metric = HOPCNT_INFINITY-1;
7187c478bd9Sstevel@tonic-gate 			new.rts_time = now.tv_sec;
7197c478bd9Sstevel@tonic-gate 			new.rts_origin = RO_RDISC;
7207c478bd9Sstevel@tonic-gate 			/*
7217c478bd9Sstevel@tonic-gate 			 * If there is no existing default route, add it
7227c478bd9Sstevel@tonic-gate 			 * to rts_spare[0].
7237c478bd9Sstevel@tonic-gate 			 */
7247c478bd9Sstevel@tonic-gate 			if (rt == NULL) {
7257c478bd9Sstevel@tonic-gate 				rtadd(RIP_DEFAULT, 0, RS_NOPROPAGATE, &new);
7267c478bd9Sstevel@tonic-gate 			} else {
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 				/*
7297c478bd9Sstevel@tonic-gate 				 * Add the spare entry for the new_drp in
7307c478bd9Sstevel@tonic-gate 				 * the first available slot
7317c478bd9Sstevel@tonic-gate 				 */
7327c478bd9Sstevel@tonic-gate 				trace_act("Switching to "
7337c478bd9Sstevel@tonic-gate 				    "default router with better "
7347c478bd9Sstevel@tonic-gate 				    "preference %s via %s ",
7357c478bd9Sstevel@tonic-gate 				    naddr_ntoa(new_drp->dr_gate),
7367c478bd9Sstevel@tonic-gate 				    new_drp->dr_ifp->int_name);
7377c478bd9Sstevel@tonic-gate 				rt->rt_spares[first_rdisc_slot] = new;
7387c478bd9Sstevel@tonic-gate 				rt = NULL; /* redo rt_spares */
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * Get ready to redo the entire table. The table should
7447c478bd9Sstevel@tonic-gate 		 * only include :
7457c478bd9Sstevel@tonic-gate 		 * 	a. empty rt_spare slots
7467c478bd9Sstevel@tonic-gate 		 * 	b. default routes learnt via RIP
7477c478bd9Sstevel@tonic-gate 		 * 	c. default route for the latest best RDISC advertiser
7487c478bd9Sstevel@tonic-gate 		 * 	d. default routes of other RDISC advertisers whose
7497c478bd9Sstevel@tonic-gate 		 *	dr_pref == best RDISC advertiser->dr_pref
7507c478bd9Sstevel@tonic-gate 		 */
7517c478bd9Sstevel@tonic-gate 		cur_drp = new_drp;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/* Redo the entire spare table (without touching RO_RIP entries) */
7557c478bd9Sstevel@tonic-gate 	if (rdisc_ok && rt == NULL) {
7567c478bd9Sstevel@tonic-gate 		int i;
7577c478bd9Sstevel@tonic-gate 		/*
7587c478bd9Sstevel@tonic-gate 		 * We've either just turned on router discovery,
7597c478bd9Sstevel@tonic-gate 		 * or switched to a router with better preference.
7607c478bd9Sstevel@tonic-gate 		 * Find all other default routers whose
7617c478bd9Sstevel@tonic-gate 		 * pref == cur_drp->dr_pref and add them as spares
7627c478bd9Sstevel@tonic-gate 		 */
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		rt = rtget(RIP_DEFAULT, 0);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		for (drp = drs; drp < &drs[max_ads]; drp++) {
7677c478bd9Sstevel@tonic-gate 			boolean_t dr_done = _B_FALSE;
7687c478bd9Sstevel@tonic-gate 			int slot = -1;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 			if (drp->dr_ts == 0)
7717c478bd9Sstevel@tonic-gate 				continue;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 			if (drp->dr_pref != cur_drp->dr_pref &&
7747c478bd9Sstevel@tonic-gate 			    ((drp->dr_flags & DR_CHANGED) == 0))
7757c478bd9Sstevel@tonic-gate 				continue;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 			/*
7787c478bd9Sstevel@tonic-gate 			 * Either pref matches cur_drp->dr_pref,
7797c478bd9Sstevel@tonic-gate 			 * or something has changed in this drp.
7807c478bd9Sstevel@tonic-gate 			 * In the former case, we may need to add
7817c478bd9Sstevel@tonic-gate 			 * this to rt_spares. In the latter case,
7827c478bd9Sstevel@tonic-gate 			 * if the pref has changed, need to take it
7837c478bd9Sstevel@tonic-gate 			 * out of rt_spares and the kernel.
7847c478bd9Sstevel@tonic-gate 			 *
7857c478bd9Sstevel@tonic-gate 			 * First, find an empty slot in rt_spares
7867c478bd9Sstevel@tonic-gate 			 * in case we have to add this drp to kernel.
7877c478bd9Sstevel@tonic-gate 			 * Also check if it is already there.
7887c478bd9Sstevel@tonic-gate 			 */
7897c478bd9Sstevel@tonic-gate 			for (i = 0; i < rt->rt_num_spares; i++) {
7907c478bd9Sstevel@tonic-gate 				if (rt->rt_spares[i].rts_gate == 0) {
7917c478bd9Sstevel@tonic-gate 					if (slot < 0)
7927c478bd9Sstevel@tonic-gate 						slot = i;
7937c478bd9Sstevel@tonic-gate 					continue;
7947c478bd9Sstevel@tonic-gate 				}
7957c478bd9Sstevel@tonic-gate 				if ((rt->rt_spares[i].rts_gate ==
7967c478bd9Sstevel@tonic-gate 				    drp->dr_gate) &&
7977c478bd9Sstevel@tonic-gate 				    (rt->rt_spares[i].rts_origin ==
7987c478bd9Sstevel@tonic-gate 				    RO_RDISC)) {
7997c478bd9Sstevel@tonic-gate 					/*
8007c478bd9Sstevel@tonic-gate 					 * a spare entry for this RDISC
8017c478bd9Sstevel@tonic-gate 					 * advertiser already exists. We need
8027c478bd9Sstevel@tonic-gate 					 * to check if this entry still belongs
8037c478bd9Sstevel@tonic-gate 					 * in the table
8047c478bd9Sstevel@tonic-gate 					 */
8057c478bd9Sstevel@tonic-gate 					dr_done = _B_TRUE;
8067c478bd9Sstevel@tonic-gate 					break;
8077c478bd9Sstevel@tonic-gate 				}
8087c478bd9Sstevel@tonic-gate 			}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 			drp->dr_flags &= ~DR_CHANGED;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 			if (drp->dr_pref != cur_drp->dr_pref) {
8137c478bd9Sstevel@tonic-gate 				if (dr_done) {
8147c478bd9Sstevel@tonic-gate 					/*
8157c478bd9Sstevel@tonic-gate 					 * The rt_spare of this RDISC advertiser
8167c478bd9Sstevel@tonic-gate 					 * needs to be removed as it no longer
8177c478bd9Sstevel@tonic-gate 					 * belongs in the table because its
8187c478bd9Sstevel@tonic-gate 					 * dr_pref is different than the latest
8197c478bd9Sstevel@tonic-gate 					 * RDISC advertiser's->dr_pref
8207c478bd9Sstevel@tonic-gate 					 */
8217c478bd9Sstevel@tonic-gate 					rts_delete(rt, &rt->rt_spares[i]);
8227c478bd9Sstevel@tonic-gate 				}
8237c478bd9Sstevel@tonic-gate 				continue;
8247c478bd9Sstevel@tonic-gate 			}
8257c478bd9Sstevel@tonic-gate 
82609f979dcSsowmini 			if (slot < 0 && !dr_done)  {
8277c478bd9Sstevel@tonic-gate 				ptrsize = (rt->rt_num_spares + SPARE_INC) *
8287c478bd9Sstevel@tonic-gate 				    sizeof (struct rt_spare);
8297c478bd9Sstevel@tonic-gate 				ptr = realloc(rt->rt_spares, ptrsize);
8307c478bd9Sstevel@tonic-gate 				if (ptr != NULL) {
8317c478bd9Sstevel@tonic-gate 					struct rt_spare *tmprts;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 					rt->rt_spares = ptr;
8347c478bd9Sstevel@tonic-gate 					slot = rt->rt_num_spares;
8357c478bd9Sstevel@tonic-gate 					rts = &rt->rt_spares[rt->rt_num_spares];
8367c478bd9Sstevel@tonic-gate 					(void) memset(rts, 0, (SPARE_INC *
8377c478bd9Sstevel@tonic-gate 					    sizeof (struct rt_spare)));
8387c478bd9Sstevel@tonic-gate 					rt->rt_num_spares += SPARE_INC;
8397c478bd9Sstevel@tonic-gate 					for (tmprts = rts, i = SPARE_INC;
8407c478bd9Sstevel@tonic-gate 					    i != 0; i--, tmprts++)
8417c478bd9Sstevel@tonic-gate 						tmprts->rts_metric =
8427c478bd9Sstevel@tonic-gate 						    HOPCNT_INFINITY;
8437c478bd9Sstevel@tonic-gate 				}
8447c478bd9Sstevel@tonic-gate 			}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			if (slot >= 0 && (dr_done != _B_TRUE)) {
8477c478bd9Sstevel@tonic-gate 				(void) memset(&new, 0, sizeof (new));
8487c478bd9Sstevel@tonic-gate 				new.rts_ifp = drp->dr_ifp;
8497c478bd9Sstevel@tonic-gate 				new.rts_gate = drp->dr_gate;
8507c478bd9Sstevel@tonic-gate 				new.rts_router = drp->dr_gate;
8517c478bd9Sstevel@tonic-gate 				new.rts_metric = HOPCNT_INFINITY-1;
8527c478bd9Sstevel@tonic-gate 				new.rts_time = now.tv_sec;
8537c478bd9Sstevel@tonic-gate 				new.rts_origin = RO_RDISC;
8547c478bd9Sstevel@tonic-gate 				rt->rt_spares[slot] = new;
8557c478bd9Sstevel@tonic-gate 				trace_act("spare default %s via %s",
8567c478bd9Sstevel@tonic-gate 				    naddr_ntoa(drp->dr_gate),
8577c478bd9Sstevel@tonic-gate 				    drp->dr_ifp->int_name);
8587c478bd9Sstevel@tonic-gate 			}
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/* turn RIP on or off */
8637c478bd9Sstevel@tonic-gate 	if (!rdisc_ok || rip_interfaces > 1) {
8647c478bd9Sstevel@tonic-gate 		rip_on(0);
8657c478bd9Sstevel@tonic-gate 	} else {
8667c478bd9Sstevel@tonic-gate 		rip_off();
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate /* Handle a single address in an advertisement */
8727c478bd9Sstevel@tonic-gate static void
parse_ad(uint32_t from,in_addr_t gate,uint32_t pref,ushort_t life,struct interface * ifp)8737c478bd9Sstevel@tonic-gate parse_ad(uint32_t from,
8747c478bd9Sstevel@tonic-gate     in_addr_t gate,
8757c478bd9Sstevel@tonic-gate     uint32_t pref,		/* signed and in network order */
8767c478bd9Sstevel@tonic-gate     ushort_t life,		/* in host byte order */
8777c478bd9Sstevel@tonic-gate     struct interface *ifp)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	static struct msg_limit bad_gate;
8807c478bd9Sstevel@tonic-gate 	struct dr *drp, *new_drp;
8817c478bd9Sstevel@tonic-gate 	void *ptr;
8827c478bd9Sstevel@tonic-gate 	size_t ptrsize;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if (gate == RIP_DEFAULT || !check_dst(gate)) {
8857c478bd9Sstevel@tonic-gate 		msglim(&bad_gate, from, "router %s advertising bad gateway %s",
8867c478bd9Sstevel@tonic-gate 		    naddr_ntoa(from), naddr_ntoa(gate));
8877c478bd9Sstevel@tonic-gate 		return;
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * ignore pointers to ourself and routes via unreachable networks
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	if (ifwithaddr(gate, _B_TRUE, _B_FALSE) != 0) {
8947c478bd9Sstevel@tonic-gate 		trace_pkt("    discard Router Discovery Ad pointing at us");
8957c478bd9Sstevel@tonic-gate 		return;
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
8987c478bd9Sstevel@tonic-gate 		trace_pkt("    discard Router Discovery Ad"
8997c478bd9Sstevel@tonic-gate 		    " toward unreachable net");
9007c478bd9Sstevel@tonic-gate 		return;
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * Convert preference to an unsigned value
9047c478bd9Sstevel@tonic-gate 	 * and later bias it by the metric of the interface.
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 	pref = UNSIGN_PREF(ntohl(pref));
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	if (pref == DEF_PREFERENCELEVEL || life < MIN_MAXADVERTISEINTERVAL) {
9097c478bd9Sstevel@tonic-gate 		pref = DEF_PREFERENCELEVEL;
9107c478bd9Sstevel@tonic-gate 		life = 0;
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	for (new_drp = NULL, drp = drs; drp < &drs[max_ads]; drp++) {
9147c478bd9Sstevel@tonic-gate 		/* accept new info for a familiar entry */
9157c478bd9Sstevel@tonic-gate 		if ((drp->dr_gate == gate) && (drp->dr_ifp == ifp)) {
9167c478bd9Sstevel@tonic-gate 			new_drp = drp;
9177c478bd9Sstevel@tonic-gate 			drp->dr_flags |= DR_CHANGED;
9187c478bd9Sstevel@tonic-gate 			break;
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		if (life == 0)
9227c478bd9Sstevel@tonic-gate 			continue;	/* do not worry about dead ads */
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		if (drp->dr_ts == 0) {
9257c478bd9Sstevel@tonic-gate 			new_drp = drp;	/* use unused entry */
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		} else if (new_drp == NULL) {
9287c478bd9Sstevel@tonic-gate 			/* look for an entry worse than the new one to reuse. */
9297c478bd9Sstevel@tonic-gate 			if ((!(ifp->int_state & IS_SICK) &&
9307c478bd9Sstevel@tonic-gate 			    (drp->dr_ifp->int_state & IS_SICK)) ||
9317c478bd9Sstevel@tonic-gate 			    (pref > drp->dr_pref &&
9327c478bd9Sstevel@tonic-gate 			    !((ifp->int_state ^ drp->dr_ifp->int_state) &
9337c478bd9Sstevel@tonic-gate 			    IS_SICK)))
9347c478bd9Sstevel@tonic-gate 				new_drp = drp;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 		} else if (new_drp->dr_ts != 0) {
9377c478bd9Sstevel@tonic-gate 			/* look for the least valuable entry to reuse */
9387c478bd9Sstevel@tonic-gate 			if ((!(new_drp->dr_ifp->int_state & IS_SICK) &&
9397c478bd9Sstevel@tonic-gate 			    (drp->dr_ifp->int_state & IS_SICK)) ||
9407c478bd9Sstevel@tonic-gate 			    (new_drp->dr_pref > drp->dr_pref &&
9417c478bd9Sstevel@tonic-gate 			    !((new_drp->dr_ifp->int_state ^
9427c478bd9Sstevel@tonic-gate 			    drp->dr_ifp->int_state) & IS_SICK)))
9437c478bd9Sstevel@tonic-gate 				new_drp = drp;
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/* if all of the current entries are better, add more drs[] */
9487c478bd9Sstevel@tonic-gate 	if (new_drp == NULL) {
9497c478bd9Sstevel@tonic-gate 		ptrsize = (max_ads + MAX_ADS) * sizeof (struct dr);
9507c478bd9Sstevel@tonic-gate 		ptr = realloc(drs, ptrsize);
9517c478bd9Sstevel@tonic-gate 		if (ptr == NULL)
9527c478bd9Sstevel@tonic-gate 			return;
9537c478bd9Sstevel@tonic-gate 		drs = ptr;
9547c478bd9Sstevel@tonic-gate 		(void) memset(&drs[max_ads], 0, MAX_ADS * sizeof (struct dr));
9557c478bd9Sstevel@tonic-gate 		new_drp = &drs[max_ads];
9567c478bd9Sstevel@tonic-gate 		max_ads += MAX_ADS;
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/*
9607c478bd9Sstevel@tonic-gate 	 * Pointer copy is safe here because if_del
9617c478bd9Sstevel@tonic-gate 	 * calls if_bad_rdisc first, so a non-NULL df_ifp
9627c478bd9Sstevel@tonic-gate 	 * is always a valid pointer.
9637c478bd9Sstevel@tonic-gate 	 */
9647c478bd9Sstevel@tonic-gate 	new_drp->dr_ifp = ifp;
9657c478bd9Sstevel@tonic-gate 	new_drp->dr_gate = gate;
9667c478bd9Sstevel@tonic-gate 	new_drp->dr_ts = now.tv_sec;
9677c478bd9Sstevel@tonic-gate 	new_drp->dr_life = life;
9687c478bd9Sstevel@tonic-gate 	new_drp->dr_recv_pref = pref;
9697c478bd9Sstevel@tonic-gate 	/* bias functional preference by metric of the interface */
9707c478bd9Sstevel@tonic-gate 	new_drp->dr_pref = PREF(pref, ifp);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/* after hearing a good advertisement, stop asking */
9737c478bd9Sstevel@tonic-gate 	if (!(ifp->int_state & IS_SICK))
9747c478bd9Sstevel@tonic-gate 		ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate /*
9797c478bd9Sstevel@tonic-gate  * Compute the IP checksum. This assumes the packet is less than 32K long.
9807c478bd9Sstevel@tonic-gate  */
9817c478bd9Sstevel@tonic-gate static uint16_t
in_cksum(uint16_t * p,uint_t len)9827c478bd9Sstevel@tonic-gate in_cksum(uint16_t *p, uint_t len)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	uint32_t sum = 0;
9857c478bd9Sstevel@tonic-gate 	int nwords = len >> 1;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	while (nwords-- != 0)
9887c478bd9Sstevel@tonic-gate 		sum += *p++;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (len & 1)
9917c478bd9Sstevel@tonic-gate 		sum += *(uchar_t *)p;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/* end-around-carry */
9947c478bd9Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);
9957c478bd9Sstevel@tonic-gate 	sum += (sum >> 16);
9967c478bd9Sstevel@tonic-gate 	return (~sum);
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate /* Send a router discovery advertisement or solicitation ICMP packet. */
10017c478bd9Sstevel@tonic-gate static void
send_rdisc(union ad_u * p,uint_t p_size,struct interface * ifp,in_addr_t dst,dstaddr_t type)10027c478bd9Sstevel@tonic-gate send_rdisc(union ad_u *p,
10037c478bd9Sstevel@tonic-gate     uint_t p_size,
10047c478bd9Sstevel@tonic-gate     struct interface *ifp,
10057c478bd9Sstevel@tonic-gate     in_addr_t dst,		/* 0 or unicast destination */
10067c478bd9Sstevel@tonic-gate     dstaddr_t type)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
10097c478bd9Sstevel@tonic-gate 	int flags = 0;
10107c478bd9Sstevel@tonic-gate 	const char *msg;
1011*e704a8f2Smeem 	int ifindex = 0;
10127c478bd9Sstevel@tonic-gate 	struct in_addr addr;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/*
10157c478bd9Sstevel@tonic-gate 	 * Don't send Rdisc packets on duplicate interfaces, we
10167c478bd9Sstevel@tonic-gate 	 * don't want to generate duplicate packets.
10177c478bd9Sstevel@tonic-gate 	 */
10187c478bd9Sstevel@tonic-gate 	if (ifp->int_state & IS_DUP)
10197c478bd9Sstevel@tonic-gate 		return;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	(void) memset(&sin, 0, sizeof (sin));
10227c478bd9Sstevel@tonic-gate 	sin.sin_addr.s_addr = dst;
10237c478bd9Sstevel@tonic-gate 	sin.sin_family = AF_INET;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	switch (type) {
10267c478bd9Sstevel@tonic-gate 	case unicast:				/* unicast */
10277c478bd9Sstevel@tonic-gate 	default:
10287c478bd9Sstevel@tonic-gate 		flags = MSG_DONTROUTE;
10297c478bd9Sstevel@tonic-gate 		msg = "Send";
10307c478bd9Sstevel@tonic-gate 		break;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	case bcast:				/* broadcast */
10337c478bd9Sstevel@tonic-gate 		if (ifp->int_if_flags & IFF_POINTOPOINT) {
10347c478bd9Sstevel@tonic-gate 			msg = "Send pt-to-pt";
10357c478bd9Sstevel@tonic-gate 			if (ifp->int_dstaddr == 0)
10367c478bd9Sstevel@tonic-gate 				sin.sin_addr.s_addr = htonl(INADDR_BROADCAST);
10377c478bd9Sstevel@tonic-gate 			else
10387c478bd9Sstevel@tonic-gate 				sin.sin_addr.s_addr = ifp->int_dstaddr;
10397c478bd9Sstevel@tonic-gate 		} else {
10407c478bd9Sstevel@tonic-gate 			msg = "Send broadcast";
10417c478bd9Sstevel@tonic-gate 			sin.sin_addr.s_addr = ifp->int_brdaddr;
10427c478bd9Sstevel@tonic-gate 		}
10437c478bd9Sstevel@tonic-gate 		break;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	case mcast:				/* multicast */
10467c478bd9Sstevel@tonic-gate 		msg = "Send multicast";
10477c478bd9Sstevel@tonic-gate 		break;
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (rdisc_sock < 0)
10517c478bd9Sstevel@tonic-gate 		get_rdisc_sock();
10527c478bd9Sstevel@tonic-gate 
1053*e704a8f2Smeem 	/* select the right interface. */
1054*e704a8f2Smeem 	ifindex = (type != mcast && ifp->int_phys != NULL) ?
1055*e704a8f2Smeem 	    ifp->int_phys->phyi_index : 0;
1056*e704a8f2Smeem 
10577c478bd9Sstevel@tonic-gate 	if (rdisc_sock_interface != ifp) {
10587c478bd9Sstevel@tonic-gate 		/*
10597c478bd9Sstevel@tonic-gate 		 * For multicast, we have to choose the source
10607c478bd9Sstevel@tonic-gate 		 * address.  This is either the local address
10617c478bd9Sstevel@tonic-gate 		 * (non-point-to-point) or the remote address.
10627c478bd9Sstevel@tonic-gate 		 */
10637c478bd9Sstevel@tonic-gate 		addr.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
10647c478bd9Sstevel@tonic-gate 		    ifp->int_dstaddr : ifp->int_addr;
10657c478bd9Sstevel@tonic-gate 		if (type == mcast &&
10667c478bd9Sstevel@tonic-gate 		    setsockopt(rdisc_sock, IPPROTO_IP, IP_MULTICAST_IF, &addr,
1067*e704a8f2Smeem 		    sizeof (addr)) == -1) {
10687c478bd9Sstevel@tonic-gate 			LOGERR("setsockopt(rdisc_sock, IP_MULTICAST_IF)");
10697c478bd9Sstevel@tonic-gate 			return;
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 		rdisc_sock_interface = ifp;
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	trace_rdisc(msg, ifp->int_addr, sin.sin_addr.s_addr, ifp, p, p_size);
10757c478bd9Sstevel@tonic-gate 
1076*e704a8f2Smeem 	if (0 > sendtoif(rdisc_sock, p, p_size, flags, &sin, ifindex)) {
10777c478bd9Sstevel@tonic-gate 		if (!(ifp->int_state & IS_BROKE))
10787c478bd9Sstevel@tonic-gate 			writelog(LOG_WARNING, "sendto(%s%s%s): %s",
10797c478bd9Sstevel@tonic-gate 			    ifp->int_name, ", ",
10807c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin.sin_addr),
10817c478bd9Sstevel@tonic-gate 			    rip_strerror(errno));
10827c478bd9Sstevel@tonic-gate 		if (ifp != NULL)
10837c478bd9Sstevel@tonic-gate 			if_sick(ifp, _B_FALSE);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /* Send an advertisement */
10897c478bd9Sstevel@tonic-gate static void
send_adv(struct interface * ifp,in_addr_t dst,dstaddr_t type)10907c478bd9Sstevel@tonic-gate send_adv(struct interface *ifp,
10917c478bd9Sstevel@tonic-gate     in_addr_t dst,
10927c478bd9Sstevel@tonic-gate     dstaddr_t type)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	union ad_u u;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if ((ifp->int_state & (IS_SUPPRESS_RDISC|IS_FLUSH_RDISC)) ==
10977c478bd9Sstevel@tonic-gate 	    IS_SUPPRESS_RDISC)
10987c478bd9Sstevel@tonic-gate 		return;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	(void) memset(&u, 0, sizeof (u.ad));
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	u.ad.icmp_type = ICMP_ROUTERADVERT;
11037c478bd9Sstevel@tonic-gate 	u.ad.icmp_code = ICMP_ROUTERADVERT_COMMON;
11047c478bd9Sstevel@tonic-gate 	u.ad.icmp_ad_num = 1;
11057c478bd9Sstevel@tonic-gate 	u.ad.icmp_ad_asize = sizeof (u.ad.icmp_ad_info[0])/4;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	u.ad.icmp_ad_life = (stopint || !should_supply(ifp) ||
11087c478bd9Sstevel@tonic-gate 	    (ifp->int_state & IS_SUPPRESS_RDISC)) ? 0 :
11097c478bd9Sstevel@tonic-gate 	    htons(ifp->int_rdisc_int*3);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/* Send the configured preference as a network byte order value */
11127c478bd9Sstevel@tonic-gate 	u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(ifp->int_rdisc_pref);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	u.ad.icmp_cksum = in_cksum((uint16_t *)&u.ad, sizeof (u.ad));
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	send_rdisc(&u, sizeof (u.ad), ifp, dst, type);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	if (ifp->int_state & IS_SUPPRESS_RDISC)
11217c478bd9Sstevel@tonic-gate 		ifp->int_state &= ~IS_FLUSH_RDISC;
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate /* Advertise as a default router by way of router discovery. */
11267c478bd9Sstevel@tonic-gate void
rdisc_adv(boolean_t forceadv)11277c478bd9Sstevel@tonic-gate rdisc_adv(boolean_t forceadv)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	struct interface *ifp;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if (!forceadv && !should_supply(NULL))
11327c478bd9Sstevel@tonic-gate 		return;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	rdisc_timer.tv_sec = now.tv_sec + NEVER;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
11377c478bd9Sstevel@tonic-gate 		if ((ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)) ||
11387c478bd9Sstevel@tonic-gate 		    (!forceadv && !IS_IFF_ROUTING(ifp->int_if_flags)))
11397c478bd9Sstevel@tonic-gate 			continue;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		/* skip interfaces we shouldn't use */
11427c478bd9Sstevel@tonic-gate 		if (IS_IFF_QUIET(ifp->int_if_flags))
11437c478bd9Sstevel@tonic-gate 			continue;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		if (!timercmp(&ifp->int_rdisc_timer, &now, > /* cstyle */) ||
11467c478bd9Sstevel@tonic-gate 		    stopint != 0 || forceadv) {
11477c478bd9Sstevel@tonic-gate 			send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
11487c478bd9Sstevel@tonic-gate 			    (ifp->int_state & IS_BCAST_RDISC) ? 1 : 2);
11497c478bd9Sstevel@tonic-gate 			ifp->int_rdisc_cnt++;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 			intvl_random(&ifp->int_rdisc_timer,
11527c478bd9Sstevel@tonic-gate 			    (ifp->int_rdisc_int*3)/4, ifp->int_rdisc_int);
11537c478bd9Sstevel@tonic-gate 			if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS &&
11547c478bd9Sstevel@tonic-gate 			    (ifp->int_rdisc_timer.tv_sec >
11557c478bd9Sstevel@tonic-gate 			    MAX_INITIAL_ADVERT_INTERVAL)) {
11567c478bd9Sstevel@tonic-gate 				ifp->int_rdisc_timer.tv_sec =
11577c478bd9Sstevel@tonic-gate 				    MAX_INITIAL_ADVERT_INTERVAL;
11587c478bd9Sstevel@tonic-gate 			}
11597c478bd9Sstevel@tonic-gate 			timevaladd(&ifp->int_rdisc_timer, &now);
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 		if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer,
11627c478bd9Sstevel@tonic-gate 		    > /* cstyle */))
11637c478bd9Sstevel@tonic-gate 			rdisc_timer = ifp->int_rdisc_timer;
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate /* Solicit for Router Discovery */
11697c478bd9Sstevel@tonic-gate void
rdisc_sol(void)11707c478bd9Sstevel@tonic-gate rdisc_sol(void)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	struct interface *ifp;
11737c478bd9Sstevel@tonic-gate 	union ad_u u;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (should_supply(NULL))
11767c478bd9Sstevel@tonic-gate 		return;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	rdisc_timer.tv_sec = now.tv_sec + NEVER;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
11817c478bd9Sstevel@tonic-gate 		if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE)) ||
11827c478bd9Sstevel@tonic-gate 		    ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
11837c478bd9Sstevel@tonic-gate 			continue;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		/* skip interfaces we shouldn't use */
11867c478bd9Sstevel@tonic-gate 		if (IS_IFF_QUIET(ifp->int_if_flags))
11877c478bd9Sstevel@tonic-gate 			continue;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 		if (!timercmp(&ifp->int_rdisc_timer, &now, > /* cstyle */)) {
11907c478bd9Sstevel@tonic-gate 			(void) memset(&u, 0, sizeof (u.so));
11917c478bd9Sstevel@tonic-gate 			u.so.icmp_type = ICMP_ROUTERSOLICIT;
11927c478bd9Sstevel@tonic-gate 			u.so.icmp_cksum = in_cksum((uint16_t *)&u.so,
11937c478bd9Sstevel@tonic-gate 			    sizeof (u.so));
11947c478bd9Sstevel@tonic-gate 			send_rdisc(&u, sizeof (u.so), ifp,
11957c478bd9Sstevel@tonic-gate 			    htonl(INADDR_ALLRTRS_GROUP),
11967c478bd9Sstevel@tonic-gate 			    ((ifp->int_state&IS_BCAST_RDISC) ? bcast : mcast));
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 			if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
11997c478bd9Sstevel@tonic-gate 				continue;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 			ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
12027c478bd9Sstevel@tonic-gate 			ifp->int_rdisc_timer.tv_usec = 0;
12037c478bd9Sstevel@tonic-gate 			timevaladd(&ifp->int_rdisc_timer, &now);
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer,
12077c478bd9Sstevel@tonic-gate 		    > /* cstyle */))
12087c478bd9Sstevel@tonic-gate 			rdisc_timer = ifp->int_rdisc_timer;
12097c478bd9Sstevel@tonic-gate 	}
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate  * check the IP header of a possible Router Discovery ICMP packet
12157c478bd9Sstevel@tonic-gate  * Returns 0 if bad
12167c478bd9Sstevel@tonic-gate  */
12177c478bd9Sstevel@tonic-gate static struct interface *
ck_icmp(const char * act,in_addr_t from,struct interface * ifp,in_addr_t to,union ad_u * p,uint_t len)12187c478bd9Sstevel@tonic-gate ck_icmp(const char *act,
12197c478bd9Sstevel@tonic-gate     in_addr_t	from,
12207c478bd9Sstevel@tonic-gate     struct interface *ifp,
12217c478bd9Sstevel@tonic-gate     in_addr_t	to,
12227c478bd9Sstevel@tonic-gate     union ad_u *p,
12237c478bd9Sstevel@tonic-gate     uint_t	len)
12247c478bd9Sstevel@tonic-gate {
12257c478bd9Sstevel@tonic-gate 	const char *type;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
12297c478bd9Sstevel@tonic-gate 		type = "advertisement";
12307c478bd9Sstevel@tonic-gate 		if (p->icmp.icmp_code == ICMP_ROUTERADVERT_NOCOMMON)
12317c478bd9Sstevel@tonic-gate 			return (NULL); /* Mobile IP */
12327c478bd9Sstevel@tonic-gate 	} else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
12337c478bd9Sstevel@tonic-gate 		type = "solicitation";
12347c478bd9Sstevel@tonic-gate 	} else {
12357c478bd9Sstevel@tonic-gate 		return (NULL);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (p->icmp.icmp_code != ICMP_ROUTERADVERT_COMMON) {
12397c478bd9Sstevel@tonic-gate 		trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
12407c478bd9Sstevel@tonic-gate 		    type, p->icmp.icmp_code, naddr_ntoa(from), naddr_ntoa(to));
12417c478bd9Sstevel@tonic-gate 		return (NULL);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	trace_rdisc(act, from, to, ifp, p, len);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	if (ifp == NULL)
12477c478bd9Sstevel@tonic-gate 		trace_pkt("unknown interface for router-discovery %s from %s "
12487c478bd9Sstevel@tonic-gate 		    "to %s", type, naddr_ntoa(from), naddr_ntoa(to));
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	return (ifp);
12517c478bd9Sstevel@tonic-gate }
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate /* Read packets from the router discovery socket */
12557c478bd9Sstevel@tonic-gate void
read_d(void)12567c478bd9Sstevel@tonic-gate read_d(void)
12577c478bd9Sstevel@tonic-gate {
12587c478bd9Sstevel@tonic-gate #define	PKTLEN	512
12597c478bd9Sstevel@tonic-gate 	static struct msg_limit bad_asize, bad_len;
12607c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
12617c478bd9Sstevel@tonic-gate 	int n, cc, hlen;
12627c478bd9Sstevel@tonic-gate 	struct {
12637c478bd9Sstevel@tonic-gate 		union {
12647c478bd9Sstevel@tonic-gate 			struct ip ip;
12657c478bd9Sstevel@tonic-gate 			uint16_t s[PKTLEN/sizeof (uint16_t)];
12667c478bd9Sstevel@tonic-gate 			uint8_t	b[PKTLEN/sizeof (uint8_t)];
12677c478bd9Sstevel@tonic-gate 		} pkt;
12687c478bd9Sstevel@tonic-gate 	} buf;
12697c478bd9Sstevel@tonic-gate 	union ad_u *p;
12707c478bd9Sstevel@tonic-gate 	n_long *wp;
12717c478bd9Sstevel@tonic-gate 	struct interface *ifp;
12727c478bd9Sstevel@tonic-gate 	boolean_t needsort = _B_FALSE;
12737c478bd9Sstevel@tonic-gate 	struct msghdr msg;
12747c478bd9Sstevel@tonic-gate 	struct iovec iov;
12757c478bd9Sstevel@tonic-gate 	uint8_t ancillary_data[CONTROL_BUFSIZE];
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	iov.iov_base = &buf;
12787c478bd9Sstevel@tonic-gate 	iov.iov_len = sizeof (buf);
12797c478bd9Sstevel@tonic-gate 	msg.msg_iov = &iov;
12807c478bd9Sstevel@tonic-gate 	msg.msg_iovlen = 1;
12817c478bd9Sstevel@tonic-gate 	msg.msg_name = &from;
12827c478bd9Sstevel@tonic-gate 	msg.msg_control = &ancillary_data;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	for (;;) {
12857c478bd9Sstevel@tonic-gate 		msg.msg_namelen = sizeof (from);
12867c478bd9Sstevel@tonic-gate 		msg.msg_controllen = sizeof (ancillary_data);
12877c478bd9Sstevel@tonic-gate 		cc = recvmsg(rdisc_sock, &msg, 0);
12887c478bd9Sstevel@tonic-gate 		if (cc <= 0) {
12897c478bd9Sstevel@tonic-gate 			if (cc < 0 && errno != EWOULDBLOCK)
1290*e704a8f2Smeem 				LOGERR("recvmsg(rdisc_sock)");
12917c478bd9Sstevel@tonic-gate 			break;
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 		hlen = buf.pkt.ip.ip_hl << 2;
12957c478bd9Sstevel@tonic-gate 		if (cc < hlen + ICMP_MINLEN)
12967c478bd9Sstevel@tonic-gate 			continue;
12977c478bd9Sstevel@tonic-gate 		/* LINTED [alignment will be lw aligned] */
12987c478bd9Sstevel@tonic-gate 		p = (union ad_u *)&buf.pkt.b[hlen];
12997c478bd9Sstevel@tonic-gate 		cc -= hlen;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 		/*
13027c478bd9Sstevel@tonic-gate 		 * If we could tell the interface on which a packet from
13037c478bd9Sstevel@tonic-gate 		 * address 0 arrived, we could deal with such solicitations.
13047c478bd9Sstevel@tonic-gate 		 */
13057c478bd9Sstevel@tonic-gate 		ifp = receiving_interface(&msg, _B_FALSE);
13067c478bd9Sstevel@tonic-gate 		ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp,
13077c478bd9Sstevel@tonic-gate 		    buf.pkt.ip.ip_dst.s_addr, p, cc);
13087c478bd9Sstevel@tonic-gate 		if (ifp == NULL)
13097c478bd9Sstevel@tonic-gate 			continue;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 		if (IS_IFF_QUIET(ifp->int_if_flags)) {
13127c478bd9Sstevel@tonic-gate 			trace_misc("discard RDISC packet received over %s, %X",
13137c478bd9Sstevel@tonic-gate 			    ifp->int_name, ifp->int_if_flags);
13147c478bd9Sstevel@tonic-gate 			continue;
13157c478bd9Sstevel@tonic-gate 		}
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 		if (from.sin_addr.s_addr != 0 &&
13187c478bd9Sstevel@tonic-gate 		    ifwithaddr(from.sin_addr.s_addr, _B_FALSE, _B_FALSE)) {
13197c478bd9Sstevel@tonic-gate 			trace_pkt("    "
13207c478bd9Sstevel@tonic-gate 			    "discard our own Router Discovery message");
13217c478bd9Sstevel@tonic-gate 			continue;
13227c478bd9Sstevel@tonic-gate 		}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 		/* The remote address *must* be directly connected. */
13257c478bd9Sstevel@tonic-gate 		if (!remote_address_ok(ifp, from.sin_addr.s_addr)) {
13267c478bd9Sstevel@tonic-gate 			trace_misc("discard rdisc message; source %s not on "
13277c478bd9Sstevel@tonic-gate 			    "interface %s", naddr_ntoa(from.sin_addr.s_addr),
13287c478bd9Sstevel@tonic-gate 			    ifp->int_name);
13297c478bd9Sstevel@tonic-gate 			continue;
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 		switch (p->icmp.icmp_type) {
13337c478bd9Sstevel@tonic-gate 		case ICMP_ROUTERADVERT:
13347c478bd9Sstevel@tonic-gate 			if (ifp->int_state & IS_NO_ADV_IN)
13357c478bd9Sstevel@tonic-gate 				continue;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 			if (p->ad.icmp_ad_asize*2*sizeof (wp[0]) <
13387c478bd9Sstevel@tonic-gate 			    sizeof (p->ad.icmp_ad_info[0])) {
13397c478bd9Sstevel@tonic-gate 				msglim(&bad_asize, from.sin_addr.s_addr,
13407c478bd9Sstevel@tonic-gate 				    "intolerable rdisc address size=%d",
13417c478bd9Sstevel@tonic-gate 				    p->ad.icmp_ad_asize);
13427c478bd9Sstevel@tonic-gate 				continue;
13437c478bd9Sstevel@tonic-gate 			}
13447c478bd9Sstevel@tonic-gate 			if (p->ad.icmp_ad_num == 0) {
13457c478bd9Sstevel@tonic-gate 				trace_pkt("    empty?");
13467c478bd9Sstevel@tonic-gate 				continue;
13477c478bd9Sstevel@tonic-gate 			}
13487c478bd9Sstevel@tonic-gate 			if (cc < (sizeof (p->ad) -
13497c478bd9Sstevel@tonic-gate 			    sizeof (p->ad.icmp_ad_info) +
13507c478bd9Sstevel@tonic-gate 			    (p->ad.icmp_ad_num *
13517c478bd9Sstevel@tonic-gate 			    sizeof (p->ad.icmp_ad_info[0])))) {
13527c478bd9Sstevel@tonic-gate 				msglim(&bad_len, from.sin_addr.s_addr,
13537c478bd9Sstevel@tonic-gate 				    "rdisc length %d does not match ad_num"
13547c478bd9Sstevel@tonic-gate 				    " %d", cc, p->ad.icmp_ad_num);
13557c478bd9Sstevel@tonic-gate 				continue;
13567c478bd9Sstevel@tonic-gate 			}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 			needsort = _B_TRUE;
13597c478bd9Sstevel@tonic-gate 			wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
13607c478bd9Sstevel@tonic-gate 			for (n = 0; n < p->ad.icmp_ad_num; n++) {
13617c478bd9Sstevel@tonic-gate 				parse_ad(from.sin_addr.s_addr,
13627c478bd9Sstevel@tonic-gate 				    wp[0], wp[1],
13637c478bd9Sstevel@tonic-gate 				    ntohs(p->ad.icmp_ad_life), ifp);
13647c478bd9Sstevel@tonic-gate 				wp += p->ad.icmp_ad_asize;
13657c478bd9Sstevel@tonic-gate 			}
13667c478bd9Sstevel@tonic-gate 			break;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		case ICMP_ROUTERSOLICIT:
13707c478bd9Sstevel@tonic-gate 			if (!should_supply(ifp))
13717c478bd9Sstevel@tonic-gate 				continue;
13727c478bd9Sstevel@tonic-gate 			if ((ifp->int_state & IS_NO_ADV_OUT) ||
13737c478bd9Sstevel@tonic-gate 			    !IS_IFF_ROUTING(ifp->int_if_flags))
13747c478bd9Sstevel@tonic-gate 				continue;
13757c478bd9Sstevel@tonic-gate 			if (stopint != 0)
13767c478bd9Sstevel@tonic-gate 				continue;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 			/*
13797c478bd9Sstevel@tonic-gate 			 * We should handle messages from address 0,
13807c478bd9Sstevel@tonic-gate 			 * but cannot due to kernel limitations.
13817c478bd9Sstevel@tonic-gate 			 */
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 			/* Respond with a point-to-point advertisement */
13847c478bd9Sstevel@tonic-gate 			send_adv(ifp, from.sin_addr.s_addr, 0);
13857c478bd9Sstevel@tonic-gate 			break;
13867c478bd9Sstevel@tonic-gate 		}
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (needsort)
13907c478bd9Sstevel@tonic-gate 		rdisc_sort();
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate void
rdisc_dump(void)13947c478bd9Sstevel@tonic-gate rdisc_dump(void)
13957c478bd9Sstevel@tonic-gate {
13967c478bd9Sstevel@tonic-gate 	struct dr *drp;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	for (drp = drs; drp < &drs[max_ads]; drp++)
13997c478bd9Sstevel@tonic-gate 		if (drp->dr_ts != 0)
14007c478bd9Sstevel@tonic-gate 			trace_dr(drp);
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate void
rdisc_suppress(struct interface * ifp)14047c478bd9Sstevel@tonic-gate rdisc_suppress(struct interface *ifp)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate 	if (ifp->int_state & IS_ADV_OUT) {
14077c478bd9Sstevel@tonic-gate 		msglog("%s \"rdisc_adv\" specified, will not "
14087c478bd9Sstevel@tonic-gate 		    "suppress rdisc adv", ifp->int_name);
14097c478bd9Sstevel@tonic-gate 	} else {
14107c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_SUPPRESS_RDISC)
14117c478bd9Sstevel@tonic-gate 			return;
14127c478bd9Sstevel@tonic-gate 		ifp->int_state |= (IS_SUPPRESS_RDISC|IS_FLUSH_RDISC);
14137c478bd9Sstevel@tonic-gate 		trace_misc("suppress rdisc adv on %s", ifp->int_name);
14147c478bd9Sstevel@tonic-gate 		rdisc_timer.tv_sec = 0;
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate void
rdisc_restore(struct interface * ifp)14197c478bd9Sstevel@tonic-gate rdisc_restore(struct interface *ifp)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	if ((ifp->int_state & IS_SUPPRESS_RDISC) == 0)
14227c478bd9Sstevel@tonic-gate 		return;
14237c478bd9Sstevel@tonic-gate 	ifp->int_state &= ~(IS_SUPPRESS_RDISC|IS_FLUSH_RDISC);
14247c478bd9Sstevel@tonic-gate 	trace_misc("restoring rdisc adv on %s", ifp->int_name);
14257c478bd9Sstevel@tonic-gate 	rdisc_timer.tv_sec = 0;
14267c478bd9Sstevel@tonic-gate }
14273173664eSapersson 
14283173664eSapersson void
process_d_mib_sock(void)14293173664eSapersson process_d_mib_sock(void)
14303173664eSapersson {
14313173664eSapersson 
14323173664eSapersson 	socklen_t fromlen;
14333173664eSapersson 	struct sockaddr_un from;
14343173664eSapersson 	ssize_t	len;
14353173664eSapersson 	int command;
14363173664eSapersson 	struct dr *drp;
14373173664eSapersson 	rdisc_info_t rdisc_info;
14383173664eSapersson 	defr_t def_router;
14393173664eSapersson 	extern int max_ads;
14403173664eSapersson 	int num = 0;
14413173664eSapersson 
14423173664eSapersson 	fromlen = (socklen_t)sizeof (from);
14433173664eSapersson 	len = recvfrom(rdisc_mib_sock, &command, sizeof (int), 0,
14443173664eSapersson 	    (struct sockaddr *)&from, &fromlen);
14453173664eSapersson 
14463173664eSapersson 	if (len < sizeof (int) || command != RDISC_SNMP_INFO_REQ) {
14473173664eSapersson 		trace_misc("Bad command on rdisc_mib_sock");
14483173664eSapersson 		return;
14493173664eSapersson 	}
14503173664eSapersson 
14513173664eSapersson 	/*
14523173664eSapersson 	 * Count number of good routers
14533173664eSapersson 	 */
14543173664eSapersson 	for (drp = drs; drp < &drs[max_ads]; drp++) {
14553173664eSapersson 		if (drp->dr_ts != 0) {
14563173664eSapersson 			num++;
14573173664eSapersson 		}
14583173664eSapersson 	}
14593173664eSapersson 
14603173664eSapersson 	rdisc_info.info_type = RDISC_SNMP_INFO_RESPONSE;
14613173664eSapersson 	rdisc_info.info_version = RDISC_SNMP_INFO_VER;
14623173664eSapersson 	rdisc_info.info_num_of_routers = num;
14633173664eSapersson 
14643173664eSapersson 	(void) sendto(rdisc_mib_sock, &rdisc_info, sizeof (rdisc_info_t), 0,
14653173664eSapersson 	    (struct sockaddr *)&from, fromlen);
14663173664eSapersson 
14673173664eSapersson 	for (drp = drs; drp < &drs[max_ads]; drp++) {
14683173664eSapersson 		if (drp->dr_ts != 0) {
14693173664eSapersson 			def_router.defr_info_type = RDISC_DEF_ROUTER_INFO;
14703173664eSapersson 			def_router.defr_version = RDISC_DEF_ROUTER_VER;
14713173664eSapersson 			def_router.defr_index =
14723173664eSapersson 			    drp->dr_ifp->int_phys->phyi_index;
14733173664eSapersson 			def_router.defr_life = drp->dr_life;
14743173664eSapersson 			def_router.defr_addr.s_addr = drp->dr_gate;
14753173664eSapersson 			def_router.defr_pref = drp->dr_pref;
14763173664eSapersson 			(void) sendto(rdisc_mib_sock, &def_router,
14773173664eSapersson 			    sizeof (defr_t), 0, (struct sockaddr *)&from,
14783173664eSapersson 			    fromlen);
14793173664eSapersson 		}
14803173664eSapersson 	}
14813173664eSapersson }
1482