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