xref: /onnv-gate/usr/src/uts/common/inet/ip/ip_rts.c (revision 1676:37f4a3e2bd99)
10Sstevel@tonic-gate /*
2*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1988, 1991, 1993
80Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate  * are met:
130Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
140Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
170Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
180Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
190Sstevel@tonic-gate  *    must display the following acknowledgement:
200Sstevel@tonic-gate  *	This product includes software developed by the University of
210Sstevel@tonic-gate  *	California, Berkeley and its contributors.
220Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
230Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
240Sstevel@tonic-gate  *    without specific prior written permission.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
270Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
280Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
290Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
300Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
310Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
320Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
330Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
340Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
350Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
360Sstevel@tonic-gate  * SUCH DAMAGE.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * This file contains routines that processes routing socket requests.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/stream.h>
490Sstevel@tonic-gate #include <sys/stropts.h>
500Sstevel@tonic-gate #include <sys/ddi.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/debug.h>
530Sstevel@tonic-gate #include <sys/policy.h>
540Sstevel@tonic-gate #include <sys/zone.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <sys/systm.h>
570Sstevel@tonic-gate #include <sys/param.h>
580Sstevel@tonic-gate #include <sys/socket.h>
590Sstevel@tonic-gate #include <sys/strsun.h>
600Sstevel@tonic-gate #include <net/if.h>
610Sstevel@tonic-gate #include <net/route.h>
620Sstevel@tonic-gate #include <netinet/in.h>
630Sstevel@tonic-gate #include <net/if_dl.h>
640Sstevel@tonic-gate #include <netinet/ip6.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <inet/common.h>
670Sstevel@tonic-gate #include <inet/ip.h>
680Sstevel@tonic-gate #include <inet/ip6.h>
690Sstevel@tonic-gate #include <inet/ip_if.h>
700Sstevel@tonic-gate #include <inet/ip_ire.h>
710Sstevel@tonic-gate #include <inet/ip_rts.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include <inet/ipclassifier.h>
740Sstevel@tonic-gate 
75*1676Sjpk #include <sys/tsol/tndb.h>
76*1676Sjpk #include <sys/tsol/tnet.h>
77*1676Sjpk 
78*1676Sjpk #define	RTS_MSG_SIZE(type, rtm_addrs, af, sacnt) \
79*1676Sjpk 	(rts_data_msg_size(rtm_addrs, af, sacnt) + rts_header_msg_size(type))
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static size_t	rts_copyfromsockaddr(struct sockaddr *sa, in6_addr_t *addrp);
820Sstevel@tonic-gate static void	rts_fill_msg(int type, int rtm_addrs, ipaddr_t dst,
830Sstevel@tonic-gate     ipaddr_t mask, ipaddr_t gateway, ipaddr_t src_addr, ipaddr_t brd_addr,
84*1676Sjpk     ipaddr_t author, const ipif_t *ipif, mblk_t *mp, uint_t, const tsol_gc_t *);
850Sstevel@tonic-gate static int	rts_getaddrs(rt_msghdr_t *rtm, in6_addr_t *dst_addrp,
860Sstevel@tonic-gate     in6_addr_t *gw_addrp, in6_addr_t *net_maskp, in6_addr_t *authorp,
870Sstevel@tonic-gate     in6_addr_t *if_addrp, in6_addr_t *src_addrp, ushort_t *indexp,
88*1676Sjpk     ushort_t *src_indexp, sa_family_t *afp, tsol_rtsecattr_t *rtsecattr,
89*1676Sjpk     int *error);
90*1676Sjpk static void	rts_getifdata(if_data_t *if_data, const ipif_t *ipif);
910Sstevel@tonic-gate static int	rts_getmetrics(ire_t *ire, rt_metrics_t *metrics);
920Sstevel@tonic-gate static mblk_t	*rts_rtmget(mblk_t *mp, ire_t *ire, ire_t *sire,
930Sstevel@tonic-gate     sa_family_t af);
940Sstevel@tonic-gate static void	rts_setmetrics(ire_t *ire, uint_t which, rt_metrics_t *metrics);
950Sstevel@tonic-gate static void	ip_rts_request_retry(ipsq_t *, queue_t *q, mblk_t *mp, void *);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Send the ack to all the routing queues.  In case of the originating queue,
990Sstevel@tonic-gate  * send it only if the loopback is set.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Messages are sent upstream only on routing sockets that did not specify an
1020Sstevel@tonic-gate  * address family when they were created or when the address family matches the
1030Sstevel@tonic-gate  * one specified by the caller.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate void
1070Sstevel@tonic-gate rts_queue_input(mblk_t *mp, queue_t *q, sa_family_t af)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	mblk_t	*mp1;
1100Sstevel@tonic-gate 	int	checkqfull;
1110Sstevel@tonic-gate 	conn_t 	*connp, *next_connp;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	mutex_enter(&rts_clients.connf_lock);
1140Sstevel@tonic-gate 	connp = rts_clients.connf_head;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	while (connp != NULL) {
1170Sstevel@tonic-gate 		/*
1180Sstevel@tonic-gate 		 * If there was a family specified when this routing socket was
1190Sstevel@tonic-gate 		 * created and it doesn't match the family of the message to
1200Sstevel@tonic-gate 		 * copy, then continue.
1210Sstevel@tonic-gate 		 */
1220Sstevel@tonic-gate 		if ((connp->conn_proto != AF_UNSPEC) &&
1230Sstevel@tonic-gate 		    (connp->conn_proto != af)) {
1240Sstevel@tonic-gate 			connp = connp->conn_next;
1250Sstevel@tonic-gate 			continue;
1260Sstevel@tonic-gate 		}
1270Sstevel@tonic-gate 		/*
1280Sstevel@tonic-gate 		 * For the originating queue, we only copy the message upstream
1290Sstevel@tonic-gate 		 * if loopback is set.  For others reading on the routing
1300Sstevel@tonic-gate 		 * socket, we check if there is room upstream for a copy of the
1310Sstevel@tonic-gate 		 * message.
1320Sstevel@tonic-gate 		 */
1330Sstevel@tonic-gate 		if ((q != NULL) && (CONNP_TO_RQ(connp) == RD(q))) {
1340Sstevel@tonic-gate 			if (connp->conn_loopback == 0) {
1350Sstevel@tonic-gate 				connp = connp->conn_next;
1360Sstevel@tonic-gate 				continue;
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 			checkqfull = B_FALSE;
1390Sstevel@tonic-gate 		} else {
1400Sstevel@tonic-gate 			checkqfull = B_TRUE;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		CONN_INC_REF(connp);
1430Sstevel@tonic-gate 		mutex_exit(&rts_clients.connf_lock);
1440Sstevel@tonic-gate 		if (!checkqfull || canputnext(CONNP_TO_RQ(connp))) {
1450Sstevel@tonic-gate 			mp1 = dupmsg(mp);
1460Sstevel@tonic-gate 			if (mp1 == NULL)
1470Sstevel@tonic-gate 				mp1 = copymsg(mp);
1480Sstevel@tonic-gate 			if (mp1 != NULL)
1490Sstevel@tonic-gate 				putnext(CONNP_TO_RQ(connp), mp1);
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		mutex_enter(&rts_clients.connf_lock);
1530Sstevel@tonic-gate 		/* Follow the next pointer before releasing the conn. */
1540Sstevel@tonic-gate 		next_connp = connp->conn_next;
1550Sstevel@tonic-gate 		CONN_DEC_REF(connp);
1560Sstevel@tonic-gate 		connp = next_connp;
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	mutex_exit(&rts_clients.connf_lock);
1590Sstevel@tonic-gate 	freemsg(mp);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Takes an ire and sends an ack to all the routing sockets. This
1640Sstevel@tonic-gate  * routine is used
1650Sstevel@tonic-gate  * - when a route is created/deleted through the ioctl interface.
1660Sstevel@tonic-gate  * - when ire_expire deletes a stale redirect
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate void
1690Sstevel@tonic-gate ip_rts_rtmsg(int type, ire_t *ire, int error)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	mblk_t		*mp;
1720Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
1730Sstevel@tonic-gate 	int		rtm_addrs = (RTA_DST | RTA_NETMASK | RTA_GATEWAY);
1740Sstevel@tonic-gate 	sa_family_t	af;
1750Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (ire == NULL)
1780Sstevel@tonic-gate 		return;
1790Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
1800Sstevel@tonic-gate 	    ire->ire_ipversion == IPV6_VERSION);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (ire->ire_flags & RTF_SETSRC)
1830Sstevel@tonic-gate 		rtm_addrs |= RTA_SRC;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	switch (ire->ire_ipversion) {
1860Sstevel@tonic-gate 	case IPV4_VERSION:
1870Sstevel@tonic-gate 		af = AF_INET;
188*1676Sjpk 		mp = rts_alloc_msg(type, rtm_addrs, af, 0);
1890Sstevel@tonic-gate 		if (mp == NULL)
1900Sstevel@tonic-gate 			return;
1910Sstevel@tonic-gate 		rts_fill_msg(type, rtm_addrs, ire->ire_addr, ire->ire_mask,
192*1676Sjpk 		    ire->ire_gateway_addr, ire->ire_src_addr, 0, 0, NULL, mp,
193*1676Sjpk 		    0, NULL);
1940Sstevel@tonic-gate 		break;
1950Sstevel@tonic-gate 	case IPV6_VERSION:
1960Sstevel@tonic-gate 		af = AF_INET6;
197*1676Sjpk 		mp = rts_alloc_msg(type, rtm_addrs, af, 0);
1980Sstevel@tonic-gate 		if (mp == NULL)
1990Sstevel@tonic-gate 			return;
2000Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
2010Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
2020Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
2030Sstevel@tonic-gate 		rts_fill_msg_v6(type, rtm_addrs, &ire->ire_addr_v6,
2040Sstevel@tonic-gate 		    &ire->ire_mask_v6, &gw_addr_v6,
2050Sstevel@tonic-gate 		    &ire->ire_src_addr_v6, &ipv6_all_zeros, &ipv6_all_zeros,
206*1676Sjpk 		    NULL, mp, 0, NULL);
2070Sstevel@tonic-gate 		break;
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
2100Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&mp->b_rptr[rtm->rtm_msglen];
2110Sstevel@tonic-gate 	rtm->rtm_addrs = rtm_addrs;
2120Sstevel@tonic-gate 	rtm->rtm_flags = ire->ire_flags;
2130Sstevel@tonic-gate 	if (error != 0)
2140Sstevel@tonic-gate 		rtm->rtm_errno = error;
2150Sstevel@tonic-gate 	else
2160Sstevel@tonic-gate 		rtm->rtm_flags |= RTF_DONE;
2170Sstevel@tonic-gate 	rts_queue_input(mp, NULL, af);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /* ARGSUSED */
2210Sstevel@tonic-gate static void
2220Sstevel@tonic-gate ip_rts_request_retry(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	(void) ip_rts_request(q, mp, DB_CRED(mp));
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * Processes requests received on a routing socket. It extracts all the
2290Sstevel@tonic-gate  * arguments and calls the appropriate function to process the request.
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * RTA_SRC bit flag requests are sent by mipagent and 'route -setsrc'.
2320Sstevel@tonic-gate  * RTA_SRCIFP bit flag requests are sent by mipagent only.
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * In general, this function does not consume the message supplied but rather
2350Sstevel@tonic-gate  * sends the message upstream with an appropriate UNIX errno.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * We may need to restart this operation if the ipif cannot be looked up
2380Sstevel@tonic-gate  * due to an exclusive operation that is currently in progress. The restart
2390Sstevel@tonic-gate  * entry point is ip_rts_request_retry. While the request is enqueud in the
2400Sstevel@tonic-gate  * ipsq the ioctl could be aborted and the conn close. To ensure that we don't
2410Sstevel@tonic-gate  * have stale conn pointers, ip_wput_ioctl does a conn refhold. This is
2420Sstevel@tonic-gate  * released at the completion of the rts ioctl at the end of this function
2430Sstevel@tonic-gate  * by calling CONN_OPER_PENDING_DONE or when the ioctl is aborted and
2440Sstevel@tonic-gate  * conn close occurs in conn_ioctl_cleanup.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate int
2470Sstevel@tonic-gate ip_rts_request(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	rt_msghdr_t	*rtm = NULL;
2500Sstevel@tonic-gate 	in6_addr_t	dst_addr_v6;
2510Sstevel@tonic-gate 	in6_addr_t	src_addr_v6;
2520Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
2530Sstevel@tonic-gate 	in6_addr_t	net_mask_v6;
2540Sstevel@tonic-gate 	in6_addr_t	author_v6;
2550Sstevel@tonic-gate 	in6_addr_t	if_addr_v6;
2560Sstevel@tonic-gate 	mblk_t		*mp1, *ioc_mp = mp;
2570Sstevel@tonic-gate 	ire_t		*ire = NULL;
2580Sstevel@tonic-gate 	ire_t		*sire = NULL;
2590Sstevel@tonic-gate 	int		error = 0;
2600Sstevel@tonic-gate 	int		match_flags = MATCH_IRE_DSTONLY;
2610Sstevel@tonic-gate 	int		found_addrs;
2620Sstevel@tonic-gate 	sa_family_t	af;
2630Sstevel@tonic-gate 	ipaddr_t	dst_addr;
2640Sstevel@tonic-gate 	ipaddr_t	gw_addr;
2650Sstevel@tonic-gate 	ipaddr_t	src_addr;
2660Sstevel@tonic-gate 	ipaddr_t	net_mask;
2670Sstevel@tonic-gate 	ushort_t	index;
2680Sstevel@tonic-gate 	ushort_t	src_index;
2690Sstevel@tonic-gate 	ipif_t		*ipif = NULL;
2700Sstevel@tonic-gate 	ipif_t		*src_ipif = NULL;
2710Sstevel@tonic-gate 	ipif_t		*tmp_ipif = NULL;
2720Sstevel@tonic-gate 	IOCP		iocp = (IOCP)mp->b_rptr;
2730Sstevel@tonic-gate 	conn_t		*connp;
274*1676Sjpk 	boolean_t	gcgrp_xtraref = B_FALSE;
275*1676Sjpk 	tsol_gcgrp_addr_t ga;
276*1676Sjpk 	tsol_rtsecattr_t rtsecattr;
277*1676Sjpk 	struct rtsa_s	*rtsap = NULL;
278*1676Sjpk 	tsol_gcgrp_t	*gcgrp = NULL;
279*1676Sjpk 	tsol_gc_t	*gc = NULL;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	ip1dbg(("ip_rts_request: mp is %x\n", DB_TYPE(mp)));
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	ASSERT(CONN_Q(q));
2840Sstevel@tonic-gate 	connp = Q_TO_CONN(q);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
2870Sstevel@tonic-gate 	/* ioc_mp holds mp */
2880Sstevel@tonic-gate 	mp = mp->b_cont;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * The Routing Socket data starts on
2920Sstevel@tonic-gate 	 * next block. If there is no next block
2930Sstevel@tonic-gate 	 * this is an indication from routing module
2940Sstevel@tonic-gate 	 * that it is a routing socket stream queue.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
2970Sstevel@tonic-gate 		mp1 = dupmsg(mp->b_cont);
2980Sstevel@tonic-gate 		if (mp1 == NULL) {
2990Sstevel@tonic-gate 			freemsg(mp);
3000Sstevel@tonic-gate 			error  = ENOBUFS;
3010Sstevel@tonic-gate 			goto done;
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 		mp = mp1;
3040Sstevel@tonic-gate 	} else {
3050Sstevel@tonic-gate 		/*
3060Sstevel@tonic-gate 		 * This is a message from RTS module
3070Sstevel@tonic-gate 		 * indicating that this is a Routing Socket
3080Sstevel@tonic-gate 		 * Stream. Insert this conn_t in routing
3090Sstevel@tonic-gate 		 * socket client list.
3100Sstevel@tonic-gate 		 */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		connp->conn_loopback = 1;
3130Sstevel@tonic-gate 		ipcl_hash_insert_wildcard(&rts_clients, connp);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		goto done;
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	if (mp->b_cont != NULL && !pullupmsg(mp, -1)) {
3180Sstevel@tonic-gate 		freemsg(mp);
3190Sstevel@tonic-gate 		error =  EINVAL;
3200Sstevel@tonic-gate 		goto done;
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
3230Sstevel@tonic-gate 		freemsg(mp);
3240Sstevel@tonic-gate 		error = EINVAL;
3250Sstevel@tonic-gate 		goto done;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * Check the routing message for basic consistency including the
3300Sstevel@tonic-gate 	 * version number and that the number of octets written is the same
3310Sstevel@tonic-gate 	 * as specified by the rtm_msglen field.
3320Sstevel@tonic-gate 	 *
3330Sstevel@tonic-gate 	 * At this point, an error can be delivered back via rtm_errno.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
3360Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) != rtm->rtm_msglen) {
3370Sstevel@tonic-gate 		error = EINVAL;
3380Sstevel@tonic-gate 		goto done;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	if (rtm->rtm_version != RTM_VERSION) {
3410Sstevel@tonic-gate 		error = EPROTONOSUPPORT;
3420Sstevel@tonic-gate 		goto done;
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/* Only allow RTM_GET or RTM_RESOLVE for unprivileged process */
3460Sstevel@tonic-gate 	if (rtm->rtm_type != RTM_GET &&
3470Sstevel@tonic-gate 	    rtm->rtm_type != RTM_RESOLVE &&
3480Sstevel@tonic-gate 	    (ioc_cr == NULL ||
3490Sstevel@tonic-gate 	    secpolicy_net_config(ioc_cr, B_FALSE) != 0)) {
3500Sstevel@tonic-gate 		error = EPERM;
3510Sstevel@tonic-gate 		goto done;
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	found_addrs = rts_getaddrs(rtm, &dst_addr_v6, &gw_addr_v6, &net_mask_v6,
355*1676Sjpk 	    &author_v6, &if_addr_v6, &src_addr_v6, &index, &src_index, &af,
356*1676Sjpk 	    &rtsecattr, &error);
357*1676Sjpk 
358*1676Sjpk 	if (error != 0)
359*1676Sjpk 		goto done;
360*1676Sjpk 
3610Sstevel@tonic-gate 	if ((found_addrs & RTA_DST) == 0) {
3620Sstevel@tonic-gate 		error = EINVAL;
3630Sstevel@tonic-gate 		goto done;
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * Based on the address family of the destination address, determine
3680Sstevel@tonic-gate 	 * the destination, gateway and netmask and return the appropriate error
3690Sstevel@tonic-gate 	 * if an unknown address family was specified (following the errno
3700Sstevel@tonic-gate 	 * values that 4.4BSD-Lite2 returns.)
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	switch (af) {
3730Sstevel@tonic-gate 	case AF_INET:
3740Sstevel@tonic-gate 		/*
3750Sstevel@tonic-gate 		 * RTA_SRCIFP is supported for interface route only.
3760Sstevel@tonic-gate 		 * Thus a gateway route with srcifindex is rejected,
3770Sstevel@tonic-gate 		 * except if it's a request to add reverse tunnel
3780Sstevel@tonic-gate 		 * route.
3790Sstevel@tonic-gate 		 */
3800Sstevel@tonic-gate 		if ((rtm->rtm_flags & RTF_GATEWAY) &&
3810Sstevel@tonic-gate 		    (found_addrs & RTA_SRCIFP) &&
3820Sstevel@tonic-gate 		    !(found_addrs & RTA_SRC)) {
3830Sstevel@tonic-gate 			error = EINVAL;
3840Sstevel@tonic-gate 			goto done;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&dst_addr_v6, dst_addr);
3870Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&src_addr_v6, src_addr);
3880Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&gw_addr_v6, gw_addr);
3890Sstevel@tonic-gate 		if (((found_addrs & RTA_NETMASK) == 0) ||
3900Sstevel@tonic-gate 		    (rtm->rtm_flags & RTF_HOST))
3910Sstevel@tonic-gate 			net_mask = IP_HOST_MASK;
3920Sstevel@tonic-gate 		else
3930Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&net_mask_v6, net_mask);
3940Sstevel@tonic-gate 		break;
3950Sstevel@tonic-gate 	case AF_INET6:
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * RTA_SRCIFP is not a valid flag for IPv6 routes.
3980Sstevel@tonic-gate 		 */
3990Sstevel@tonic-gate 		if (found_addrs & RTA_SRCIFP) {
4000Sstevel@tonic-gate 			error = EINVAL;
4010Sstevel@tonic-gate 			goto done;
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		if (((found_addrs & RTA_NETMASK) == 0) ||
4040Sstevel@tonic-gate 		    (rtm->rtm_flags & RTF_HOST))
4050Sstevel@tonic-gate 			net_mask_v6 = ipv6_all_ones;
4060Sstevel@tonic-gate 		break;
4070Sstevel@tonic-gate 	default:
4080Sstevel@tonic-gate 		/*
4090Sstevel@tonic-gate 		 * These errno values are meant to be compatible with
4100Sstevel@tonic-gate 		 * 4.4BSD-Lite2 for the given message types.
4110Sstevel@tonic-gate 		 */
4120Sstevel@tonic-gate 		switch (rtm->rtm_type) {
4130Sstevel@tonic-gate 		case RTM_ADD:
4140Sstevel@tonic-gate 		case RTM_DELETE:
4150Sstevel@tonic-gate 			error = ESRCH;
4160Sstevel@tonic-gate 			goto done;
4170Sstevel@tonic-gate 		case RTM_GET:
4180Sstevel@tonic-gate 		case RTM_CHANGE:
4190Sstevel@tonic-gate 			error = EAFNOSUPPORT;
4200Sstevel@tonic-gate 			goto done;
4210Sstevel@tonic-gate 		default:
4220Sstevel@tonic-gate 			error = EOPNOTSUPP;
4230Sstevel@tonic-gate 			goto done;
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * At this point, the address family must be something known.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	ASSERT(af == AF_INET || af == AF_INET6);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (index != 0) {
4330Sstevel@tonic-gate 		ill_t   *ill;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		/*
4360Sstevel@tonic-gate 		 * IPC must be refheld somewhere in ip_wput_nondata or
4370Sstevel@tonic-gate 		 * ip_wput_ioctl etc... and cleaned up if ioctl is killed.
4380Sstevel@tonic-gate 		 * If ILL_CHANGING the request is queued in the ipsq.
4390Sstevel@tonic-gate 		 */
4400Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(index, af == AF_INET6,
4410Sstevel@tonic-gate 		    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry, &error);
4420Sstevel@tonic-gate 		if (ill == NULL) {
4430Sstevel@tonic-gate 			if (error != EINPROGRESS)
4440Sstevel@tonic-gate 				error = EINVAL;
4450Sstevel@tonic-gate 			goto done;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 		ipif = ipif_get_next_ipif(NULL, ill);
4490Sstevel@tonic-gate 		ill_refrele(ill);
4500Sstevel@tonic-gate 		/*
4510Sstevel@tonic-gate 		 * If this is replacement ipif, prevent a route from
4520Sstevel@tonic-gate 		 * being added.
4530Sstevel@tonic-gate 		 */
4540Sstevel@tonic-gate 		if (ipif != NULL && ipif->ipif_replace_zero) {
4550Sstevel@tonic-gate 			error = ENETDOWN;
4560Sstevel@tonic-gate 			goto done;
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/* RTA_SRCIFP is unsupported on AF_INET6. */
4620Sstevel@tonic-gate 	if (af == AF_INET && src_index != 0) {
4630Sstevel@tonic-gate 		ill_t   *ill;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		/* If ILL_CHANGING the request is queued in the ipsq. */
4660Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(src_index, B_FALSE,
4670Sstevel@tonic-gate 		    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry, &error);
4680Sstevel@tonic-gate 		if (ill == NULL) {
4690Sstevel@tonic-gate 			if (error != EINPROGRESS)
4700Sstevel@tonic-gate 				error = EINVAL;
4710Sstevel@tonic-gate 			goto done;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		src_ipif = ipif_get_next_ipif(NULL, ill);
4750Sstevel@tonic-gate 		ill_refrele(ill);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	/*
4780Sstevel@tonic-gate 	 * If a netmask was supplied in the message, then subsequent route
4790Sstevel@tonic-gate 	 * lookups will attempt to match on the netmask as well.
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	if ((found_addrs & RTA_NETMASK) != 0)
4820Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
4830Sstevel@tonic-gate 
484*1676Sjpk 	/*
485*1676Sjpk 	 * We only process any passed-in route security attributes for
486*1676Sjpk 	 * either RTM_ADD or RTM_CHANGE message; ignore otherwise.
487*1676Sjpk 	 */
488*1676Sjpk 	if (rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE) {
489*1676Sjpk 		ASSERT(rtsecattr.rtsa_cnt <= TSOL_RTSA_REQUEST_MAX);
490*1676Sjpk 		if (rtsecattr.rtsa_cnt > 0)
491*1676Sjpk 			rtsap = &rtsecattr.rtsa_attr[0];
492*1676Sjpk 	}
493*1676Sjpk 
4940Sstevel@tonic-gate 	switch (rtm->rtm_type) {
4950Sstevel@tonic-gate 	case RTM_ADD:
4960Sstevel@tonic-gate 		/* if we are adding a route, gateway is a must */
4970Sstevel@tonic-gate 		if ((found_addrs & RTA_GATEWAY) == 0) {
4980Sstevel@tonic-gate 			error = EINVAL;
4990Sstevel@tonic-gate 			goto done;
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		/* Multirouting does not support net routes. */
5030Sstevel@tonic-gate 		if ((rtm->rtm_flags & (RTF_MULTIRT | RTF_HOST)) ==
5040Sstevel@tonic-gate 		    RTF_MULTIRT) {
5050Sstevel@tonic-gate 			error = EADDRNOTAVAIL;
5060Sstevel@tonic-gate 			goto done;
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		/*
5100Sstevel@tonic-gate 		 * Multirouting and user-specified source addresses
5110Sstevel@tonic-gate 		 * do not support interface based routing.
5120Sstevel@tonic-gate 		 * Assigning a source address to an interface based
5130Sstevel@tonic-gate 		 * route is achievable by plumbing a new ipif and
5140Sstevel@tonic-gate 		 * setting up the interface route via this ipif,
5150Sstevel@tonic-gate 		 * though.
5160Sstevel@tonic-gate 		 */
5170Sstevel@tonic-gate 		if (rtm->rtm_flags & (RTF_MULTIRT | RTF_SETSRC)) {
5180Sstevel@tonic-gate 			if ((rtm->rtm_flags & RTF_GATEWAY) == 0) {
5190Sstevel@tonic-gate 				error = EADDRNOTAVAIL;
5200Sstevel@tonic-gate 				goto done;
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		switch (af) {
5250Sstevel@tonic-gate 		case AF_INET:
5260Sstevel@tonic-gate 			if (src_addr != INADDR_ANY) {
5270Sstevel@tonic-gate 				/*
5280Sstevel@tonic-gate 				 * If there is a source address, but
5290Sstevel@tonic-gate 				 * no RTF_SETSRC modifier, setup a MobileIP
5300Sstevel@tonic-gate 				 * reverse tunnel.
5310Sstevel@tonic-gate 				 */
5320Sstevel@tonic-gate 				if ((rtm->rtm_flags & RTF_SETSRC) == 0) {
5330Sstevel@tonic-gate 					error = ip_mrtun_rt_add(src_addr,
5340Sstevel@tonic-gate 					    rtm->rtm_flags, ipif,
5350Sstevel@tonic-gate 					    src_ipif, &ire, CONNP_TO_WQ(connp),
5360Sstevel@tonic-gate 					    ioc_mp, ip_rts_request_retry);
5370Sstevel@tonic-gate 					break;
5380Sstevel@tonic-gate 				}
5390Sstevel@tonic-gate 				/*
5400Sstevel@tonic-gate 				 * The RTF_SETSRC flag is present, check that
5410Sstevel@tonic-gate 				 * the supplied src address is not the loopback
5420Sstevel@tonic-gate 				 * address. This would produce martian packets.
5430Sstevel@tonic-gate 				 */
5440Sstevel@tonic-gate 				if (src_addr == htonl(INADDR_LOOPBACK)) {
5450Sstevel@tonic-gate 					error = EINVAL;
5460Sstevel@tonic-gate 					goto done;
5470Sstevel@tonic-gate 				}
5480Sstevel@tonic-gate 				/*
5490Sstevel@tonic-gate 				 * Also check that the supplied address is a
5500Sstevel@tonic-gate 				 * valid, local one.
5510Sstevel@tonic-gate 				 */
5520Sstevel@tonic-gate 				tmp_ipif = ipif_lookup_addr(src_addr, NULL,
5530Sstevel@tonic-gate 				    ALL_ZONES, CONNP_TO_WQ(connp), ioc_mp,
5540Sstevel@tonic-gate 				    ip_rts_request_retry, &error);
5550Sstevel@tonic-gate 				if (tmp_ipif == NULL) {
5560Sstevel@tonic-gate 					if (error != EINPROGRESS)
5570Sstevel@tonic-gate 						error = EADDRNOTAVAIL;
5580Sstevel@tonic-gate 					goto done;
5590Sstevel@tonic-gate 				}
5600Sstevel@tonic-gate 				if (!(tmp_ipif->ipif_flags & IPIF_UP) ||
5610Sstevel@tonic-gate 				    (tmp_ipif->ipif_flags &
5620Sstevel@tonic-gate 				    (IPIF_NOLOCAL | IPIF_ANYCAST))) {
5630Sstevel@tonic-gate 					error = EINVAL;
5640Sstevel@tonic-gate 					goto done;
5650Sstevel@tonic-gate 				}
5660Sstevel@tonic-gate 			} else {
5670Sstevel@tonic-gate 				/*
5680Sstevel@tonic-gate 				 * The RTF_SETSRC modifier must be associated
5690Sstevel@tonic-gate 				 * to a non-null source address.
5700Sstevel@tonic-gate 				 */
5710Sstevel@tonic-gate 				if (rtm->rtm_flags & RTF_SETSRC) {
5720Sstevel@tonic-gate 					error = EINVAL;
5730Sstevel@tonic-gate 					goto done;
5740Sstevel@tonic-gate 				}
5750Sstevel@tonic-gate 			}
5760Sstevel@tonic-gate 
577*1676Sjpk 			error = ip_rt_add(dst_addr, net_mask, gw_addr, src_addr,
5780Sstevel@tonic-gate 			    rtm->rtm_flags, ipif, src_ipif, &ire, B_FALSE,
579*1676Sjpk 			    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry,
580*1676Sjpk 			    rtsap);
5810Sstevel@tonic-gate 			if (ipif != NULL)
5820Sstevel@tonic-gate 				ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5830Sstevel@tonic-gate 			break;
5840Sstevel@tonic-gate 		case AF_INET6:
5850Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&src_addr_v6)) {
5860Sstevel@tonic-gate 				/*
5870Sstevel@tonic-gate 				 * If there is a source address, but
5880Sstevel@tonic-gate 				 * no RTF_SETSRC modifier, reject, as
5890Sstevel@tonic-gate 				 * MobileIP IPv6 reverse tunnels are
5900Sstevel@tonic-gate 				 * not supported.
5910Sstevel@tonic-gate 				 */
5920Sstevel@tonic-gate 				if ((rtm->rtm_flags & RTF_SETSRC) == 0) {
5930Sstevel@tonic-gate 					error = EINVAL;
5940Sstevel@tonic-gate 					goto done;
5950Sstevel@tonic-gate 				}
5960Sstevel@tonic-gate 				/*
5970Sstevel@tonic-gate 				 * The RTF_SETSRC flag is present, check that
5980Sstevel@tonic-gate 				 * the supplied src address is not the loopback
5990Sstevel@tonic-gate 				 * address. This would produce martian packets.
6000Sstevel@tonic-gate 				 */
6010Sstevel@tonic-gate 				if (IN6_IS_ADDR_LOOPBACK(&src_addr_v6)) {
6020Sstevel@tonic-gate 					error = EINVAL;
6030Sstevel@tonic-gate 					goto done;
6040Sstevel@tonic-gate 				}
6050Sstevel@tonic-gate 				/*
6060Sstevel@tonic-gate 				 * Also check that the supplied address is a
6070Sstevel@tonic-gate 				 * valid, local one.
6080Sstevel@tonic-gate 				 */
6090Sstevel@tonic-gate 				tmp_ipif = ipif_lookup_addr_v6(&src_addr_v6,
6100Sstevel@tonic-gate 				    NULL, ALL_ZONES, CONNP_TO_WQ(connp), ioc_mp,
6110Sstevel@tonic-gate 				    ip_rts_request_retry, &error);
6120Sstevel@tonic-gate 				if (tmp_ipif == NULL) {
6130Sstevel@tonic-gate 					if (error != EINPROGRESS)
6140Sstevel@tonic-gate 						error = EADDRNOTAVAIL;
6150Sstevel@tonic-gate 					goto done;
6160Sstevel@tonic-gate 				}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 				if (!(tmp_ipif->ipif_flags & IPIF_UP) ||
6190Sstevel@tonic-gate 				    (tmp_ipif->ipif_flags &
6200Sstevel@tonic-gate 				    (IPIF_NOLOCAL | IPIF_ANYCAST))) {
6210Sstevel@tonic-gate 					error = EINVAL;
6220Sstevel@tonic-gate 					goto done;
6230Sstevel@tonic-gate 				}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 				error = ip_rt_add_v6(&dst_addr_v6, &net_mask_v6,
6260Sstevel@tonic-gate 				    &gw_addr_v6, &src_addr_v6, rtm->rtm_flags,
6270Sstevel@tonic-gate 				    ipif, &ire, CONNP_TO_WQ(connp), ioc_mp,
628*1676Sjpk 				    ip_rts_request_retry, rtsap);
6290Sstevel@tonic-gate 				break;
6300Sstevel@tonic-gate 			}
6310Sstevel@tonic-gate 			/*
6320Sstevel@tonic-gate 			 * The RTF_SETSRC modifier must be associated
6330Sstevel@tonic-gate 			 * to a non-null source address.
6340Sstevel@tonic-gate 			 */
6350Sstevel@tonic-gate 			if (rtm->rtm_flags & RTF_SETSRC) {
6360Sstevel@tonic-gate 				error = EINVAL;
6370Sstevel@tonic-gate 				goto done;
6380Sstevel@tonic-gate 			}
6390Sstevel@tonic-gate 			error = ip_rt_add_v6(&dst_addr_v6, &net_mask_v6,
6400Sstevel@tonic-gate 			    &gw_addr_v6, NULL, rtm->rtm_flags,
6410Sstevel@tonic-gate 			    ipif, &ire, CONNP_TO_WQ(connp), ioc_mp,
642*1676Sjpk 			    ip_rts_request_retry, rtsap);
6430Sstevel@tonic-gate 			if (ipif != NULL)
6440Sstevel@tonic-gate 				ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6450Sstevel@tonic-gate 			break;
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 		if (error != 0)
6480Sstevel@tonic-gate 			goto done;
6490Sstevel@tonic-gate 		ASSERT(ire != NULL);
6500Sstevel@tonic-gate 		rts_setmetrics(ire, rtm->rtm_inits, &rtm->rtm_rmx);
6510Sstevel@tonic-gate 		break;
6520Sstevel@tonic-gate 	case RTM_DELETE:
6530Sstevel@tonic-gate 		/* if we are deleting a route, gateway is a must */
6540Sstevel@tonic-gate 		if ((found_addrs & RTA_GATEWAY) == 0) {
6550Sstevel@tonic-gate 			error = EINVAL;
6560Sstevel@tonic-gate 			goto done;
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		/*
6590Sstevel@tonic-gate 		 * The RTF_SETSRC modifier does not make sense
6600Sstevel@tonic-gate 		 * when deleting a route.
6610Sstevel@tonic-gate 		 */
6620Sstevel@tonic-gate 		if (rtm->rtm_flags & RTF_SETSRC) {
6630Sstevel@tonic-gate 			error = EINVAL;
6640Sstevel@tonic-gate 			goto done;
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		switch (af) {
6680Sstevel@tonic-gate 		case AF_INET:
6690Sstevel@tonic-gate 			/*
6700Sstevel@tonic-gate 			 * If there is a source address, delete
6710Sstevel@tonic-gate 			 * a MobileIP reverse tunnel.
6720Sstevel@tonic-gate 			 */
6730Sstevel@tonic-gate 			if (src_addr != INADDR_ANY) {
6740Sstevel@tonic-gate 				error = ip_mrtun_rt_delete(src_addr,
6750Sstevel@tonic-gate 				    src_ipif);
6760Sstevel@tonic-gate 				break;
6770Sstevel@tonic-gate 			}
6780Sstevel@tonic-gate 			error = ip_rt_delete(dst_addr, net_mask, gw_addr,
6790Sstevel@tonic-gate 			    found_addrs, rtm->rtm_flags, ipif, src_ipif,
6800Sstevel@tonic-gate 			    B_FALSE, CONNP_TO_WQ(connp), ioc_mp,
6810Sstevel@tonic-gate 			    ip_rts_request_retry);
6820Sstevel@tonic-gate 			break;
6830Sstevel@tonic-gate 		case AF_INET6:
6840Sstevel@tonic-gate 			error = ip_rt_delete_v6(&dst_addr_v6, &net_mask_v6,
6850Sstevel@tonic-gate 			    &gw_addr_v6, found_addrs, rtm->rtm_flags, ipif,
6860Sstevel@tonic-gate 			    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry);
6870Sstevel@tonic-gate 			break;
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 		break;
6900Sstevel@tonic-gate 	case RTM_GET:
6910Sstevel@tonic-gate 	case RTM_CHANGE:
6920Sstevel@tonic-gate 		/*
6930Sstevel@tonic-gate 		 * In the case of RTM_GET, the forwarding table should be
6940Sstevel@tonic-gate 		 * searched recursively with default being matched if the
6950Sstevel@tonic-gate 		 * specific route doesn't exist.  Also, if a gateway was
6960Sstevel@tonic-gate 		 * specified then the gateway address must also be matched.
6970Sstevel@tonic-gate 		 *
6980Sstevel@tonic-gate 		 * In the case of RTM_CHANGE, the gateway address (if supplied)
6990Sstevel@tonic-gate 		 * is the new gateway address so matching on the gateway address
7000Sstevel@tonic-gate 		 * is not done.  This can lead to ambiguity when looking up the
7010Sstevel@tonic-gate 		 * route to change as usually only the destination (and netmask,
7020Sstevel@tonic-gate 		 * if supplied) is used for the lookup.  However if a RTA_IFP
7030Sstevel@tonic-gate 		 * sockaddr is also supplied, it can disambiguate which route to
7040Sstevel@tonic-gate 		 * change provided the ambigous routes are tied to distinct
7050Sstevel@tonic-gate 		 * ill's (or interface indices).  If the routes are not tied to
7060Sstevel@tonic-gate 		 * any particular interfaces (for example, with traditional
7070Sstevel@tonic-gate 		 * gateway routes), then a RTA_IFP sockaddr will be of no use as
7080Sstevel@tonic-gate 		 * it won't match any such routes.
7090Sstevel@tonic-gate 		 * RTA_SRC is not supported for RTM_GET and RTM_CHANGE,
7100Sstevel@tonic-gate 		 * except when RTM_CHANGE is combined to RTF_SETSRC.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		if (((found_addrs & RTA_SRC) != 0) &&
7130Sstevel@tonic-gate 		    ((rtm->rtm_type == RTM_GET) ||
7140Sstevel@tonic-gate 		    !(rtm->rtm_flags & RTF_SETSRC))) {
7150Sstevel@tonic-gate 			error = EOPNOTSUPP;
7160Sstevel@tonic-gate 			goto done;
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		if (rtm->rtm_type == RTM_GET) {
7200Sstevel@tonic-gate 			match_flags |=
7210Sstevel@tonic-gate 			    (MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE);
7220Sstevel@tonic-gate 			if ((found_addrs & RTA_GATEWAY) != 0)
7230Sstevel@tonic-gate 				match_flags |= MATCH_IRE_GW;
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 		if (rtm->rtm_type == RTM_CHANGE) {
7260Sstevel@tonic-gate 			if ((found_addrs & RTA_GATEWAY) &&
7270Sstevel@tonic-gate 			    (rtm->rtm_flags & RTF_SETSRC)) {
7280Sstevel@tonic-gate 				/*
7290Sstevel@tonic-gate 				 * Do not want to change the gateway,
7300Sstevel@tonic-gate 				 * but rather the source address.
7310Sstevel@tonic-gate 				 */
7320Sstevel@tonic-gate 				match_flags |= MATCH_IRE_GW;
7330Sstevel@tonic-gate 			}
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		/*
7370Sstevel@tonic-gate 		 * If the netmask is all ones (either as supplied or as derived
7380Sstevel@tonic-gate 		 * above), then first check for an IRE_LOOPBACK or
7390Sstevel@tonic-gate 		 * IRE_LOCAL entry.
7400Sstevel@tonic-gate 		 *
7410Sstevel@tonic-gate 		 * If we didn't check for or find an IRE_LOOPBACK or IRE_LOCAL
7420Sstevel@tonic-gate 		 * entry, then look in the forwarding table.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		switch (af) {
7450Sstevel@tonic-gate 		case AF_INET:
7460Sstevel@tonic-gate 			if (net_mask == IP_HOST_MASK) {
7470Sstevel@tonic-gate 				ire = ire_ctable_lookup(dst_addr, gw_addr,
7480Sstevel@tonic-gate 				    IRE_LOCAL | IRE_LOOPBACK, NULL, ALL_ZONES,
749*1676Sjpk 				    NULL, MATCH_IRE_TYPE | MATCH_IRE_GW);
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 			if (ire == NULL) {
7520Sstevel@tonic-gate 				ire = ire_ftable_lookup(dst_addr, net_mask,
7530Sstevel@tonic-gate 				    gw_addr, 0, ipif, &sire, ALL_ZONES, 0,
754*1676Sjpk 				    NULL, match_flags);
7550Sstevel@tonic-gate 			}
7560Sstevel@tonic-gate 			break;
7570Sstevel@tonic-gate 		case AF_INET6:
7580Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&net_mask_v6, &ipv6_all_ones)) {
7590Sstevel@tonic-gate 				ire = ire_ctable_lookup_v6(&dst_addr_v6,
7600Sstevel@tonic-gate 				    &gw_addr_v6, IRE_LOCAL | IRE_LOOPBACK, NULL,
761*1676Sjpk 				    ALL_ZONES, NULL,
762*1676Sjpk 				    MATCH_IRE_TYPE | MATCH_IRE_GW);
7630Sstevel@tonic-gate 			}
7640Sstevel@tonic-gate 			if (ire == NULL) {
7650Sstevel@tonic-gate 				ire = ire_ftable_lookup_v6(&dst_addr_v6,
7660Sstevel@tonic-gate 				    &net_mask_v6, &gw_addr_v6, 0, ipif, &sire,
767*1676Sjpk 				    ALL_ZONES, 0, NULL, match_flags);
7680Sstevel@tonic-gate 			}
7690Sstevel@tonic-gate 			break;
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		if (ire == NULL) {
7730Sstevel@tonic-gate 			error = ESRCH;
7740Sstevel@tonic-gate 			goto done;
7750Sstevel@tonic-gate 		}
7760Sstevel@tonic-gate 		/* we know the IRE before we come here */
7770Sstevel@tonic-gate 		switch (rtm->rtm_type) {
7780Sstevel@tonic-gate 		case RTM_GET:
7790Sstevel@tonic-gate 			mp1 = rts_rtmget(mp, ire, sire, af);
7800Sstevel@tonic-gate 			if (mp1 == NULL) {
7810Sstevel@tonic-gate 				error = ENOBUFS;
7820Sstevel@tonic-gate 				goto done;
7830Sstevel@tonic-gate 			}
7840Sstevel@tonic-gate 			freemsg(mp);
7850Sstevel@tonic-gate 			mp = mp1;
7860Sstevel@tonic-gate 			rtm = (rt_msghdr_t *)mp->b_rptr;
7870Sstevel@tonic-gate 			break;
7880Sstevel@tonic-gate 		case RTM_CHANGE:
7890Sstevel@tonic-gate 			/*
7900Sstevel@tonic-gate 			 * Do not allow to the multirouting state of a route
7910Sstevel@tonic-gate 			 * to be changed. This aims to prevent undesirable
7920Sstevel@tonic-gate 			 * stages where both multirt and non-multirt routes
7930Sstevel@tonic-gate 			 * for the same destination are declared.
7940Sstevel@tonic-gate 			 */
7950Sstevel@tonic-gate 			if ((ire->ire_flags & RTF_MULTIRT) !=
7960Sstevel@tonic-gate 			    (rtm->rtm_flags & RTF_MULTIRT)) {
7970Sstevel@tonic-gate 				error = EINVAL;
7980Sstevel@tonic-gate 				goto done;
7990Sstevel@tonic-gate 			}
8000Sstevel@tonic-gate 			/*
8010Sstevel@tonic-gate 			 * Note that we do not need to do
8020Sstevel@tonic-gate 			 * ire_flush_cache_*(IRE_FLUSH_ADD) as a change
8030Sstevel@tonic-gate 			 * in metrics or gateway will not affect existing
8040Sstevel@tonic-gate 			 * routes since it does not create a more specific
8050Sstevel@tonic-gate 			 * route.
8060Sstevel@tonic-gate 			 */
8070Sstevel@tonic-gate 			switch (af) {
8080Sstevel@tonic-gate 			case AF_INET:
8090Sstevel@tonic-gate 				ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
8100Sstevel@tonic-gate 				if ((found_addrs & RTA_GATEWAY) != 0 &&
8110Sstevel@tonic-gate 				    (ire->ire_gateway_addr != gw_addr)) {
8120Sstevel@tonic-gate 					ire->ire_gateway_addr = gw_addr;
8130Sstevel@tonic-gate 				}
814*1676Sjpk 
815*1676Sjpk 				if (rtsap != NULL) {
816*1676Sjpk 					ga.ga_af = AF_INET;
817*1676Sjpk 					IN6_IPADDR_TO_V4MAPPED(
818*1676Sjpk 					    ire->ire_gateway_addr, &ga.ga_addr);
819*1676Sjpk 
820*1676Sjpk 					gcgrp = gcgrp_lookup(&ga, B_TRUE);
821*1676Sjpk 					if (gcgrp == NULL) {
822*1676Sjpk 						error = ENOMEM;
823*1676Sjpk 						goto done;
824*1676Sjpk 					}
825*1676Sjpk 				}
826*1676Sjpk 
8270Sstevel@tonic-gate 				if ((found_addrs & RTA_SRC) != 0 &&
8280Sstevel@tonic-gate 				    (rtm->rtm_flags & RTF_SETSRC) != 0 &&
8290Sstevel@tonic-gate 				    (ire->ire_src_addr != src_addr)) {
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 					if (src_addr != INADDR_ANY) {
8320Sstevel@tonic-gate 						/*
8330Sstevel@tonic-gate 						 * The RTF_SETSRC flag is
8340Sstevel@tonic-gate 						 * present, check that the
8350Sstevel@tonic-gate 						 * supplied src address is not
8360Sstevel@tonic-gate 						 * the loopback address. This
8370Sstevel@tonic-gate 						 * would produce martian
8380Sstevel@tonic-gate 						 * packets.
8390Sstevel@tonic-gate 						 */
8400Sstevel@tonic-gate 						if (src_addr ==
8410Sstevel@tonic-gate 						    htonl(INADDR_LOOPBACK)) {
8420Sstevel@tonic-gate 							error = EINVAL;
8430Sstevel@tonic-gate 							goto done;
8440Sstevel@tonic-gate 						}
8450Sstevel@tonic-gate 						/*
8460Sstevel@tonic-gate 						 * Also check that the the
8470Sstevel@tonic-gate 						 * supplied addr is a valid
8480Sstevel@tonic-gate 						 * local address.
8490Sstevel@tonic-gate 						 */
8500Sstevel@tonic-gate 						tmp_ipif = ipif_lookup_addr(
8510Sstevel@tonic-gate 						    src_addr, NULL, ALL_ZONES,
8520Sstevel@tonic-gate 						    CONNP_TO_WQ(connp), ioc_mp,
8530Sstevel@tonic-gate 						    ip_rts_request_retry,
8540Sstevel@tonic-gate 						    &error);
8550Sstevel@tonic-gate 						if (tmp_ipif == NULL) {
8560Sstevel@tonic-gate 							error = (error ==
8570Sstevel@tonic-gate 							    EINPROGRESS) ?
8580Sstevel@tonic-gate 							    error :
8590Sstevel@tonic-gate 							    EADDRNOTAVAIL;
8600Sstevel@tonic-gate 							goto done;
8610Sstevel@tonic-gate 						}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 						if (!(tmp_ipif->ipif_flags &
8640Sstevel@tonic-gate 						    IPIF_UP) ||
8650Sstevel@tonic-gate 						    (tmp_ipif->ipif_flags &
8660Sstevel@tonic-gate 						    (IPIF_NOLOCAL |
8670Sstevel@tonic-gate 						    IPIF_ANYCAST))) {
8680Sstevel@tonic-gate 							error = EINVAL;
8690Sstevel@tonic-gate 							goto done;
8700Sstevel@tonic-gate 						}
8710Sstevel@tonic-gate 						ire->ire_flags |= RTF_SETSRC;
8720Sstevel@tonic-gate 					} else {
8730Sstevel@tonic-gate 						ire->ire_flags &= ~RTF_SETSRC;
8740Sstevel@tonic-gate 					}
8750Sstevel@tonic-gate 					ire->ire_src_addr = src_addr;
8760Sstevel@tonic-gate 				}
8770Sstevel@tonic-gate 				break;
8780Sstevel@tonic-gate 			case AF_INET6:
8790Sstevel@tonic-gate 				ire_flush_cache_v6(ire, IRE_FLUSH_DELETE);
8800Sstevel@tonic-gate 				mutex_enter(&ire->ire_lock);
8810Sstevel@tonic-gate 				if ((found_addrs & RTA_GATEWAY) != 0 &&
8820Sstevel@tonic-gate 				    !IN6_ARE_ADDR_EQUAL(
8830Sstevel@tonic-gate 				    &ire->ire_gateway_addr_v6, &gw_addr_v6)) {
8840Sstevel@tonic-gate 					ire->ire_gateway_addr_v6 = gw_addr_v6;
8850Sstevel@tonic-gate 				}
886*1676Sjpk 
887*1676Sjpk 				if (rtsap != NULL) {
888*1676Sjpk 					ga.ga_af = AF_INET6;
889*1676Sjpk 					ga.ga_addr = ire->ire_gateway_addr_v6;
890*1676Sjpk 
891*1676Sjpk 					gcgrp = gcgrp_lookup(&ga, B_TRUE);
892*1676Sjpk 					if (gcgrp == NULL) {
893*1676Sjpk 						error = ENOMEM;
894*1676Sjpk 						goto done;
895*1676Sjpk 					}
896*1676Sjpk 				}
897*1676Sjpk 
8980Sstevel@tonic-gate 				if ((found_addrs & RTA_SRC) != 0 &&
8990Sstevel@tonic-gate 				    (rtm->rtm_flags & RTF_SETSRC) != 0 &&
9000Sstevel@tonic-gate 				    !IN6_ARE_ADDR_EQUAL(
9010Sstevel@tonic-gate 					&ire->ire_src_addr_v6, &src_addr_v6)) {
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 					if (!IN6_IS_ADDR_UNSPECIFIED(
9040Sstevel@tonic-gate 					    &src_addr_v6)) {
9050Sstevel@tonic-gate 						/*
9060Sstevel@tonic-gate 						 * The RTF_SETSRC flag is
9070Sstevel@tonic-gate 						 * present, check that the
9080Sstevel@tonic-gate 						 * supplied src address is not
9090Sstevel@tonic-gate 						 * the loopback address. This
9100Sstevel@tonic-gate 						 * would produce martian
9110Sstevel@tonic-gate 						 * packets.
9120Sstevel@tonic-gate 						 */
9130Sstevel@tonic-gate 						if (IN6_IS_ADDR_LOOPBACK(
9140Sstevel@tonic-gate 						    &src_addr_v6)) {
9150Sstevel@tonic-gate 							mutex_exit(
9160Sstevel@tonic-gate 							    &ire->ire_lock);
9170Sstevel@tonic-gate 							error = EINVAL;
9180Sstevel@tonic-gate 							goto done;
9190Sstevel@tonic-gate 						}
9200Sstevel@tonic-gate 						/*
9210Sstevel@tonic-gate 						 * Also check that the the
9220Sstevel@tonic-gate 						 * supplied addr is a valid
9230Sstevel@tonic-gate 						 * local address.
9240Sstevel@tonic-gate 						 */
9250Sstevel@tonic-gate 						tmp_ipif = ipif_lookup_addr_v6(
9260Sstevel@tonic-gate 						    &src_addr_v6, NULL,
9270Sstevel@tonic-gate 						    ALL_ZONES,
9280Sstevel@tonic-gate 						    CONNP_TO_WQ(connp), ioc_mp,
9290Sstevel@tonic-gate 						    ip_rts_request_retry,
9300Sstevel@tonic-gate 						    &error);
9310Sstevel@tonic-gate 						if (tmp_ipif == NULL) {
9320Sstevel@tonic-gate 							mutex_exit(
9330Sstevel@tonic-gate 							    &ire->ire_lock);
9340Sstevel@tonic-gate 							error = (error ==
9350Sstevel@tonic-gate 							    EINPROGRESS) ?
9360Sstevel@tonic-gate 							    error :
9370Sstevel@tonic-gate 							    EADDRNOTAVAIL;
9380Sstevel@tonic-gate 							goto done;
9390Sstevel@tonic-gate 						}
9400Sstevel@tonic-gate 						if (!(tmp_ipif->ipif_flags &
9410Sstevel@tonic-gate 						    IPIF_UP) ||
9420Sstevel@tonic-gate 						    (tmp_ipif->ipif_flags &
9430Sstevel@tonic-gate 						    (IPIF_NOLOCAL |
9440Sstevel@tonic-gate 						    IPIF_ANYCAST))) {
9450Sstevel@tonic-gate 							mutex_exit(
9460Sstevel@tonic-gate 							    &ire->ire_lock);
9470Sstevel@tonic-gate 							error = EINVAL;
9480Sstevel@tonic-gate 							goto done;
9490Sstevel@tonic-gate 						}
9500Sstevel@tonic-gate 						ire->ire_flags |= RTF_SETSRC;
9510Sstevel@tonic-gate 					} else {
9520Sstevel@tonic-gate 						ire->ire_flags &= ~RTF_SETSRC;
9530Sstevel@tonic-gate 					}
9540Sstevel@tonic-gate 					ire->ire_src_addr_v6 = src_addr_v6;
9550Sstevel@tonic-gate 				}
9560Sstevel@tonic-gate 				mutex_exit(&ire->ire_lock);
9570Sstevel@tonic-gate 				break;
9580Sstevel@tonic-gate 			}
959*1676Sjpk 
960*1676Sjpk 			if (rtsap != NULL) {
961*1676Sjpk 				in_addr_t ga_addr4;
962*1676Sjpk 
963*1676Sjpk 				ASSERT(gcgrp != NULL);
964*1676Sjpk 
965*1676Sjpk 				/*
966*1676Sjpk 				 * Create and add the security attribute to
967*1676Sjpk 				 * prefix IRE; it will add a reference to the
968*1676Sjpk 				 * group upon allocating a new entry.  If it
969*1676Sjpk 				 * finds an already-existing entry for the
970*1676Sjpk 				 * security attribute, it simply returns it
971*1676Sjpk 				 * and no new group reference is made.
972*1676Sjpk 				 */
973*1676Sjpk 				gc = gc_create(rtsap, gcgrp, &gcgrp_xtraref);
974*1676Sjpk 				if (gc == NULL ||
975*1676Sjpk 				    (error = tsol_ire_init_gwattr(ire,
976*1676Sjpk 				    ire->ire_ipversion, gc, NULL)) != 0) {
977*1676Sjpk 					if (gc != NULL) {
978*1676Sjpk 						GC_REFRELE(gc);
979*1676Sjpk 					} else {
980*1676Sjpk 						/* gc_create failed */
981*1676Sjpk 						error = ENOMEM;
982*1676Sjpk 					}
983*1676Sjpk 					goto done;
984*1676Sjpk 				}
985*1676Sjpk 
986*1676Sjpk 				/*
987*1676Sjpk 				 * Now delete any existing gateway IRE caches
988*1676Sjpk 				 * as well as all caches using the gateway,
989*1676Sjpk 				 * and allow them to be created on demand
990*1676Sjpk 				 * through ip_newroute{_v6}.
991*1676Sjpk 				 */
992*1676Sjpk 				IN6_V4MAPPED_TO_IPADDR(&ga.ga_addr, ga_addr4);
993*1676Sjpk 				if (af == AF_INET) {
994*1676Sjpk 					ire_clookup_delete_cache_gw(
995*1676Sjpk 					    ga_addr4, ALL_ZONES);
996*1676Sjpk 				} else {
997*1676Sjpk 					ire_clookup_delete_cache_gw_v6(
998*1676Sjpk 					    &ga.ga_addr, ALL_ZONES);
999*1676Sjpk 				}
1000*1676Sjpk 			}
10010Sstevel@tonic-gate 			rts_setmetrics(ire, rtm->rtm_inits, &rtm->rtm_rmx);
10020Sstevel@tonic-gate 			break;
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 		break;
10050Sstevel@tonic-gate 	default:
10060Sstevel@tonic-gate 		error = EOPNOTSUPP;
10070Sstevel@tonic-gate 		break;
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate done:
10100Sstevel@tonic-gate 	if (ire != NULL)
10110Sstevel@tonic-gate 		ire_refrele(ire);
10120Sstevel@tonic-gate 	if (sire != NULL)
10130Sstevel@tonic-gate 		ire_refrele(sire);
10140Sstevel@tonic-gate 	if (ipif != NULL)
10150Sstevel@tonic-gate 		ipif_refrele(ipif);
10160Sstevel@tonic-gate 	if (src_ipif != NULL)
10170Sstevel@tonic-gate 		ipif_refrele(src_ipif);
10180Sstevel@tonic-gate 	if (tmp_ipif != NULL)
10190Sstevel@tonic-gate 		ipif_refrele(tmp_ipif);
10200Sstevel@tonic-gate 
1021*1676Sjpk 	if (gcgrp_xtraref)
1022*1676Sjpk 		GCGRP_REFRELE(gcgrp);
1023*1676Sjpk 
10240Sstevel@tonic-gate 	if (error == EINPROGRESS)
10250Sstevel@tonic-gate 		return (error);
10260Sstevel@tonic-gate 	if (rtm != NULL) {
10270Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
10280Sstevel@tonic-gate 		if (error != 0) {
10290Sstevel@tonic-gate 			rtm->rtm_errno = error;
10300Sstevel@tonic-gate 			/* Send error ACK */
10310Sstevel@tonic-gate 			ip1dbg(("ip_rts_request: error %d\n", error));
10320Sstevel@tonic-gate 		} else {
10330Sstevel@tonic-gate 			rtm->rtm_flags |= RTF_DONE;
10340Sstevel@tonic-gate 			/* OK ACK already set up by caller except this */
10350Sstevel@tonic-gate 			ip2dbg(("ip_rts_request: OK ACK\n"));
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 		rts_queue_input(mp, q, af);
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	iocp->ioc_error = error;
10400Sstevel@tonic-gate 	ioc_mp->b_datap->db_type = M_IOCACK;
10410Sstevel@tonic-gate 	if (iocp->ioc_error != 0)
10420Sstevel@tonic-gate 		iocp->ioc_count = 0;
10430Sstevel@tonic-gate 	qreply(q, ioc_mp);
10440Sstevel@tonic-gate 	/* conn was refheld in ip_wput_ioctl. */
10450Sstevel@tonic-gate 	CONN_OPER_PENDING_DONE(connp);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	return (error);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate /*
10510Sstevel@tonic-gate  * Build a reply to the RTM_GET request contained in the given message block
10520Sstevel@tonic-gate  * using the retrieved IRE of the destination address, the parent IRE (if it
10530Sstevel@tonic-gate  * exists) and the address family.
10540Sstevel@tonic-gate  *
10550Sstevel@tonic-gate  * Returns a pointer to a message block containing the reply if successful,
10560Sstevel@tonic-gate  * otherwise NULL is returned.
10570Sstevel@tonic-gate  */
1058*1676Sjpk static mblk_t *
10590Sstevel@tonic-gate rts_rtmget(mblk_t *mp, ire_t *ire, ire_t *sire, sa_family_t af)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
10620Sstevel@tonic-gate 	rt_msghdr_t	*new_rtm;
10630Sstevel@tonic-gate 	mblk_t		*new_mp;
10640Sstevel@tonic-gate 	int		rtm_addrs;
10650Sstevel@tonic-gate 	int		rtm_flags;
10660Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
1067*1676Sjpk 	tsol_ire_gw_secattr_t *attrp = NULL;
1068*1676Sjpk 	tsol_gc_t	*gc = NULL;
1069*1676Sjpk 	tsol_gcgrp_t	*gcgrp = NULL;
1070*1676Sjpk 	int		sacnt = 0;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
10730Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
10740Sstevel@tonic-gate 
1075*1676Sjpk 	if (sire != NULL && sire->ire_gw_secattr != NULL)
1076*1676Sjpk 		attrp = sire->ire_gw_secattr;
1077*1676Sjpk 	else if (ire->ire_gw_secattr != NULL)
1078*1676Sjpk 		attrp = ire->ire_gw_secattr;
1079*1676Sjpk 
1080*1676Sjpk 	if (attrp != NULL) {
1081*1676Sjpk 		mutex_enter(&attrp->igsa_lock);
1082*1676Sjpk 		if ((gc = attrp->igsa_gc) != NULL) {
1083*1676Sjpk 			gcgrp = gc->gc_grp;
1084*1676Sjpk 			ASSERT(gcgrp != NULL);
1085*1676Sjpk 			rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
1086*1676Sjpk 			sacnt = 1;
1087*1676Sjpk 		} else if ((gcgrp = attrp->igsa_gcgrp) != NULL) {
1088*1676Sjpk 			rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
1089*1676Sjpk 			gc = gcgrp->gcgrp_head;
1090*1676Sjpk 			sacnt = gcgrp->gcgrp_count;
1091*1676Sjpk 		}
1092*1676Sjpk 		mutex_exit(&attrp->igsa_lock);
1093*1676Sjpk 
1094*1676Sjpk 		/* do nothing if there's no gc to report */
1095*1676Sjpk 		if (gc == NULL) {
1096*1676Sjpk 			ASSERT(sacnt == 0);
1097*1676Sjpk 			if (gcgrp != NULL) {
1098*1676Sjpk 				/* we might as well drop the lock now */
1099*1676Sjpk 				rw_exit(&gcgrp->gcgrp_rwlock);
1100*1676Sjpk 				gcgrp = NULL;
1101*1676Sjpk 			}
1102*1676Sjpk 			attrp = NULL;
1103*1676Sjpk 		}
1104*1676Sjpk 
1105*1676Sjpk 		ASSERT(gc == NULL || (gcgrp != NULL &&
1106*1676Sjpk 		    RW_LOCK_HELD(&gcgrp->gcgrp_rwlock)));
1107*1676Sjpk 	}
1108*1676Sjpk 	ASSERT(sacnt == 0 || gc != NULL);
1109*1676Sjpk 
11100Sstevel@tonic-gate 	/*
11110Sstevel@tonic-gate 	 * Always return RTA_DST, RTA_GATEWAY and RTA_NETMASK.
11120Sstevel@tonic-gate 	 *
11130Sstevel@tonic-gate 	 * The 4.4BSD-Lite2 code (net/rtsock.c) returns both
11140Sstevel@tonic-gate 	 * RTA_IFP and RTA_IFA if either is defined, and also
11150Sstevel@tonic-gate 	 * returns RTA_BRD if the appropriate interface is
11160Sstevel@tonic-gate 	 * point-to-point.
11170Sstevel@tonic-gate 	 */
11180Sstevel@tonic-gate 	rtm_addrs = (RTA_DST | RTA_GATEWAY | RTA_NETMASK);
11190Sstevel@tonic-gate 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
11200Sstevel@tonic-gate 		rtm_addrs |= (RTA_IFP | RTA_IFA);
11210Sstevel@tonic-gate 		if (ire->ire_ipif->ipif_flags & IPIF_POINTOPOINT)
11220Sstevel@tonic-gate 			rtm_addrs |= RTA_BRD;
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
1125*1676Sjpk 	new_mp = rts_alloc_msg(RTM_GET, rtm_addrs, af, sacnt);
1126*1676Sjpk 	if (new_mp == NULL) {
1127*1676Sjpk 		if (gcgrp != NULL)
1128*1676Sjpk 			rw_exit(&gcgrp->gcgrp_rwlock);
11290Sstevel@tonic-gate 		return (NULL);
1130*1676Sjpk 	}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/*
11330Sstevel@tonic-gate 	 * We set the destination address, gateway address,
11340Sstevel@tonic-gate 	 * netmask and flags in the RTM_GET response depending
11350Sstevel@tonic-gate 	 * on whether we found a parent IRE or not.
11360Sstevel@tonic-gate 	 * In particular, if we did find a parent IRE during the
11370Sstevel@tonic-gate 	 * recursive search, use that IRE's gateway address.
11380Sstevel@tonic-gate 	 * Otherwise, we use the IRE's source address for the
11390Sstevel@tonic-gate 	 * gateway address.
11400Sstevel@tonic-gate 	 */
11410Sstevel@tonic-gate 	ASSERT(af == AF_INET || af == AF_INET6);
11420Sstevel@tonic-gate 	switch (af) {
11430Sstevel@tonic-gate 	case AF_INET:
11440Sstevel@tonic-gate 		if (sire == NULL) {
11450Sstevel@tonic-gate 			rtm_flags = ire->ire_flags;
11460Sstevel@tonic-gate 			rts_fill_msg(RTM_GET, rtm_addrs, ire->ire_addr,
11470Sstevel@tonic-gate 			    ire->ire_mask, ire->ire_src_addr, ire->ire_src_addr,
11480Sstevel@tonic-gate 			    ire->ire_ipif->ipif_pp_dst_addr, 0, ire->ire_ipif,
1149*1676Sjpk 			    new_mp, sacnt, gc);
11500Sstevel@tonic-gate 		} else {
11510Sstevel@tonic-gate 			if (sire->ire_flags & RTF_SETSRC)
11520Sstevel@tonic-gate 				rtm_addrs |= RTA_SRC;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 			rtm_flags = sire->ire_flags;
11550Sstevel@tonic-gate 			rts_fill_msg(RTM_GET, rtm_addrs, sire->ire_addr,
11560Sstevel@tonic-gate 			    sire->ire_mask, sire->ire_gateway_addr,
11570Sstevel@tonic-gate 			    (sire->ire_flags & RTF_SETSRC) ?
11580Sstevel@tonic-gate 				sire->ire_src_addr : ire->ire_src_addr,
11590Sstevel@tonic-gate 			    ire->ire_ipif->ipif_pp_dst_addr,
1160*1676Sjpk 			    0, ire->ire_ipif, new_mp, sacnt, gc);
11610Sstevel@tonic-gate 		}
11620Sstevel@tonic-gate 		break;
11630Sstevel@tonic-gate 	case AF_INET6:
11640Sstevel@tonic-gate 		if (sire == NULL) {
11650Sstevel@tonic-gate 			rtm_flags = ire->ire_flags;
11660Sstevel@tonic-gate 			rts_fill_msg_v6(RTM_GET, rtm_addrs, &ire->ire_addr_v6,
11670Sstevel@tonic-gate 			    &ire->ire_mask_v6, &ire->ire_src_addr_v6,
11680Sstevel@tonic-gate 			    &ire->ire_src_addr_v6,
11690Sstevel@tonic-gate 			    &ire->ire_ipif->ipif_v6pp_dst_addr,
1170*1676Sjpk 			    &ipv6_all_zeros, ire->ire_ipif, new_mp,
1171*1676Sjpk 			    sacnt, gc);
11720Sstevel@tonic-gate 		} else {
11730Sstevel@tonic-gate 			if (sire->ire_flags & RTF_SETSRC)
11740Sstevel@tonic-gate 				rtm_addrs |= RTA_SRC;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 			rtm_flags = sire->ire_flags;
11770Sstevel@tonic-gate 			mutex_enter(&sire->ire_lock);
11780Sstevel@tonic-gate 			gw_addr_v6 = sire->ire_gateway_addr_v6;
11790Sstevel@tonic-gate 			mutex_exit(&sire->ire_lock);
11800Sstevel@tonic-gate 			rts_fill_msg_v6(RTM_GET, rtm_addrs, &sire->ire_addr_v6,
11810Sstevel@tonic-gate 			    &sire->ire_mask_v6, &gw_addr_v6,
11820Sstevel@tonic-gate 			    (sire->ire_flags & RTF_SETSRC) ?
11830Sstevel@tonic-gate 				&sire->ire_src_addr_v6 : &ire->ire_src_addr_v6,
11840Sstevel@tonic-gate 			    &ire->ire_ipif->ipif_v6pp_dst_addr, &ipv6_all_zeros,
1185*1676Sjpk 			    ire->ire_ipif, new_mp, sacnt, gc);
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 		break;
11880Sstevel@tonic-gate 	}
1189*1676Sjpk 
1190*1676Sjpk 	if (gcgrp != NULL)
1191*1676Sjpk 		rw_exit(&gcgrp->gcgrp_rwlock);
1192*1676Sjpk 
11930Sstevel@tonic-gate 	new_rtm = (rt_msghdr_t *)new_mp->b_rptr;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	/*
11960Sstevel@tonic-gate 	 * The rtm_msglen, rtm_version and rtm_type fields in
11970Sstevel@tonic-gate 	 * RTM_GET response are filled in by rts_fill_msg.
11980Sstevel@tonic-gate 	 *
11990Sstevel@tonic-gate 	 * rtm_addrs and rtm_flags are filled in based on what
12000Sstevel@tonic-gate 	 * was requested and the state of the IREs looked up
12010Sstevel@tonic-gate 	 * above.
12020Sstevel@tonic-gate 	 *
12030Sstevel@tonic-gate 	 * rtm_inits and rtm_rmx are filled in with metrics
12040Sstevel@tonic-gate 	 * based on whether a parent IRE was found or not.
12050Sstevel@tonic-gate 	 *
12060Sstevel@tonic-gate 	 * TODO: rtm_index and rtm_use should probably be
12070Sstevel@tonic-gate 	 * filled in with something resonable here and not just
12080Sstevel@tonic-gate 	 * copied from the request.
12090Sstevel@tonic-gate 	 */
12100Sstevel@tonic-gate 	new_rtm->rtm_index = rtm->rtm_index;
12110Sstevel@tonic-gate 	new_rtm->rtm_pid = rtm->rtm_pid;
12120Sstevel@tonic-gate 	new_rtm->rtm_seq = rtm->rtm_seq;
12130Sstevel@tonic-gate 	new_rtm->rtm_use = rtm->rtm_use;
12140Sstevel@tonic-gate 	new_rtm->rtm_addrs = rtm_addrs;
12150Sstevel@tonic-gate 	new_rtm->rtm_flags = rtm_flags;
12160Sstevel@tonic-gate 	if (sire == NULL)
12170Sstevel@tonic-gate 		new_rtm->rtm_inits = rts_getmetrics(ire, &new_rtm->rtm_rmx);
12180Sstevel@tonic-gate 	else
12190Sstevel@tonic-gate 		new_rtm->rtm_inits = rts_getmetrics(sire, &new_rtm->rtm_rmx);
1220*1676Sjpk 
12210Sstevel@tonic-gate 	return (new_mp);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate  * Fill the given if_data_t with interface statistics.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate static void
1228*1676Sjpk rts_getifdata(if_data_t *if_data, const ipif_t *ipif)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	if_data->ifi_type = ipif->ipif_type;	/* ethernet, tokenring, etc */
12310Sstevel@tonic-gate 	if_data->ifi_addrlen = 0;		/* media address length */
12320Sstevel@tonic-gate 	if_data->ifi_hdrlen = 0;		/* media header length */
12330Sstevel@tonic-gate 	if_data->ifi_mtu = ipif->ipif_mtu;	/* maximum transmission unit */
12340Sstevel@tonic-gate 	if_data->ifi_metric = ipif->ipif_metric; /* metric (external only) */
12350Sstevel@tonic-gate 	if_data->ifi_baudrate = 0;		/* linespeed */
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if_data->ifi_ipackets = 0;		/* packets received on if */
12380Sstevel@tonic-gate 	if_data->ifi_ierrors = 0;		/* input errors on interface */
12390Sstevel@tonic-gate 	if_data->ifi_opackets = 0;		/* packets sent on interface */
12400Sstevel@tonic-gate 	if_data->ifi_oerrors = 0;		/* output errors on if */
12410Sstevel@tonic-gate 	if_data->ifi_collisions = 0;		/* collisions on csma if */
12420Sstevel@tonic-gate 	if_data->ifi_ibytes = 0;		/* total number received */
12430Sstevel@tonic-gate 	if_data->ifi_obytes = 0;		/* total number sent */
12440Sstevel@tonic-gate 	if_data->ifi_imcasts = 0;		/* multicast packets received */
12450Sstevel@tonic-gate 	if_data->ifi_omcasts = 0;		/* multicast packets sent */
12460Sstevel@tonic-gate 	if_data->ifi_iqdrops = 0;		/* dropped on input */
12470Sstevel@tonic-gate 	if_data->ifi_noproto = 0;		/* destined for unsupported */
12480Sstevel@tonic-gate 						/* protocol. */
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate /*
12520Sstevel@tonic-gate  * Set the metrics on a forwarding table route.
12530Sstevel@tonic-gate  */
12540Sstevel@tonic-gate static void
12550Sstevel@tonic-gate rts_setmetrics(ire_t *ire, uint_t which, rt_metrics_t *metrics)
12560Sstevel@tonic-gate {
12570Sstevel@tonic-gate 	clock_t		rtt;
12580Sstevel@tonic-gate 	clock_t		rtt_sd;
12590Sstevel@tonic-gate 	ipif_t		*ipif;
12600Sstevel@tonic-gate 	ifrt_t		*ifrt;
12610Sstevel@tonic-gate 	mblk_t		*mp;
12620Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/*
12650Sstevel@tonic-gate 	 * Bypass obtaining the lock and searching ipif_saved_ire_mp in the
12660Sstevel@tonic-gate 	 * common case of no metrics.
12670Sstevel@tonic-gate 	 */
12680Sstevel@tonic-gate 	if (which == 0)
12690Sstevel@tonic-gate 		return;
12700Sstevel@tonic-gate 	ire->ire_uinfo.iulp_set = B_TRUE;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	/*
12730Sstevel@tonic-gate 	 * iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
12740Sstevel@tonic-gate 	 * <net/route.h> says: rmx_rtt and rmx_rttvar are stored as
12750Sstevel@tonic-gate 	 * microseconds.
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	if (which & RTV_RTT)
12780Sstevel@tonic-gate 		rtt = metrics->rmx_rtt / 1000;
12790Sstevel@tonic-gate 	if (which & RTV_RTTVAR)
12800Sstevel@tonic-gate 		rtt_sd = metrics->rmx_rttvar / 1000;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * Update the metrics in the IRE itself.
12840Sstevel@tonic-gate 	 */
12850Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
12860Sstevel@tonic-gate 	if (which & RTV_MTU)
12870Sstevel@tonic-gate 		ire->ire_max_frag = metrics->rmx_mtu;
12880Sstevel@tonic-gate 	if (which & RTV_RTT)
12890Sstevel@tonic-gate 		ire->ire_uinfo.iulp_rtt = rtt;
12900Sstevel@tonic-gate 	if (which & RTV_SSTHRESH)
12910Sstevel@tonic-gate 		ire->ire_uinfo.iulp_ssthresh = metrics->rmx_ssthresh;
12920Sstevel@tonic-gate 	if (which & RTV_RTTVAR)
12930Sstevel@tonic-gate 		ire->ire_uinfo.iulp_rtt_sd = rtt_sd;
12940Sstevel@tonic-gate 	if (which & RTV_SPIPE)
12950Sstevel@tonic-gate 		ire->ire_uinfo.iulp_spipe = metrics->rmx_sendpipe;
12960Sstevel@tonic-gate 	if (which & RTV_RPIPE)
12970Sstevel@tonic-gate 		ire->ire_uinfo.iulp_rpipe = metrics->rmx_recvpipe;
12980Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	/*
13010Sstevel@tonic-gate 	 * Search through the ifrt_t chain hanging off the IPIF in order to
13020Sstevel@tonic-gate 	 * reflect the metric change there.
13030Sstevel@tonic-gate 	 */
13040Sstevel@tonic-gate 	ipif = ire->ire_ipif;
13050Sstevel@tonic-gate 	if (ipif == NULL)
13060Sstevel@tonic-gate 		return;
13070Sstevel@tonic-gate 	ASSERT((ipif->ipif_isv6 && ire->ire_ipversion == IPV6_VERSION) ||
13080Sstevel@tonic-gate 	    ((!ipif->ipif_isv6 && ire->ire_ipversion == IPV4_VERSION)));
13090Sstevel@tonic-gate 	if (ipif->ipif_isv6) {
13100Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
13110Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
13120Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
13130Sstevel@tonic-gate 	}
13140Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_saved_ire_lock);
13150Sstevel@tonic-gate 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13160Sstevel@tonic-gate 		/*
13170Sstevel@tonic-gate 		 * On a given ipif, the triple of address, gateway and mask is
13180Sstevel@tonic-gate 		 * unique for each saved IRE (in the case of ordinary interface
13190Sstevel@tonic-gate 		 * routes, the gateway address is all-zeroes).
13200Sstevel@tonic-gate 		 */
13210Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
13220Sstevel@tonic-gate 		if (ipif->ipif_isv6) {
13230Sstevel@tonic-gate 			if (!IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
13240Sstevel@tonic-gate 			    &ire->ire_addr_v6) ||
13250Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
13260Sstevel@tonic-gate 			    &gw_addr_v6) ||
13270Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
13280Sstevel@tonic-gate 			    &ire->ire_mask_v6))
13290Sstevel@tonic-gate 				continue;
13300Sstevel@tonic-gate 		} else {
13310Sstevel@tonic-gate 			if (ifrt->ifrt_addr != ire->ire_addr ||
13320Sstevel@tonic-gate 			    ifrt->ifrt_gateway_addr != ire->ire_gateway_addr ||
13330Sstevel@tonic-gate 			    ifrt->ifrt_mask != ire->ire_mask)
13340Sstevel@tonic-gate 				continue;
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 		if (which & RTV_MTU)
13370Sstevel@tonic-gate 			ifrt->ifrt_max_frag = metrics->rmx_mtu;
13380Sstevel@tonic-gate 		if (which & RTV_RTT)
13390Sstevel@tonic-gate 			ifrt->ifrt_iulp_info.iulp_rtt = rtt;
13400Sstevel@tonic-gate 		if (which & RTV_SSTHRESH) {
13410Sstevel@tonic-gate 			ifrt->ifrt_iulp_info.iulp_ssthresh =
13420Sstevel@tonic-gate 			    metrics->rmx_ssthresh;
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 		if (which & RTV_RTTVAR)
13450Sstevel@tonic-gate 			ifrt->ifrt_iulp_info.iulp_rtt_sd = metrics->rmx_rttvar;
13460Sstevel@tonic-gate 		if (which & RTV_SPIPE)
13470Sstevel@tonic-gate 			ifrt->ifrt_iulp_info.iulp_spipe = metrics->rmx_sendpipe;
13480Sstevel@tonic-gate 		if (which & RTV_RPIPE)
13490Sstevel@tonic-gate 			ifrt->ifrt_iulp_info.iulp_rpipe = metrics->rmx_recvpipe;
13500Sstevel@tonic-gate 		break;
13510Sstevel@tonic-gate 	}
13520Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_saved_ire_lock);
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate /*
13560Sstevel@tonic-gate  * Get the metrics from a forwarding table route.
13570Sstevel@tonic-gate  */
13580Sstevel@tonic-gate static int
13590Sstevel@tonic-gate rts_getmetrics(ire_t *ire, rt_metrics_t *metrics)
13600Sstevel@tonic-gate {
13610Sstevel@tonic-gate 	int	metrics_set = 0;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	bzero(metrics, sizeof (rt_metrics_t));
13640Sstevel@tonic-gate 	/*
13650Sstevel@tonic-gate 	 * iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
13660Sstevel@tonic-gate 	 * <net/route.h> says: rmx_rtt and rmx_rttvar are stored as
13670Sstevel@tonic-gate 	 * microseconds.
13680Sstevel@tonic-gate 	 */
13690Sstevel@tonic-gate 	metrics->rmx_rtt = ire->ire_uinfo.iulp_rtt * 1000;
13700Sstevel@tonic-gate 	metrics_set |= RTV_RTT;
13710Sstevel@tonic-gate 	metrics->rmx_mtu = ire->ire_max_frag;
13720Sstevel@tonic-gate 	metrics_set |= RTV_MTU;
13730Sstevel@tonic-gate 	metrics->rmx_ssthresh = ire->ire_uinfo.iulp_ssthresh;
13740Sstevel@tonic-gate 	metrics_set |= RTV_SSTHRESH;
13750Sstevel@tonic-gate 	metrics->rmx_rttvar = ire->ire_uinfo.iulp_rtt_sd * 1000;
13760Sstevel@tonic-gate 	metrics_set |= RTV_RTTVAR;
13770Sstevel@tonic-gate 	metrics->rmx_sendpipe = ire->ire_uinfo.iulp_spipe;
13780Sstevel@tonic-gate 	metrics_set |= RTV_SPIPE;
13790Sstevel@tonic-gate 	metrics->rmx_recvpipe = ire->ire_uinfo.iulp_rpipe;
13800Sstevel@tonic-gate 	metrics_set |= RTV_RPIPE;
13810Sstevel@tonic-gate 	return (metrics_set);
13820Sstevel@tonic-gate }
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate /*
13850Sstevel@tonic-gate  * Takes a pointer to a routing message and extracts necessary info by looking
13860Sstevel@tonic-gate  * at the rtm->rtm_addrs bits and store the requested sockaddrs in the pointers
13870Sstevel@tonic-gate  * passed (all of which must be valid).
13880Sstevel@tonic-gate  *
13890Sstevel@tonic-gate  * The bitmask of sockaddrs actually found in the message is returned, or zero
13900Sstevel@tonic-gate  * is returned in the case of an error.
13910Sstevel@tonic-gate  */
13920Sstevel@tonic-gate static int
13930Sstevel@tonic-gate rts_getaddrs(rt_msghdr_t *rtm, in6_addr_t *dst_addrp, in6_addr_t *gw_addrp,
13940Sstevel@tonic-gate     in6_addr_t *net_maskp, in6_addr_t *authorp, in6_addr_t *if_addrp,
13950Sstevel@tonic-gate     in6_addr_t *in_src_addrp, ushort_t *indexp, ushort_t *src_indexp,
1396*1676Sjpk     sa_family_t *afp, tsol_rtsecattr_t *rtsecattr, int *error)
13970Sstevel@tonic-gate {
13980Sstevel@tonic-gate 	struct sockaddr *sa;
13990Sstevel@tonic-gate 	int	i;
14000Sstevel@tonic-gate 	int	addr_bits;
14010Sstevel@tonic-gate 	int	length;
14020Sstevel@tonic-gate 	int	found_addrs = 0;
14030Sstevel@tonic-gate 	caddr_t	cp;
14040Sstevel@tonic-gate 	size_t	size;
14050Sstevel@tonic-gate 	struct sockaddr_dl *sdl;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	*dst_addrp = ipv6_all_zeros;
14080Sstevel@tonic-gate 	*gw_addrp = ipv6_all_zeros;
14090Sstevel@tonic-gate 	*net_maskp = ipv6_all_zeros;
14100Sstevel@tonic-gate 	*authorp = ipv6_all_zeros;
14110Sstevel@tonic-gate 	*if_addrp = ipv6_all_zeros;
14120Sstevel@tonic-gate 	*in_src_addrp = ipv6_all_zeros;
14130Sstevel@tonic-gate 	*indexp = 0;
14140Sstevel@tonic-gate 	*src_indexp = 0;
14150Sstevel@tonic-gate 	*afp = AF_UNSPEC;
1416*1676Sjpk 	rtsecattr->rtsa_cnt = 0;
1417*1676Sjpk 	*error = 0;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/*
14200Sstevel@tonic-gate 	 * At present we handle only RTA_DST, RTA_GATEWAY, RTA_NETMASK, RTA_IFP,
14210Sstevel@tonic-gate 	 * RTA_IFA and RTA_AUTHOR.  The rest will be added as we need them.
14220Sstevel@tonic-gate 	 */
14230Sstevel@tonic-gate 	cp = (caddr_t)&rtm[1];
14240Sstevel@tonic-gate 	length = rtm->rtm_msglen;
14250Sstevel@tonic-gate 	for (i = 0; (i < RTA_NUMBITS) && ((cp - (caddr_t)rtm) < length); i++) {
14260Sstevel@tonic-gate 		/*
14270Sstevel@tonic-gate 		 * The address family we are working with starts out as
14280Sstevel@tonic-gate 		 * AF_UNSPEC, but is set to the one specified with the
14290Sstevel@tonic-gate 		 * destination address.
14300Sstevel@tonic-gate 		 *
14310Sstevel@tonic-gate 		 * If the "working" address family that has been set to
14320Sstevel@tonic-gate 		 * something other than AF_UNSPEC, then the address family of
14330Sstevel@tonic-gate 		 * subsequent sockaddrs must either be AF_UNSPEC (for
14340Sstevel@tonic-gate 		 * compatibility with older programs) or must be the same as our
14350Sstevel@tonic-gate 		 * "working" one.
14360Sstevel@tonic-gate 		 *
14370Sstevel@tonic-gate 		 * This code assumes that RTA_DST (1) comes first in the loop.
14380Sstevel@tonic-gate 		 */
14390Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
14400Sstevel@tonic-gate 		addr_bits = (rtm->rtm_addrs & (1 << i));
14410Sstevel@tonic-gate 		if (addr_bits == 0)
14420Sstevel@tonic-gate 			continue;
14430Sstevel@tonic-gate 		switch (addr_bits) {
14440Sstevel@tonic-gate 		case RTA_DST:
14450Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, dst_addrp);
14460Sstevel@tonic-gate 			*afp = sa->sa_family;
14470Sstevel@tonic-gate 			break;
14480Sstevel@tonic-gate 		case RTA_GATEWAY:
14490Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
14500Sstevel@tonic-gate 				return (0);
14510Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, gw_addrp);
14520Sstevel@tonic-gate 			break;
14530Sstevel@tonic-gate 		case RTA_NETMASK:
14540Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
14550Sstevel@tonic-gate 				return (0);
14560Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, net_maskp);
14570Sstevel@tonic-gate 			break;
14580Sstevel@tonic-gate 		case RTA_IFP:
14590Sstevel@tonic-gate 			if (sa->sa_family != AF_LINK &&
14600Sstevel@tonic-gate 			    sa->sa_family != AF_UNSPEC)
14610Sstevel@tonic-gate 				return (0);
14620Sstevel@tonic-gate 			sdl = (struct sockaddr_dl *)cp;
14630Sstevel@tonic-gate 			*indexp = sdl->sdl_index;
14640Sstevel@tonic-gate 			size = sizeof (struct sockaddr_dl);
14650Sstevel@tonic-gate 			break;
14660Sstevel@tonic-gate 		case RTA_SRC:
14670Sstevel@tonic-gate 			/* Source address of the incoming packet */
14680Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, in_src_addrp);
14690Sstevel@tonic-gate 			*afp = sa->sa_family;
14700Sstevel@tonic-gate 			break;
14710Sstevel@tonic-gate 		case RTA_SRCIFP:
14720Sstevel@tonic-gate 			/* Return incoming interface index pointer */
14730Sstevel@tonic-gate 			if (sa->sa_family != AF_LINK &&
14740Sstevel@tonic-gate 			    sa->sa_family != AF_UNSPEC)
14750Sstevel@tonic-gate 				return (0);
14760Sstevel@tonic-gate 			sdl = (struct sockaddr_dl *)cp;
14770Sstevel@tonic-gate 			*src_indexp = sdl->sdl_index;
14780Sstevel@tonic-gate 			size = sizeof (struct sockaddr_dl);
14790Sstevel@tonic-gate 			break;
14800Sstevel@tonic-gate 		case RTA_IFA:
14810Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
14820Sstevel@tonic-gate 				return (0);
14830Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, if_addrp);
14840Sstevel@tonic-gate 			break;
14850Sstevel@tonic-gate 		case RTA_AUTHOR:
14860Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
14870Sstevel@tonic-gate 				return (0);
14880Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, authorp);
14890Sstevel@tonic-gate 			break;
14900Sstevel@tonic-gate 		default:
14910Sstevel@tonic-gate 			return (0);
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 		if (size == 0)
14940Sstevel@tonic-gate 			return (0);
14950Sstevel@tonic-gate 		cp += size;
14960Sstevel@tonic-gate 		found_addrs |= addr_bits;
14970Sstevel@tonic-gate 	}
1498*1676Sjpk 
1499*1676Sjpk 	/*
1500*1676Sjpk 	 * Parse the routing message and look for any security-
1501*1676Sjpk 	 * related attributes for the route.  For each valid
1502*1676Sjpk 	 * attribute, allocate/obtain the corresponding kernel
1503*1676Sjpk 	 * route security attributes.
1504*1676Sjpk 	 */
1505*1676Sjpk 	*error = tsol_rtsa_init(rtm, rtsecattr, cp);
1506*1676Sjpk 	ASSERT(rtsecattr->rtsa_cnt <= TSOL_RTSA_REQUEST_MAX);
1507*1676Sjpk 
15080Sstevel@tonic-gate 	return (found_addrs);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate /*
15120Sstevel@tonic-gate  * Fills the message with the given info.
15130Sstevel@tonic-gate  */
15140Sstevel@tonic-gate static void
15150Sstevel@tonic-gate rts_fill_msg(int type, int rtm_addrs, ipaddr_t dst, ipaddr_t mask,
15160Sstevel@tonic-gate     ipaddr_t gateway, ipaddr_t src_addr, ipaddr_t brd_addr, ipaddr_t author,
1517*1676Sjpk     const ipif_t *ipif, mblk_t *mp, uint_t sacnt, const tsol_gc_t *gc)
15180Sstevel@tonic-gate {
15190Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
15200Sstevel@tonic-gate 	sin_t		*sin;
15210Sstevel@tonic-gate 	size_t		data_size, header_size;
15220Sstevel@tonic-gate 	uchar_t		*cp;
15230Sstevel@tonic-gate 	int		i;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	ASSERT(mp != NULL);
1526*1676Sjpk 	ASSERT(sacnt == 0 || gc != NULL);
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * First find the type of the message
15290Sstevel@tonic-gate 	 * and its length.
15300Sstevel@tonic-gate 	 */
15310Sstevel@tonic-gate 	header_size = rts_header_msg_size(type);
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * Now find the size of the data
15340Sstevel@tonic-gate 	 * that follows the message header.
15350Sstevel@tonic-gate 	 */
1536*1676Sjpk 	data_size = rts_data_msg_size(rtm_addrs, AF_INET, sacnt);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
15390Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[header_size];
15400Sstevel@tonic-gate 	cp = mp->b_wptr;
15410Sstevel@tonic-gate 	bzero(cp, data_size);
15420Sstevel@tonic-gate 	for (i = 0; i < RTA_NUMBITS; i++) {
15430Sstevel@tonic-gate 		sin = (sin_t *)cp;
15440Sstevel@tonic-gate 		switch (rtm_addrs & (1 << i)) {
15450Sstevel@tonic-gate 		case RTA_DST:
15460Sstevel@tonic-gate 			sin->sin_addr.s_addr = dst;
15470Sstevel@tonic-gate 			sin->sin_family = AF_INET;
15480Sstevel@tonic-gate 			cp += sizeof (sin_t);
15490Sstevel@tonic-gate 			break;
15500Sstevel@tonic-gate 		case RTA_GATEWAY:
15510Sstevel@tonic-gate 			sin->sin_addr.s_addr = gateway;
15520Sstevel@tonic-gate 			sin->sin_family = AF_INET;
15530Sstevel@tonic-gate 			cp += sizeof (sin_t);
15540Sstevel@tonic-gate 			break;
15550Sstevel@tonic-gate 		case RTA_NETMASK:
15560Sstevel@tonic-gate 			sin->sin_addr.s_addr = mask;
15570Sstevel@tonic-gate 			sin->sin_family = AF_INET;
15580Sstevel@tonic-gate 			cp += sizeof (sin_t);
15590Sstevel@tonic-gate 			break;
15600Sstevel@tonic-gate 		case RTA_IFP:
15610Sstevel@tonic-gate 			cp += ill_dls_info((struct sockaddr_dl *)cp, ipif);
15620Sstevel@tonic-gate 			break;
15630Sstevel@tonic-gate 		case RTA_SRCIFP:
15640Sstevel@tonic-gate 			/*
15650Sstevel@tonic-gate 			 * RTA_SRCIFP is not yet supported
15660Sstevel@tonic-gate 			 * for RTM_GET and RTM_CHANGE
15670Sstevel@tonic-gate 			 */
15680Sstevel@tonic-gate 			break;
15690Sstevel@tonic-gate 		case RTA_IFA:
15700Sstevel@tonic-gate 		case RTA_SRC:
15710Sstevel@tonic-gate 			sin->sin_addr.s_addr = src_addr;
15720Sstevel@tonic-gate 			sin->sin_family = AF_INET;
15730Sstevel@tonic-gate 			cp += sizeof (sin_t);
15740Sstevel@tonic-gate 			break;
15750Sstevel@tonic-gate 		case RTA_AUTHOR:
15760Sstevel@tonic-gate 			sin->sin_addr.s_addr = author;
15770Sstevel@tonic-gate 			sin->sin_family = AF_INET;
15780Sstevel@tonic-gate 			cp += sizeof (sin_t);
15790Sstevel@tonic-gate 			break;
15800Sstevel@tonic-gate 		case RTA_BRD:
15810Sstevel@tonic-gate 			/*
15820Sstevel@tonic-gate 			 * RTA_BRD is used typically to specify a point-to-point
15830Sstevel@tonic-gate 			 * destination address.
15840Sstevel@tonic-gate 			 */
15850Sstevel@tonic-gate 			sin->sin_addr.s_addr = brd_addr;
15860Sstevel@tonic-gate 			sin->sin_family = AF_INET;
15870Sstevel@tonic-gate 			cp += sizeof (sin_t);
15880Sstevel@tonic-gate 			break;
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 	}
1591*1676Sjpk 
1592*1676Sjpk 	if (gc != NULL) {
1593*1676Sjpk 		rtm_ext_t *rtm_ext;
1594*1676Sjpk 		struct rtsa_s *rp_dst;
1595*1676Sjpk 		tsol_rtsecattr_t *rsap;
1596*1676Sjpk 		int i;
1597*1676Sjpk 
1598*1676Sjpk 		ASSERT(gc->gc_grp != NULL);
1599*1676Sjpk 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
1600*1676Sjpk 		ASSERT(sacnt > 0);
1601*1676Sjpk 
1602*1676Sjpk 		rtm_ext = (rtm_ext_t *)cp;
1603*1676Sjpk 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
1604*1676Sjpk 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(sacnt);
1605*1676Sjpk 
1606*1676Sjpk 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
1607*1676Sjpk 		rsap->rtsa_cnt = sacnt;
1608*1676Sjpk 		rp_dst = rsap->rtsa_attr;
1609*1676Sjpk 
1610*1676Sjpk 		for (i = 0; i < sacnt; i++, gc = gc->gc_next, rp_dst++) {
1611*1676Sjpk 			ASSERT(gc->gc_db != NULL);
1612*1676Sjpk 			bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
1613*1676Sjpk 		}
1614*1676Sjpk 		cp = (uchar_t *)rp_dst;
1615*1676Sjpk 	}
1616*1676Sjpk 
16170Sstevel@tonic-gate 	mp->b_wptr = cp;
16180Sstevel@tonic-gate 	mp->b_cont = NULL;
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * set the fields that are common to
16210Sstevel@tonic-gate 	 * to different messages.
16220Sstevel@tonic-gate 	 */
16230Sstevel@tonic-gate 	rtm->rtm_msglen = (short)(header_size + data_size);
16240Sstevel@tonic-gate 	rtm->rtm_version = RTM_VERSION;
16250Sstevel@tonic-gate 	rtm->rtm_type = (uchar_t)type;
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate /*
16290Sstevel@tonic-gate  * Allocates and initializes a routing socket message.
16300Sstevel@tonic-gate  */
16310Sstevel@tonic-gate mblk_t *
1632*1676Sjpk rts_alloc_msg(int type, int rtm_addrs, sa_family_t af, uint_t sacnt)
16330Sstevel@tonic-gate {
16340Sstevel@tonic-gate 	size_t	length;
16350Sstevel@tonic-gate 	mblk_t	*mp;
16360Sstevel@tonic-gate 
1637*1676Sjpk 	length = RTS_MSG_SIZE(type, rtm_addrs, af, sacnt);
16380Sstevel@tonic-gate 	mp = allocb(length, BPRI_MED);
16390Sstevel@tonic-gate 	if (mp == NULL)
16400Sstevel@tonic-gate 		return (mp);
16410Sstevel@tonic-gate 	bzero(mp->b_rptr, length);
16420Sstevel@tonic-gate 	return (mp);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate  * Returns the size of the routing
16470Sstevel@tonic-gate  * socket message header size.
16480Sstevel@tonic-gate  */
16490Sstevel@tonic-gate size_t
16500Sstevel@tonic-gate rts_header_msg_size(int type)
16510Sstevel@tonic-gate {
16520Sstevel@tonic-gate 	switch (type) {
16530Sstevel@tonic-gate 	case RTM_DELADDR:
16540Sstevel@tonic-gate 	case RTM_NEWADDR:
16550Sstevel@tonic-gate 		return (sizeof (ifa_msghdr_t));
16560Sstevel@tonic-gate 	case RTM_IFINFO:
16570Sstevel@tonic-gate 		return (sizeof (if_msghdr_t));
16580Sstevel@tonic-gate 	default:
16590Sstevel@tonic-gate 		return (sizeof (rt_msghdr_t));
16600Sstevel@tonic-gate 	}
16610Sstevel@tonic-gate }
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate /*
16640Sstevel@tonic-gate  * Returns the size of the message needed with the given rtm_addrs and family.
16650Sstevel@tonic-gate  *
16660Sstevel@tonic-gate  * It is assumed that all of the sockaddrs (with the exception of RTA_IFP) are
16670Sstevel@tonic-gate  * of the same family (currently either AF_INET or AF_INET6).
16680Sstevel@tonic-gate  */
16690Sstevel@tonic-gate size_t
1670*1676Sjpk rts_data_msg_size(int rtm_addrs, sa_family_t af, uint_t sacnt)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	int	i;
16730Sstevel@tonic-gate 	size_t	length = 0;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	for (i = 0; i < RTA_NUMBITS; i++) {
16760Sstevel@tonic-gate 		switch (rtm_addrs & (1 << i)) {
16770Sstevel@tonic-gate 		case RTA_IFP:
16780Sstevel@tonic-gate 			length += sizeof (struct sockaddr_dl);
16790Sstevel@tonic-gate 			break;
16800Sstevel@tonic-gate 		case RTA_DST:
16810Sstevel@tonic-gate 		case RTA_GATEWAY:
16820Sstevel@tonic-gate 		case RTA_NETMASK:
16830Sstevel@tonic-gate 		case RTA_SRC:
16840Sstevel@tonic-gate 		case RTA_SRCIFP:
16850Sstevel@tonic-gate 		case RTA_IFA:
16860Sstevel@tonic-gate 		case RTA_AUTHOR:
16870Sstevel@tonic-gate 		case RTA_BRD:
16880Sstevel@tonic-gate 			ASSERT(af == AF_INET || af == AF_INET6);
16890Sstevel@tonic-gate 			switch (af) {
16900Sstevel@tonic-gate 			case AF_INET:
16910Sstevel@tonic-gate 				length += sizeof (sin_t);
16920Sstevel@tonic-gate 				break;
16930Sstevel@tonic-gate 			case AF_INET6:
16940Sstevel@tonic-gate 				length += sizeof (sin6_t);
16950Sstevel@tonic-gate 				break;
16960Sstevel@tonic-gate 			}
16970Sstevel@tonic-gate 			break;
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 	}
1700*1676Sjpk 	if (sacnt > 0)
1701*1676Sjpk 		length += sizeof (rtm_ext_t) + TSOL_RTSECATTR_SIZE(sacnt);
1702*1676Sjpk 
17030Sstevel@tonic-gate 	return (length);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate  * This routine is called to generate a message to the routing
17080Sstevel@tonic-gate  * socket indicating that a redirect has occured, a routing lookup
17090Sstevel@tonic-gate  * has failed, or that a protocol has detected timeouts to a particular
17100Sstevel@tonic-gate  * destination. This routine is called for message types RTM_LOSING,
17110Sstevel@tonic-gate  * RTM_REDIRECT, and RTM_MISS.
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate void
17140Sstevel@tonic-gate ip_rts_change(int type, ipaddr_t dst_addr, ipaddr_t gw_addr, ipaddr_t net_mask,
17150Sstevel@tonic-gate     ipaddr_t source, ipaddr_t author, int flags, int error, int rtm_addrs)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
17180Sstevel@tonic-gate 	mblk_t		*mp;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	if (rtm_addrs == 0)
17210Sstevel@tonic-gate 		return;
1722*1676Sjpk 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET, 0);
17230Sstevel@tonic-gate 	if (mp == NULL)
17240Sstevel@tonic-gate 		return;
17250Sstevel@tonic-gate 	rts_fill_msg(type, rtm_addrs, dst_addr, net_mask, gw_addr, source, 0,
1726*1676Sjpk 	    author, NULL, mp, 0, NULL);
17270Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
17280Sstevel@tonic-gate 	rtm->rtm_flags = flags;
17290Sstevel@tonic-gate 	rtm->rtm_errno = error;
17300Sstevel@tonic-gate 	rtm->rtm_flags |= RTF_DONE;
17310Sstevel@tonic-gate 	rtm->rtm_addrs = rtm_addrs;
17320Sstevel@tonic-gate 	rts_queue_input(mp, NULL, AF_INET);
17330Sstevel@tonic-gate }
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate /*
17360Sstevel@tonic-gate  * This routine is called to generate a message to the routing
17370Sstevel@tonic-gate  * socket indicating that the status of a network interface has changed.
17380Sstevel@tonic-gate  * Message type generated RTM_IFINFO.
17390Sstevel@tonic-gate  */
17400Sstevel@tonic-gate void
1741*1676Sjpk ip_rts_ifmsg(const ipif_t *ipif)
17420Sstevel@tonic-gate {
17430Sstevel@tonic-gate 	if_msghdr_t	*ifm;
17440Sstevel@tonic-gate 	mblk_t		*mp;
17450Sstevel@tonic-gate 	sa_family_t	af;
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	/*
17480Sstevel@tonic-gate 	 * This message should be generated only
17490Sstevel@tonic-gate 	 * when the physical device is changing
17500Sstevel@tonic-gate 	 * state.
17510Sstevel@tonic-gate 	 */
17520Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
17530Sstevel@tonic-gate 		return;
17540Sstevel@tonic-gate 	if (ipif->ipif_isv6) {
17550Sstevel@tonic-gate 		af = AF_INET6;
1756*1676Sjpk 		mp = rts_alloc_msg(RTM_IFINFO, RTA_IFP, af, 0);
17570Sstevel@tonic-gate 		if (mp == NULL)
17580Sstevel@tonic-gate 			return;
17590Sstevel@tonic-gate 		rts_fill_msg_v6(RTM_IFINFO, RTA_IFP, &ipv6_all_zeros,
17600Sstevel@tonic-gate 		    &ipv6_all_zeros, &ipv6_all_zeros, &ipv6_all_zeros,
1761*1676Sjpk 		    &ipv6_all_zeros, &ipv6_all_zeros, ipif, mp, 0, NULL);
17620Sstevel@tonic-gate 	} else {
17630Sstevel@tonic-gate 		af = AF_INET;
1764*1676Sjpk 		mp = rts_alloc_msg(RTM_IFINFO, RTA_IFP, af, 0);
17650Sstevel@tonic-gate 		if (mp == NULL)
17660Sstevel@tonic-gate 			return;
1767*1676Sjpk 		rts_fill_msg(RTM_IFINFO, RTA_IFP, 0, 0, 0, 0, 0, 0, ipif, mp,
1768*1676Sjpk 		    0, NULL);
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 	ifm = (if_msghdr_t *)mp->b_rptr;
17710Sstevel@tonic-gate 	ifm->ifm_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17720Sstevel@tonic-gate 	ifm->ifm_flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
17730Sstevel@tonic-gate 	    ipif->ipif_ill->ill_phyint->phyint_flags;
17740Sstevel@tonic-gate 	rts_getifdata(&ifm->ifm_data, ipif);
17750Sstevel@tonic-gate 	ifm->ifm_addrs = RTA_IFP;
17760Sstevel@tonic-gate 	rts_queue_input(mp, NULL, af);
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate /*
17800Sstevel@tonic-gate  * This is called to generate messages to the routing socket
17810Sstevel@tonic-gate  * indicating a network interface has had addresses associated with it.
17820Sstevel@tonic-gate  * The structure of the code is based on the 4.4BSD-Lite2 <net/rtsock.c>.
17830Sstevel@tonic-gate  */
17840Sstevel@tonic-gate void
1785*1676Sjpk ip_rts_newaddrmsg(int cmd, int error, const ipif_t *ipif)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	int		pass;
17880Sstevel@tonic-gate 	int		ncmd;
17890Sstevel@tonic-gate 	int		rtm_addrs;
17900Sstevel@tonic-gate 	mblk_t		*mp;
17910Sstevel@tonic-gate 	ifa_msghdr_t	*ifam;
17920Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
17930Sstevel@tonic-gate 	sa_family_t	af;
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	if (ipif->ipif_isv6)
17960Sstevel@tonic-gate 		af = AF_INET6;
17970Sstevel@tonic-gate 	else
17980Sstevel@tonic-gate 		af = AF_INET;
17990Sstevel@tonic-gate 	/*
18000Sstevel@tonic-gate 	 * If the request is DELETE, send RTM_DELETE and RTM_DELADDR.
18010Sstevel@tonic-gate 	 * if the request is ADD, send RTM_NEWADDR and RTM_ADD.
18020Sstevel@tonic-gate 	 */
18030Sstevel@tonic-gate 	for (pass = 1; pass < 3; pass++) {
18040Sstevel@tonic-gate 		if ((cmd == RTM_ADD && pass == 1) ||
18050Sstevel@tonic-gate 		    (cmd == RTM_DELETE && pass == 2)) {
18060Sstevel@tonic-gate 			ncmd = ((cmd == RTM_ADD) ? RTM_NEWADDR : RTM_DELADDR);
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 			rtm_addrs = (RTA_IFA | RTA_NETMASK | RTA_BRD);
1809*1676Sjpk 			mp = rts_alloc_msg(ncmd, rtm_addrs, af, 0);
18100Sstevel@tonic-gate 			if (mp == NULL)
18110Sstevel@tonic-gate 				continue;
18120Sstevel@tonic-gate 			switch (af) {
18130Sstevel@tonic-gate 			case AF_INET:
18140Sstevel@tonic-gate 				rts_fill_msg(ncmd, rtm_addrs, 0,
18150Sstevel@tonic-gate 				    ipif->ipif_net_mask, 0, ipif->ipif_lcl_addr,
1816*1676Sjpk 				    ipif->ipif_pp_dst_addr, 0, NULL, mp,
1817*1676Sjpk 				    0, NULL);
18180Sstevel@tonic-gate 				break;
18190Sstevel@tonic-gate 			case AF_INET6:
18200Sstevel@tonic-gate 				rts_fill_msg_v6(ncmd, rtm_addrs,
18210Sstevel@tonic-gate 				    &ipv6_all_zeros, &ipif->ipif_v6net_mask,
18220Sstevel@tonic-gate 				    &ipv6_all_zeros, &ipif->ipif_v6lcl_addr,
18230Sstevel@tonic-gate 				    &ipif->ipif_v6pp_dst_addr, &ipv6_all_zeros,
1824*1676Sjpk 				    NULL, mp, 0, NULL);
18250Sstevel@tonic-gate 				break;
18260Sstevel@tonic-gate 			}
18270Sstevel@tonic-gate 			ifam = (ifa_msghdr_t *)mp->b_rptr;
18280Sstevel@tonic-gate 			ifam->ifam_index =
18290Sstevel@tonic-gate 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
18300Sstevel@tonic-gate 			ifam->ifam_metric = ipif->ipif_metric;
18310Sstevel@tonic-gate 			ifam->ifam_flags = ((cmd == RTM_ADD) ? RTF_UP : 0);
18320Sstevel@tonic-gate 			ifam->ifam_addrs = rtm_addrs;
18330Sstevel@tonic-gate 			rts_queue_input(mp, NULL, af);
18340Sstevel@tonic-gate 		}
18350Sstevel@tonic-gate 		if ((cmd == RTM_ADD && pass == 2) ||
18360Sstevel@tonic-gate 		    (cmd == RTM_DELETE && pass == 1)) {
18370Sstevel@tonic-gate 			rtm_addrs = (RTA_DST | RTA_NETMASK);
1838*1676Sjpk 			mp = rts_alloc_msg(cmd, rtm_addrs, af, 0);
18390Sstevel@tonic-gate 			if (mp == NULL)
18400Sstevel@tonic-gate 				continue;
18410Sstevel@tonic-gate 			switch (af) {
18420Sstevel@tonic-gate 			case AF_INET:
18430Sstevel@tonic-gate 				rts_fill_msg(cmd, rtm_addrs,
18440Sstevel@tonic-gate 				    ipif->ipif_lcl_addr, ipif->ipif_net_mask, 0,
1845*1676Sjpk 				    0, 0, 0, NULL, mp, 0, NULL);
18460Sstevel@tonic-gate 				break;
18470Sstevel@tonic-gate 			case AF_INET6:
18480Sstevel@tonic-gate 				rts_fill_msg_v6(cmd, rtm_addrs,
18490Sstevel@tonic-gate 				    &ipif->ipif_v6lcl_addr,
18500Sstevel@tonic-gate 				    &ipif->ipif_v6net_mask, &ipv6_all_zeros,
18510Sstevel@tonic-gate 				    &ipv6_all_zeros, &ipv6_all_zeros,
1852*1676Sjpk 				    &ipv6_all_zeros, NULL, mp, 0, NULL);
18530Sstevel@tonic-gate 				break;
18540Sstevel@tonic-gate 			}
18550Sstevel@tonic-gate 			rtm = (rt_msghdr_t *)mp->b_rptr;
18560Sstevel@tonic-gate 			rtm->rtm_index =
18570Sstevel@tonic-gate 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
18580Sstevel@tonic-gate 			rtm->rtm_flags = ((cmd == RTM_ADD) ? RTF_UP : 0);
18590Sstevel@tonic-gate 			rtm->rtm_errno = error;
18600Sstevel@tonic-gate 			if (error == 0)
18610Sstevel@tonic-gate 				rtm->rtm_flags |= RTF_DONE;
18620Sstevel@tonic-gate 			rtm->rtm_addrs = rtm_addrs;
18630Sstevel@tonic-gate 			rts_queue_input(mp, NULL, af);
18640Sstevel@tonic-gate 		}
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate }
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate /*
18690Sstevel@tonic-gate  * Based on the address family specified in a sockaddr, copy the address field
18700Sstevel@tonic-gate  * into an in6_addr_t.
18710Sstevel@tonic-gate  *
18720Sstevel@tonic-gate  * In the case of AF_UNSPEC, we assume the family is actually AF_INET for
18730Sstevel@tonic-gate  * compatibility with programs that leave the family cleared in the sockaddr.
18740Sstevel@tonic-gate  * Callers of rts_copyfromsockaddr should check the family themselves if they
18750Sstevel@tonic-gate  * wish to verify its value.
18760Sstevel@tonic-gate  *
18770Sstevel@tonic-gate  * In the case of AF_INET6, a check is made to ensure that address is not an
18780Sstevel@tonic-gate  * IPv4-mapped address.
18790Sstevel@tonic-gate  */
18800Sstevel@tonic-gate size_t
18810Sstevel@tonic-gate rts_copyfromsockaddr(struct sockaddr *sa, in6_addr_t *addrp)
18820Sstevel@tonic-gate {
18830Sstevel@tonic-gate 	switch (sa->sa_family) {
18840Sstevel@tonic-gate 	case AF_INET:
18850Sstevel@tonic-gate 	case AF_UNSPEC:
18860Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(((sin_t *)sa)->sin_addr.s_addr, addrp);
18870Sstevel@tonic-gate 		return (sizeof (sin_t));
18880Sstevel@tonic-gate 	case AF_INET6:
18890Sstevel@tonic-gate 		*addrp = ((sin6_t *)sa)->sin6_addr;
18900Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(addrp))
18910Sstevel@tonic-gate 			return (0);
18920Sstevel@tonic-gate 		return (sizeof (sin6_t));
18930Sstevel@tonic-gate 	default:
18940Sstevel@tonic-gate 		return (0);
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate }
1897