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 2004 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * Routing Table Management Daemon
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate #include "defs.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate boolean_t	install = _B_TRUE;	/* update kernel routing table */
43*7c478bd9Sstevel@tonic-gate struct rthash	*net_hashes[IPV6_ABITS + 1];
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * Size of routing socket message used by in.ripngd which includes the header,
47*7c478bd9Sstevel@tonic-gate  * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6)
48*7c478bd9Sstevel@tonic-gate  * plus space for the RTA_IFP (a sockaddr_dl).
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate #define	RIPNG_RTM_MSGLEN	sizeof (struct rt_msghdr) +	\
51*7c478bd9Sstevel@tonic-gate 				sizeof (struct sockaddr_in6) +	\
52*7c478bd9Sstevel@tonic-gate 				sizeof (struct sockaddr_in6) +	\
53*7c478bd9Sstevel@tonic-gate 				sizeof (struct sockaddr_in6) +	\
54*7c478bd9Sstevel@tonic-gate 				sizeof (struct sockaddr_dl)
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int	rtmseq;				/* rtm_seq sequence number */
57*7c478bd9Sstevel@tonic-gate static int	rtsock;				/* Routing socket */
58*7c478bd9Sstevel@tonic-gate static struct	rt_msghdr	*rt_msg;	/* Routing socket message */
59*7c478bd9Sstevel@tonic-gate static struct	sockaddr_in6	*rta_dst;	/* RTA_DST sockaddr */
60*7c478bd9Sstevel@tonic-gate static struct	sockaddr_in6	*rta_gateway;	/* RTA_GATEWAY sockaddr */
61*7c478bd9Sstevel@tonic-gate static struct	sockaddr_in6	*rta_netmask;	/* RTA_NETMASK sockaddr */
62*7c478bd9Sstevel@tonic-gate static struct	sockaddr_dl	*rta_ifp;	/* RTA_IFP sockaddr */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /* simulate vax insque and remque instructions. */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate typedef struct vq {
67*7c478bd9Sstevel@tonic-gate 	caddr_t	 fwd, back;
68*7c478bd9Sstevel@tonic-gate } vq_t;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #define	insque(e, p)	((vq_t *)(e))->back = (caddr_t)(p); \
71*7c478bd9Sstevel@tonic-gate 			((vq_t *)(e))->fwd = \
72*7c478bd9Sstevel@tonic-gate 				(caddr_t)((vq_t *)((vq_t *)(p))->fwd); \
73*7c478bd9Sstevel@tonic-gate 			((vq_t *)((vq_t *)(p))->fwd)->back = (caddr_t)(e); \
74*7c478bd9Sstevel@tonic-gate 			((vq_t *)(p))->fwd = (caddr_t)(e);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate #define	remque(e)	((vq_t *)((vq_t *)(e))->back)->fwd =  \
77*7c478bd9Sstevel@tonic-gate 				(caddr_t)((vq_t *)(e))->fwd; \
78*7c478bd9Sstevel@tonic-gate 			((vq_t *)((vq_t *)(e))->fwd)->back = \
79*7c478bd9Sstevel@tonic-gate 				(caddr_t)((vq_t *)(e))->back; \
80*7c478bd9Sstevel@tonic-gate 			((vq_t *)(e))->fwd = NULL; \
81*7c478bd9Sstevel@tonic-gate 			((vq_t *)(e))->back = NULL;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static void
84*7c478bd9Sstevel@tonic-gate log_change(int level, struct rt_entry *orig, struct rt_entry *new)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	char buf1[INET6_ADDRSTRLEN];
87*7c478bd9Sstevel@tonic-gate 	char buf2[INET6_ADDRSTRLEN];
88*7c478bd9Sstevel@tonic-gate 	char buf3[INET6_ADDRSTRLEN];
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, (void *) &new->rt_dst, buf1, sizeof (buf1));
91*7c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, (void *) &orig->rt_router, buf2,
92*7c478bd9Sstevel@tonic-gate 	    sizeof (buf2));
93*7c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, (void *) &new->rt_router, buf3,
94*7c478bd9Sstevel@tonic-gate 	    sizeof (buf3));
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	syslog(level, "\tdst %s from gw %s if %s to gw %s if %s metric %d",
97*7c478bd9Sstevel@tonic-gate 	    buf1, buf2,
98*7c478bd9Sstevel@tonic-gate 	    (orig->rt_ifp != NULL && orig->rt_ifp->int_name != NULL) ?
99*7c478bd9Sstevel@tonic-gate 		orig->rt_ifp->int_name : "(noname)",
100*7c478bd9Sstevel@tonic-gate 	    buf3,
101*7c478bd9Sstevel@tonic-gate 	    (new->rt_ifp != NULL && new->rt_ifp->int_name != NULL) ?
102*7c478bd9Sstevel@tonic-gate 		new->rt_ifp->int_name : "(noname)", new->rt_metric);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate static void
106*7c478bd9Sstevel@tonic-gate log_single(int level, struct rt_entry *rt)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	char buf1[INET6_ADDRSTRLEN];
109*7c478bd9Sstevel@tonic-gate 	char buf2[INET6_ADDRSTRLEN];
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, (void *)&rt->rt_dst, buf1, sizeof (buf1));
112*7c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, (void *)&rt->rt_router, buf2, sizeof (buf2));
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	syslog(level, "\tdst %s gw %s if %s metric %d",
115*7c478bd9Sstevel@tonic-gate 	    buf1, buf2,
116*7c478bd9Sstevel@tonic-gate 	    (rt->rt_ifp != NULL && rt->rt_ifp->int_name != NULL) ?
117*7c478bd9Sstevel@tonic-gate 		rt->rt_ifp->int_name : "(noname)",
118*7c478bd9Sstevel@tonic-gate 	    rt->rt_metric);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * Computes a hash by XOR-ing the (up to sixteen) octets that make up an IPv6
123*7c478bd9Sstevel@tonic-gate  * address.  This function assumes that that there are no one-bits in the
124*7c478bd9Sstevel@tonic-gate  * address beyond the prefix length.
125*7c478bd9Sstevel@tonic-gate  */
126*7c478bd9Sstevel@tonic-gate static uint8_t
127*7c478bd9Sstevel@tonic-gate rthash(struct in6_addr *dst, int prefix_length)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	uint8_t val = 0;
130*7c478bd9Sstevel@tonic-gate 	int i;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	for (i = 0; prefix_length > 0; prefix_length -= 8, i++)
133*7c478bd9Sstevel@tonic-gate 		val ^= dst->s6_addr[i];
134*7c478bd9Sstevel@tonic-gate 	return (val);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate  * Given a prefix length, fill in the struct in6_addr representing an IPv6
139*7c478bd9Sstevel@tonic-gate  * netmask.
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate static void
142*7c478bd9Sstevel@tonic-gate rtmask_to_bits(uint_t prefix_length, struct in6_addr *prefix)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	uint_t mask = 0xff;
145*7c478bd9Sstevel@tonic-gate 	int i;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	bzero((caddr_t)prefix, sizeof (struct in6_addr));
148*7c478bd9Sstevel@tonic-gate 	for (i = 0; prefix_length >= 8; prefix_length -= 8, i++)
149*7c478bd9Sstevel@tonic-gate 		prefix->s6_addr[i] = 0xff;
150*7c478bd9Sstevel@tonic-gate 	mask = (mask << (8 - prefix_length));
151*7c478bd9Sstevel@tonic-gate 	if (mask != 0)
152*7c478bd9Sstevel@tonic-gate 		prefix->s6_addr[i] = mask;
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate void
156*7c478bd9Sstevel@tonic-gate rtcreate_prefix(struct in6_addr *p1, struct in6_addr *dst, int bits)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	uchar_t mask;
159*7c478bd9Sstevel@tonic-gate 	int j;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	for (j = 0; bits >= 8; bits -= 8, j++)
162*7c478bd9Sstevel@tonic-gate 		dst->s6_addr[j] = p1->s6_addr[j];
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if (bits != 0) {
165*7c478bd9Sstevel@tonic-gate 		mask = 0xff << (8 - bits);
166*7c478bd9Sstevel@tonic-gate 		dst->s6_addr[j] = p1->s6_addr[j] & mask;
167*7c478bd9Sstevel@tonic-gate 		j++;
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	for (; j < 16; j++)
171*7c478bd9Sstevel@tonic-gate 		dst->s6_addr[j] = 0;
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate /*
175*7c478bd9Sstevel@tonic-gate  * Lookup dst in the tables for an exact match.
176*7c478bd9Sstevel@tonic-gate  */
177*7c478bd9Sstevel@tonic-gate struct rt_entry *
178*7c478bd9Sstevel@tonic-gate rtlookup(struct in6_addr *dst, int prefix_length)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
181*7c478bd9Sstevel@tonic-gate 	struct rthash *rh;
182*7c478bd9Sstevel@tonic-gate 	uint_t	hash;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (net_hashes[prefix_length] == NULL)
185*7c478bd9Sstevel@tonic-gate 		return (NULL);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	hash = rthash(dst, prefix_length);
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	rh = &net_hashes[prefix_length][hash & ROUTEHASHMASK];
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
192*7c478bd9Sstevel@tonic-gate 		if (rt->rt_hash != hash)
193*7c478bd9Sstevel@tonic-gate 			continue;
194*7c478bd9Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&rt->rt_dst, dst) &&
195*7c478bd9Sstevel@tonic-gate 		    rt->rt_prefix_length == prefix_length)
196*7c478bd9Sstevel@tonic-gate 			return (rt);
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	return (NULL);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate /*
202*7c478bd9Sstevel@tonic-gate  * Given an IPv6 prefix (destination and prefix length), a gateway, an
203*7c478bd9Sstevel@tonic-gate  * interface name and route flags, send down the requested command returning
204*7c478bd9Sstevel@tonic-gate  * the return value and errno (in the case of error) from the write() on the
205*7c478bd9Sstevel@tonic-gate  * routing socket.
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate static int
208*7c478bd9Sstevel@tonic-gate rtcmd(uchar_t type, struct in6_addr *dst, struct in6_addr *gateway,
209*7c478bd9Sstevel@tonic-gate     uint_t prefix_length, char *name, int flags)
210*7c478bd9Sstevel@tonic-gate {
211*7c478bd9Sstevel@tonic-gate 	int rlen;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	rta_ifp->sdl_index = if_nametoindex(name);
214*7c478bd9Sstevel@tonic-gate 	if (rta_ifp->sdl_index == 0)
215*7c478bd9Sstevel@tonic-gate 		return (-1);
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	rta_dst->sin6_addr = *dst;
218*7c478bd9Sstevel@tonic-gate 	rta_gateway->sin6_addr = *gateway;
219*7c478bd9Sstevel@tonic-gate 	rtmask_to_bits(prefix_length, &rta_netmask->sin6_addr);
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_type = type;
222*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_flags = flags;
223*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_seq = ++rtmseq;
224*7c478bd9Sstevel@tonic-gate 	rlen = write(rtsock, rt_msg, RIPNG_RTM_MSGLEN);
225*7c478bd9Sstevel@tonic-gate 	if (rlen >= 0 && rlen < RIPNG_RTM_MSGLEN) {
226*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
227*7c478bd9Sstevel@tonic-gate 		    "rtcmd: write to routing socket got only %d for rlen\n",
228*7c478bd9Sstevel@tonic-gate 		    rlen);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	return (rlen);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate void
234*7c478bd9Sstevel@tonic-gate rtadd(struct in6_addr *dst, struct in6_addr *gate, int prefix_length,
235*7c478bd9Sstevel@tonic-gate     int metric, int tag, boolean_t ifroute, struct interface *ifp)
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
238*7c478bd9Sstevel@tonic-gate 	struct rthash *rh;
239*7c478bd9Sstevel@tonic-gate 	uint_t hash;
240*7c478bd9Sstevel@tonic-gate 	struct in6_addr pdst;
241*7c478bd9Sstevel@tonic-gate 	int rlen;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	if (metric >= HOPCNT_INFINITY)
244*7c478bd9Sstevel@tonic-gate 		return;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (net_hashes[prefix_length] == NULL) {
247*7c478bd9Sstevel@tonic-gate 		struct rthash *trh;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		rh = (struct rthash *)
250*7c478bd9Sstevel@tonic-gate 		    calloc(ROUTEHASHSIZ, sizeof (struct rt_entry));
251*7c478bd9Sstevel@tonic-gate 		if (rh == NULL)
252*7c478bd9Sstevel@tonic-gate 			return;
253*7c478bd9Sstevel@tonic-gate 		for (trh = rh; trh < &rh[ROUTEHASHSIZ]; trh++)
254*7c478bd9Sstevel@tonic-gate 			trh->rt_forw = trh->rt_back = (struct rt_entry *)trh;
255*7c478bd9Sstevel@tonic-gate 		net_hashes[prefix_length] = rh;
256*7c478bd9Sstevel@tonic-gate 	}
257*7c478bd9Sstevel@tonic-gate 	rtcreate_prefix(dst, &pdst, prefix_length);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	hash = rthash(&pdst, prefix_length);
260*7c478bd9Sstevel@tonic-gate 	rh = &net_hashes[prefix_length][hash & ROUTEHASHMASK];
261*7c478bd9Sstevel@tonic-gate 	rt = (struct rt_entry *)malloc(sizeof (*rt));
262*7c478bd9Sstevel@tonic-gate 	if (rt == NULL) {
263*7c478bd9Sstevel@tonic-gate 		/*
264*7c478bd9Sstevel@tonic-gate 		 * In the event of an allocation failure, log the error and
265*7c478bd9Sstevel@tonic-gate 		 * continue since on the next update another attempt will be
266*7c478bd9Sstevel@tonic-gate 		 * made.
267*7c478bd9Sstevel@tonic-gate 		 */
268*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "rtadd: malloc: %m");
269*7c478bd9Sstevel@tonic-gate 		return;
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate 	rt->rt_hash = hash;
272*7c478bd9Sstevel@tonic-gate 	rt->rt_dst = pdst;
273*7c478bd9Sstevel@tonic-gate 	rt->rt_prefix_length = prefix_length;
274*7c478bd9Sstevel@tonic-gate 	rt->rt_router = *gate;
275*7c478bd9Sstevel@tonic-gate 	rt->rt_metric = metric;
276*7c478bd9Sstevel@tonic-gate 	rt->rt_tag = tag;
277*7c478bd9Sstevel@tonic-gate 	rt->rt_timer = 0;
278*7c478bd9Sstevel@tonic-gate 	rt->rt_flags = RTF_UP;
279*7c478bd9Sstevel@tonic-gate 	if (prefix_length == IPV6_ABITS)
280*7c478bd9Sstevel@tonic-gate 		rt->rt_flags |= RTF_HOST;
281*7c478bd9Sstevel@tonic-gate 	rt->rt_state = RTS_CHANGED;
282*7c478bd9Sstevel@tonic-gate 	if (ifroute) {
283*7c478bd9Sstevel@tonic-gate 		rt->rt_state |= RTS_INTERFACE;
284*7c478bd9Sstevel@tonic-gate 		if (ifp->int_flags & RIP6_IFF_PRIVATE)
285*7c478bd9Sstevel@tonic-gate 			rt->rt_state |= RTS_PRIVATE;
286*7c478bd9Sstevel@tonic-gate 	} else {
287*7c478bd9Sstevel@tonic-gate 		rt->rt_flags |= RTF_GATEWAY;
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 	rt->rt_ifp = ifp;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	insque(rt, rh);
292*7c478bd9Sstevel@tonic-gate 	TRACE_ACTION("ADD", rt);
293*7c478bd9Sstevel@tonic-gate 	/*
294*7c478bd9Sstevel@tonic-gate 	 * If the RTM_ADD fails because the gateway is unreachable
295*7c478bd9Sstevel@tonic-gate 	 * from this host, discard the entry.  This should never
296*7c478bd9Sstevel@tonic-gate 	 * happen.
297*7c478bd9Sstevel@tonic-gate 	 */
298*7c478bd9Sstevel@tonic-gate 	if (install && (rt->rt_state & RTS_INTERFACE) == 0) {
299*7c478bd9Sstevel@tonic-gate 		rlen = rtcmd(RTM_ADD, &rt->rt_dst, &rt->rt_router,
300*7c478bd9Sstevel@tonic-gate 		    prefix_length, ifp->int_name, rt->rt_flags);
301*7c478bd9Sstevel@tonic-gate 		if (rlen < 0) {
302*7c478bd9Sstevel@tonic-gate 			if (errno != EEXIST) {
303*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "rtadd: RTM_ADD: %m");
304*7c478bd9Sstevel@tonic-gate 				log_single(LOG_ERR, rt);
305*7c478bd9Sstevel@tonic-gate 			}
306*7c478bd9Sstevel@tonic-gate 			if (errno == ENETUNREACH) {
307*7c478bd9Sstevel@tonic-gate 				TRACE_ACTION("DELETE", rt);
308*7c478bd9Sstevel@tonic-gate 				remque(rt);
309*7c478bd9Sstevel@tonic-gate 				free((char *)rt);
310*7c478bd9Sstevel@tonic-gate 			}
311*7c478bd9Sstevel@tonic-gate 		} else if (rlen < RIPNG_RTM_MSGLEN) {
312*7c478bd9Sstevel@tonic-gate 			log_single(LOG_ERR, rt);
313*7c478bd9Sstevel@tonic-gate 		}
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate /*
318*7c478bd9Sstevel@tonic-gate  * Handle the case when the metric changes but the gateway is the same (or the
319*7c478bd9Sstevel@tonic-gate  * interface index associated with the gateway changes), or when both gateway
320*7c478bd9Sstevel@tonic-gate  * and metric changes, or when only the gateway changes but the existing route
321*7c478bd9Sstevel@tonic-gate  * is more than one-half of EXPIRE_TIME in age. Note that routes with metric >=
322*7c478bd9Sstevel@tonic-gate  * HOPCNT_INFINITY are not in the kernel.
323*7c478bd9Sstevel@tonic-gate  */
324*7c478bd9Sstevel@tonic-gate void
325*7c478bd9Sstevel@tonic-gate rtchange(struct rt_entry *rt, struct in6_addr *gate, short metric,
326*7c478bd9Sstevel@tonic-gate     struct interface *ifp)
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate 	boolean_t dokern = _B_FALSE;
329*7c478bd9Sstevel@tonic-gate 	boolean_t dokerndelete;
330*7c478bd9Sstevel@tonic-gate 	boolean_t metricchanged = _B_FALSE;
331*7c478bd9Sstevel@tonic-gate 	int oldmetric;
332*7c478bd9Sstevel@tonic-gate 	struct rt_entry oldroute;
333*7c478bd9Sstevel@tonic-gate 	int rlen;
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (metric >= HOPCNT_INFINITY) {
336*7c478bd9Sstevel@tonic-gate 		rtdown(rt);
337*7c478bd9Sstevel@tonic-gate 		return;
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	if (!IN6_ARE_ADDR_EQUAL(&rt->rt_router, gate) || rt->rt_ifp != ifp)
341*7c478bd9Sstevel@tonic-gate 		dokern = _B_TRUE;
342*7c478bd9Sstevel@tonic-gate 	oldmetric = rt->rt_metric;
343*7c478bd9Sstevel@tonic-gate 	if (oldmetric >= HOPCNT_INFINITY)
344*7c478bd9Sstevel@tonic-gate 		dokerndelete = _B_FALSE;
345*7c478bd9Sstevel@tonic-gate 	else
346*7c478bd9Sstevel@tonic-gate 		dokerndelete = dokern;
347*7c478bd9Sstevel@tonic-gate 	if (metric != rt->rt_metric)
348*7c478bd9Sstevel@tonic-gate 		metricchanged = _B_TRUE;
349*7c478bd9Sstevel@tonic-gate 	rt->rt_timer = 0;
350*7c478bd9Sstevel@tonic-gate 	if (dokern || metricchanged) {
351*7c478bd9Sstevel@tonic-gate 		TRACE_ACTION("CHANGE FROM", rt);
352*7c478bd9Sstevel@tonic-gate 		if ((rt->rt_state & RTS_INTERFACE) && metric != 0) {
353*7c478bd9Sstevel@tonic-gate 			rt->rt_state &= ~RTS_INTERFACE;
354*7c478bd9Sstevel@tonic-gate 			if (rt->rt_ifp != NULL) {
355*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
356*7c478bd9Sstevel@tonic-gate 				    "rtchange: changing route from "
357*7c478bd9Sstevel@tonic-gate 				    "interface %s (timed out)",
358*7c478bd9Sstevel@tonic-gate 				    (rt->rt_ifp->int_name != NULL) ?
359*7c478bd9Sstevel@tonic-gate 					rt->rt_ifp->int_name : "(noname)");
360*7c478bd9Sstevel@tonic-gate 			} else {
361*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
362*7c478bd9Sstevel@tonic-gate 				    "rtchange: "
363*7c478bd9Sstevel@tonic-gate 				    "changing route no interface for route");
364*7c478bd9Sstevel@tonic-gate 			}
365*7c478bd9Sstevel@tonic-gate 		}
366*7c478bd9Sstevel@tonic-gate 		if (dokern) {
367*7c478bd9Sstevel@tonic-gate 			oldroute = *rt;
368*7c478bd9Sstevel@tonic-gate 			rt->rt_router = *gate;
369*7c478bd9Sstevel@tonic-gate 			rt->rt_ifp = ifp;
370*7c478bd9Sstevel@tonic-gate 		}
371*7c478bd9Sstevel@tonic-gate 		rt->rt_metric = metric;
372*7c478bd9Sstevel@tonic-gate 		if (!(rt->rt_state & RTS_INTERFACE))
373*7c478bd9Sstevel@tonic-gate 			rt->rt_flags |= RTF_GATEWAY;
374*7c478bd9Sstevel@tonic-gate 		else
375*7c478bd9Sstevel@tonic-gate 			rt->rt_flags &= ~RTF_GATEWAY;
376*7c478bd9Sstevel@tonic-gate 		rt->rt_state |= RTS_CHANGED;
377*7c478bd9Sstevel@tonic-gate 		TRACE_ACTION("CHANGE TO", rt);
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 	if (install && (rt->rt_state & RTS_INTERFACE) == 0) {
380*7c478bd9Sstevel@tonic-gate 		if (dokerndelete) {
381*7c478bd9Sstevel@tonic-gate 			rlen = rtcmd(RTM_ADD, &rt->rt_dst, &rt->rt_router,
382*7c478bd9Sstevel@tonic-gate 			    rt->rt_prefix_length, rt->rt_ifp->int_name,
383*7c478bd9Sstevel@tonic-gate 			    rt->rt_flags);
384*7c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
385*7c478bd9Sstevel@tonic-gate 				if (errno != EEXIST) {
386*7c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
387*7c478bd9Sstevel@tonic-gate 					    "rtchange: RTM_ADD: %m");
388*7c478bd9Sstevel@tonic-gate 					log_change(LOG_ERR, rt,
389*7c478bd9Sstevel@tonic-gate 					    (struct rt_entry *)&oldroute);
390*7c478bd9Sstevel@tonic-gate 				}
391*7c478bd9Sstevel@tonic-gate 			} else if (rlen < RIPNG_RTM_MSGLEN) {
392*7c478bd9Sstevel@tonic-gate 				log_change(LOG_ERR, rt,
393*7c478bd9Sstevel@tonic-gate 				    (struct rt_entry *)&oldroute);
394*7c478bd9Sstevel@tonic-gate 			}
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 			rlen = rtcmd(RTM_DELETE, &oldroute.rt_dst,
397*7c478bd9Sstevel@tonic-gate 			    &oldroute.rt_router, oldroute.rt_prefix_length,
398*7c478bd9Sstevel@tonic-gate 			    oldroute.rt_ifp->int_name, oldroute.rt_flags);
399*7c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
400*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "rtchange: RTM_DELETE: %m");
401*7c478bd9Sstevel@tonic-gate 				log_change(LOG_ERR, rt,
402*7c478bd9Sstevel@tonic-gate 				    (struct rt_entry *)&oldroute);
403*7c478bd9Sstevel@tonic-gate 			} else if (rlen < RIPNG_RTM_MSGLEN) {
404*7c478bd9Sstevel@tonic-gate 				log_change(LOG_ERR, rt,
405*7c478bd9Sstevel@tonic-gate 				    (struct rt_entry *)&oldroute);
406*7c478bd9Sstevel@tonic-gate 			}
407*7c478bd9Sstevel@tonic-gate 		} else if (dokern || oldmetric >= HOPCNT_INFINITY) {
408*7c478bd9Sstevel@tonic-gate 			rlen = rtcmd(RTM_ADD, &rt->rt_dst, &rt->rt_router,
409*7c478bd9Sstevel@tonic-gate 			    rt->rt_prefix_length, ifp->int_name, rt->rt_flags);
410*7c478bd9Sstevel@tonic-gate 			if (rlen < 0 && errno != EEXIST) {
411*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "rtchange: RTM_ADD: %m");
412*7c478bd9Sstevel@tonic-gate 				log_change(LOG_ERR, rt,
413*7c478bd9Sstevel@tonic-gate 				    (struct rt_entry *)&oldroute);
414*7c478bd9Sstevel@tonic-gate 			} else if (rlen < RIPNG_RTM_MSGLEN) {
415*7c478bd9Sstevel@tonic-gate 				log_change(LOG_ERR, rt,
416*7c478bd9Sstevel@tonic-gate 				    (struct rt_entry *)&oldroute);
417*7c478bd9Sstevel@tonic-gate 			}
418*7c478bd9Sstevel@tonic-gate 		}
419*7c478bd9Sstevel@tonic-gate 	}
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate void
423*7c478bd9Sstevel@tonic-gate rtdown(struct rt_entry *rt)
424*7c478bd9Sstevel@tonic-gate {
425*7c478bd9Sstevel@tonic-gate 	int rlen;
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	if (rt->rt_metric != HOPCNT_INFINITY) {
428*7c478bd9Sstevel@tonic-gate 		TRACE_ACTION("DELETE", rt);
429*7c478bd9Sstevel@tonic-gate 		if (install && (rt->rt_state & RTS_INTERFACE) == 0) {
430*7c478bd9Sstevel@tonic-gate 			rlen = rtcmd(RTM_DELETE, &rt->rt_dst,
431*7c478bd9Sstevel@tonic-gate 			    &rt->rt_router, rt->rt_prefix_length,
432*7c478bd9Sstevel@tonic-gate 			    rt->rt_ifp->int_name, rt->rt_flags);
433*7c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
434*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "rtdown: RTM_DELETE: %m");
435*7c478bd9Sstevel@tonic-gate 				log_single(LOG_ERR, rt);
436*7c478bd9Sstevel@tonic-gate 			} else if (rlen < RIPNG_RTM_MSGLEN) {
437*7c478bd9Sstevel@tonic-gate 				log_single(LOG_ERR, rt);
438*7c478bd9Sstevel@tonic-gate 			}
439*7c478bd9Sstevel@tonic-gate 		}
440*7c478bd9Sstevel@tonic-gate 		rt->rt_metric = HOPCNT_INFINITY;
441*7c478bd9Sstevel@tonic-gate 		rt->rt_state |= RTS_CHANGED;
442*7c478bd9Sstevel@tonic-gate 	}
443*7c478bd9Sstevel@tonic-gate 	if (rt->rt_timer < EXPIRE_TIME)
444*7c478bd9Sstevel@tonic-gate 		rt->rt_timer = EXPIRE_TIME;
445*7c478bd9Sstevel@tonic-gate }
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate void
448*7c478bd9Sstevel@tonic-gate rtdelete(struct rt_entry *rt)
449*7c478bd9Sstevel@tonic-gate {
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	if (rt->rt_state & RTS_INTERFACE) {
452*7c478bd9Sstevel@tonic-gate 		if (rt->rt_ifp != NULL) {
453*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
454*7c478bd9Sstevel@tonic-gate 			    "rtdelete: "
455*7c478bd9Sstevel@tonic-gate 			    "deleting route to interface %s (timed out)",
456*7c478bd9Sstevel@tonic-gate 			    (rt->rt_ifp->int_name != NULL) ?
457*7c478bd9Sstevel@tonic-gate 				rt->rt_ifp->int_name : "(noname)");
458*7c478bd9Sstevel@tonic-gate 			log_single(LOG_ERR, rt);
459*7c478bd9Sstevel@tonic-gate 		}
460*7c478bd9Sstevel@tonic-gate 	}
461*7c478bd9Sstevel@tonic-gate 	rtdown(rt);
462*7c478bd9Sstevel@tonic-gate 	remque(rt);
463*7c478bd9Sstevel@tonic-gate 	free((char *)rt);
464*7c478bd9Sstevel@tonic-gate }
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate /*
467*7c478bd9Sstevel@tonic-gate  * Mark all the routes heard off a particular interface "down".  Unlike the
468*7c478bd9Sstevel@tonic-gate  * routes managed by in.routed, all of these routes have an interface associated
469*7c478bd9Sstevel@tonic-gate  * with them.
470*7c478bd9Sstevel@tonic-gate  */
471*7c478bd9Sstevel@tonic-gate void
472*7c478bd9Sstevel@tonic-gate rtpurgeif(struct interface *ifp)
473*7c478bd9Sstevel@tonic-gate {
474*7c478bd9Sstevel@tonic-gate 	struct rthash *rh;
475*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
476*7c478bd9Sstevel@tonic-gate 	int i;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	for (i = IPV6_ABITS; i >= 0; i--) {
479*7c478bd9Sstevel@tonic-gate 		if (net_hashes[i] == NULL)
480*7c478bd9Sstevel@tonic-gate 			continue;
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 		for (rh = net_hashes[i];
483*7c478bd9Sstevel@tonic-gate 		    rh < &net_hashes[i][ROUTEHASHSIZ]; rh++) {
484*7c478bd9Sstevel@tonic-gate 			for (rt = rh->rt_forw; rt != (struct rt_entry *)rh;
485*7c478bd9Sstevel@tonic-gate 			    rt = rt->rt_forw) {
486*7c478bd9Sstevel@tonic-gate 				if (rt->rt_ifp == ifp) {
487*7c478bd9Sstevel@tonic-gate 					rtdown(rt);
488*7c478bd9Sstevel@tonic-gate 					rt->rt_ifp = NULL;
489*7c478bd9Sstevel@tonic-gate 					rt->rt_state &= ~RTS_INTERFACE;
490*7c478bd9Sstevel@tonic-gate 				}
491*7c478bd9Sstevel@tonic-gate 			}
492*7c478bd9Sstevel@tonic-gate 		}
493*7c478bd9Sstevel@tonic-gate 	}
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate /*
497*7c478bd9Sstevel@tonic-gate  * Called when the subnetmask has changed on one or more interfaces.
498*7c478bd9Sstevel@tonic-gate  * Re-evaluates all non-interface routes by doing a rtchange so that
499*7c478bd9Sstevel@tonic-gate  * routes that were believed to be host routes before the netmask change
500*7c478bd9Sstevel@tonic-gate  * can be converted to network routes and vice versa.
501*7c478bd9Sstevel@tonic-gate  */
502*7c478bd9Sstevel@tonic-gate void
503*7c478bd9Sstevel@tonic-gate rtchangeall(void)
504*7c478bd9Sstevel@tonic-gate {
505*7c478bd9Sstevel@tonic-gate 	struct rthash *rh;
506*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
507*7c478bd9Sstevel@tonic-gate 	int i;
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	for (i = IPV6_ABITS; i >= 0; i--) {
510*7c478bd9Sstevel@tonic-gate 		if (net_hashes[i] == NULL)
511*7c478bd9Sstevel@tonic-gate 			continue;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 		for (rh = net_hashes[i];
514*7c478bd9Sstevel@tonic-gate 		    rh < &net_hashes[i][ROUTEHASHSIZ]; rh++) {
515*7c478bd9Sstevel@tonic-gate 			for (rt = rh->rt_forw; rt != (struct rt_entry *)rh;
516*7c478bd9Sstevel@tonic-gate 			    rt = rt->rt_forw) {
517*7c478bd9Sstevel@tonic-gate 				if ((rt->rt_state & RTS_INTERFACE) == 0) {
518*7c478bd9Sstevel@tonic-gate 					rtchange(rt, &rt->rt_router,
519*7c478bd9Sstevel@tonic-gate 					    rt->rt_metric, rt->rt_ifp);
520*7c478bd9Sstevel@tonic-gate 				}
521*7c478bd9Sstevel@tonic-gate 			}
522*7c478bd9Sstevel@tonic-gate 		}
523*7c478bd9Sstevel@tonic-gate 	}
524*7c478bd9Sstevel@tonic-gate }
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate static void
527*7c478bd9Sstevel@tonic-gate rtdumpentry(FILE *fp, struct rt_entry *rt)
528*7c478bd9Sstevel@tonic-gate {
529*7c478bd9Sstevel@tonic-gate 	char buf1[INET6_ADDRSTRLEN];
530*7c478bd9Sstevel@tonic-gate 	static struct bits {
531*7c478bd9Sstevel@tonic-gate 		ulong_t	t_bits;
532*7c478bd9Sstevel@tonic-gate 		char	*t_name;
533*7c478bd9Sstevel@tonic-gate 	} flagbits[] = {
534*7c478bd9Sstevel@tonic-gate 		/* BEGIN CSTYLED */
535*7c478bd9Sstevel@tonic-gate 		{ RTF_UP,		"UP" },
536*7c478bd9Sstevel@tonic-gate 		{ RTF_GATEWAY,		"GATEWAY" },
537*7c478bd9Sstevel@tonic-gate 		{ RTF_HOST,		"HOST" },
538*7c478bd9Sstevel@tonic-gate 		{ 0,			NULL }
539*7c478bd9Sstevel@tonic-gate 		/* END CSTYLED */
540*7c478bd9Sstevel@tonic-gate 	}, statebits[] = {
541*7c478bd9Sstevel@tonic-gate 		/* BEGIN CSTYLED */
542*7c478bd9Sstevel@tonic-gate 		{ RTS_INTERFACE,	"INTERFACE" },
543*7c478bd9Sstevel@tonic-gate 		{ RTS_CHANGED,		"CHANGED" },
544*7c478bd9Sstevel@tonic-gate 		{ RTS_PRIVATE,		"PRIVATE" },
545*7c478bd9Sstevel@tonic-gate 		{ 0,			NULL }
546*7c478bd9Sstevel@tonic-gate 		/* END CSTYLED */
547*7c478bd9Sstevel@tonic-gate 	};
548*7c478bd9Sstevel@tonic-gate 	struct bits *p;
549*7c478bd9Sstevel@tonic-gate 	boolean_t first;
550*7c478bd9Sstevel@tonic-gate 	char c;
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "prefix %s/%d ",
553*7c478bd9Sstevel@tonic-gate 	    inet_ntop(AF_INET6, (void *)&rt->rt_dst, buf1, sizeof (buf1)),
554*7c478bd9Sstevel@tonic-gate 	    rt->rt_prefix_length);
555*7c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "via %s metric %d timer %d",
556*7c478bd9Sstevel@tonic-gate 	    inet_ntop(AF_INET6, (void *)&rt->rt_router, buf1, sizeof (buf1)),
557*7c478bd9Sstevel@tonic-gate 	    rt->rt_metric, rt->rt_timer);
558*7c478bd9Sstevel@tonic-gate 	if (rt->rt_ifp != NULL) {
559*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, " if %s",
560*7c478bd9Sstevel@tonic-gate 		    (rt->rt_ifp->int_name != NULL) ?
561*7c478bd9Sstevel@tonic-gate 			rt->rt_ifp->int_name : "(noname)");
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, " state");
564*7c478bd9Sstevel@tonic-gate 	c = ' ';
565*7c478bd9Sstevel@tonic-gate 	for (first = _B_TRUE, p = statebits; p->t_bits > 0; p++) {
566*7c478bd9Sstevel@tonic-gate 		if ((rt->rt_state & p->t_bits) == 0)
567*7c478bd9Sstevel@tonic-gate 			continue;
568*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%c%s", c, p->t_name);
569*7c478bd9Sstevel@tonic-gate 		if (first) {
570*7c478bd9Sstevel@tonic-gate 			c = '|';
571*7c478bd9Sstevel@tonic-gate 			first = _B_FALSE;
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 	}
574*7c478bd9Sstevel@tonic-gate 	if (first)
575*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, " 0");
576*7c478bd9Sstevel@tonic-gate 	if (rt->rt_flags & (RTF_UP | RTF_GATEWAY)) {
577*7c478bd9Sstevel@tonic-gate 		c = ' ';
578*7c478bd9Sstevel@tonic-gate 		for (first = _B_TRUE, p = flagbits; p->t_bits > 0; p++) {
579*7c478bd9Sstevel@tonic-gate 			if ((rt->rt_flags & p->t_bits) == 0)
580*7c478bd9Sstevel@tonic-gate 				continue;
581*7c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%c%s", c, p->t_name);
582*7c478bd9Sstevel@tonic-gate 			if (first) {
583*7c478bd9Sstevel@tonic-gate 				c = '|';
584*7c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
585*7c478bd9Sstevel@tonic-gate 			}
586*7c478bd9Sstevel@tonic-gate 		}
587*7c478bd9Sstevel@tonic-gate 	}
588*7c478bd9Sstevel@tonic-gate 	(void) putc('\n', fp);
589*7c478bd9Sstevel@tonic-gate 	(void) fflush(fp);
590*7c478bd9Sstevel@tonic-gate }
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate static void
593*7c478bd9Sstevel@tonic-gate rtdump2(FILE *fp)
594*7c478bd9Sstevel@tonic-gate {
595*7c478bd9Sstevel@tonic-gate 	struct rthash *rh;
596*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
597*7c478bd9Sstevel@tonic-gate 	int i;
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 	for (i = IPV6_ABITS; i >= 0; i--) {
600*7c478bd9Sstevel@tonic-gate 		if (net_hashes[i] == NULL)
601*7c478bd9Sstevel@tonic-gate 			continue;
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 		for (rh = net_hashes[i];
604*7c478bd9Sstevel@tonic-gate 		    rh < &net_hashes[i][ROUTEHASHSIZ]; rh++) {
605*7c478bd9Sstevel@tonic-gate 			for (rt = rh->rt_forw; rt != (struct rt_entry *)rh;
606*7c478bd9Sstevel@tonic-gate 			    rt = rt->rt_forw) {
607*7c478bd9Sstevel@tonic-gate 				rtdumpentry(fp, rt);
608*7c478bd9Sstevel@tonic-gate 			}
609*7c478bd9Sstevel@tonic-gate 		}
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate }
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate void
614*7c478bd9Sstevel@tonic-gate rtdump(void)
615*7c478bd9Sstevel@tonic-gate {
616*7c478bd9Sstevel@tonic-gate 	if (ftrace != NULL)
617*7c478bd9Sstevel@tonic-gate 		rtdump2(ftrace);
618*7c478bd9Sstevel@tonic-gate 	else
619*7c478bd9Sstevel@tonic-gate 		rtdump2(stderr);
620*7c478bd9Sstevel@tonic-gate }
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate /*
623*7c478bd9Sstevel@tonic-gate  * Create a routing socket for sending RTM_ADD and RTM_DELETE messages and
624*7c478bd9Sstevel@tonic-gate  * initialize the routing socket message header and as much of the sockaddrs
625*7c478bd9Sstevel@tonic-gate  * as possible.
626*7c478bd9Sstevel@tonic-gate  */
627*7c478bd9Sstevel@tonic-gate void
628*7c478bd9Sstevel@tonic-gate setup_rtsock(void)
629*7c478bd9Sstevel@tonic-gate {
630*7c478bd9Sstevel@tonic-gate 	char *cp;
631*7c478bd9Sstevel@tonic-gate 	int off = 0;
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	rtsock = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
634*7c478bd9Sstevel@tonic-gate 	if (rtsock < 0) {
635*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_rtsock: socket: %m");
636*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	/* We don't want to listen to our own messages */
640*7c478bd9Sstevel@tonic-gate 	if (setsockopt(rtsock, SOL_SOCKET, SO_USELOOPBACK, (char *)&off,
641*7c478bd9Sstevel@tonic-gate 	    sizeof (off)) < 0) {
642*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_rtsock: setsockopt: SO_USELOOPBACK: %m");
643*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
644*7c478bd9Sstevel@tonic-gate 	}
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate 	/*
647*7c478bd9Sstevel@tonic-gate 	 * Allocate storage for the routing socket message.
648*7c478bd9Sstevel@tonic-gate 	 */
649*7c478bd9Sstevel@tonic-gate 	rt_msg = (struct rt_msghdr *)malloc(RIPNG_RTM_MSGLEN);
650*7c478bd9Sstevel@tonic-gate 	if (rt_msg == NULL) {
651*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_rtsock: malloc: %m");
652*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
653*7c478bd9Sstevel@tonic-gate 	}
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 	/*
656*7c478bd9Sstevel@tonic-gate 	 * Initialize the routing socket message by zero-filling it and then
657*7c478bd9Sstevel@tonic-gate 	 * setting the fields where are constant through the lifetime of the
658*7c478bd9Sstevel@tonic-gate 	 * process.
659*7c478bd9Sstevel@tonic-gate 	 */
660*7c478bd9Sstevel@tonic-gate 	bzero(rt_msg, RIPNG_RTM_MSGLEN);
661*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_msglen = RIPNG_RTM_MSGLEN;
662*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_version = RTM_VERSION;
663*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP;
664*7c478bd9Sstevel@tonic-gate 	rt_msg->rtm_pid = getpid();
665*7c478bd9Sstevel@tonic-gate 	if (rt_msg->rtm_pid < 0) {
666*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setup_rtsock: getpid: %m");
667*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
668*7c478bd9Sstevel@tonic-gate 	}
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	/*
671*7c478bd9Sstevel@tonic-gate 	 * Initialize the constant portion of the RTA_DST sockaddr.
672*7c478bd9Sstevel@tonic-gate 	 */
673*7c478bd9Sstevel@tonic-gate 	cp = (char *)rt_msg + sizeof (struct rt_msghdr);
674*7c478bd9Sstevel@tonic-gate 	rta_dst = (struct sockaddr_in6 *)cp;
675*7c478bd9Sstevel@tonic-gate 	rta_dst->sin6_family = AF_INET6;
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 	/*
678*7c478bd9Sstevel@tonic-gate 	 * Initialize the constant portion of the RTA_GATEWAY sockaddr.
679*7c478bd9Sstevel@tonic-gate 	 */
680*7c478bd9Sstevel@tonic-gate 	cp += sizeof (struct sockaddr_in6);
681*7c478bd9Sstevel@tonic-gate 	rta_gateway = (struct sockaddr_in6 *)cp;
682*7c478bd9Sstevel@tonic-gate 	rta_gateway->sin6_family = AF_INET6;
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate 	/*
685*7c478bd9Sstevel@tonic-gate 	 * Initialize the constant portion of the RTA_NETMASK sockaddr.
686*7c478bd9Sstevel@tonic-gate 	 */
687*7c478bd9Sstevel@tonic-gate 	cp += sizeof (struct sockaddr_in6);
688*7c478bd9Sstevel@tonic-gate 	rta_netmask = (struct sockaddr_in6 *)cp;
689*7c478bd9Sstevel@tonic-gate 	rta_netmask->sin6_family = AF_INET6;
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate 	/*
692*7c478bd9Sstevel@tonic-gate 	 * Initialize the constant portion of the RTA_IFP sockaddr.
693*7c478bd9Sstevel@tonic-gate 	 */
694*7c478bd9Sstevel@tonic-gate 	cp += sizeof (struct sockaddr_in6);
695*7c478bd9Sstevel@tonic-gate 	rta_ifp = (struct sockaddr_dl *)cp;
696*7c478bd9Sstevel@tonic-gate 	rta_ifp->sdl_family = AF_LINK;
697*7c478bd9Sstevel@tonic-gate }
698