1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "defs.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	IF_SEPARATOR	':'
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate struct interface	*ifnet;
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int		setup_listen_sock(int ifindex);
42*7c478bd9Sstevel@tonic-gate static void		addrouteforif(struct interface *ifp);
43*7c478bd9Sstevel@tonic-gate static void		resetup_listen_sock(struct interface *, int);
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * This is called at startup and after that, every CHECK_INTERVAL seconds or
47*7c478bd9Sstevel@tonic-gate  * when a SIGHUP is received.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate void
initifs(void)50*7c478bd9Sstevel@tonic-gate initifs(void)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	static char *buf = NULL;
53*7c478bd9Sstevel@tonic-gate 	static uint_t maxbufsize = 0;
54*7c478bd9Sstevel@tonic-gate 	int bufsize;
55*7c478bd9Sstevel@tonic-gate 	int numifs;
56*7c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
57*7c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
58*7c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
59*7c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
60*7c478bd9Sstevel@tonic-gate 	int n;
61*7c478bd9Sstevel@tonic-gate 	struct interface ifs;
62*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
63*7c478bd9Sstevel@tonic-gate 	int netmaskchange = 0;
64*7c478bd9Sstevel@tonic-gate 	boolean_t changes = _B_FALSE;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_INET6;
67*7c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = 0;
68*7c478bd9Sstevel@tonic-gate 	if (ioctl(iocsoc, SIOCGLIFNUM, (char *)&lifn) < 0) {
69*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "initifs: ioctl (get interface numbers): %m");
70*7c478bd9Sstevel@tonic-gate 		return;
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	numifs = lifn.lifn_count;
73*7c478bd9Sstevel@tonic-gate 	bufsize = numifs * sizeof (struct lifreq);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (buf == NULL || bufsize > maxbufsize) {
76*7c478bd9Sstevel@tonic-gate 		if (buf != NULL)
77*7c478bd9Sstevel@tonic-gate 			free(buf);
78*7c478bd9Sstevel@tonic-gate 		maxbufsize = bufsize;
79*7c478bd9Sstevel@tonic-gate 		buf = (char *)malloc(maxbufsize);
80*7c478bd9Sstevel@tonic-gate 		if (buf == NULL) {
81*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "initifs: out of memory");
82*7c478bd9Sstevel@tonic-gate 			return;
83*7c478bd9Sstevel@tonic-gate 		}
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_INET6;
87*7c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = 0;
88*7c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
89*7c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
90*7c478bd9Sstevel@tonic-gate 	if (ioctl(iocsoc, SIOCGLIFCONF, (char *)&lifc) < 0) {
91*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
92*7c478bd9Sstevel@tonic-gate 		    "initifs: ioctl (get interface configuration): %m");
93*7c478bd9Sstevel@tonic-gate 		return;
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	/*
97*7c478bd9Sstevel@tonic-gate 	 * Mark all of the currently known interfaces in order to determine
98*7c478bd9Sstevel@tonic-gate 	 * which of the these interfaces no longer exist.
99*7c478bd9Sstevel@tonic-gate 	 */
100*7c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next)
101*7c478bd9Sstevel@tonic-gate 		ifp->int_flags |= RIP6_IFF_MARKED;
102*7c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
103*7c478bd9Sstevel@tonic-gate 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
104*7c478bd9Sstevel@tonic-gate 		bzero((char *)&ifs, sizeof (ifs));
105*7c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, lifrp->lifr_name,
106*7c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
107*7c478bd9Sstevel@tonic-gate 		if (ioctl(iocsoc, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
108*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
109*7c478bd9Sstevel@tonic-gate 			    "initifs: ioctl (get interface flags): %m");
110*7c478bd9Sstevel@tonic-gate 			continue;
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 		if (!(lifr.lifr_flags & IFF_IPV6) ||
113*7c478bd9Sstevel@tonic-gate 		    !(lifr.lifr_flags & IFF_MULTICAST) ||
114*7c478bd9Sstevel@tonic-gate 		    (lifr.lifr_flags & IFF_LOOPBACK))
115*7c478bd9Sstevel@tonic-gate 			continue;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		ifp = if_ifwithname(lifr.lifr_name);
118*7c478bd9Sstevel@tonic-gate 		if (ifp != NULL)
119*7c478bd9Sstevel@tonic-gate 			ifp->int_flags &= ~RIP6_IFF_MARKED;
120*7c478bd9Sstevel@tonic-gate 		if (lifr.lifr_flags & IFF_POINTOPOINT)
121*7c478bd9Sstevel@tonic-gate 			ifs.int_flags |= RIP6_IFF_POINTOPOINT;
122*7c478bd9Sstevel@tonic-gate 		if (lifr.lifr_flags & IFF_NORTEXCH)
123*7c478bd9Sstevel@tonic-gate 			ifs.int_flags |= RIP6_IFF_NORTEXCH;
124*7c478bd9Sstevel@tonic-gate 		if (lifr.lifr_flags & IFF_PRIVATE)
125*7c478bd9Sstevel@tonic-gate 			ifs.int_flags |= RIP6_IFF_PRIVATE;
126*7c478bd9Sstevel@tonic-gate 		if (lifr.lifr_flags & IFF_UP) {
127*7c478bd9Sstevel@tonic-gate 			ifs.int_flags |= RIP6_IFF_UP;
128*7c478bd9Sstevel@tonic-gate 		} else {
129*7c478bd9Sstevel@tonic-gate 			if (ifp != NULL) {
130*7c478bd9Sstevel@tonic-gate 				if (ifp->int_flags & RIP6_IFF_UP) {
131*7c478bd9Sstevel@tonic-gate 					/*
132*7c478bd9Sstevel@tonic-gate 					 * If there is an transition from up to
133*7c478bd9Sstevel@tonic-gate 					 * down for an exisiting interface,
134*7c478bd9Sstevel@tonic-gate 					 * increment the counter.
135*7c478bd9Sstevel@tonic-gate 					 */
136*7c478bd9Sstevel@tonic-gate 					ifp->int_transitions++;
137*7c478bd9Sstevel@tonic-gate 					changes = _B_TRUE;
138*7c478bd9Sstevel@tonic-gate 				}
139*7c478bd9Sstevel@tonic-gate 				if_purge(ifp);
140*7c478bd9Sstevel@tonic-gate 			}
141*7c478bd9Sstevel@tonic-gate 			continue;
142*7c478bd9Sstevel@tonic-gate 		}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 		if (ifs.int_flags & RIP6_IFF_POINTOPOINT) {
145*7c478bd9Sstevel@tonic-gate 			/*
146*7c478bd9Sstevel@tonic-gate 			 * For point-to-point interfaces, retrieve both the
147*7c478bd9Sstevel@tonic-gate 			 * local and the remote addresses.
148*7c478bd9Sstevel@tonic-gate 			 */
149*7c478bd9Sstevel@tonic-gate 			if (ioctl(iocsoc, SIOCGLIFADDR, (char *)&lifr) < 0) {
150*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
151*7c478bd9Sstevel@tonic-gate 				    "initifs: ioctl (get interface address): "
152*7c478bd9Sstevel@tonic-gate 				    "%m");
153*7c478bd9Sstevel@tonic-gate 				continue;
154*7c478bd9Sstevel@tonic-gate 			}
155*7c478bd9Sstevel@tonic-gate 			ifs.int_addr =
156*7c478bd9Sstevel@tonic-gate 			    ((struct sockaddr_in6 *)&lifr.lifr_addr)->sin6_addr;
157*7c478bd9Sstevel@tonic-gate 			if (ioctl(iocsoc, SIOCGLIFDSTADDR, (char *)&lifr) < 0) {
158*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
159*7c478bd9Sstevel@tonic-gate 				    "initifs: ioctl (get destination address): "
160*7c478bd9Sstevel@tonic-gate 				    "%m");
161*7c478bd9Sstevel@tonic-gate 				continue;
162*7c478bd9Sstevel@tonic-gate 			}
163*7c478bd9Sstevel@tonic-gate 			ifs.int_dstaddr = ((struct sockaddr_in6 *)
164*7c478bd9Sstevel@tonic-gate 			    &lifr.lifr_dstaddr)->sin6_addr;
165*7c478bd9Sstevel@tonic-gate 			ifs.int_prefix_length = IPV6_ABITS;
166*7c478bd9Sstevel@tonic-gate 		} else {
167*7c478bd9Sstevel@tonic-gate 			/*
168*7c478bd9Sstevel@tonic-gate 			 * For other interfaces, retreieve the prefix (including
169*7c478bd9Sstevel@tonic-gate 			 * the prefix length.
170*7c478bd9Sstevel@tonic-gate 			 */
171*7c478bd9Sstevel@tonic-gate 			if (ioctl(iocsoc, SIOCGLIFSUBNET, (char *)&lifr) < 0) {
172*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
173*7c478bd9Sstevel@tonic-gate 				    "initifs: ioctl (get subnet prefix): %m");
174*7c478bd9Sstevel@tonic-gate 				continue;
175*7c478bd9Sstevel@tonic-gate 			}
176*7c478bd9Sstevel@tonic-gate 			/*
177*7c478bd9Sstevel@tonic-gate 			 * This should never happen but check for it in any case
178*7c478bd9Sstevel@tonic-gate 			 * since the kernel stores it as an signed integer.
179*7c478bd9Sstevel@tonic-gate 			 */
180*7c478bd9Sstevel@tonic-gate 			if (lifr.lifr_addrlen < 0 ||
181*7c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen > IPV6_ABITS) {
182*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
183*7c478bd9Sstevel@tonic-gate 				    "initifs: ioctl (get subnet prefix) "
184*7c478bd9Sstevel@tonic-gate 				    "returned invalid prefix length of %d",
185*7c478bd9Sstevel@tonic-gate 				    lifr.lifr_addrlen);
186*7c478bd9Sstevel@tonic-gate 				continue;
187*7c478bd9Sstevel@tonic-gate 			}
188*7c478bd9Sstevel@tonic-gate 			ifs.int_prefix_length = lifr.lifr_addrlen;
189*7c478bd9Sstevel@tonic-gate 			ifs.int_addr = ((struct sockaddr_in6 *)
190*7c478bd9Sstevel@tonic-gate 			    &lifr.lifr_subnet)->sin6_addr;
191*7c478bd9Sstevel@tonic-gate 		}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 		if (ioctl(iocsoc, SIOCGLIFMETRIC, (char *)&lifr) < 0 ||
194*7c478bd9Sstevel@tonic-gate 		    lifr.lifr_metric < 0)
195*7c478bd9Sstevel@tonic-gate 			ifs.int_metric = 1;
196*7c478bd9Sstevel@tonic-gate 		else
197*7c478bd9Sstevel@tonic-gate 			ifs.int_metric = lifr.lifr_metric + 1;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		if (ioctl(iocsoc, SIOCGLIFINDEX, (char *)&lifr) < 0) {
200*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "initifs: ioctl (get index): %m");
201*7c478bd9Sstevel@tonic-gate 			continue;
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		ifs.int_ifindex = lifr.lifr_index;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		if (ioctl(iocsoc, SIOCGLIFMTU, (char *)&lifr) < 0) {
206*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "initifs: ioctl (get mtu): %m");
207*7c478bd9Sstevel@tonic-gate 			continue;
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		/*
211*7c478bd9Sstevel@tonic-gate 		 * If the interface's recorded MTU doesn't make sense, use
212*7c478bd9Sstevel@tonic-gate 		 * IPV6_MIN_MTU instead.
213*7c478bd9Sstevel@tonic-gate 		 */
214*7c478bd9Sstevel@tonic-gate 		if (lifr.lifr_mtu < IPV6_MIN_MTU)
215*7c478bd9Sstevel@tonic-gate 			ifs.int_mtu = IPV6_MIN_MTU;
216*7c478bd9Sstevel@tonic-gate 		else
217*7c478bd9Sstevel@tonic-gate 			ifs.int_mtu = lifr.lifr_mtu;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		if (ifp != NULL) {
220*7c478bd9Sstevel@tonic-gate 			/*
221*7c478bd9Sstevel@tonic-gate 			 * RIP6_IFF_NORTEXCH flag change by itself shouldn't
222*7c478bd9Sstevel@tonic-gate 			 * cause an if_purge() call, which also purges all the
223*7c478bd9Sstevel@tonic-gate 			 * routes heard off this interface. So, let's suppress
224*7c478bd9Sstevel@tonic-gate 			 * changes of RIP6_IFF_NORTEXCH	in the following
225*7c478bd9Sstevel@tonic-gate 			 * comparisons.
226*7c478bd9Sstevel@tonic-gate 			 */
227*7c478bd9Sstevel@tonic-gate 			if (ifp->int_prefix_length == ifs.int_prefix_length &&
228*7c478bd9Sstevel@tonic-gate 			    ((ifp->int_flags | RIP6_IFF_NORTEXCH) ==
229*7c478bd9Sstevel@tonic-gate 			    (ifs.int_flags | RIP6_IFF_NORTEXCH)) &&
230*7c478bd9Sstevel@tonic-gate 			    ifp->int_metric == ifs.int_metric &&
231*7c478bd9Sstevel@tonic-gate 			    ifp->int_ifindex == ifs.int_ifindex) {
232*7c478bd9Sstevel@tonic-gate 				/*
233*7c478bd9Sstevel@tonic-gate 				 * Now let's make sure we capture the latest
234*7c478bd9Sstevel@tonic-gate 				 * value of RIP6_IFF_NORTEXCH flag.
235*7c478bd9Sstevel@tonic-gate 				 */
236*7c478bd9Sstevel@tonic-gate 				if (ifs.int_flags & RIP6_IFF_NORTEXCH)
237*7c478bd9Sstevel@tonic-gate 					ifp->int_flags |= RIP6_IFF_NORTEXCH;
238*7c478bd9Sstevel@tonic-gate 				else
239*7c478bd9Sstevel@tonic-gate 					ifp->int_flags &= ~RIP6_IFF_NORTEXCH;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 				if (!(ifp->int_flags & RIP6_IFF_POINTOPOINT) &&
242*7c478bd9Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&ifp->int_addr,
243*7c478bd9Sstevel@tonic-gate 					&ifs.int_addr))
244*7c478bd9Sstevel@tonic-gate 					continue;
245*7c478bd9Sstevel@tonic-gate 				if ((ifp->int_flags & RIP6_IFF_POINTOPOINT) &&
246*7c478bd9Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&ifp->int_dstaddr,
247*7c478bd9Sstevel@tonic-gate 					&ifs.int_dstaddr))
248*7c478bd9Sstevel@tonic-gate 					continue;
249*7c478bd9Sstevel@tonic-gate 			}
250*7c478bd9Sstevel@tonic-gate 			if_purge(ifp);
251*7c478bd9Sstevel@tonic-gate 			if (ifp->int_prefix_length != ifs.int_prefix_length)
252*7c478bd9Sstevel@tonic-gate 				netmaskchange = 1;
253*7c478bd9Sstevel@tonic-gate 			ifp->int_addr = ifs.int_addr;
254*7c478bd9Sstevel@tonic-gate 			ifp->int_dstaddr = ifs.int_dstaddr;
255*7c478bd9Sstevel@tonic-gate 			ifp->int_metric = ifs.int_metric;
256*7c478bd9Sstevel@tonic-gate 			/*
257*7c478bd9Sstevel@tonic-gate 			 * If there is an transition from down to up for an
258*7c478bd9Sstevel@tonic-gate 			 * exisiting interface, increment the counter.
259*7c478bd9Sstevel@tonic-gate 			 */
260*7c478bd9Sstevel@tonic-gate 			if (!(ifp->int_flags & RIP6_IFF_UP) &&
261*7c478bd9Sstevel@tonic-gate 			    (ifs.int_flags & RIP6_IFF_UP))
262*7c478bd9Sstevel@tonic-gate 				ifp->int_transitions++;
263*7c478bd9Sstevel@tonic-gate 			ifp->int_flags |= ifs.int_flags;
264*7c478bd9Sstevel@tonic-gate 			ifp->int_prefix_length = ifs.int_prefix_length;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 			/*
267*7c478bd9Sstevel@tonic-gate 			 * If the interface index has changed, we may need to
268*7c478bd9Sstevel@tonic-gate 			 * set up the listen socket again.
269*7c478bd9Sstevel@tonic-gate 			 */
270*7c478bd9Sstevel@tonic-gate 			if (ifp->int_ifindex != ifs.int_ifindex) {
271*7c478bd9Sstevel@tonic-gate 				if (ifp->int_sock != -1) {
272*7c478bd9Sstevel@tonic-gate 					resetup_listen_sock(ifp,
273*7c478bd9Sstevel@tonic-gate 					    ifs.int_ifindex);
274*7c478bd9Sstevel@tonic-gate 				}
275*7c478bd9Sstevel@tonic-gate 				ifp->int_ifindex = ifs.int_ifindex;
276*7c478bd9Sstevel@tonic-gate 			}
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 			ifp->int_mtu = ifs.int_mtu;
279*7c478bd9Sstevel@tonic-gate 		} else {
280*7c478bd9Sstevel@tonic-gate 			char *cp;
281*7c478bd9Sstevel@tonic-gate 			int log_num;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 			ifp = (struct interface *)
284*7c478bd9Sstevel@tonic-gate 			    malloc(sizeof (struct interface));
285*7c478bd9Sstevel@tonic-gate 			if (ifp == NULL) {
286*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "initifs: out of memory");
287*7c478bd9Sstevel@tonic-gate 				return;
288*7c478bd9Sstevel@tonic-gate 			}
289*7c478bd9Sstevel@tonic-gate 			*ifp = ifs;
290*7c478bd9Sstevel@tonic-gate 			ifp->int_name = ifp->int_ifbase = NULL;
291*7c478bd9Sstevel@tonic-gate 			ifp->int_name =
292*7c478bd9Sstevel@tonic-gate 			    (char *)malloc((size_t)strlen(lifr.lifr_name) + 1);
293*7c478bd9Sstevel@tonic-gate 			if (ifp->int_name == NULL) {
294*7c478bd9Sstevel@tonic-gate 				free(ifp);
295*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "initifs: out of memory");
296*7c478bd9Sstevel@tonic-gate 				return;
297*7c478bd9Sstevel@tonic-gate 			}
298*7c478bd9Sstevel@tonic-gate 			(void) strcpy(ifp->int_name, lifr.lifr_name);
299*7c478bd9Sstevel@tonic-gate 			ifp->int_ifbase =
300*7c478bd9Sstevel@tonic-gate 			    (char *)malloc((size_t)strlen(lifr.lifr_name) + 1);
301*7c478bd9Sstevel@tonic-gate 			if (ifp->int_ifbase == NULL) {
302*7c478bd9Sstevel@tonic-gate 				free(ifp->int_name);
303*7c478bd9Sstevel@tonic-gate 				free(ifp);
304*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "initifs: out of memory");
305*7c478bd9Sstevel@tonic-gate 				return;
306*7c478bd9Sstevel@tonic-gate 			}
307*7c478bd9Sstevel@tonic-gate 			(void) strcpy(ifp->int_ifbase, lifr.lifr_name);
308*7c478bd9Sstevel@tonic-gate 			cp = (char *)index(ifp->int_ifbase, IF_SEPARATOR);
309*7c478bd9Sstevel@tonic-gate 			if (cp != NULL) {
310*7c478bd9Sstevel@tonic-gate 				/*
311*7c478bd9Sstevel@tonic-gate 				 * Verify that the value following the separator
312*7c478bd9Sstevel@tonic-gate 				 * is an integer greater than zero (the only
313*7c478bd9Sstevel@tonic-gate 				 * possible value for a logical interface).
314*7c478bd9Sstevel@tonic-gate 				 */
315*7c478bd9Sstevel@tonic-gate 				log_num = atoi((char *)(cp + 1));
316*7c478bd9Sstevel@tonic-gate 				if (log_num <= 0) {
317*7c478bd9Sstevel@tonic-gate 					free(ifp->int_ifbase);
318*7c478bd9Sstevel@tonic-gate 					free(ifp->int_name);
319*7c478bd9Sstevel@tonic-gate 					free(ifp);
320*7c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
321*7c478bd9Sstevel@tonic-gate 					    "initifs: interface name %s could "
322*7c478bd9Sstevel@tonic-gate 					    "not be parsed", ifp->int_name);
323*7c478bd9Sstevel@tonic-gate 					return;
324*7c478bd9Sstevel@tonic-gate 				}
325*7c478bd9Sstevel@tonic-gate 				*cp = '\0';
326*7c478bd9Sstevel@tonic-gate 			} else {
327*7c478bd9Sstevel@tonic-gate 				log_num = 0;
328*7c478bd9Sstevel@tonic-gate 			}
329*7c478bd9Sstevel@tonic-gate 			if (log_num == 0) {
330*7c478bd9Sstevel@tonic-gate 				ifp->int_sock =
331*7c478bd9Sstevel@tonic-gate 				    setup_listen_sock(ifp->int_ifindex);
332*7c478bd9Sstevel@tonic-gate 			} else {
333*7c478bd9Sstevel@tonic-gate 				ifp->int_sock = -1;
334*7c478bd9Sstevel@tonic-gate 			}
335*7c478bd9Sstevel@tonic-gate 			ifp->int_next = ifnet;
336*7c478bd9Sstevel@tonic-gate 			ifnet = ifp;
337*7c478bd9Sstevel@tonic-gate 			traceinit(ifp);
338*7c478bd9Sstevel@tonic-gate 		}
339*7c478bd9Sstevel@tonic-gate 		addrouteforif(ifp);
340*7c478bd9Sstevel@tonic-gate 		changes = _B_TRUE;
341*7c478bd9Sstevel@tonic-gate 	}
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	/*
344*7c478bd9Sstevel@tonic-gate 	 * Any remaining interfaces that are still marked and which were in an
345*7c478bd9Sstevel@tonic-gate 	 * up state (RIP6_IFF_UP) need to removed from the routing table.
346*7c478bd9Sstevel@tonic-gate 	 */
347*7c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
348*7c478bd9Sstevel@tonic-gate 		if ((ifp->int_flags & (RIP6_IFF_MARKED | RIP6_IFF_UP)) ==
349*7c478bd9Sstevel@tonic-gate 		    (RIP6_IFF_MARKED | RIP6_IFF_UP)) {
350*7c478bd9Sstevel@tonic-gate 			if_purge(ifp);
351*7c478bd9Sstevel@tonic-gate 			ifp->int_flags &= ~RIP6_IFF_MARKED;
352*7c478bd9Sstevel@tonic-gate 			changes = _B_TRUE;
353*7c478bd9Sstevel@tonic-gate 		}
354*7c478bd9Sstevel@tonic-gate 	}
355*7c478bd9Sstevel@tonic-gate 	if (netmaskchange)
356*7c478bd9Sstevel@tonic-gate 		rtchangeall();
357*7c478bd9Sstevel@tonic-gate 	if (supplier & changes)
358*7c478bd9Sstevel@tonic-gate 		dynamic_update((struct interface *)NULL);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate static void
addrouteforif(struct interface * ifp)362*7c478bd9Sstevel@tonic-gate addrouteforif(struct interface *ifp)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
365*7c478bd9Sstevel@tonic-gate 	struct in6_addr *dst;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	if (ifp->int_flags & RIP6_IFF_POINTOPOINT)
368*7c478bd9Sstevel@tonic-gate 		dst = &ifp->int_dstaddr;
369*7c478bd9Sstevel@tonic-gate 	else
370*7c478bd9Sstevel@tonic-gate 		dst = &ifp->int_addr;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	rt = rtlookup(dst, ifp->int_prefix_length);
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	if (rt != NULL) {
375*7c478bd9Sstevel@tonic-gate 		if (rt->rt_state & RTS_INTERFACE)
376*7c478bd9Sstevel@tonic-gate 			return;
377*7c478bd9Sstevel@tonic-gate 		rtdelete(rt);
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 	rtadd(dst, &ifp->int_addr, ifp->int_prefix_length, ifp->int_metric, 0,
380*7c478bd9Sstevel@tonic-gate 	    _B_TRUE, ifp);
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate static int
setup_listen_sock(int ifindex)384*7c478bd9Sstevel@tonic-gate setup_listen_sock(int ifindex)
385*7c478bd9Sstevel@tonic-gate {
386*7c478bd9Sstevel@tonic-gate 	int sock;
387*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
388*7c478bd9Sstevel@tonic-gate 	uint_t hops;
389*7c478bd9Sstevel@tonic-gate 	struct ipv6_mreq allrouters_mreq;
390*7c478bd9Sstevel@tonic-gate 	int on = 1;
391*7c478bd9Sstevel@tonic-gate 	int off = 0;
392*7c478bd9Sstevel@tonic-gate 	int recvsize;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	sock = socket(AF_INET6, SOCK_DGRAM, 0);
395*7c478bd9Sstevel@tonic-gate 	if (sock == -1)
396*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_BOUND_IF, (char *)&ifindex,
399*7c478bd9Sstevel@tonic-gate 	    sizeof (ifindex)) < 0) {
400*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
401*7c478bd9Sstevel@tonic-gate 		    "setup_listen_sock: setsockopt: IPV6_BOUND_IF: %m");
402*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	hops = IPV6_MAX_HOPS;
406*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char *)&hops,
407*7c478bd9Sstevel@tonic-gate 	    sizeof (hops)) < 0) {
408*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
409*7c478bd9Sstevel@tonic-gate 		    "setup_listen_sock: setsockopt: IPV6_UNICAST_HOPS: %m");
410*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&hops,
414*7c478bd9Sstevel@tonic-gate 	    sizeof (hops)) < 0) {
415*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
416*7c478bd9Sstevel@tonic-gate 		    "setup_listen_sock: setsockopt: IPV6_MULTICAST_HOPS: %m");
417*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&off,
421*7c478bd9Sstevel@tonic-gate 	    sizeof (off)) < 0) {
422*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
423*7c478bd9Sstevel@tonic-gate 		    "setup_listen_sock: setsockopt: IPV6_MULTICAST_LOOP: %m");
424*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	allrouters_mreq.ipv6mr_multiaddr = allrouters_in6;
428*7c478bd9Sstevel@tonic-gate 	allrouters_mreq.ipv6mr_interface = ifindex;
429*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
430*7c478bd9Sstevel@tonic-gate 	    (char *)&allrouters_mreq, sizeof (allrouters_mreq)) < 0) {
431*7c478bd9Sstevel@tonic-gate 		if (errno != EADDRINUSE) {
432*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
433*7c478bd9Sstevel@tonic-gate 			    "setup_listen_sock: setsockopt: "
434*7c478bd9Sstevel@tonic-gate 			    "IPV6_JOIN_GROUP: %m");
435*7c478bd9Sstevel@tonic-gate 			goto sock_fail;
436*7c478bd9Sstevel@tonic-gate 		}
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, (char *)&on,
440*7c478bd9Sstevel@tonic-gate 	    sizeof (off)) < 0) {
441*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
442*7c478bd9Sstevel@tonic-gate 		    "setup_listen_sock: setsockopt: IPV6_RECVHOPLIMIT: %m");
443*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
444*7c478bd9Sstevel@tonic-gate 	}
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
447*7c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0) {
448*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
449*7c478bd9Sstevel@tonic-gate 		    "setup_listen_sock: setsockopt: SO_REUSEADDR: %m");
450*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	recvsize = RCVBUFSIZ;
454*7c478bd9Sstevel@tonic-gate 	if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&recvsize,
455*7c478bd9Sstevel@tonic-gate 	    sizeof (int)) < 0) {
456*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_listen_sock: setsockopt: SO_RCVBUF: %m");
457*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	bzero((char *)&sin6, sizeof (sin6));
461*7c478bd9Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
462*7c478bd9Sstevel@tonic-gate 	sin6.sin6_port = rip6_port;
463*7c478bd9Sstevel@tonic-gate 	if (bind(sock, (struct sockaddr *)&sin6, sizeof (sin6)) < 0) {
464*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_listen_sock: bind: %m");
465*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	poll_ifs_num++;
469*7c478bd9Sstevel@tonic-gate 	if (poll_ifs == NULL) {
470*7c478bd9Sstevel@tonic-gate 		poll_ifs = (struct pollfd *)
471*7c478bd9Sstevel@tonic-gate 		    malloc(max_poll_ifs * sizeof (struct pollfd));
472*7c478bd9Sstevel@tonic-gate 	} else if (poll_ifs_num > max_poll_ifs) {
473*7c478bd9Sstevel@tonic-gate 		max_poll_ifs *= 2;
474*7c478bd9Sstevel@tonic-gate 		poll_ifs = (struct pollfd *)realloc((char *)poll_ifs,
475*7c478bd9Sstevel@tonic-gate 		    max_poll_ifs * sizeof (struct pollfd));
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 	if (poll_ifs == NULL) {
478*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_listen_sock: out of memory");
479*7c478bd9Sstevel@tonic-gate 		goto sock_fail;
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	poll_ifs[poll_ifs_num - 1].fd = sock;
483*7c478bd9Sstevel@tonic-gate 	poll_ifs[poll_ifs_num - 1].events = POLLIN;
484*7c478bd9Sstevel@tonic-gate 	return (sock);
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate sock_fail:
487*7c478bd9Sstevel@tonic-gate 	if (sock > 0)
488*7c478bd9Sstevel@tonic-gate 		(void) close(sock);
489*7c478bd9Sstevel@tonic-gate 	return (-1);
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate /*
493*7c478bd9Sstevel@tonic-gate  * resetup_listen_sock is primarily used in the case where a tunnel was
494*7c478bd9Sstevel@tonic-gate  * plumbed, unplumbed, then plumbed again.  This would cause the binding set by
495*7c478bd9Sstevel@tonic-gate  * IPV6_BOUND_IF to be useless, and sends to the associated socket will be
496*7c478bd9Sstevel@tonic-gate  * transmitted on the wrong interface.  resetup_listen_sock
497*7c478bd9Sstevel@tonic-gate  *	closes the socket,
498*7c478bd9Sstevel@tonic-gate  *	removes the socket from poll_ifs[]
499*7c478bd9Sstevel@tonic-gate  *	plugs the hole in poll_ifs[]
500*7c478bd9Sstevel@tonic-gate  *	calls setup_listen_sock to set up the socket again
501*7c478bd9Sstevel@tonic-gate  */
502*7c478bd9Sstevel@tonic-gate void
resetup_listen_sock(struct interface * ifp,int newindex)503*7c478bd9Sstevel@tonic-gate resetup_listen_sock(struct interface *ifp, int newindex)
504*7c478bd9Sstevel@tonic-gate {
505*7c478bd9Sstevel@tonic-gate 	int i;
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	(void) close(ifp->int_sock);
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	/* Remove socket from poll_ifs[]. */
510*7c478bd9Sstevel@tonic-gate 	for (i = poll_ifs_num - 1; i >= 0; i--) {
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 		if (poll_ifs[i].fd == ifp->int_sock) {
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 			poll_ifs[i].fd = 0;
515*7c478bd9Sstevel@tonic-gate 			poll_ifs[i].events = 0;
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 			/*
518*7c478bd9Sstevel@tonic-gate 			 * Remove hole in poll_ifs.  Possibly exchange
519*7c478bd9Sstevel@tonic-gate 			 * poll_ifs[i] with poll_ifs[poll_ifs_num-1].
520*7c478bd9Sstevel@tonic-gate 			 */
521*7c478bd9Sstevel@tonic-gate 			if (i != poll_ifs_num - 1) {
522*7c478bd9Sstevel@tonic-gate 				poll_ifs[i] = poll_ifs[poll_ifs_num - 1];
523*7c478bd9Sstevel@tonic-gate 				poll_ifs[poll_ifs_num - 1].fd = 0;
524*7c478bd9Sstevel@tonic-gate 				poll_ifs[poll_ifs_num - 1].events = 0;
525*7c478bd9Sstevel@tonic-gate 			}
526*7c478bd9Sstevel@tonic-gate 			poll_ifs_num--;
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 			/* Now set everything up again. */
529*7c478bd9Sstevel@tonic-gate 			ifp->int_sock = setup_listen_sock(newindex);
530*7c478bd9Sstevel@tonic-gate 			break;
531*7c478bd9Sstevel@tonic-gate 		}
532*7c478bd9Sstevel@tonic-gate 	}
533*7c478bd9Sstevel@tonic-gate }
534