xref: /onnv-gate/usr/src/uts/common/inet/ip/ip_rts.c (revision 12670:0ac66a96529d)
10Sstevel@tonic-gate /*
2*12670SRamesh.K@Sun.COM  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate  */
40Sstevel@tonic-gate 
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate  * Copyright (c) 1988, 1991, 1993
70Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
100Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
110Sstevel@tonic-gate  * are met:
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
150Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
160Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
170Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
180Sstevel@tonic-gate  *    must display the following acknowledgement:
190Sstevel@tonic-gate  *	This product includes software developed by the University of
200Sstevel@tonic-gate  *	California, Berkeley and its contributors.
210Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
220Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
230Sstevel@tonic-gate  *    without specific prior written permission.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
260Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
270Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
280Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
290Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
300Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
310Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
320Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
330Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
340Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
350Sstevel@tonic-gate  * SUCH DAMAGE.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * This file contains routines that processes routing socket requests.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/stream.h>
460Sstevel@tonic-gate #include <sys/stropts.h>
470Sstevel@tonic-gate #include <sys/ddi.h>
488778SErik.Nordmark@Sun.COM #include <sys/strsubr.h>
490Sstevel@tonic-gate #include <sys/cmn_err.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate #include <sys/policy.h>
520Sstevel@tonic-gate #include <sys/zone.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/systm.h>
550Sstevel@tonic-gate #include <sys/param.h>
560Sstevel@tonic-gate #include <sys/socket.h>
570Sstevel@tonic-gate #include <sys/strsun.h>
580Sstevel@tonic-gate #include <net/if.h>
590Sstevel@tonic-gate #include <net/route.h>
600Sstevel@tonic-gate #include <netinet/in.h>
610Sstevel@tonic-gate #include <net/if_dl.h>
620Sstevel@tonic-gate #include <netinet/ip6.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <inet/common.h>
650Sstevel@tonic-gate #include <inet/ip.h>
660Sstevel@tonic-gate #include <inet/ip6.h>
670Sstevel@tonic-gate #include <inet/ip_if.h>
680Sstevel@tonic-gate #include <inet/ip_ire.h>
692535Ssangeeta #include <inet/ip_ftable.h>
700Sstevel@tonic-gate #include <inet/ip_rts.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <inet/ipclassifier.h>
730Sstevel@tonic-gate 
741676Sjpk #include <sys/tsol/tndb.h>
751676Sjpk #include <sys/tsol/tnet.h>
761676Sjpk 
771676Sjpk #define	RTS_MSG_SIZE(type, rtm_addrs, af, sacnt) \
781676Sjpk 	(rts_data_msg_size(rtm_addrs, af, sacnt) + rts_header_msg_size(type))
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static size_t	rts_copyfromsockaddr(struct sockaddr *sa, in6_addr_t *addrp);
810Sstevel@tonic-gate static void	rts_fill_msg(int type, int rtm_addrs, ipaddr_t dst,
820Sstevel@tonic-gate     ipaddr_t mask, ipaddr_t gateway, ipaddr_t src_addr, ipaddr_t brd_addr,
8311042SErik.Nordmark@Sun.COM     ipaddr_t author, ipaddr_t ifaddr, const ill_t *ill, mblk_t *mp,
8411042SErik.Nordmark@Sun.COM     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,
884823Sseb     sa_family_t *afp, tsol_rtsecattr_t *rtsecattr, int *error);
891676Sjpk static void	rts_getifdata(if_data_t *if_data, const ipif_t *ipif);
9011457SErik.Nordmark@Sun.COM static int	rts_getmetrics(ire_t *ire, ill_t *ill, rt_metrics_t *metrics);
9111042SErik.Nordmark@Sun.COM static mblk_t	*rts_rtmget(mblk_t *mp, ire_t *ire, ire_t *ifire,
9211042SErik.Nordmark@Sun.COM     const in6_addr_t *setsrc, tsol_ire_gw_secattr_t *attrp, sa_family_t af);
930Sstevel@tonic-gate static void	rts_setmetrics(ire_t *ire, uint_t which, rt_metrics_t *metrics);
9411042SErik.Nordmark@Sun.COM static ire_t	*ire_lookup_v4(ipaddr_t dst_addr, ipaddr_t net_mask,
9511042SErik.Nordmark@Sun.COM     ipaddr_t gw_addr, const ill_t *ill, zoneid_t zoneid,
9611042SErik.Nordmark@Sun.COM     const ts_label_t *tsl, int match_flags, ip_stack_t *ipst, ire_t **pifire,
9711042SErik.Nordmark@Sun.COM     ipaddr_t *v4setsrcp, tsol_ire_gw_secattr_t **gwattrp);
9811042SErik.Nordmark@Sun.COM static ire_t	*ire_lookup_v6(const in6_addr_t *dst_addr_v6,
9911042SErik.Nordmark@Sun.COM     const in6_addr_t *net_mask_v6, const in6_addr_t *gw_addr_v6,
10011042SErik.Nordmark@Sun.COM     const ill_t *ill, zoneid_t zoneid, const ts_label_t *tsl, int match_flags,
10111042SErik.Nordmark@Sun.COM     ip_stack_t *ipst, ire_t **pifire,
10211042SErik.Nordmark@Sun.COM     in6_addr_t *v6setsrcp, tsol_ire_gw_secattr_t **gwattrp);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1058485SPeter.Memishian@Sun.COM  * Send `mp' to all eligible routing queues.  A queue is ineligible if:
1060Sstevel@tonic-gate  *
1078485SPeter.Memishian@Sun.COM  *  1. SO_USELOOPBACK is off and it is not the originating queue.
10811042SErik.Nordmark@Sun.COM  *  2. RTA_UNDER_IPMP is on and RTSQ_UNDER_IPMP is not set in `flags'.
10911042SErik.Nordmark@Sun.COM  *  3. RTA_UNDER_IPMP is off and RTSQ_NORMAL is not set in `flags'.
1108485SPeter.Memishian@Sun.COM  *  4. It is not the same address family as `af', and `af' isn't AF_UNSPEC.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate void
rts_queue_input(mblk_t * mp,conn_t * o_connp,sa_family_t af,uint_t flags,ip_stack_t * ipst)1138485SPeter.Memishian@Sun.COM rts_queue_input(mblk_t *mp, conn_t *o_connp, sa_family_t af, uint_t flags,
1148485SPeter.Memishian@Sun.COM     ip_stack_t *ipst)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	mblk_t	*mp1;
1170Sstevel@tonic-gate 	conn_t 	*connp, *next_connp;
1180Sstevel@tonic-gate 
1198485SPeter.Memishian@Sun.COM 	/*
1208485SPeter.Memishian@Sun.COM 	 * Since we don't have an ill_t here, RTSQ_DEFAULT must already be
12111042SErik.Nordmark@Sun.COM 	 * resolved to one or more of RTSQ_NORMAL|RTSQ_UNDER_IPMP at this point.
1228485SPeter.Memishian@Sun.COM 	 */
1238485SPeter.Memishian@Sun.COM 	ASSERT(!(flags & RTSQ_DEFAULT));
1248485SPeter.Memishian@Sun.COM 
1253448Sdh155122 	mutex_enter(&ipst->ips_rts_clients->connf_lock);
1263448Sdh155122 	connp = ipst->ips_rts_clients->connf_head;
1270Sstevel@tonic-gate 
1288485SPeter.Memishian@Sun.COM 	for (; connp != NULL; connp = next_connp) {
1298485SPeter.Memishian@Sun.COM 		next_connp = connp->conn_next;
1300Sstevel@tonic-gate 		/*
1310Sstevel@tonic-gate 		 * If there was a family specified when this routing socket was
1320Sstevel@tonic-gate 		 * created and it doesn't match the family of the message to
1330Sstevel@tonic-gate 		 * copy, then continue.
1340Sstevel@tonic-gate 		 */
1350Sstevel@tonic-gate 		if ((connp->conn_proto != AF_UNSPEC) &&
1368485SPeter.Memishian@Sun.COM 		    (connp->conn_proto != af))
1370Sstevel@tonic-gate 			continue;
1388485SPeter.Memishian@Sun.COM 
1398485SPeter.Memishian@Sun.COM 		/*
1408485SPeter.Memishian@Sun.COM 		 * Queue the message only if the conn_t and flags match.
1418485SPeter.Memishian@Sun.COM 		 */
1428485SPeter.Memishian@Sun.COM 		if (connp->conn_rtaware & RTAW_UNDER_IPMP) {
1438485SPeter.Memishian@Sun.COM 			if (!(flags & RTSQ_UNDER_IPMP))
1448485SPeter.Memishian@Sun.COM 				continue;
1458485SPeter.Memishian@Sun.COM 		} else {
1468485SPeter.Memishian@Sun.COM 			if (!(flags & RTSQ_NORMAL))
1478485SPeter.Memishian@Sun.COM 				continue;
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 		/*
1500Sstevel@tonic-gate 		 * For the originating queue, we only copy the message upstream
1510Sstevel@tonic-gate 		 * if loopback is set.  For others reading on the routing
1520Sstevel@tonic-gate 		 * socket, we check if there is room upstream for a copy of the
1530Sstevel@tonic-gate 		 * message.
1540Sstevel@tonic-gate 		 */
15511042SErik.Nordmark@Sun.COM 		if ((o_connp == connp) && connp->conn_useloopback == 0) {
1568485SPeter.Memishian@Sun.COM 			connp = connp->conn_next;
1578485SPeter.Memishian@Sun.COM 			continue;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		CONN_INC_REF(connp);
1603448Sdh155122 		mutex_exit(&ipst->ips_rts_clients->connf_lock);
1615240Snordmark 		/* Pass to rts_input */
16211042SErik.Nordmark@Sun.COM 		if (IPCL_IS_NONSTR(connp) ? !connp->conn_flow_cntrld :
16311042SErik.Nordmark@Sun.COM 		    canputnext(connp->conn_rq)) {
1640Sstevel@tonic-gate 			mp1 = dupmsg(mp);
1650Sstevel@tonic-gate 			if (mp1 == NULL)
1660Sstevel@tonic-gate 				mp1 = copymsg(mp);
16711042SErik.Nordmark@Sun.COM 			/* Note that we pass a NULL ira to rts_input */
1680Sstevel@tonic-gate 			if (mp1 != NULL)
16911042SErik.Nordmark@Sun.COM 				(connp->conn_recv)(connp, mp1, NULL, NULL);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 
1723448Sdh155122 		mutex_enter(&ipst->ips_rts_clients->connf_lock);
1738485SPeter.Memishian@Sun.COM 		/* reload next_connp since conn_next may have changed */
1740Sstevel@tonic-gate 		next_connp = connp->conn_next;
1750Sstevel@tonic-gate 		CONN_DEC_REF(connp);
1760Sstevel@tonic-gate 	}
1773448Sdh155122 	mutex_exit(&ipst->ips_rts_clients->connf_lock);
1780Sstevel@tonic-gate 	freemsg(mp);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * Takes an ire and sends an ack to all the routing sockets. This
1830Sstevel@tonic-gate  * routine is used
1840Sstevel@tonic-gate  * - when a route is created/deleted through the ioctl interface.
18511042SErik.Nordmark@Sun.COM  * - when a stale redirect is deleted
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate void
ip_rts_rtmsg(int type,ire_t * ire,int error,ip_stack_t * ipst)1883448Sdh155122 ip_rts_rtmsg(int type, ire_t *ire, int error, ip_stack_t *ipst)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	mblk_t		*mp;
1910Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
1920Sstevel@tonic-gate 	int		rtm_addrs = (RTA_DST | RTA_NETMASK | RTA_GATEWAY);
1930Sstevel@tonic-gate 	sa_family_t	af;
1940Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (ire == NULL)
1970Sstevel@tonic-gate 		return;
1980Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
1990Sstevel@tonic-gate 	    ire->ire_ipversion == IPV6_VERSION);
2000Sstevel@tonic-gate 
20111042SErik.Nordmark@Sun.COM 	ASSERT(!(ire->ire_type & IRE_IF_CLONE));
20211042SErik.Nordmark@Sun.COM 
2030Sstevel@tonic-gate 	if (ire->ire_flags & RTF_SETSRC)
2040Sstevel@tonic-gate 		rtm_addrs |= RTA_SRC;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	switch (ire->ire_ipversion) {
2070Sstevel@tonic-gate 	case IPV4_VERSION:
2080Sstevel@tonic-gate 		af = AF_INET;
2091676Sjpk 		mp = rts_alloc_msg(type, rtm_addrs, af, 0);
2100Sstevel@tonic-gate 		if (mp == NULL)
2110Sstevel@tonic-gate 			return;
2120Sstevel@tonic-gate 		rts_fill_msg(type, rtm_addrs, ire->ire_addr, ire->ire_mask,
21311042SErik.Nordmark@Sun.COM 		    ire->ire_gateway_addr, ire->ire_setsrc_addr, 0, 0, 0, NULL,
21411042SErik.Nordmark@Sun.COM 		    mp, NULL);
2150Sstevel@tonic-gate 		break;
2160Sstevel@tonic-gate 	case IPV6_VERSION:
2170Sstevel@tonic-gate 		af = AF_INET6;
2181676Sjpk 		mp = rts_alloc_msg(type, rtm_addrs, af, 0);
2190Sstevel@tonic-gate 		if (mp == NULL)
2200Sstevel@tonic-gate 			return;
2210Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
2220Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
2230Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
2240Sstevel@tonic-gate 		rts_fill_msg_v6(type, rtm_addrs, &ire->ire_addr_v6,
2250Sstevel@tonic-gate 		    &ire->ire_mask_v6, &gw_addr_v6,
22611042SErik.Nordmark@Sun.COM 		    &ire->ire_setsrc_addr_v6, &ipv6_all_zeros, &ipv6_all_zeros,
22711042SErik.Nordmark@Sun.COM 		    &ipv6_all_zeros, NULL, mp, NULL);
2280Sstevel@tonic-gate 		break;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
2310Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&mp->b_rptr[rtm->rtm_msglen];
2320Sstevel@tonic-gate 	rtm->rtm_addrs = rtm_addrs;
2330Sstevel@tonic-gate 	rtm->rtm_flags = ire->ire_flags;
2340Sstevel@tonic-gate 	if (error != 0)
2350Sstevel@tonic-gate 		rtm->rtm_errno = error;
2360Sstevel@tonic-gate 	else
2370Sstevel@tonic-gate 		rtm->rtm_flags |= RTF_DONE;
2388485SPeter.Memishian@Sun.COM 	rts_queue_input(mp, NULL, af, RTSQ_ALL, ipst);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2425240Snordmark  * This is a call from the RTS module
2435240Snordmark  * indicating that this is a Routing Socket
2445240Snordmark  * Stream. Insert this conn_t in routing
2455240Snordmark  * socket client list.
2465240Snordmark  */
2475240Snordmark void
ip_rts_register(conn_t * connp)2485240Snordmark ip_rts_register(conn_t *connp)
2495240Snordmark {
2505240Snordmark 	ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
2515240Snordmark 
25211042SErik.Nordmark@Sun.COM 	connp->conn_useloopback = 1;
2535240Snordmark 	ipcl_hash_insert_wildcard(ipst->ips_rts_clients, connp);
2545240Snordmark }
2555240Snordmark 
2565240Snordmark /*
2575240Snordmark  * This is a call from the RTS module indicating that it is closing.
2585240Snordmark  */
2595240Snordmark void
ip_rts_unregister(conn_t * connp)2605240Snordmark ip_rts_unregister(conn_t *connp)
2615240Snordmark {
2625240Snordmark 	ipcl_hash_remove(connp);
2635240Snordmark }
2645240Snordmark 
2655240Snordmark /*
2660Sstevel@tonic-gate  * Processes requests received on a routing socket. It extracts all the
2670Sstevel@tonic-gate  * arguments and calls the appropriate function to process the request.
2680Sstevel@tonic-gate  *
2694823Sseb  * RTA_SRC bit flag requests are sent by 'route -setsrc'.
2700Sstevel@tonic-gate  *
2710Sstevel@tonic-gate  * In general, this function does not consume the message supplied but rather
2720Sstevel@tonic-gate  * sends the message upstream with an appropriate UNIX errno.
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate int
ip_rts_request_common(mblk_t * mp,conn_t * connp,cred_t * ioc_cr)27511042SErik.Nordmark@Sun.COM ip_rts_request_common(mblk_t *mp, conn_t *connp, cred_t *ioc_cr)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	rt_msghdr_t	*rtm = NULL;
2780Sstevel@tonic-gate 	in6_addr_t	dst_addr_v6;
2790Sstevel@tonic-gate 	in6_addr_t	src_addr_v6;
2800Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
2810Sstevel@tonic-gate 	in6_addr_t	net_mask_v6;
2820Sstevel@tonic-gate 	in6_addr_t	author_v6;
2830Sstevel@tonic-gate 	in6_addr_t	if_addr_v6;
28411042SErik.Nordmark@Sun.COM 	mblk_t		*mp1;
2850Sstevel@tonic-gate 	ire_t		*ire = NULL;
28611042SErik.Nordmark@Sun.COM 	ire_t		*ifire = NULL;
28711042SErik.Nordmark@Sun.COM 	ipaddr_t	v4setsrc;
28811042SErik.Nordmark@Sun.COM 	in6_addr_t	v6setsrc = ipv6_all_zeros;
28911042SErik.Nordmark@Sun.COM 	tsol_ire_gw_secattr_t *gwattr = NULL;
2900Sstevel@tonic-gate 	int		error = 0;
2910Sstevel@tonic-gate 	int		match_flags = MATCH_IRE_DSTONLY;
2922304Swy83408 	int		match_flags_local = MATCH_IRE_TYPE | MATCH_IRE_GW;
2930Sstevel@tonic-gate 	int		found_addrs;
2940Sstevel@tonic-gate 	sa_family_t	af;
2950Sstevel@tonic-gate 	ipaddr_t	dst_addr;
2960Sstevel@tonic-gate 	ipaddr_t	gw_addr;
2970Sstevel@tonic-gate 	ipaddr_t	src_addr;
2980Sstevel@tonic-gate 	ipaddr_t	net_mask;
2990Sstevel@tonic-gate 	ushort_t	index;
3001676Sjpk 	boolean_t	gcgrp_xtraref = B_FALSE;
3011676Sjpk 	tsol_gcgrp_addr_t ga;
3021676Sjpk 	tsol_rtsecattr_t rtsecattr;
3031676Sjpk 	struct rtsa_s	*rtsap = NULL;
3041676Sjpk 	tsol_gcgrp_t	*gcgrp = NULL;
3051676Sjpk 	tsol_gc_t	*gc = NULL;
3062601Swy83408 	ts_label_t	*tsl = NULL;
3072733Snordmark 	zoneid_t	zoneid;
3083448Sdh155122 	ip_stack_t	*ipst;
30911042SErik.Nordmark@Sun.COM 	ill_t   	*ill = NULL;
3100Sstevel@tonic-gate 
3112733Snordmark 	zoneid = connp->conn_zoneid;
3123448Sdh155122 	ipst = connp->conn_netstack->netstack_ip;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (mp->b_cont != NULL && !pullupmsg(mp, -1)) {
3150Sstevel@tonic-gate 		freemsg(mp);
3160Sstevel@tonic-gate 		error =  EINVAL;
3170Sstevel@tonic-gate 		goto done;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
3200Sstevel@tonic-gate 		freemsg(mp);
3210Sstevel@tonic-gate 		error = EINVAL;
3220Sstevel@tonic-gate 		goto done;
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * Check the routing message for basic consistency including the
3270Sstevel@tonic-gate 	 * version number and that the number of octets written is the same
3280Sstevel@tonic-gate 	 * as specified by the rtm_msglen field.
3290Sstevel@tonic-gate 	 *
3300Sstevel@tonic-gate 	 * At this point, an error can be delivered back via rtm_errno.
3310Sstevel@tonic-gate 	 */
3320Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
3330Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) != rtm->rtm_msglen) {
3340Sstevel@tonic-gate 		error = EINVAL;
3350Sstevel@tonic-gate 		goto done;
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 	if (rtm->rtm_version != RTM_VERSION) {
3380Sstevel@tonic-gate 		error = EPROTONOSUPPORT;
3390Sstevel@tonic-gate 		goto done;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/* Only allow RTM_GET or RTM_RESOLVE for unprivileged process */
3430Sstevel@tonic-gate 	if (rtm->rtm_type != RTM_GET &&
3440Sstevel@tonic-gate 	    rtm->rtm_type != RTM_RESOLVE &&
3450Sstevel@tonic-gate 	    (ioc_cr == NULL ||
3463448Sdh155122 	    secpolicy_ip_config(ioc_cr, B_FALSE) != 0)) {
3470Sstevel@tonic-gate 		error = EPERM;
3480Sstevel@tonic-gate 		goto done;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	found_addrs = rts_getaddrs(rtm, &dst_addr_v6, &gw_addr_v6, &net_mask_v6,
3524823Sseb 	    &author_v6, &if_addr_v6, &src_addr_v6, &index, &af, &rtsecattr,
3534823Sseb 	    &error);
3541676Sjpk 
3551676Sjpk 	if (error != 0)
3561676Sjpk 		goto done;
3571676Sjpk 
3580Sstevel@tonic-gate 	if ((found_addrs & RTA_DST) == 0) {
3590Sstevel@tonic-gate 		error = EINVAL;
3600Sstevel@tonic-gate 		goto done;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
3640Sstevel@tonic-gate 	 * Based on the address family of the destination address, determine
3650Sstevel@tonic-gate 	 * the destination, gateway and netmask and return the appropriate error
3660Sstevel@tonic-gate 	 * if an unknown address family was specified (following the errno
3670Sstevel@tonic-gate 	 * values that 4.4BSD-Lite2 returns.)
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	switch (af) {
3700Sstevel@tonic-gate 	case AF_INET:
3710Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&dst_addr_v6, dst_addr);
3720Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&src_addr_v6, src_addr);
3730Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&gw_addr_v6, gw_addr);
3740Sstevel@tonic-gate 		if (((found_addrs & RTA_NETMASK) == 0) ||
3750Sstevel@tonic-gate 		    (rtm->rtm_flags & RTF_HOST))
3760Sstevel@tonic-gate 			net_mask = IP_HOST_MASK;
3770Sstevel@tonic-gate 		else
3780Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&net_mask_v6, net_mask);
3790Sstevel@tonic-gate 		break;
3800Sstevel@tonic-gate 	case AF_INET6:
3810Sstevel@tonic-gate 		if (((found_addrs & RTA_NETMASK) == 0) ||
3820Sstevel@tonic-gate 		    (rtm->rtm_flags & RTF_HOST))
3830Sstevel@tonic-gate 			net_mask_v6 = ipv6_all_ones;
3840Sstevel@tonic-gate 		break;
3850Sstevel@tonic-gate 	default:
3860Sstevel@tonic-gate 		/*
3870Sstevel@tonic-gate 		 * These errno values are meant to be compatible with
3880Sstevel@tonic-gate 		 * 4.4BSD-Lite2 for the given message types.
3890Sstevel@tonic-gate 		 */
3900Sstevel@tonic-gate 		switch (rtm->rtm_type) {
3910Sstevel@tonic-gate 		case RTM_ADD:
3920Sstevel@tonic-gate 		case RTM_DELETE:
3930Sstevel@tonic-gate 			error = ESRCH;
3940Sstevel@tonic-gate 			goto done;
3950Sstevel@tonic-gate 		case RTM_GET:
3960Sstevel@tonic-gate 		case RTM_CHANGE:
3970Sstevel@tonic-gate 			error = EAFNOSUPPORT;
3980Sstevel@tonic-gate 			goto done;
3990Sstevel@tonic-gate 		default:
4000Sstevel@tonic-gate 			error = EOPNOTSUPP;
4010Sstevel@tonic-gate 			goto done;
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/*
4060Sstevel@tonic-gate 	 * At this point, the address family must be something known.
4070Sstevel@tonic-gate 	 */
4080Sstevel@tonic-gate 	ASSERT(af == AF_INET || af == AF_INET6);
4090Sstevel@tonic-gate 
41011042SErik.Nordmark@Sun.COM 	/* Handle RTA_IFP */
4110Sstevel@tonic-gate 	if (index != 0) {
41211042SErik.Nordmark@Sun.COM 		ipif_t		*ipif;
4138485SPeter.Memishian@Sun.COM lookup:
41411042SErik.Nordmark@Sun.COM 		ill = ill_lookup_on_ifindex(index, af == AF_INET6, ipst);
4150Sstevel@tonic-gate 		if (ill == NULL) {
41611042SErik.Nordmark@Sun.COM 			error = EINVAL;
4170Sstevel@tonic-gate 			goto done;
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4208485SPeter.Memishian@Sun.COM 		/*
4218485SPeter.Memishian@Sun.COM 		 * Since all interfaces in an IPMP group must be equivalent,
4228485SPeter.Memishian@Sun.COM 		 * we prevent changes to a specific underlying interface's
4238485SPeter.Memishian@Sun.COM 		 * routing configuration.  However, for backward compatibility,
4248485SPeter.Memishian@Sun.COM 		 * we intepret a request to add a route on an underlying
4258485SPeter.Memishian@Sun.COM 		 * interface as a request to add a route on its IPMP interface.
4268485SPeter.Memishian@Sun.COM 		 */
4278485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ill)) {
4288485SPeter.Memishian@Sun.COM 			switch (rtm->rtm_type) {
4298485SPeter.Memishian@Sun.COM 			case RTM_CHANGE:
4308485SPeter.Memishian@Sun.COM 			case RTM_DELETE:
4318485SPeter.Memishian@Sun.COM 				error = EINVAL;
4328485SPeter.Memishian@Sun.COM 				goto done;
4338485SPeter.Memishian@Sun.COM 			case RTM_ADD:
4348485SPeter.Memishian@Sun.COM 				index = ipmp_ill_get_ipmp_ifindex(ill);
4358485SPeter.Memishian@Sun.COM 				ill_refrele(ill);
4368485SPeter.Memishian@Sun.COM 				if (index == 0) {
43711042SErik.Nordmark@Sun.COM 					ill = NULL; /* already refrele'd */
4388485SPeter.Memishian@Sun.COM 					error = EINVAL;
4398485SPeter.Memishian@Sun.COM 					goto done;
4408485SPeter.Memishian@Sun.COM 				}
4418485SPeter.Memishian@Sun.COM 				goto lookup;
4428485SPeter.Memishian@Sun.COM 			}
4438485SPeter.Memishian@Sun.COM 		}
4448485SPeter.Memishian@Sun.COM 
44511042SErik.Nordmark@Sun.COM 		match_flags |= MATCH_IRE_ILL;
44611042SErik.Nordmark@Sun.COM 		/*
44711042SErik.Nordmark@Sun.COM 		 * This provides the same zoneid as in Solaris 10
44811042SErik.Nordmark@Sun.COM 		 * that -ifp picks the zoneid from the first ipif on the ill.
44911042SErik.Nordmark@Sun.COM 		 * But it might not be useful since the first ipif will always
45011042SErik.Nordmark@Sun.COM 		 * have the same zoneid as the ill.
45111042SErik.Nordmark@Sun.COM 		 */
4520Sstevel@tonic-gate 		ipif = ipif_get_next_ipif(NULL, ill);
45311042SErik.Nordmark@Sun.COM 		if (ipif != NULL) {
45411042SErik.Nordmark@Sun.COM 			zoneid = ipif->ipif_zoneid;
45511042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
45611042SErik.Nordmark@Sun.COM 		}
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * If a netmask was supplied in the message, then subsequent route
4610Sstevel@tonic-gate 	 * lookups will attempt to match on the netmask as well.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	if ((found_addrs & RTA_NETMASK) != 0)
4640Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
4650Sstevel@tonic-gate 
4661676Sjpk 	/*
4671676Sjpk 	 * We only process any passed-in route security attributes for
4682304Swy83408 	 * either RTM_ADD or RTM_CHANGE message; We overload them
4692304Swy83408 	 * to do an RTM_GET as a different label; ignore otherwise.
4701676Sjpk 	 */
4712304Swy83408 	if (rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE ||
4722304Swy83408 	    rtm->rtm_type == RTM_GET) {
4731676Sjpk 		ASSERT(rtsecattr.rtsa_cnt <= TSOL_RTSA_REQUEST_MAX);
4741676Sjpk 		if (rtsecattr.rtsa_cnt > 0)
4751676Sjpk 			rtsap = &rtsecattr.rtsa_attr[0];
4761676Sjpk 	}
4771676Sjpk 
4780Sstevel@tonic-gate 	switch (rtm->rtm_type) {
4790Sstevel@tonic-gate 	case RTM_ADD:
4800Sstevel@tonic-gate 		/* if we are adding a route, gateway is a must */
4810Sstevel@tonic-gate 		if ((found_addrs & RTA_GATEWAY) == 0) {
4820Sstevel@tonic-gate 			error = EINVAL;
4830Sstevel@tonic-gate 			goto done;
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		/* Multirouting does not support net routes. */
4870Sstevel@tonic-gate 		if ((rtm->rtm_flags & (RTF_MULTIRT | RTF_HOST)) ==
4880Sstevel@tonic-gate 		    RTF_MULTIRT) {
4890Sstevel@tonic-gate 			error = EADDRNOTAVAIL;
4900Sstevel@tonic-gate 			goto done;
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * Multirouting and user-specified source addresses
4950Sstevel@tonic-gate 		 * do not support interface based routing.
4960Sstevel@tonic-gate 		 * Assigning a source address to an interface based
4970Sstevel@tonic-gate 		 * route is achievable by plumbing a new ipif and
4980Sstevel@tonic-gate 		 * setting up the interface route via this ipif,
4990Sstevel@tonic-gate 		 * though.
5000Sstevel@tonic-gate 		 */
5010Sstevel@tonic-gate 		if (rtm->rtm_flags & (RTF_MULTIRT | RTF_SETSRC)) {
5020Sstevel@tonic-gate 			if ((rtm->rtm_flags & RTF_GATEWAY) == 0) {
5030Sstevel@tonic-gate 				error = EADDRNOTAVAIL;
5040Sstevel@tonic-gate 				goto done;
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		switch (af) {
5090Sstevel@tonic-gate 		case AF_INET:
5100Sstevel@tonic-gate 			if (src_addr != INADDR_ANY) {
51111042SErik.Nordmark@Sun.COM 				uint_t type;
51211042SErik.Nordmark@Sun.COM 
5130Sstevel@tonic-gate 				/*
5140Sstevel@tonic-gate 				 * The RTF_SETSRC flag is present, check that
5150Sstevel@tonic-gate 				 * the supplied src address is not the loopback
5160Sstevel@tonic-gate 				 * address. This would produce martian packets.
5170Sstevel@tonic-gate 				 */
5180Sstevel@tonic-gate 				if (src_addr == htonl(INADDR_LOOPBACK)) {
5190Sstevel@tonic-gate 					error = EINVAL;
5200Sstevel@tonic-gate 					goto done;
5210Sstevel@tonic-gate 				}
5220Sstevel@tonic-gate 				/*
5230Sstevel@tonic-gate 				 * Also check that the supplied address is a
52411042SErik.Nordmark@Sun.COM 				 * valid, local one. Only allow IFF_UP ones
5250Sstevel@tonic-gate 				 */
52611042SErik.Nordmark@Sun.COM 				type = ip_type_v4(src_addr, ipst);
52711042SErik.Nordmark@Sun.COM 				if (!(type & (IRE_LOCAL|IRE_LOOPBACK))) {
52811042SErik.Nordmark@Sun.COM 					error = EADDRNOTAVAIL;
5290Sstevel@tonic-gate 					goto done;
5300Sstevel@tonic-gate 				}
5310Sstevel@tonic-gate 			} else {
5320Sstevel@tonic-gate 				/*
5330Sstevel@tonic-gate 				 * The RTF_SETSRC modifier must be associated
5340Sstevel@tonic-gate 				 * to a non-null source address.
5350Sstevel@tonic-gate 				 */
5360Sstevel@tonic-gate 				if (rtm->rtm_flags & RTF_SETSRC) {
5370Sstevel@tonic-gate 					error = EINVAL;
5380Sstevel@tonic-gate 					goto done;
5390Sstevel@tonic-gate 				}
5400Sstevel@tonic-gate 			}
5410Sstevel@tonic-gate 
5421676Sjpk 			error = ip_rt_add(dst_addr, net_mask, gw_addr, src_addr,
54311042SErik.Nordmark@Sun.COM 			    rtm->rtm_flags, ill, &ire, B_FALSE,
54411042SErik.Nordmark@Sun.COM 			    rtsap, ipst, zoneid);
54511042SErik.Nordmark@Sun.COM 			if (ill != NULL)
54611042SErik.Nordmark@Sun.COM 				ASSERT(!MUTEX_HELD(&ill->ill_lock));
5470Sstevel@tonic-gate 			break;
5480Sstevel@tonic-gate 		case AF_INET6:
5490Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&src_addr_v6)) {
55011042SErik.Nordmark@Sun.COM 				uint_t type;
55111042SErik.Nordmark@Sun.COM 
5520Sstevel@tonic-gate 				/*
5530Sstevel@tonic-gate 				 * The RTF_SETSRC flag is present, check that
5540Sstevel@tonic-gate 				 * the supplied src address is not the loopback
5550Sstevel@tonic-gate 				 * address. This would produce martian packets.
5560Sstevel@tonic-gate 				 */
5570Sstevel@tonic-gate 				if (IN6_IS_ADDR_LOOPBACK(&src_addr_v6)) {
5580Sstevel@tonic-gate 					error = EINVAL;
5590Sstevel@tonic-gate 					goto done;
5600Sstevel@tonic-gate 				}
5610Sstevel@tonic-gate 				/*
5620Sstevel@tonic-gate 				 * Also check that the supplied address is a
56311042SErik.Nordmark@Sun.COM 				 * valid, local one. Only allow UP ones.
5640Sstevel@tonic-gate 				 */
56511042SErik.Nordmark@Sun.COM 				type = ip_type_v6(&src_addr_v6, ipst);
56611042SErik.Nordmark@Sun.COM 				if (!(type & (IRE_LOCAL|IRE_LOOPBACK))) {
56711042SErik.Nordmark@Sun.COM 					error = EADDRNOTAVAIL;
5680Sstevel@tonic-gate 					goto done;
5690Sstevel@tonic-gate 				}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 				error = ip_rt_add_v6(&dst_addr_v6, &net_mask_v6,
5720Sstevel@tonic-gate 				    &gw_addr_v6, &src_addr_v6, rtm->rtm_flags,
57311042SErik.Nordmark@Sun.COM 				    ill, &ire, rtsap, ipst, zoneid);
5740Sstevel@tonic-gate 				break;
5750Sstevel@tonic-gate 			}
5760Sstevel@tonic-gate 			/*
5770Sstevel@tonic-gate 			 * The RTF_SETSRC modifier must be associated
5780Sstevel@tonic-gate 			 * to a non-null source address.
5790Sstevel@tonic-gate 			 */
5800Sstevel@tonic-gate 			if (rtm->rtm_flags & RTF_SETSRC) {
5810Sstevel@tonic-gate 				error = EINVAL;
5820Sstevel@tonic-gate 				goto done;
5830Sstevel@tonic-gate 			}
5840Sstevel@tonic-gate 			error = ip_rt_add_v6(&dst_addr_v6, &net_mask_v6,
5850Sstevel@tonic-gate 			    &gw_addr_v6, NULL, rtm->rtm_flags,
58611042SErik.Nordmark@Sun.COM 			    ill, &ire, rtsap, ipst, zoneid);
58711042SErik.Nordmark@Sun.COM 			if (ill != NULL)
58811042SErik.Nordmark@Sun.COM 				ASSERT(!MUTEX_HELD(&ill->ill_lock));
5890Sstevel@tonic-gate 			break;
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 		if (error != 0)
5920Sstevel@tonic-gate 			goto done;
5930Sstevel@tonic-gate 		ASSERT(ire != NULL);
5940Sstevel@tonic-gate 		rts_setmetrics(ire, rtm->rtm_inits, &rtm->rtm_rmx);
5950Sstevel@tonic-gate 		break;
5960Sstevel@tonic-gate 	case RTM_DELETE:
5970Sstevel@tonic-gate 		/* if we are deleting a route, gateway is a must */
5980Sstevel@tonic-gate 		if ((found_addrs & RTA_GATEWAY) == 0) {
5990Sstevel@tonic-gate 			error = EINVAL;
6000Sstevel@tonic-gate 			goto done;
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 		/*
6030Sstevel@tonic-gate 		 * The RTF_SETSRC modifier does not make sense
6040Sstevel@tonic-gate 		 * when deleting a route.
6050Sstevel@tonic-gate 		 */
6060Sstevel@tonic-gate 		if (rtm->rtm_flags & RTF_SETSRC) {
6070Sstevel@tonic-gate 			error = EINVAL;
6080Sstevel@tonic-gate 			goto done;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		switch (af) {
6120Sstevel@tonic-gate 		case AF_INET:
6130Sstevel@tonic-gate 			error = ip_rt_delete(dst_addr, net_mask, gw_addr,
61411042SErik.Nordmark@Sun.COM 			    found_addrs, rtm->rtm_flags, ill, B_FALSE,
61511042SErik.Nordmark@Sun.COM 			    ipst, zoneid);
6160Sstevel@tonic-gate 			break;
6170Sstevel@tonic-gate 		case AF_INET6:
6180Sstevel@tonic-gate 			error = ip_rt_delete_v6(&dst_addr_v6, &net_mask_v6,
61911042SErik.Nordmark@Sun.COM 			    &gw_addr_v6, found_addrs, rtm->rtm_flags, ill,
62011042SErik.Nordmark@Sun.COM 			    ipst, zoneid);
6210Sstevel@tonic-gate 			break;
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 		break;
6240Sstevel@tonic-gate 	case RTM_GET:
6250Sstevel@tonic-gate 	case RTM_CHANGE:
6260Sstevel@tonic-gate 		/*
6270Sstevel@tonic-gate 		 * In the case of RTM_GET, the forwarding table should be
62811042SErik.Nordmark@Sun.COM 		 * searched recursively.  Also, if a gateway was
6290Sstevel@tonic-gate 		 * specified then the gateway address must also be matched.
6300Sstevel@tonic-gate 		 *
6310Sstevel@tonic-gate 		 * In the case of RTM_CHANGE, the gateway address (if supplied)
6320Sstevel@tonic-gate 		 * is the new gateway address so matching on the gateway address
6330Sstevel@tonic-gate 		 * is not done.  This can lead to ambiguity when looking up the
6340Sstevel@tonic-gate 		 * route to change as usually only the destination (and netmask,
6350Sstevel@tonic-gate 		 * if supplied) is used for the lookup.  However if a RTA_IFP
6360Sstevel@tonic-gate 		 * sockaddr is also supplied, it can disambiguate which route to
6370Sstevel@tonic-gate 		 * change provided the ambigous routes are tied to distinct
6380Sstevel@tonic-gate 		 * ill's (or interface indices).  If the routes are not tied to
6390Sstevel@tonic-gate 		 * any particular interfaces (for example, with traditional
6400Sstevel@tonic-gate 		 * gateway routes), then a RTA_IFP sockaddr will be of no use as
6410Sstevel@tonic-gate 		 * it won't match any such routes.
6420Sstevel@tonic-gate 		 * RTA_SRC is not supported for RTM_GET and RTM_CHANGE,
6430Sstevel@tonic-gate 		 * except when RTM_CHANGE is combined to RTF_SETSRC.
6440Sstevel@tonic-gate 		 */
6450Sstevel@tonic-gate 		if (((found_addrs & RTA_SRC) != 0) &&
6460Sstevel@tonic-gate 		    ((rtm->rtm_type == RTM_GET) ||
6470Sstevel@tonic-gate 		    !(rtm->rtm_flags & RTF_SETSRC))) {
6480Sstevel@tonic-gate 			error = EOPNOTSUPP;
6490Sstevel@tonic-gate 			goto done;
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 		if (rtm->rtm_type == RTM_GET) {
65311042SErik.Nordmark@Sun.COM 			match_flags |= MATCH_IRE_SECATTR;
6542304Swy83408 			match_flags_local |= MATCH_IRE_SECATTR;
6550Sstevel@tonic-gate 			if ((found_addrs & RTA_GATEWAY) != 0)
6560Sstevel@tonic-gate 				match_flags |= MATCH_IRE_GW;
6572601Swy83408 			if (ioc_cr)
6582601Swy83408 				tsl = crgetlabel(ioc_cr);
6592304Swy83408 			if (rtsap != NULL) {
6602304Swy83408 				if (rtsa_validate(rtsap) != 0) {
6612304Swy83408 					error = EINVAL;
6622304Swy83408 					goto done;
6632304Swy83408 				}
6642601Swy83408 				if (tsl != NULL &&
6652601Swy83408 				    crgetzoneid(ioc_cr) != GLOBAL_ZONEID &&
6662304Swy83408 				    (tsl->tsl_doi != rtsap->rtsa_doi ||
6672304Swy83408 				    !bldominates(&tsl->tsl_label,
6682304Swy83408 				    &rtsap->rtsa_slrange.lower_bound))) {
6692304Swy83408 					error = EPERM;
6702304Swy83408 					goto done;
6712304Swy83408 				}
6722304Swy83408 				tsl = labelalloc(
6732304Swy83408 				    &rtsap->rtsa_slrange.lower_bound,
6742304Swy83408 				    rtsap->rtsa_doi, KM_NOSLEEP);
6752304Swy83408 			}
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		if (rtm->rtm_type == RTM_CHANGE) {
6780Sstevel@tonic-gate 			if ((found_addrs & RTA_GATEWAY) &&
6790Sstevel@tonic-gate 			    (rtm->rtm_flags & RTF_SETSRC)) {
6800Sstevel@tonic-gate 				/*
6810Sstevel@tonic-gate 				 * Do not want to change the gateway,
6820Sstevel@tonic-gate 				 * but rather the source address.
6830Sstevel@tonic-gate 				 */
6840Sstevel@tonic-gate 				match_flags |= MATCH_IRE_GW;
6850Sstevel@tonic-gate 			}
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		/*
6890Sstevel@tonic-gate 		 * If the netmask is all ones (either as supplied or as derived
6900Sstevel@tonic-gate 		 * above), then first check for an IRE_LOOPBACK or
6910Sstevel@tonic-gate 		 * IRE_LOCAL entry.
6920Sstevel@tonic-gate 		 *
6930Sstevel@tonic-gate 		 * If we didn't check for or find an IRE_LOOPBACK or IRE_LOCAL
69411042SErik.Nordmark@Sun.COM 		 * entry, then look for any other type of IRE.
6950Sstevel@tonic-gate 		 */
6960Sstevel@tonic-gate 		switch (af) {
6970Sstevel@tonic-gate 		case AF_INET:
6980Sstevel@tonic-gate 			if (net_mask == IP_HOST_MASK) {
69911042SErik.Nordmark@Sun.COM 				ire = ire_ftable_lookup_v4(dst_addr, 0, gw_addr,
7002733Snordmark 				    IRE_LOCAL | IRE_LOOPBACK, NULL, zoneid,
70111042SErik.Nordmark@Sun.COM 				    tsl, match_flags_local, 0, ipst, NULL);
7020Sstevel@tonic-gate 			}
7030Sstevel@tonic-gate 			if (ire == NULL) {
70411042SErik.Nordmark@Sun.COM 				ire = ire_lookup_v4(dst_addr, net_mask,
70511042SErik.Nordmark@Sun.COM 				    gw_addr, ill, zoneid, tsl, match_flags,
70611042SErik.Nordmark@Sun.COM 				    ipst, &ifire, &v4setsrc, &gwattr);
70711042SErik.Nordmark@Sun.COM 				IN6_IPADDR_TO_V4MAPPED(v4setsrc, &v6setsrc);
7080Sstevel@tonic-gate 			}
7090Sstevel@tonic-gate 			break;
7100Sstevel@tonic-gate 		case AF_INET6:
7110Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&net_mask_v6, &ipv6_all_ones)) {
71211042SErik.Nordmark@Sun.COM 				ire = ire_ftable_lookup_v6(&dst_addr_v6, NULL,
7130Sstevel@tonic-gate 				    &gw_addr_v6, IRE_LOCAL | IRE_LOOPBACK, NULL,
71411042SErik.Nordmark@Sun.COM 				    zoneid, tsl, match_flags_local, 0, ipst,
71511042SErik.Nordmark@Sun.COM 				    NULL);
7160Sstevel@tonic-gate 			}
7170Sstevel@tonic-gate 			if (ire == NULL) {
71811042SErik.Nordmark@Sun.COM 				ire = ire_lookup_v6(&dst_addr_v6,
71911042SErik.Nordmark@Sun.COM 				    &net_mask_v6, &gw_addr_v6, ill, zoneid,
72011042SErik.Nordmark@Sun.COM 				    tsl, match_flags, ipst, &ifire, &v6setsrc,
72111042SErik.Nordmark@Sun.COM 				    &gwattr);
7220Sstevel@tonic-gate 			}
7230Sstevel@tonic-gate 			break;
7240Sstevel@tonic-gate 		}
7252304Swy83408 		if (tsl != NULL && tsl != crgetlabel(ioc_cr))
7262304Swy83408 			label_rele(tsl);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		if (ire == NULL) {
7290Sstevel@tonic-gate 			error = ESRCH;
7300Sstevel@tonic-gate 			goto done;
7310Sstevel@tonic-gate 		}
73211042SErik.Nordmark@Sun.COM 		/*
73311042SErik.Nordmark@Sun.COM 		 * Want to return failure if we get an IRE_NOROUTE from
73411042SErik.Nordmark@Sun.COM 		 * ire_route_recursive
73511042SErik.Nordmark@Sun.COM 		 */
73611042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_NOROUTE) {
73711042SErik.Nordmark@Sun.COM 			ire_refrele(ire);
73811042SErik.Nordmark@Sun.COM 			ire = NULL;
73911042SErik.Nordmark@Sun.COM 			error = ESRCH;
74011042SErik.Nordmark@Sun.COM 			goto done;
74111042SErik.Nordmark@Sun.COM 		}
74211042SErik.Nordmark@Sun.COM 
7430Sstevel@tonic-gate 		/* we know the IRE before we come here */
7440Sstevel@tonic-gate 		switch (rtm->rtm_type) {
7450Sstevel@tonic-gate 		case RTM_GET:
74611042SErik.Nordmark@Sun.COM 			mp1 = rts_rtmget(mp, ire, ifire, &v6setsrc, gwattr, af);
7470Sstevel@tonic-gate 			if (mp1 == NULL) {
7480Sstevel@tonic-gate 				error = ENOBUFS;
7490Sstevel@tonic-gate 				goto done;
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 			freemsg(mp);
7520Sstevel@tonic-gate 			mp = mp1;
7530Sstevel@tonic-gate 			rtm = (rt_msghdr_t *)mp->b_rptr;
7540Sstevel@tonic-gate 			break;
7550Sstevel@tonic-gate 		case RTM_CHANGE:
7560Sstevel@tonic-gate 			/*
7570Sstevel@tonic-gate 			 * Do not allow to the multirouting state of a route
7580Sstevel@tonic-gate 			 * to be changed. This aims to prevent undesirable
7590Sstevel@tonic-gate 			 * stages where both multirt and non-multirt routes
7600Sstevel@tonic-gate 			 * for the same destination are declared.
7610Sstevel@tonic-gate 			 */
7620Sstevel@tonic-gate 			if ((ire->ire_flags & RTF_MULTIRT) !=
7630Sstevel@tonic-gate 			    (rtm->rtm_flags & RTF_MULTIRT)) {
7640Sstevel@tonic-gate 				error = EINVAL;
7650Sstevel@tonic-gate 				goto done;
7660Sstevel@tonic-gate 			}
7670Sstevel@tonic-gate 			/*
7680Sstevel@tonic-gate 			 * Note that we do not need to do
7690Sstevel@tonic-gate 			 * ire_flush_cache_*(IRE_FLUSH_ADD) as a change
7700Sstevel@tonic-gate 			 * in metrics or gateway will not affect existing
7710Sstevel@tonic-gate 			 * routes since it does not create a more specific
7720Sstevel@tonic-gate 			 * route.
7730Sstevel@tonic-gate 			 */
7740Sstevel@tonic-gate 			switch (af) {
7750Sstevel@tonic-gate 			case AF_INET:
7760Sstevel@tonic-gate 				if ((found_addrs & RTA_GATEWAY) != 0 &&
7770Sstevel@tonic-gate 				    (ire->ire_gateway_addr != gw_addr)) {
7780Sstevel@tonic-gate 					ire->ire_gateway_addr = gw_addr;
7790Sstevel@tonic-gate 				}
7801676Sjpk 
7811676Sjpk 				if (rtsap != NULL) {
7821676Sjpk 					ga.ga_af = AF_INET;
7831676Sjpk 					IN6_IPADDR_TO_V4MAPPED(
7841676Sjpk 					    ire->ire_gateway_addr, &ga.ga_addr);
7851676Sjpk 
7861676Sjpk 					gcgrp = gcgrp_lookup(&ga, B_TRUE);
7871676Sjpk 					if (gcgrp == NULL) {
7881676Sjpk 						error = ENOMEM;
7891676Sjpk 						goto done;
7901676Sjpk 					}
7911676Sjpk 				}
7921676Sjpk 
7930Sstevel@tonic-gate 				if ((found_addrs & RTA_SRC) != 0 &&
7940Sstevel@tonic-gate 				    (rtm->rtm_flags & RTF_SETSRC) != 0 &&
79511042SErik.Nordmark@Sun.COM 				    (ire->ire_setsrc_addr != src_addr)) {
79611042SErik.Nordmark@Sun.COM 					if (src_addr != INADDR_ANY) {
79711042SErik.Nordmark@Sun.COM 						uint_t type;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 						/*
8000Sstevel@tonic-gate 						 * The RTF_SETSRC flag is
8010Sstevel@tonic-gate 						 * present, check that the
8020Sstevel@tonic-gate 						 * supplied src address is not
8030Sstevel@tonic-gate 						 * the loopback address. This
8040Sstevel@tonic-gate 						 * would produce martian
8050Sstevel@tonic-gate 						 * packets.
8060Sstevel@tonic-gate 						 */
8070Sstevel@tonic-gate 						if (src_addr ==
8080Sstevel@tonic-gate 						    htonl(INADDR_LOOPBACK)) {
8090Sstevel@tonic-gate 							error = EINVAL;
8100Sstevel@tonic-gate 							goto done;
8110Sstevel@tonic-gate 						}
8120Sstevel@tonic-gate 						/*
81311042SErik.Nordmark@Sun.COM 						 * Also check that the
8140Sstevel@tonic-gate 						 * supplied addr is a valid
8150Sstevel@tonic-gate 						 * local address.
8160Sstevel@tonic-gate 						 */
81711042SErik.Nordmark@Sun.COM 						type = ip_type_v4(src_addr,
81811042SErik.Nordmark@Sun.COM 						    ipst);
81911042SErik.Nordmark@Sun.COM 						if (!(type &
82011042SErik.Nordmark@Sun.COM 						    (IRE_LOCAL|IRE_LOOPBACK))) {
82111042SErik.Nordmark@Sun.COM 							error = EADDRNOTAVAIL;
8220Sstevel@tonic-gate 							goto done;
8230Sstevel@tonic-gate 						}
8240Sstevel@tonic-gate 						ire->ire_flags |= RTF_SETSRC;
82511042SErik.Nordmark@Sun.COM 						ire->ire_setsrc_addr =
82611042SErik.Nordmark@Sun.COM 						    src_addr;
8270Sstevel@tonic-gate 					} else {
8280Sstevel@tonic-gate 						ire->ire_flags &= ~RTF_SETSRC;
82911042SErik.Nordmark@Sun.COM 						ire->ire_setsrc_addr =
83011042SErik.Nordmark@Sun.COM 						    INADDR_ANY;
8310Sstevel@tonic-gate 					}
83211042SErik.Nordmark@Sun.COM 					/*
83311042SErik.Nordmark@Sun.COM 					 * Let conn_ixa caching know that
83411042SErik.Nordmark@Sun.COM 					 * source address selection changed
83511042SErik.Nordmark@Sun.COM 					 */
83611042SErik.Nordmark@Sun.COM 					ip_update_source_selection(ipst);
8370Sstevel@tonic-gate 				}
83811042SErik.Nordmark@Sun.COM 				ire_flush_cache_v4(ire, IRE_FLUSH_GWCHANGE);
8390Sstevel@tonic-gate 				break;
8400Sstevel@tonic-gate 			case AF_INET6:
8410Sstevel@tonic-gate 				mutex_enter(&ire->ire_lock);
8420Sstevel@tonic-gate 				if ((found_addrs & RTA_GATEWAY) != 0 &&
8430Sstevel@tonic-gate 				    !IN6_ARE_ADDR_EQUAL(
8440Sstevel@tonic-gate 				    &ire->ire_gateway_addr_v6, &gw_addr_v6)) {
8450Sstevel@tonic-gate 					ire->ire_gateway_addr_v6 = gw_addr_v6;
8460Sstevel@tonic-gate 				}
84711042SErik.Nordmark@Sun.COM 				mutex_exit(&ire->ire_lock);
8481676Sjpk 
8491676Sjpk 				if (rtsap != NULL) {
8501676Sjpk 					ga.ga_af = AF_INET6;
85111042SErik.Nordmark@Sun.COM 					mutex_enter(&ire->ire_lock);
8521676Sjpk 					ga.ga_addr = ire->ire_gateway_addr_v6;
85311042SErik.Nordmark@Sun.COM 					mutex_exit(&ire->ire_lock);
8541676Sjpk 
8551676Sjpk 					gcgrp = gcgrp_lookup(&ga, B_TRUE);
8561676Sjpk 					if (gcgrp == NULL) {
8571676Sjpk 						error = ENOMEM;
8581676Sjpk 						goto done;
8591676Sjpk 					}
8601676Sjpk 				}
8611676Sjpk 
8620Sstevel@tonic-gate 				if ((found_addrs & RTA_SRC) != 0 &&
8630Sstevel@tonic-gate 				    (rtm->rtm_flags & RTF_SETSRC) != 0 &&
8640Sstevel@tonic-gate 				    !IN6_ARE_ADDR_EQUAL(
86511042SErik.Nordmark@Sun.COM 				    &ire->ire_setsrc_addr_v6, &src_addr_v6)) {
8660Sstevel@tonic-gate 					if (!IN6_IS_ADDR_UNSPECIFIED(
8670Sstevel@tonic-gate 					    &src_addr_v6)) {
86811042SErik.Nordmark@Sun.COM 						uint_t type;
86911042SErik.Nordmark@Sun.COM 
8700Sstevel@tonic-gate 						/*
8710Sstevel@tonic-gate 						 * The RTF_SETSRC flag is
8720Sstevel@tonic-gate 						 * present, check that the
8730Sstevel@tonic-gate 						 * supplied src address is not
8740Sstevel@tonic-gate 						 * the loopback address. This
8750Sstevel@tonic-gate 						 * would produce martian
8760Sstevel@tonic-gate 						 * packets.
8770Sstevel@tonic-gate 						 */
8780Sstevel@tonic-gate 						if (IN6_IS_ADDR_LOOPBACK(
8790Sstevel@tonic-gate 						    &src_addr_v6)) {
8800Sstevel@tonic-gate 							error = EINVAL;
8810Sstevel@tonic-gate 							goto done;
8820Sstevel@tonic-gate 						}
8830Sstevel@tonic-gate 						/*
88411042SErik.Nordmark@Sun.COM 						 * Also check that the
8850Sstevel@tonic-gate 						 * supplied addr is a valid
8860Sstevel@tonic-gate 						 * local address.
8870Sstevel@tonic-gate 						 */
88811042SErik.Nordmark@Sun.COM 						type = ip_type_v6(&src_addr_v6,
88911042SErik.Nordmark@Sun.COM 						    ipst);
89011042SErik.Nordmark@Sun.COM 						if (!(type &
89111042SErik.Nordmark@Sun.COM 						    (IRE_LOCAL|IRE_LOOPBACK))) {
89211042SErik.Nordmark@Sun.COM 							error = EADDRNOTAVAIL;
8930Sstevel@tonic-gate 							goto done;
8940Sstevel@tonic-gate 						}
89511042SErik.Nordmark@Sun.COM 						mutex_enter(&ire->ire_lock);
8960Sstevel@tonic-gate 						ire->ire_flags |= RTF_SETSRC;
89711042SErik.Nordmark@Sun.COM 						ire->ire_setsrc_addr_v6 =
89811042SErik.Nordmark@Sun.COM 						    src_addr_v6;
89911042SErik.Nordmark@Sun.COM 						mutex_exit(&ire->ire_lock);
9000Sstevel@tonic-gate 					} else {
90111042SErik.Nordmark@Sun.COM 						mutex_enter(&ire->ire_lock);
9020Sstevel@tonic-gate 						ire->ire_flags &= ~RTF_SETSRC;
90311042SErik.Nordmark@Sun.COM 						ire->ire_setsrc_addr_v6 =
90411042SErik.Nordmark@Sun.COM 						    ipv6_all_zeros;
90511042SErik.Nordmark@Sun.COM 						mutex_exit(&ire->ire_lock);
9060Sstevel@tonic-gate 					}
90711042SErik.Nordmark@Sun.COM 					/*
90811042SErik.Nordmark@Sun.COM 					 * Let conn_ixa caching know that
90911042SErik.Nordmark@Sun.COM 					 * source address selection changed
91011042SErik.Nordmark@Sun.COM 					 */
91111042SErik.Nordmark@Sun.COM 					ip_update_source_selection(ipst);
9120Sstevel@tonic-gate 				}
91311042SErik.Nordmark@Sun.COM 				ire_flush_cache_v6(ire, IRE_FLUSH_GWCHANGE);
9140Sstevel@tonic-gate 				break;
9150Sstevel@tonic-gate 			}
9161676Sjpk 
9171676Sjpk 			if (rtsap != NULL) {
9181676Sjpk 				ASSERT(gcgrp != NULL);
9191676Sjpk 
9201676Sjpk 				/*
9211676Sjpk 				 * Create and add the security attribute to
9221676Sjpk 				 * prefix IRE; it will add a reference to the
9231676Sjpk 				 * group upon allocating a new entry.  If it
9241676Sjpk 				 * finds an already-existing entry for the
9251676Sjpk 				 * security attribute, it simply returns it
9261676Sjpk 				 * and no new group reference is made.
9271676Sjpk 				 */
9281676Sjpk 				gc = gc_create(rtsap, gcgrp, &gcgrp_xtraref);
9291676Sjpk 				if (gc == NULL ||
9301676Sjpk 				    (error = tsol_ire_init_gwattr(ire,
93111042SErik.Nordmark@Sun.COM 				    ire->ire_ipversion, gc)) != 0) {
9321676Sjpk 					if (gc != NULL) {
9331676Sjpk 						GC_REFRELE(gc);
9341676Sjpk 					} else {
9351676Sjpk 						/* gc_create failed */
9361676Sjpk 						error = ENOMEM;
9371676Sjpk 					}
9381676Sjpk 					goto done;
9391676Sjpk 				}
9401676Sjpk 			}
9410Sstevel@tonic-gate 			rts_setmetrics(ire, rtm->rtm_inits, &rtm->rtm_rmx);
9420Sstevel@tonic-gate 			break;
9430Sstevel@tonic-gate 		}
9440Sstevel@tonic-gate 		break;
9450Sstevel@tonic-gate 	default:
9460Sstevel@tonic-gate 		error = EOPNOTSUPP;
9470Sstevel@tonic-gate 		break;
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate done:
9500Sstevel@tonic-gate 	if (ire != NULL)
9510Sstevel@tonic-gate 		ire_refrele(ire);
95211042SErik.Nordmark@Sun.COM 	if (ifire != NULL)
95311042SErik.Nordmark@Sun.COM 		ire_refrele(ifire);
95411042SErik.Nordmark@Sun.COM 	if (ill != NULL)
95511042SErik.Nordmark@Sun.COM 		ill_refrele(ill);
9560Sstevel@tonic-gate 
9571676Sjpk 	if (gcgrp_xtraref)
9581676Sjpk 		GCGRP_REFRELE(gcgrp);
9591676Sjpk 
9600Sstevel@tonic-gate 	if (rtm != NULL) {
9610Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
9620Sstevel@tonic-gate 		if (error != 0) {
9630Sstevel@tonic-gate 			rtm->rtm_errno = error;
9640Sstevel@tonic-gate 			/* Send error ACK */
9650Sstevel@tonic-gate 			ip1dbg(("ip_rts_request: error %d\n", error));
9660Sstevel@tonic-gate 		} else {
9670Sstevel@tonic-gate 			rtm->rtm_flags |= RTF_DONE;
9680Sstevel@tonic-gate 			/* OK ACK already set up by caller except this */
9690Sstevel@tonic-gate 			ip2dbg(("ip_rts_request: OK ACK\n"));
9700Sstevel@tonic-gate 		}
9718485SPeter.Memishian@Sun.COM 		rts_queue_input(mp, connp, af, RTSQ_ALL, ipst);
9720Sstevel@tonic-gate 	}
97311042SErik.Nordmark@Sun.COM 	return (error);
97411042SErik.Nordmark@Sun.COM }
9758348SEric.Yu@Sun.COM 
97611042SErik.Nordmark@Sun.COM /*
97711042SErik.Nordmark@Sun.COM  * Helper function that can do recursive lookups including when
97811042SErik.Nordmark@Sun.COM  * MATCH_IRE_GW and/or MATCH_IRE_MASK is set.
97911042SErik.Nordmark@Sun.COM  */
98011042SErik.Nordmark@Sun.COM static ire_t *
ire_lookup_v4(ipaddr_t dst_addr,ipaddr_t net_mask,ipaddr_t gw_addr,const ill_t * ill,zoneid_t zoneid,const ts_label_t * tsl,int match_flags,ip_stack_t * ipst,ire_t ** pifire,ipaddr_t * v4setsrcp,tsol_ire_gw_secattr_t ** gwattrp)98111042SErik.Nordmark@Sun.COM ire_lookup_v4(ipaddr_t dst_addr, ipaddr_t net_mask, ipaddr_t gw_addr,
98211042SErik.Nordmark@Sun.COM     const ill_t *ill, zoneid_t zoneid, const ts_label_t *tsl,
98311042SErik.Nordmark@Sun.COM     int match_flags, ip_stack_t *ipst, ire_t **pifire, ipaddr_t *v4setsrcp,
98411042SErik.Nordmark@Sun.COM     tsol_ire_gw_secattr_t **gwattrp)
98511042SErik.Nordmark@Sun.COM {
98611042SErik.Nordmark@Sun.COM 	ire_t		*ire;
98711042SErik.Nordmark@Sun.COM 	ire_t		*ifire = NULL;
98811042SErik.Nordmark@Sun.COM 	uint_t		ire_type;
98911042SErik.Nordmark@Sun.COM 
99011042SErik.Nordmark@Sun.COM 	*pifire = NULL;
99111042SErik.Nordmark@Sun.COM 	*v4setsrcp = INADDR_ANY;
99211042SErik.Nordmark@Sun.COM 	*gwattrp = NULL;
99311042SErik.Nordmark@Sun.COM 
99411042SErik.Nordmark@Sun.COM 	/* Skip IRE_IF_CLONE */
99511042SErik.Nordmark@Sun.COM 	match_flags |= MATCH_IRE_TYPE;
99611042SErik.Nordmark@Sun.COM 	ire_type = (IRE_ONLINK|IRE_OFFLINK) & ~IRE_IF_CLONE;
99711042SErik.Nordmark@Sun.COM 
99811042SErik.Nordmark@Sun.COM 	/*
99911042SErik.Nordmark@Sun.COM 	 * ire_route_recursive can't match gateway or mask thus if they are
100011042SErik.Nordmark@Sun.COM 	 * set we have to do two steps of lookups
100111042SErik.Nordmark@Sun.COM 	 */
100211042SErik.Nordmark@Sun.COM 	if (match_flags & (MATCH_IRE_GW|MATCH_IRE_MASK)) {
100311042SErik.Nordmark@Sun.COM 		ire = ire_ftable_lookup_v4(dst_addr, net_mask, gw_addr,
100411042SErik.Nordmark@Sun.COM 		    ire_type, ill, zoneid, tsl, match_flags, 0, ipst, NULL);
100511042SErik.Nordmark@Sun.COM 
100611042SErik.Nordmark@Sun.COM 		if (ire == NULL ||(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)))
100711042SErik.Nordmark@Sun.COM 			return (ire);
100811042SErik.Nordmark@Sun.COM 
100911042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_ONLINK)
101011042SErik.Nordmark@Sun.COM 			return (ire);
101111042SErik.Nordmark@Sun.COM 
101211042SErik.Nordmark@Sun.COM 		if (ire->ire_flags & RTF_SETSRC) {
101311042SErik.Nordmark@Sun.COM 			ASSERT(ire->ire_setsrc_addr != INADDR_ANY);
101411042SErik.Nordmark@Sun.COM 			*v4setsrcp = ire->ire_setsrc_addr;
101511042SErik.Nordmark@Sun.COM 			v4setsrcp = NULL;
101611042SErik.Nordmark@Sun.COM 		}
101711042SErik.Nordmark@Sun.COM 
101811042SErik.Nordmark@Sun.COM 		/* The first ire_gw_secattr is passed back */
101911042SErik.Nordmark@Sun.COM 		if (ire->ire_gw_secattr != NULL) {
102011042SErik.Nordmark@Sun.COM 			*gwattrp = ire->ire_gw_secattr;
102111042SErik.Nordmark@Sun.COM 			gwattrp = NULL;
102211042SErik.Nordmark@Sun.COM 		}
102311042SErik.Nordmark@Sun.COM 
102411042SErik.Nordmark@Sun.COM 		/* Look for an interface ire recursively based on the gateway */
102511042SErik.Nordmark@Sun.COM 		dst_addr = ire->ire_gateway_addr;
102611042SErik.Nordmark@Sun.COM 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_MASK);
102712038SSowmini.Varadhan@Sun.COM 		/*
102812038SSowmini.Varadhan@Sun.COM 		 * Don't allow anything unusual past the first iteration.
102912038SSowmini.Varadhan@Sun.COM 		 * After the first lookup, we should no longer look for
103012038SSowmini.Varadhan@Sun.COM 		 * (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST) or RTF_INDIRECT
103112038SSowmini.Varadhan@Sun.COM 		 * routes.
103212038SSowmini.Varadhan@Sun.COM 		 *
103312038SSowmini.Varadhan@Sun.COM 		 * In addition, after we have found a direct IRE_OFFLINK,
103412038SSowmini.Varadhan@Sun.COM 		 * we should only look for interface or clone routes.
103512038SSowmini.Varadhan@Sun.COM 		 */
103612038SSowmini.Varadhan@Sun.COM 		match_flags |= MATCH_IRE_DIRECT; /* no more RTF_INDIRECTs */
103712038SSowmini.Varadhan@Sun.COM 
103812038SSowmini.Varadhan@Sun.COM 		if ((ire->ire_type & IRE_OFFLINK) &&
103912038SSowmini.Varadhan@Sun.COM 		    !(ire->ire_flags & RTF_INDIRECT)) {
104012038SSowmini.Varadhan@Sun.COM 			ire_type = IRE_IF_ALL;
104112038SSowmini.Varadhan@Sun.COM 		} else {
104212038SSowmini.Varadhan@Sun.COM 			/*
104312038SSowmini.Varadhan@Sun.COM 			 * no more local, loopback, broadcast routes
104412038SSowmini.Varadhan@Sun.COM 			 */
104512038SSowmini.Varadhan@Sun.COM 			if (!(match_flags & MATCH_IRE_TYPE))
104612038SSowmini.Varadhan@Sun.COM 				ire_type = (IRE_OFFLINK|IRE_ONLINK);
104712038SSowmini.Varadhan@Sun.COM 			ire_type &= ~(IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST);
104812038SSowmini.Varadhan@Sun.COM 		}
104912038SSowmini.Varadhan@Sun.COM 		match_flags |= MATCH_IRE_TYPE;
105012038SSowmini.Varadhan@Sun.COM 
105111042SErik.Nordmark@Sun.COM 		ifire = ire_route_recursive_v4(dst_addr, ire_type, ill, zoneid,
105211457SErik.Nordmark@Sun.COM 		    tsl, match_flags, IRR_INCOMPLETE, 0, ipst, v4setsrcp,
105311457SErik.Nordmark@Sun.COM 		    gwattrp, NULL);
105411042SErik.Nordmark@Sun.COM 	} else {
105511042SErik.Nordmark@Sun.COM 		ire = ire_route_recursive_v4(dst_addr, ire_type, ill, zoneid,
105611457SErik.Nordmark@Sun.COM 		    tsl, match_flags, IRR_INCOMPLETE, 0, ipst, v4setsrcp,
105711457SErik.Nordmark@Sun.COM 		    gwattrp, NULL);
105811042SErik.Nordmark@Sun.COM 	}
105911042SErik.Nordmark@Sun.COM 	*pifire = ifire;
106011042SErik.Nordmark@Sun.COM 	return (ire);
106111042SErik.Nordmark@Sun.COM }
106211042SErik.Nordmark@Sun.COM 
106311042SErik.Nordmark@Sun.COM static ire_t *
ire_lookup_v6(const in6_addr_t * dst_addr_v6,const in6_addr_t * net_mask_v6,const in6_addr_t * gw_addr_v6,const ill_t * ill,zoneid_t zoneid,const ts_label_t * tsl,int match_flags,ip_stack_t * ipst,ire_t ** pifire,in6_addr_t * v6setsrcp,tsol_ire_gw_secattr_t ** gwattrp)106411042SErik.Nordmark@Sun.COM ire_lookup_v6(const in6_addr_t *dst_addr_v6,
106511042SErik.Nordmark@Sun.COM     const in6_addr_t *net_mask_v6, const in6_addr_t *gw_addr_v6,
106611042SErik.Nordmark@Sun.COM     const ill_t *ill, zoneid_t zoneid, const ts_label_t *tsl, int match_flags,
106711042SErik.Nordmark@Sun.COM     ip_stack_t *ipst, ire_t **pifire,
106811042SErik.Nordmark@Sun.COM     in6_addr_t *v6setsrcp, tsol_ire_gw_secattr_t **gwattrp)
106911042SErik.Nordmark@Sun.COM {
107011042SErik.Nordmark@Sun.COM 	ire_t		*ire;
107111042SErik.Nordmark@Sun.COM 	ire_t		*ifire = NULL;
107211042SErik.Nordmark@Sun.COM 	uint_t		ire_type;
107311042SErik.Nordmark@Sun.COM 
107411042SErik.Nordmark@Sun.COM 	*pifire = NULL;
107511042SErik.Nordmark@Sun.COM 	*v6setsrcp = ipv6_all_zeros;
107611042SErik.Nordmark@Sun.COM 	*gwattrp = NULL;
107711042SErik.Nordmark@Sun.COM 
107811042SErik.Nordmark@Sun.COM 	/* Skip IRE_IF_CLONE */
107911042SErik.Nordmark@Sun.COM 	match_flags |= MATCH_IRE_TYPE;
108011042SErik.Nordmark@Sun.COM 	ire_type = (IRE_ONLINK|IRE_OFFLINK) & ~IRE_IF_CLONE;
108111042SErik.Nordmark@Sun.COM 
108211042SErik.Nordmark@Sun.COM 	/*
108311042SErik.Nordmark@Sun.COM 	 * ire_route_recursive can't match gateway or mask thus if they are
108411042SErik.Nordmark@Sun.COM 	 * set we have to do two steps of lookups
108511042SErik.Nordmark@Sun.COM 	 */
108611042SErik.Nordmark@Sun.COM 	if (match_flags & (MATCH_IRE_GW|MATCH_IRE_MASK)) {
108711042SErik.Nordmark@Sun.COM 		in6_addr_t dst;
108811042SErik.Nordmark@Sun.COM 
108911042SErik.Nordmark@Sun.COM 		ire = ire_ftable_lookup_v6(dst_addr_v6, net_mask_v6,
109011042SErik.Nordmark@Sun.COM 		    gw_addr_v6, ire_type, ill, zoneid, tsl, match_flags, 0,
109111042SErik.Nordmark@Sun.COM 		    ipst, NULL);
109211042SErik.Nordmark@Sun.COM 
109311042SErik.Nordmark@Sun.COM 		if (ire == NULL ||(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)))
109411042SErik.Nordmark@Sun.COM 			return (ire);
109511042SErik.Nordmark@Sun.COM 
109611042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_ONLINK)
109711042SErik.Nordmark@Sun.COM 			return (ire);
109811042SErik.Nordmark@Sun.COM 
109911042SErik.Nordmark@Sun.COM 		if (ire->ire_flags & RTF_SETSRC) {
110011042SErik.Nordmark@Sun.COM 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(
110111042SErik.Nordmark@Sun.COM 			    &ire->ire_setsrc_addr_v6));
110211042SErik.Nordmark@Sun.COM 			*v6setsrcp = ire->ire_setsrc_addr_v6;
110311042SErik.Nordmark@Sun.COM 			v6setsrcp = NULL;
110411042SErik.Nordmark@Sun.COM 		}
110511042SErik.Nordmark@Sun.COM 
110611042SErik.Nordmark@Sun.COM 		/* The first ire_gw_secattr is passed back */
110711042SErik.Nordmark@Sun.COM 		if (ire->ire_gw_secattr != NULL) {
110811042SErik.Nordmark@Sun.COM 			*gwattrp = ire->ire_gw_secattr;
110911042SErik.Nordmark@Sun.COM 			gwattrp = NULL;
111011042SErik.Nordmark@Sun.COM 		}
111111042SErik.Nordmark@Sun.COM 
111211042SErik.Nordmark@Sun.COM 		mutex_enter(&ire->ire_lock);
111311042SErik.Nordmark@Sun.COM 		dst = ire->ire_gateway_addr_v6;
111411042SErik.Nordmark@Sun.COM 		mutex_exit(&ire->ire_lock);
111511042SErik.Nordmark@Sun.COM 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_MASK);
111612038SSowmini.Varadhan@Sun.COM 		/*
111712038SSowmini.Varadhan@Sun.COM 		 * Don't allow anything unusual past the first iteration.
111812038SSowmini.Varadhan@Sun.COM 		 * After the first lookup, we should no longer look for
111912038SSowmini.Varadhan@Sun.COM 		 * (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST) or RTF_INDIRECT
112012038SSowmini.Varadhan@Sun.COM 		 * routes.
112112038SSowmini.Varadhan@Sun.COM 		 *
112212038SSowmini.Varadhan@Sun.COM 		 * In addition, after we have found a direct IRE_OFFLINK,
112312038SSowmini.Varadhan@Sun.COM 		 * we should only look for interface or clone routes.
112412038SSowmini.Varadhan@Sun.COM 		 */
112512038SSowmini.Varadhan@Sun.COM 		match_flags |= MATCH_IRE_DIRECT; /* no more RTF_INDIRECTs */
112612038SSowmini.Varadhan@Sun.COM 
112712038SSowmini.Varadhan@Sun.COM 		if ((ire->ire_type & IRE_OFFLINK) &&
112812038SSowmini.Varadhan@Sun.COM 		    !(ire->ire_flags & RTF_INDIRECT)) {
112912038SSowmini.Varadhan@Sun.COM 			ire_type = IRE_IF_ALL;
113012038SSowmini.Varadhan@Sun.COM 		} else {
113112038SSowmini.Varadhan@Sun.COM 			/*
113212038SSowmini.Varadhan@Sun.COM 			 * no more local, loopback routes
113312038SSowmini.Varadhan@Sun.COM 			 */
113412038SSowmini.Varadhan@Sun.COM 			if (!(match_flags & MATCH_IRE_TYPE))
113512038SSowmini.Varadhan@Sun.COM 				ire_type = (IRE_OFFLINK|IRE_ONLINK);
113612038SSowmini.Varadhan@Sun.COM 			ire_type &= ~(IRE_LOCAL|IRE_LOOPBACK);
113712038SSowmini.Varadhan@Sun.COM 		}
113812038SSowmini.Varadhan@Sun.COM 		match_flags |= MATCH_IRE_TYPE;
113912038SSowmini.Varadhan@Sun.COM 
114011042SErik.Nordmark@Sun.COM 		ifire = ire_route_recursive_v6(&dst, ire_type, ill, zoneid, tsl,
114111457SErik.Nordmark@Sun.COM 		    match_flags, IRR_INCOMPLETE, 0, ipst, v6setsrcp, gwattrp,
114211457SErik.Nordmark@Sun.COM 		    NULL);
114311042SErik.Nordmark@Sun.COM 	} else {
114411042SErik.Nordmark@Sun.COM 		ire = ire_route_recursive_v6(dst_addr_v6, ire_type, ill, zoneid,
114511457SErik.Nordmark@Sun.COM 		    tsl, match_flags, IRR_INCOMPLETE, 0, ipst, v6setsrcp,
114611457SErik.Nordmark@Sun.COM 		    gwattrp, NULL);
114711042SErik.Nordmark@Sun.COM 	}
114811042SErik.Nordmark@Sun.COM 	*pifire = ifire;
114911042SErik.Nordmark@Sun.COM 	return (ire);
115011042SErik.Nordmark@Sun.COM }
115111042SErik.Nordmark@Sun.COM 
115211042SErik.Nordmark@Sun.COM 
115311042SErik.Nordmark@Sun.COM /*
115411042SErik.Nordmark@Sun.COM  * Handle IP_IOC_RTS_REQUEST ioctls
115511042SErik.Nordmark@Sun.COM  */
115611042SErik.Nordmark@Sun.COM int
ip_rts_request(queue_t * q,mblk_t * mp,cred_t * ioc_cr)115711042SErik.Nordmark@Sun.COM ip_rts_request(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
115811042SErik.Nordmark@Sun.COM {
115911042SErik.Nordmark@Sun.COM 	conn_t	*connp = Q_TO_CONN(q);
116011042SErik.Nordmark@Sun.COM 	IOCP	iocp = (IOCP)mp->b_rptr;
116111042SErik.Nordmark@Sun.COM 	mblk_t	*mp1, *ioc_mp = mp;
116211042SErik.Nordmark@Sun.COM 	int	error = 0;
116311042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst;
116411042SErik.Nordmark@Sun.COM 
116511042SErik.Nordmark@Sun.COM 	ipst = connp->conn_netstack->netstack_ip;
116611042SErik.Nordmark@Sun.COM 
116711042SErik.Nordmark@Sun.COM 	ASSERT(mp->b_cont != NULL);
116811042SErik.Nordmark@Sun.COM 	/* ioc_mp holds mp */
116911042SErik.Nordmark@Sun.COM 	mp = mp->b_cont;
117011042SErik.Nordmark@Sun.COM 
117111042SErik.Nordmark@Sun.COM 	/*
117211042SErik.Nordmark@Sun.COM 	 * The Routing Socket data starts on
117311042SErik.Nordmark@Sun.COM 	 * next block. If there is no next block
117411042SErik.Nordmark@Sun.COM 	 * this is an indication from routing module
117511042SErik.Nordmark@Sun.COM 	 * that it is a routing socket stream queue.
117611042SErik.Nordmark@Sun.COM 	 * We need to support that for compatibility with SDP since
117711042SErik.Nordmark@Sun.COM 	 * it has a contract private interface to use IP_IOC_RTS_REQUEST.
117811042SErik.Nordmark@Sun.COM 	 * Note: SDP no longer uses IP_IOC_RTS_REQUEST - we can remove this.
117911042SErik.Nordmark@Sun.COM 	 */
118011042SErik.Nordmark@Sun.COM 	if (mp->b_cont == NULL) {
118111042SErik.Nordmark@Sun.COM 		/*
118211042SErik.Nordmark@Sun.COM 		 * This is a message from SDP
118311042SErik.Nordmark@Sun.COM 		 * indicating that this is a Routing Socket
118411042SErik.Nordmark@Sun.COM 		 * Stream. Insert this conn_t in routing
118511042SErik.Nordmark@Sun.COM 		 * socket client list.
118611042SErik.Nordmark@Sun.COM 		 */
118711042SErik.Nordmark@Sun.COM 		connp->conn_useloopback = 1;
118811042SErik.Nordmark@Sun.COM 		ipcl_hash_insert_wildcard(ipst->ips_rts_clients, connp);
118911042SErik.Nordmark@Sun.COM 		goto done;
119011042SErik.Nordmark@Sun.COM 	}
119111042SErik.Nordmark@Sun.COM 	mp1 = dupmsg(mp->b_cont);
119211042SErik.Nordmark@Sun.COM 	if (mp1 == NULL) {
119311042SErik.Nordmark@Sun.COM 		error  = ENOBUFS;
119411042SErik.Nordmark@Sun.COM 		goto done;
119511042SErik.Nordmark@Sun.COM 	}
119611042SErik.Nordmark@Sun.COM 	mp = mp1;
119711042SErik.Nordmark@Sun.COM 
119811042SErik.Nordmark@Sun.COM 	error = ip_rts_request_common(mp, connp, ioc_cr);
119911042SErik.Nordmark@Sun.COM done:
12000Sstevel@tonic-gate 	iocp->ioc_error = error;
12010Sstevel@tonic-gate 	ioc_mp->b_datap->db_type = M_IOCACK;
12020Sstevel@tonic-gate 	if (iocp->ioc_error != 0)
12030Sstevel@tonic-gate 		iocp->ioc_count = 0;
120411042SErik.Nordmark@Sun.COM 	/* Note that we pass a NULL ira to rts_input */
120511042SErik.Nordmark@Sun.COM 	(connp->conn_recv)(connp, ioc_mp, NULL, NULL);
12068348SEric.Yu@Sun.COM 
12070Sstevel@tonic-gate 	/* conn was refheld in ip_wput_ioctl. */
1208*12670SRamesh.K@Sun.COM 	CONN_DEC_IOCTLREF(connp);
12090Sstevel@tonic-gate 	CONN_OPER_PENDING_DONE(connp);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	return (error);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate /*
12150Sstevel@tonic-gate  * Build a reply to the RTM_GET request contained in the given message block
12160Sstevel@tonic-gate  * using the retrieved IRE of the destination address, the parent IRE (if it
12170Sstevel@tonic-gate  * exists) and the address family.
12180Sstevel@tonic-gate  *
12190Sstevel@tonic-gate  * Returns a pointer to a message block containing the reply if successful,
12200Sstevel@tonic-gate  * otherwise NULL is returned.
12210Sstevel@tonic-gate  */
12221676Sjpk static mblk_t *
rts_rtmget(mblk_t * mp,ire_t * ire,ire_t * ifire,const in6_addr_t * setsrc,tsol_ire_gw_secattr_t * attrp,sa_family_t af)122311042SErik.Nordmark@Sun.COM rts_rtmget(mblk_t *mp, ire_t *ire, ire_t *ifire, const in6_addr_t *setsrc,
122411042SErik.Nordmark@Sun.COM     tsol_ire_gw_secattr_t *attrp, sa_family_t af)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
12270Sstevel@tonic-gate 	rt_msghdr_t	*new_rtm;
12280Sstevel@tonic-gate 	mblk_t		*new_mp;
12290Sstevel@tonic-gate 	int		rtm_addrs;
12300Sstevel@tonic-gate 	int		rtm_flags;
12311676Sjpk 	tsol_gc_t	*gc = NULL;
12321676Sjpk 	tsol_gcgrp_t	*gcgrp = NULL;
123311042SErik.Nordmark@Sun.COM 	ill_t		*ill;
123411042SErik.Nordmark@Sun.COM 	ipif_t		*ipif = NULL;
123511042SErik.Nordmark@Sun.COM 	ipaddr_t	brdaddr;	/* IFF_POINTOPOINT destination */
123611042SErik.Nordmark@Sun.COM 	ipaddr_t	ifaddr;
123711042SErik.Nordmark@Sun.COM 	in6_addr_t	brdaddr6;	/* IFF_POINTOPOINT destination */
123811042SErik.Nordmark@Sun.COM 	in6_addr_t	ifaddr6;
123911042SErik.Nordmark@Sun.COM 	ipaddr_t	v4setsrc;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
12420Sstevel@tonic-gate 
124311042SErik.Nordmark@Sun.COM 	/*
124411042SErik.Nordmark@Sun.COM 	 * Find the ill used to send packets. This will be NULL in case
124511042SErik.Nordmark@Sun.COM 	 * of a reject or blackhole.
124611042SErik.Nordmark@Sun.COM 	 */
124711042SErik.Nordmark@Sun.COM 	if (ifire != NULL)
124811042SErik.Nordmark@Sun.COM 		ill = ire_nexthop_ill(ifire);
124911042SErik.Nordmark@Sun.COM 	else
125011042SErik.Nordmark@Sun.COM 		ill = ire_nexthop_ill(ire);
12511676Sjpk 
12521676Sjpk 	if (attrp != NULL) {
12531676Sjpk 		mutex_enter(&attrp->igsa_lock);
12541676Sjpk 		if ((gc = attrp->igsa_gc) != NULL) {
12551676Sjpk 			gcgrp = gc->gc_grp;
12561676Sjpk 			ASSERT(gcgrp != NULL);
12571676Sjpk 			rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
12581676Sjpk 		}
12591676Sjpk 		mutex_exit(&attrp->igsa_lock);
12601676Sjpk 	}
12611676Sjpk 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * Always return RTA_DST, RTA_GATEWAY and RTA_NETMASK.
12640Sstevel@tonic-gate 	 *
12650Sstevel@tonic-gate 	 * The 4.4BSD-Lite2 code (net/rtsock.c) returns both
12660Sstevel@tonic-gate 	 * RTA_IFP and RTA_IFA if either is defined, and also
12670Sstevel@tonic-gate 	 * returns RTA_BRD if the appropriate interface is
12680Sstevel@tonic-gate 	 * point-to-point.
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 	rtm_addrs = (RTA_DST | RTA_GATEWAY | RTA_NETMASK);
127111042SErik.Nordmark@Sun.COM 	if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) && ill != NULL) {
12720Sstevel@tonic-gate 		rtm_addrs |= (RTA_IFP | RTA_IFA);
127311042SErik.Nordmark@Sun.COM 		/*
127411042SErik.Nordmark@Sun.COM 		 * We associate an IRE with an ILL, hence we don't exactly
127511042SErik.Nordmark@Sun.COM 		 * know what might make sense for RTA_IFA and RTA_BRD. We
127611042SErik.Nordmark@Sun.COM 		 * pick the first ipif on the ill.
127711042SErik.Nordmark@Sun.COM 		 */
127811042SErik.Nordmark@Sun.COM 		ipif = ipif_get_next_ipif(NULL, ill);
127911042SErik.Nordmark@Sun.COM 		if (ipif != NULL) {
128011042SErik.Nordmark@Sun.COM 			if (ipif->ipif_isv6)
128111042SErik.Nordmark@Sun.COM 				ifaddr6 = ipif->ipif_v6lcl_addr;
128211042SErik.Nordmark@Sun.COM 			else
128311042SErik.Nordmark@Sun.COM 				ifaddr = ipif->ipif_lcl_addr;
128411042SErik.Nordmark@Sun.COM 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
128511042SErik.Nordmark@Sun.COM 				rtm_addrs |= RTA_BRD;
128611042SErik.Nordmark@Sun.COM 				if (ipif->ipif_isv6)
128711042SErik.Nordmark@Sun.COM 					brdaddr6 = ipif->ipif_v6pp_dst_addr;
128811042SErik.Nordmark@Sun.COM 				else
128911042SErik.Nordmark@Sun.COM 					brdaddr = ipif->ipif_pp_dst_addr;
129011042SErik.Nordmark@Sun.COM 			}
129111042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
129211042SErik.Nordmark@Sun.COM 		}
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate 
129511042SErik.Nordmark@Sun.COM 	new_mp = rts_alloc_msg(RTM_GET, rtm_addrs, af, gc != NULL ? 1 : 0);
12961676Sjpk 	if (new_mp == NULL) {
12971676Sjpk 		if (gcgrp != NULL)
12981676Sjpk 			rw_exit(&gcgrp->gcgrp_rwlock);
129911042SErik.Nordmark@Sun.COM 		if (ill != NULL)
130011042SErik.Nordmark@Sun.COM 			ill_refrele(ill);
13010Sstevel@tonic-gate 		return (NULL);
13021676Sjpk 	}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/*
13050Sstevel@tonic-gate 	 * We set the destination address, gateway address,
13060Sstevel@tonic-gate 	 * netmask and flags in the RTM_GET response depending
13070Sstevel@tonic-gate 	 * on whether we found a parent IRE or not.
13080Sstevel@tonic-gate 	 * In particular, if we did find a parent IRE during the
13090Sstevel@tonic-gate 	 * recursive search, use that IRE's gateway address.
13100Sstevel@tonic-gate 	 * Otherwise, we use the IRE's source address for the
13110Sstevel@tonic-gate 	 * gateway address.
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	ASSERT(af == AF_INET || af == AF_INET6);
13140Sstevel@tonic-gate 	switch (af) {
13150Sstevel@tonic-gate 	case AF_INET:
131611042SErik.Nordmark@Sun.COM 		IN6_V4MAPPED_TO_IPADDR(setsrc, v4setsrc);
131711042SErik.Nordmark@Sun.COM 		if (v4setsrc != INADDR_ANY)
131811042SErik.Nordmark@Sun.COM 			rtm_addrs |= RTA_SRC;
13190Sstevel@tonic-gate 
132011042SErik.Nordmark@Sun.COM 		rtm_flags = ire->ire_flags;
132111042SErik.Nordmark@Sun.COM 		rts_fill_msg(RTM_GET, rtm_addrs, ire->ire_addr,
132211042SErik.Nordmark@Sun.COM 		    ire->ire_mask, ire->ire_gateway_addr, v4setsrc,
132311042SErik.Nordmark@Sun.COM 		    brdaddr, 0, ifaddr, ill, new_mp, gc);
13240Sstevel@tonic-gate 		break;
13250Sstevel@tonic-gate 	case AF_INET6:
132611042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(setsrc))
132711042SErik.Nordmark@Sun.COM 			rtm_addrs |= RTA_SRC;
13280Sstevel@tonic-gate 
132911042SErik.Nordmark@Sun.COM 		rtm_flags = ire->ire_flags;
133011042SErik.Nordmark@Sun.COM 		rts_fill_msg_v6(RTM_GET, rtm_addrs, &ire->ire_addr_v6,
133111042SErik.Nordmark@Sun.COM 		    &ire->ire_mask_v6, &ire->ire_gateway_addr_v6,
133211042SErik.Nordmark@Sun.COM 		    setsrc, &brdaddr6, &ipv6_all_zeros,
133311042SErik.Nordmark@Sun.COM 		    &ifaddr6, ill, new_mp, gc);
13340Sstevel@tonic-gate 		break;
13350Sstevel@tonic-gate 	}
13361676Sjpk 
13371676Sjpk 	if (gcgrp != NULL)
13381676Sjpk 		rw_exit(&gcgrp->gcgrp_rwlock);
13391676Sjpk 
13400Sstevel@tonic-gate 	new_rtm = (rt_msghdr_t *)new_mp->b_rptr;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	/*
13430Sstevel@tonic-gate 	 * The rtm_msglen, rtm_version and rtm_type fields in
13440Sstevel@tonic-gate 	 * RTM_GET response are filled in by rts_fill_msg.
13450Sstevel@tonic-gate 	 *
13460Sstevel@tonic-gate 	 * rtm_addrs and rtm_flags are filled in based on what
13470Sstevel@tonic-gate 	 * was requested and the state of the IREs looked up
13480Sstevel@tonic-gate 	 * above.
13490Sstevel@tonic-gate 	 *
13500Sstevel@tonic-gate 	 * rtm_inits and rtm_rmx are filled in with metrics
13510Sstevel@tonic-gate 	 * based on whether a parent IRE was found or not.
13520Sstevel@tonic-gate 	 *
13530Sstevel@tonic-gate 	 * TODO: rtm_index and rtm_use should probably be
13540Sstevel@tonic-gate 	 * filled in with something resonable here and not just
13550Sstevel@tonic-gate 	 * copied from the request.
13560Sstevel@tonic-gate 	 */
13570Sstevel@tonic-gate 	new_rtm->rtm_index = rtm->rtm_index;
13580Sstevel@tonic-gate 	new_rtm->rtm_pid = rtm->rtm_pid;
13590Sstevel@tonic-gate 	new_rtm->rtm_seq = rtm->rtm_seq;
13600Sstevel@tonic-gate 	new_rtm->rtm_use = rtm->rtm_use;
13610Sstevel@tonic-gate 	new_rtm->rtm_addrs = rtm_addrs;
13620Sstevel@tonic-gate 	new_rtm->rtm_flags = rtm_flags;
136311457SErik.Nordmark@Sun.COM 	new_rtm->rtm_inits = rts_getmetrics(ire, ill, &new_rtm->rtm_rmx);
136411042SErik.Nordmark@Sun.COM 	if (ill != NULL)
136511042SErik.Nordmark@Sun.COM 		ill_refrele(ill);
13660Sstevel@tonic-gate 	return (new_mp);
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate  * Fill the given if_data_t with interface statistics.
13710Sstevel@tonic-gate  */
13720Sstevel@tonic-gate static void
rts_getifdata(if_data_t * if_data,const ipif_t * ipif)13731676Sjpk rts_getifdata(if_data_t *if_data, const ipif_t *ipif)
13740Sstevel@tonic-gate {
137511042SErik.Nordmark@Sun.COM 	if_data->ifi_type = ipif->ipif_ill->ill_type;
137611042SErik.Nordmark@Sun.COM 						/* ethernet, tokenring, etc */
13770Sstevel@tonic-gate 	if_data->ifi_addrlen = 0;		/* media address length */
13780Sstevel@tonic-gate 	if_data->ifi_hdrlen = 0;		/* media header length */
137911042SErik.Nordmark@Sun.COM 	if_data->ifi_mtu = ipif->ipif_ill->ill_mtu;	/* mtu */
138012016SGirish.Moodalbail@Sun.COM 						/* metric (external only) */
138112016SGirish.Moodalbail@Sun.COM 	if_data->ifi_metric = ipif->ipif_ill->ill_metric;
13820Sstevel@tonic-gate 	if_data->ifi_baudrate = 0;		/* linespeed */
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	if_data->ifi_ipackets = 0;		/* packets received on if */
13850Sstevel@tonic-gate 	if_data->ifi_ierrors = 0;		/* input errors on interface */
13860Sstevel@tonic-gate 	if_data->ifi_opackets = 0;		/* packets sent on interface */
13870Sstevel@tonic-gate 	if_data->ifi_oerrors = 0;		/* output errors on if */
13880Sstevel@tonic-gate 	if_data->ifi_collisions = 0;		/* collisions on csma if */
13890Sstevel@tonic-gate 	if_data->ifi_ibytes = 0;		/* total number received */
13900Sstevel@tonic-gate 	if_data->ifi_obytes = 0;		/* total number sent */
13910Sstevel@tonic-gate 	if_data->ifi_imcasts = 0;		/* multicast packets received */
13920Sstevel@tonic-gate 	if_data->ifi_omcasts = 0;		/* multicast packets sent */
13930Sstevel@tonic-gate 	if_data->ifi_iqdrops = 0;		/* dropped on input */
13940Sstevel@tonic-gate 	if_data->ifi_noproto = 0;		/* destined for unsupported */
13950Sstevel@tonic-gate 						/* protocol. */
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate  * Set the metrics on a forwarding table route.
14000Sstevel@tonic-gate  */
14010Sstevel@tonic-gate static void
rts_setmetrics(ire_t * ire,uint_t which,rt_metrics_t * metrics)14020Sstevel@tonic-gate rts_setmetrics(ire_t *ire, uint_t which, rt_metrics_t *metrics)
14030Sstevel@tonic-gate {
14040Sstevel@tonic-gate 	clock_t		rtt;
14050Sstevel@tonic-gate 	clock_t		rtt_sd;
140611042SErik.Nordmark@Sun.COM 	ill_t		*ill;
14070Sstevel@tonic-gate 	ifrt_t		*ifrt;
14080Sstevel@tonic-gate 	mblk_t		*mp;
14090Sstevel@tonic-gate 	in6_addr_t	gw_addr_v6;
14100Sstevel@tonic-gate 
141111042SErik.Nordmark@Sun.COM 	/* Need to add back some metrics to the IRE? */
14120Sstevel@tonic-gate 	/*
141311042SErik.Nordmark@Sun.COM 	 * Bypass obtaining the lock and searching ill_saved_ire_mp in the
14140Sstevel@tonic-gate 	 * common case of no metrics.
14150Sstevel@tonic-gate 	 */
14160Sstevel@tonic-gate 	if (which == 0)
14170Sstevel@tonic-gate 		return;
141811042SErik.Nordmark@Sun.COM 	ire->ire_metrics.iulp_set = B_TRUE;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
14220Sstevel@tonic-gate 	 * <net/route.h> says: rmx_rtt and rmx_rttvar are stored as
14230Sstevel@tonic-gate 	 * microseconds.
14240Sstevel@tonic-gate 	 */
14250Sstevel@tonic-gate 	if (which & RTV_RTT)
14260Sstevel@tonic-gate 		rtt = metrics->rmx_rtt / 1000;
14270Sstevel@tonic-gate 	if (which & RTV_RTTVAR)
14280Sstevel@tonic-gate 		rtt_sd = metrics->rmx_rttvar / 1000;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * Update the metrics in the IRE itself.
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
14340Sstevel@tonic-gate 	if (which & RTV_MTU)
143511042SErik.Nordmark@Sun.COM 		ire->ire_metrics.iulp_mtu = metrics->rmx_mtu;
14360Sstevel@tonic-gate 	if (which & RTV_RTT)
143711042SErik.Nordmark@Sun.COM 		ire->ire_metrics.iulp_rtt = rtt;
14380Sstevel@tonic-gate 	if (which & RTV_SSTHRESH)
143911042SErik.Nordmark@Sun.COM 		ire->ire_metrics.iulp_ssthresh = metrics->rmx_ssthresh;
14400Sstevel@tonic-gate 	if (which & RTV_RTTVAR)
144111042SErik.Nordmark@Sun.COM 		ire->ire_metrics.iulp_rtt_sd = rtt_sd;
14420Sstevel@tonic-gate 	if (which & RTV_SPIPE)
144311042SErik.Nordmark@Sun.COM 		ire->ire_metrics.iulp_spipe = metrics->rmx_sendpipe;
14440Sstevel@tonic-gate 	if (which & RTV_RPIPE)
144511042SErik.Nordmark@Sun.COM 		ire->ire_metrics.iulp_rpipe = metrics->rmx_recvpipe;
14460Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	/*
144911042SErik.Nordmark@Sun.COM 	 * Search through the ifrt_t chain hanging off the ILL in order to
14500Sstevel@tonic-gate 	 * reflect the metric change there.
14510Sstevel@tonic-gate 	 */
145211042SErik.Nordmark@Sun.COM 	ill = ire->ire_ill;
145311042SErik.Nordmark@Sun.COM 	if (ill == NULL)
14540Sstevel@tonic-gate 		return;
145511042SErik.Nordmark@Sun.COM 	ASSERT((ill->ill_isv6 && ire->ire_ipversion == IPV6_VERSION) ||
145611042SErik.Nordmark@Sun.COM 	    ((!ill->ill_isv6 && ire->ire_ipversion == IPV4_VERSION)));
145711042SErik.Nordmark@Sun.COM 	if (ill->ill_isv6) {
14580Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
14590Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
14600Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
14610Sstevel@tonic-gate 	}
146211042SErik.Nordmark@Sun.COM 	mutex_enter(&ill->ill_saved_ire_lock);
146311042SErik.Nordmark@Sun.COM 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14640Sstevel@tonic-gate 		/*
146511042SErik.Nordmark@Sun.COM 		 * On a given ill, the tuple of address, gateway, mask,
146611042SErik.Nordmark@Sun.COM 		 * ire_type and zoneid unique for each saved IRE.
14670Sstevel@tonic-gate 		 */
14680Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
146911042SErik.Nordmark@Sun.COM 		if (ill->ill_isv6) {
14700Sstevel@tonic-gate 			if (!IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
14710Sstevel@tonic-gate 			    &ire->ire_addr_v6) ||
14720Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
14730Sstevel@tonic-gate 			    &gw_addr_v6) ||
14740Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
14750Sstevel@tonic-gate 			    &ire->ire_mask_v6))
14760Sstevel@tonic-gate 				continue;
14770Sstevel@tonic-gate 		} else {
14780Sstevel@tonic-gate 			if (ifrt->ifrt_addr != ire->ire_addr ||
14790Sstevel@tonic-gate 			    ifrt->ifrt_gateway_addr != ire->ire_gateway_addr ||
14800Sstevel@tonic-gate 			    ifrt->ifrt_mask != ire->ire_mask)
14810Sstevel@tonic-gate 				continue;
14820Sstevel@tonic-gate 		}
148311042SErik.Nordmark@Sun.COM 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
148411042SErik.Nordmark@Sun.COM 		    ifrt->ifrt_type != ire->ire_type)
148511042SErik.Nordmark@Sun.COM 			continue;
148611042SErik.Nordmark@Sun.COM 
14870Sstevel@tonic-gate 		if (which & RTV_MTU)
148811042SErik.Nordmark@Sun.COM 			ifrt->ifrt_metrics.iulp_mtu = metrics->rmx_mtu;
14890Sstevel@tonic-gate 		if (which & RTV_RTT)
149011042SErik.Nordmark@Sun.COM 			ifrt->ifrt_metrics.iulp_rtt = rtt;
14910Sstevel@tonic-gate 		if (which & RTV_SSTHRESH) {
149211042SErik.Nordmark@Sun.COM 			ifrt->ifrt_metrics.iulp_ssthresh =
14930Sstevel@tonic-gate 			    metrics->rmx_ssthresh;
14940Sstevel@tonic-gate 		}
14950Sstevel@tonic-gate 		if (which & RTV_RTTVAR)
149611042SErik.Nordmark@Sun.COM 			ifrt->ifrt_metrics.iulp_rtt_sd = metrics->rmx_rttvar;
14970Sstevel@tonic-gate 		if (which & RTV_SPIPE)
149811042SErik.Nordmark@Sun.COM 			ifrt->ifrt_metrics.iulp_spipe = metrics->rmx_sendpipe;
14990Sstevel@tonic-gate 		if (which & RTV_RPIPE)
150011042SErik.Nordmark@Sun.COM 			ifrt->ifrt_metrics.iulp_rpipe = metrics->rmx_recvpipe;
15010Sstevel@tonic-gate 		break;
15020Sstevel@tonic-gate 	}
150311042SErik.Nordmark@Sun.COM 	mutex_exit(&ill->ill_saved_ire_lock);
150411042SErik.Nordmark@Sun.COM 
150511042SErik.Nordmark@Sun.COM 	/*
150611042SErik.Nordmark@Sun.COM 	 * Update any IRE_IF_CLONE hanging created from this IRE_IF so they
150711042SErik.Nordmark@Sun.COM 	 * get any new iulp_mtu.
150811042SErik.Nordmark@Sun.COM 	 * We do that by deleting them; ire_create_if_clone will pick
150911042SErik.Nordmark@Sun.COM 	 * up the new metrics.
151011042SErik.Nordmark@Sun.COM 	 */
151111042SErik.Nordmark@Sun.COM 	if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != 0)
151211042SErik.Nordmark@Sun.COM 		ire_dep_delete_if_clone(ire);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate /*
15160Sstevel@tonic-gate  * Get the metrics from a forwarding table route.
15170Sstevel@tonic-gate  */
15180Sstevel@tonic-gate static int
rts_getmetrics(ire_t * ire,ill_t * ill,rt_metrics_t * metrics)151911457SErik.Nordmark@Sun.COM rts_getmetrics(ire_t *ire, ill_t *ill, rt_metrics_t *metrics)
15200Sstevel@tonic-gate {
15210Sstevel@tonic-gate 	int	metrics_set = 0;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	bzero(metrics, sizeof (rt_metrics_t));
152411042SErik.Nordmark@Sun.COM 
15250Sstevel@tonic-gate 	/*
15260Sstevel@tonic-gate 	 * iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
15270Sstevel@tonic-gate 	 * <net/route.h> says: rmx_rtt and rmx_rttvar are stored as
15280Sstevel@tonic-gate 	 * microseconds.
15290Sstevel@tonic-gate 	 */
153011042SErik.Nordmark@Sun.COM 	metrics->rmx_rtt = ire->ire_metrics.iulp_rtt * 1000;
15310Sstevel@tonic-gate 	metrics_set |= RTV_RTT;
153211457SErik.Nordmark@Sun.COM 	if (ire->ire_metrics.iulp_mtu != 0) {
153311457SErik.Nordmark@Sun.COM 		metrics->rmx_mtu = ire->ire_metrics.iulp_mtu;
153411457SErik.Nordmark@Sun.COM 		metrics_set |= RTV_MTU;
153511457SErik.Nordmark@Sun.COM 	} else if (ill != NULL) {
153611457SErik.Nordmark@Sun.COM 		metrics->rmx_mtu = ill->ill_mtu;
153711457SErik.Nordmark@Sun.COM 		metrics_set |= RTV_MTU;
153811457SErik.Nordmark@Sun.COM 	}
153911042SErik.Nordmark@Sun.COM 	metrics->rmx_ssthresh = ire->ire_metrics.iulp_ssthresh;
15400Sstevel@tonic-gate 	metrics_set |= RTV_SSTHRESH;
154111042SErik.Nordmark@Sun.COM 	metrics->rmx_rttvar = ire->ire_metrics.iulp_rtt_sd * 1000;
15420Sstevel@tonic-gate 	metrics_set |= RTV_RTTVAR;
154311042SErik.Nordmark@Sun.COM 	metrics->rmx_sendpipe = ire->ire_metrics.iulp_spipe;
15440Sstevel@tonic-gate 	metrics_set |= RTV_SPIPE;
154511042SErik.Nordmark@Sun.COM 	metrics->rmx_recvpipe = ire->ire_metrics.iulp_rpipe;
15460Sstevel@tonic-gate 	metrics_set |= RTV_RPIPE;
15470Sstevel@tonic-gate 	return (metrics_set);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate /*
155111042SErik.Nordmark@Sun.COM  * Given two sets of metrics (src and dst), use the dst values if they are
155211042SErik.Nordmark@Sun.COM  * set. If a dst value is not set but the src value is set, then we use
155311042SErik.Nordmark@Sun.COM  * the src value.
155411042SErik.Nordmark@Sun.COM  * dst is updated with the new values.
155511042SErik.Nordmark@Sun.COM  * This is used to merge information from a dce_t and ire_metrics, where the
155611042SErik.Nordmark@Sun.COM  * dce values takes precedence.
155711042SErik.Nordmark@Sun.COM  */
155811042SErik.Nordmark@Sun.COM void
rts_merge_metrics(iulp_t * dst,const iulp_t * src)155911042SErik.Nordmark@Sun.COM rts_merge_metrics(iulp_t *dst, const iulp_t *src)
156011042SErik.Nordmark@Sun.COM {
156111042SErik.Nordmark@Sun.COM 	if (!src->iulp_set)
156211042SErik.Nordmark@Sun.COM 		return;
156311042SErik.Nordmark@Sun.COM 
156411042SErik.Nordmark@Sun.COM 	if (dst->iulp_ssthresh == 0)
156511042SErik.Nordmark@Sun.COM 		dst->iulp_ssthresh = src->iulp_ssthresh;
156611042SErik.Nordmark@Sun.COM 	if (dst->iulp_rtt == 0)
156711042SErik.Nordmark@Sun.COM 		dst->iulp_rtt = src->iulp_rtt;
156811042SErik.Nordmark@Sun.COM 	if (dst->iulp_rtt_sd == 0)
156911042SErik.Nordmark@Sun.COM 		dst->iulp_rtt_sd = src->iulp_rtt_sd;
157011042SErik.Nordmark@Sun.COM 	if (dst->iulp_spipe == 0)
157111042SErik.Nordmark@Sun.COM 		dst->iulp_spipe = src->iulp_spipe;
157211042SErik.Nordmark@Sun.COM 	if (dst->iulp_rpipe == 0)
157311042SErik.Nordmark@Sun.COM 		dst->iulp_rpipe = src->iulp_rpipe;
157411042SErik.Nordmark@Sun.COM 	if (dst->iulp_rtomax == 0)
157511042SErik.Nordmark@Sun.COM 		dst->iulp_rtomax = src->iulp_rtomax;
157611042SErik.Nordmark@Sun.COM 	if (dst->iulp_sack == 0)
157711042SErik.Nordmark@Sun.COM 		dst->iulp_sack = src->iulp_sack;
157811042SErik.Nordmark@Sun.COM 	if (dst->iulp_tstamp_ok == 0)
157911042SErik.Nordmark@Sun.COM 		dst->iulp_tstamp_ok = src->iulp_tstamp_ok;
158011042SErik.Nordmark@Sun.COM 	if (dst->iulp_wscale_ok == 0)
158111042SErik.Nordmark@Sun.COM 		dst->iulp_wscale_ok = src->iulp_wscale_ok;
158211042SErik.Nordmark@Sun.COM 	if (dst->iulp_ecn_ok == 0)
158311042SErik.Nordmark@Sun.COM 		dst->iulp_ecn_ok = src->iulp_ecn_ok;
158411042SErik.Nordmark@Sun.COM 	if (dst->iulp_pmtud_ok == 0)
158511042SErik.Nordmark@Sun.COM 		dst->iulp_pmtud_ok = src->iulp_pmtud_ok;
158611042SErik.Nordmark@Sun.COM 	if (dst->iulp_mtu == 0)
158711042SErik.Nordmark@Sun.COM 		dst->iulp_mtu = src->iulp_mtu;
158811042SErik.Nordmark@Sun.COM }
158911042SErik.Nordmark@Sun.COM 
159011042SErik.Nordmark@Sun.COM 
159111042SErik.Nordmark@Sun.COM /*
15920Sstevel@tonic-gate  * Takes a pointer to a routing message and extracts necessary info by looking
15930Sstevel@tonic-gate  * at the rtm->rtm_addrs bits and store the requested sockaddrs in the pointers
15940Sstevel@tonic-gate  * passed (all of which must be valid).
15950Sstevel@tonic-gate  *
15960Sstevel@tonic-gate  * The bitmask of sockaddrs actually found in the message is returned, or zero
15970Sstevel@tonic-gate  * is returned in the case of an error.
15980Sstevel@tonic-gate  */
15990Sstevel@tonic-gate static int
rts_getaddrs(rt_msghdr_t * rtm,in6_addr_t * dst_addrp,in6_addr_t * gw_addrp,in6_addr_t * net_maskp,in6_addr_t * authorp,in6_addr_t * if_addrp,in6_addr_t * in_src_addrp,ushort_t * indexp,sa_family_t * afp,tsol_rtsecattr_t * rtsecattr,int * error)16000Sstevel@tonic-gate rts_getaddrs(rt_msghdr_t *rtm, in6_addr_t *dst_addrp, in6_addr_t *gw_addrp,
16010Sstevel@tonic-gate     in6_addr_t *net_maskp, in6_addr_t *authorp, in6_addr_t *if_addrp,
16024823Sseb     in6_addr_t *in_src_addrp, ushort_t *indexp, sa_family_t *afp,
16034823Sseb     tsol_rtsecattr_t *rtsecattr, int *error)
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate 	struct sockaddr *sa;
16060Sstevel@tonic-gate 	int	i;
16070Sstevel@tonic-gate 	int	addr_bits;
16080Sstevel@tonic-gate 	int	length;
16090Sstevel@tonic-gate 	int	found_addrs = 0;
16100Sstevel@tonic-gate 	caddr_t	cp;
16110Sstevel@tonic-gate 	size_t	size;
16120Sstevel@tonic-gate 	struct sockaddr_dl *sdl;
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	*dst_addrp = ipv6_all_zeros;
16150Sstevel@tonic-gate 	*gw_addrp = ipv6_all_zeros;
16160Sstevel@tonic-gate 	*net_maskp = ipv6_all_zeros;
16170Sstevel@tonic-gate 	*authorp = ipv6_all_zeros;
16180Sstevel@tonic-gate 	*if_addrp = ipv6_all_zeros;
16190Sstevel@tonic-gate 	*in_src_addrp = ipv6_all_zeros;
16200Sstevel@tonic-gate 	*indexp = 0;
16210Sstevel@tonic-gate 	*afp = AF_UNSPEC;
16221676Sjpk 	rtsecattr->rtsa_cnt = 0;
16231676Sjpk 	*error = 0;
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * At present we handle only RTA_DST, RTA_GATEWAY, RTA_NETMASK, RTA_IFP,
16270Sstevel@tonic-gate 	 * RTA_IFA and RTA_AUTHOR.  The rest will be added as we need them.
16280Sstevel@tonic-gate 	 */
16290Sstevel@tonic-gate 	cp = (caddr_t)&rtm[1];
16300Sstevel@tonic-gate 	length = rtm->rtm_msglen;
16310Sstevel@tonic-gate 	for (i = 0; (i < RTA_NUMBITS) && ((cp - (caddr_t)rtm) < length); i++) {
16320Sstevel@tonic-gate 		/*
16330Sstevel@tonic-gate 		 * The address family we are working with starts out as
16340Sstevel@tonic-gate 		 * AF_UNSPEC, but is set to the one specified with the
16350Sstevel@tonic-gate 		 * destination address.
16360Sstevel@tonic-gate 		 *
16370Sstevel@tonic-gate 		 * If the "working" address family that has been set to
16380Sstevel@tonic-gate 		 * something other than AF_UNSPEC, then the address family of
16390Sstevel@tonic-gate 		 * subsequent sockaddrs must either be AF_UNSPEC (for
16400Sstevel@tonic-gate 		 * compatibility with older programs) or must be the same as our
16410Sstevel@tonic-gate 		 * "working" one.
16420Sstevel@tonic-gate 		 *
16430Sstevel@tonic-gate 		 * This code assumes that RTA_DST (1) comes first in the loop.
16440Sstevel@tonic-gate 		 */
16450Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
16460Sstevel@tonic-gate 		addr_bits = (rtm->rtm_addrs & (1 << i));
16470Sstevel@tonic-gate 		if (addr_bits == 0)
16480Sstevel@tonic-gate 			continue;
16490Sstevel@tonic-gate 		switch (addr_bits) {
16500Sstevel@tonic-gate 		case RTA_DST:
16510Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, dst_addrp);
16520Sstevel@tonic-gate 			*afp = sa->sa_family;
16530Sstevel@tonic-gate 			break;
16540Sstevel@tonic-gate 		case RTA_GATEWAY:
16550Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
16560Sstevel@tonic-gate 				return (0);
16570Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, gw_addrp);
16580Sstevel@tonic-gate 			break;
16590Sstevel@tonic-gate 		case RTA_NETMASK:
16600Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
16610Sstevel@tonic-gate 				return (0);
16620Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, net_maskp);
16630Sstevel@tonic-gate 			break;
16640Sstevel@tonic-gate 		case RTA_IFP:
16650Sstevel@tonic-gate 			if (sa->sa_family != AF_LINK &&
16660Sstevel@tonic-gate 			    sa->sa_family != AF_UNSPEC)
16670Sstevel@tonic-gate 				return (0);
16680Sstevel@tonic-gate 			sdl = (struct sockaddr_dl *)cp;
16690Sstevel@tonic-gate 			*indexp = sdl->sdl_index;
16700Sstevel@tonic-gate 			size = sizeof (struct sockaddr_dl);
16710Sstevel@tonic-gate 			break;
16720Sstevel@tonic-gate 		case RTA_SRC:
16730Sstevel@tonic-gate 			/* Source address of the incoming packet */
16740Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, in_src_addrp);
16750Sstevel@tonic-gate 			*afp = sa->sa_family;
16760Sstevel@tonic-gate 			break;
16770Sstevel@tonic-gate 		case RTA_IFA:
16780Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
16790Sstevel@tonic-gate 				return (0);
16800Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, if_addrp);
16810Sstevel@tonic-gate 			break;
16820Sstevel@tonic-gate 		case RTA_AUTHOR:
16830Sstevel@tonic-gate 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
16840Sstevel@tonic-gate 				return (0);
16850Sstevel@tonic-gate 			size = rts_copyfromsockaddr(sa, authorp);
16860Sstevel@tonic-gate 			break;
16870Sstevel@tonic-gate 		default:
16880Sstevel@tonic-gate 			return (0);
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 		if (size == 0)
16910Sstevel@tonic-gate 			return (0);
16920Sstevel@tonic-gate 		cp += size;
16930Sstevel@tonic-gate 		found_addrs |= addr_bits;
16940Sstevel@tonic-gate 	}
16951676Sjpk 
16961676Sjpk 	/*
16971676Sjpk 	 * Parse the routing message and look for any security-
16981676Sjpk 	 * related attributes for the route.  For each valid
16991676Sjpk 	 * attribute, allocate/obtain the corresponding kernel
17001676Sjpk 	 * route security attributes.
17011676Sjpk 	 */
170210138Shn.vijay@Sun.COM 	if (((cp - (caddr_t)rtm) < length) && is_system_labeled()) {
170310138Shn.vijay@Sun.COM 		*error = tsol_rtsa_init(rtm, rtsecattr, cp);
170410138Shn.vijay@Sun.COM 		ASSERT(rtsecattr->rtsa_cnt <= TSOL_RTSA_REQUEST_MAX);
170510138Shn.vijay@Sun.COM 	}
17061676Sjpk 
17070Sstevel@tonic-gate 	return (found_addrs);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate /*
17110Sstevel@tonic-gate  * Fills the message with the given info.
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate static void
rts_fill_msg(int type,int rtm_addrs,ipaddr_t dst,ipaddr_t mask,ipaddr_t gateway,ipaddr_t src_addr,ipaddr_t brd_addr,ipaddr_t author,ipaddr_t ifaddr,const ill_t * ill,mblk_t * mp,const tsol_gc_t * gc)17140Sstevel@tonic-gate rts_fill_msg(int type, int rtm_addrs, ipaddr_t dst, ipaddr_t mask,
17150Sstevel@tonic-gate     ipaddr_t gateway, ipaddr_t src_addr, ipaddr_t brd_addr, ipaddr_t author,
171611042SErik.Nordmark@Sun.COM     ipaddr_t ifaddr, const ill_t *ill, mblk_t *mp,
171711042SErik.Nordmark@Sun.COM     const tsol_gc_t *gc)
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
17200Sstevel@tonic-gate 	sin_t		*sin;
17210Sstevel@tonic-gate 	size_t		data_size, header_size;
17220Sstevel@tonic-gate 	uchar_t		*cp;
17230Sstevel@tonic-gate 	int		i;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	ASSERT(mp != NULL);
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * First find the type of the message
17280Sstevel@tonic-gate 	 * and its length.
17290Sstevel@tonic-gate 	 */
17300Sstevel@tonic-gate 	header_size = rts_header_msg_size(type);
17310Sstevel@tonic-gate 	/*
17320Sstevel@tonic-gate 	 * Now find the size of the data
17330Sstevel@tonic-gate 	 * that follows the message header.
17340Sstevel@tonic-gate 	 */
173511042SErik.Nordmark@Sun.COM 	data_size = rts_data_msg_size(rtm_addrs, AF_INET, gc != NULL ? 1 : 0);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
17380Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[header_size];
17390Sstevel@tonic-gate 	cp = mp->b_wptr;
17400Sstevel@tonic-gate 	bzero(cp, data_size);
17410Sstevel@tonic-gate 	for (i = 0; i < RTA_NUMBITS; i++) {
17420Sstevel@tonic-gate 		sin = (sin_t *)cp;
17430Sstevel@tonic-gate 		switch (rtm_addrs & (1 << i)) {
17440Sstevel@tonic-gate 		case RTA_DST:
17450Sstevel@tonic-gate 			sin->sin_addr.s_addr = dst;
17460Sstevel@tonic-gate 			sin->sin_family = AF_INET;
17470Sstevel@tonic-gate 			cp += sizeof (sin_t);
17480Sstevel@tonic-gate 			break;
17490Sstevel@tonic-gate 		case RTA_GATEWAY:
17500Sstevel@tonic-gate 			sin->sin_addr.s_addr = gateway;
17510Sstevel@tonic-gate 			sin->sin_family = AF_INET;
17520Sstevel@tonic-gate 			cp += sizeof (sin_t);
17530Sstevel@tonic-gate 			break;
17540Sstevel@tonic-gate 		case RTA_NETMASK:
17550Sstevel@tonic-gate 			sin->sin_addr.s_addr = mask;
17560Sstevel@tonic-gate 			sin->sin_family = AF_INET;
17570Sstevel@tonic-gate 			cp += sizeof (sin_t);
17580Sstevel@tonic-gate 			break;
17590Sstevel@tonic-gate 		case RTA_IFP:
176011042SErik.Nordmark@Sun.COM 			cp += ill_dls_info((struct sockaddr_dl *)cp, ill);
17610Sstevel@tonic-gate 			break;
17620Sstevel@tonic-gate 		case RTA_IFA:
176311042SErik.Nordmark@Sun.COM 			sin->sin_addr.s_addr = ifaddr;
176411042SErik.Nordmark@Sun.COM 			sin->sin_family = AF_INET;
176511042SErik.Nordmark@Sun.COM 			cp += sizeof (sin_t);
176611042SErik.Nordmark@Sun.COM 			break;
17670Sstevel@tonic-gate 		case RTA_SRC:
17680Sstevel@tonic-gate 			sin->sin_addr.s_addr = src_addr;
17690Sstevel@tonic-gate 			sin->sin_family = AF_INET;
17700Sstevel@tonic-gate 			cp += sizeof (sin_t);
17710Sstevel@tonic-gate 			break;
17720Sstevel@tonic-gate 		case RTA_AUTHOR:
17730Sstevel@tonic-gate 			sin->sin_addr.s_addr = author;
17740Sstevel@tonic-gate 			sin->sin_family = AF_INET;
17750Sstevel@tonic-gate 			cp += sizeof (sin_t);
17760Sstevel@tonic-gate 			break;
17770Sstevel@tonic-gate 		case RTA_BRD:
17780Sstevel@tonic-gate 			/*
17790Sstevel@tonic-gate 			 * RTA_BRD is used typically to specify a point-to-point
17800Sstevel@tonic-gate 			 * destination address.
17810Sstevel@tonic-gate 			 */
17820Sstevel@tonic-gate 			sin->sin_addr.s_addr = brd_addr;
17830Sstevel@tonic-gate 			sin->sin_family = AF_INET;
17840Sstevel@tonic-gate 			cp += sizeof (sin_t);
17850Sstevel@tonic-gate 			break;
17860Sstevel@tonic-gate 		}
17870Sstevel@tonic-gate 	}
17881676Sjpk 
17891676Sjpk 	if (gc != NULL) {
17901676Sjpk 		rtm_ext_t *rtm_ext;
17911676Sjpk 		struct rtsa_s *rp_dst;
17921676Sjpk 		tsol_rtsecattr_t *rsap;
17931676Sjpk 
17941676Sjpk 		ASSERT(gc->gc_grp != NULL);
17951676Sjpk 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
17961676Sjpk 
17971676Sjpk 		rtm_ext = (rtm_ext_t *)cp;
17981676Sjpk 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
179911042SErik.Nordmark@Sun.COM 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(1);
18001676Sjpk 
18011676Sjpk 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
180211042SErik.Nordmark@Sun.COM 		rsap->rtsa_cnt = 1;
18031676Sjpk 		rp_dst = rsap->rtsa_attr;
18041676Sjpk 
180511042SErik.Nordmark@Sun.COM 		ASSERT(gc->gc_db != NULL);
180611042SErik.Nordmark@Sun.COM 		bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
18071676Sjpk 		cp = (uchar_t *)rp_dst;
18081676Sjpk 	}
18091676Sjpk 
18100Sstevel@tonic-gate 	mp->b_wptr = cp;
18110Sstevel@tonic-gate 	mp->b_cont = NULL;
18120Sstevel@tonic-gate 	/*
18130Sstevel@tonic-gate 	 * set the fields that are common to
18140Sstevel@tonic-gate 	 * to different messages.
18150Sstevel@tonic-gate 	 */
18160Sstevel@tonic-gate 	rtm->rtm_msglen = (short)(header_size + data_size);
18170Sstevel@tonic-gate 	rtm->rtm_version = RTM_VERSION;
18180Sstevel@tonic-gate 	rtm->rtm_type = (uchar_t)type;
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate /*
18220Sstevel@tonic-gate  * Allocates and initializes a routing socket message.
182311042SErik.Nordmark@Sun.COM  * Note that sacnt is either zero or one.
18240Sstevel@tonic-gate  */
18250Sstevel@tonic-gate mblk_t *
rts_alloc_msg(int type,int rtm_addrs,sa_family_t af,uint_t sacnt)18261676Sjpk rts_alloc_msg(int type, int rtm_addrs, sa_family_t af, uint_t sacnt)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	size_t	length;
18290Sstevel@tonic-gate 	mblk_t	*mp;
18300Sstevel@tonic-gate 
18311676Sjpk 	length = RTS_MSG_SIZE(type, rtm_addrs, af, sacnt);
18320Sstevel@tonic-gate 	mp = allocb(length, BPRI_MED);
18330Sstevel@tonic-gate 	if (mp == NULL)
18340Sstevel@tonic-gate 		return (mp);
18350Sstevel@tonic-gate 	bzero(mp->b_rptr, length);
18360Sstevel@tonic-gate 	return (mp);
18370Sstevel@tonic-gate }
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate /*
18400Sstevel@tonic-gate  * Returns the size of the routing
18410Sstevel@tonic-gate  * socket message header size.
18420Sstevel@tonic-gate  */
18430Sstevel@tonic-gate size_t
rts_header_msg_size(int type)18440Sstevel@tonic-gate rts_header_msg_size(int type)
18450Sstevel@tonic-gate {
18460Sstevel@tonic-gate 	switch (type) {
18470Sstevel@tonic-gate 	case RTM_DELADDR:
18480Sstevel@tonic-gate 	case RTM_NEWADDR:
184911076SCathy.Zhou@Sun.COM 	case RTM_CHGADDR:
185011076SCathy.Zhou@Sun.COM 	case RTM_FREEADDR:
18510Sstevel@tonic-gate 		return (sizeof (ifa_msghdr_t));
18520Sstevel@tonic-gate 	case RTM_IFINFO:
18530Sstevel@tonic-gate 		return (sizeof (if_msghdr_t));
18540Sstevel@tonic-gate 	default:
18550Sstevel@tonic-gate 		return (sizeof (rt_msghdr_t));
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate /*
18600Sstevel@tonic-gate  * Returns the size of the message needed with the given rtm_addrs and family.
18610Sstevel@tonic-gate  *
18620Sstevel@tonic-gate  * It is assumed that all of the sockaddrs (with the exception of RTA_IFP) are
18630Sstevel@tonic-gate  * of the same family (currently either AF_INET or AF_INET6).
18640Sstevel@tonic-gate  */
18650Sstevel@tonic-gate size_t
rts_data_msg_size(int rtm_addrs,sa_family_t af,uint_t sacnt)18661676Sjpk rts_data_msg_size(int rtm_addrs, sa_family_t af, uint_t sacnt)
18670Sstevel@tonic-gate {
18680Sstevel@tonic-gate 	int	i;
18690Sstevel@tonic-gate 	size_t	length = 0;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	for (i = 0; i < RTA_NUMBITS; i++) {
18720Sstevel@tonic-gate 		switch (rtm_addrs & (1 << i)) {
18730Sstevel@tonic-gate 		case RTA_IFP:
18740Sstevel@tonic-gate 			length += sizeof (struct sockaddr_dl);
18750Sstevel@tonic-gate 			break;
18760Sstevel@tonic-gate 		case RTA_DST:
18770Sstevel@tonic-gate 		case RTA_GATEWAY:
18780Sstevel@tonic-gate 		case RTA_NETMASK:
18790Sstevel@tonic-gate 		case RTA_SRC:
18800Sstevel@tonic-gate 		case RTA_IFA:
18810Sstevel@tonic-gate 		case RTA_AUTHOR:
18820Sstevel@tonic-gate 		case RTA_BRD:
18830Sstevel@tonic-gate 			ASSERT(af == AF_INET || af == AF_INET6);
18840Sstevel@tonic-gate 			switch (af) {
18850Sstevel@tonic-gate 			case AF_INET:
18860Sstevel@tonic-gate 				length += sizeof (sin_t);
18870Sstevel@tonic-gate 				break;
18880Sstevel@tonic-gate 			case AF_INET6:
18890Sstevel@tonic-gate 				length += sizeof (sin6_t);
18900Sstevel@tonic-gate 				break;
18910Sstevel@tonic-gate 			}
18920Sstevel@tonic-gate 			break;
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate 	}
18951676Sjpk 	if (sacnt > 0)
18961676Sjpk 		length += sizeof (rtm_ext_t) + TSOL_RTSECATTR_SIZE(sacnt);
18971676Sjpk 
18980Sstevel@tonic-gate 	return (length);
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate /*
19020Sstevel@tonic-gate  * This routine is called to generate a message to the routing
19030Sstevel@tonic-gate  * socket indicating that a redirect has occured, a routing lookup
19040Sstevel@tonic-gate  * has failed, or that a protocol has detected timeouts to a particular
19050Sstevel@tonic-gate  * destination. This routine is called for message types RTM_LOSING,
19060Sstevel@tonic-gate  * RTM_REDIRECT, and RTM_MISS.
19070Sstevel@tonic-gate  */
19080Sstevel@tonic-gate void
ip_rts_change(int type,ipaddr_t dst_addr,ipaddr_t gw_addr,ipaddr_t net_mask,ipaddr_t source,ipaddr_t author,int flags,int error,int rtm_addrs,ip_stack_t * ipst)19090Sstevel@tonic-gate ip_rts_change(int type, ipaddr_t dst_addr, ipaddr_t gw_addr, ipaddr_t net_mask,
19103448Sdh155122     ipaddr_t source, ipaddr_t author, int flags, int error, int rtm_addrs,
19113448Sdh155122     ip_stack_t *ipst)
19120Sstevel@tonic-gate {
19130Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
19140Sstevel@tonic-gate 	mblk_t		*mp;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	if (rtm_addrs == 0)
19170Sstevel@tonic-gate 		return;
19181676Sjpk 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET, 0);
19190Sstevel@tonic-gate 	if (mp == NULL)
19200Sstevel@tonic-gate 		return;
19210Sstevel@tonic-gate 	rts_fill_msg(type, rtm_addrs, dst_addr, net_mask, gw_addr, source, 0,
192211042SErik.Nordmark@Sun.COM 	    author, 0, NULL, mp, NULL);
19230Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
19240Sstevel@tonic-gate 	rtm->rtm_flags = flags;
19250Sstevel@tonic-gate 	rtm->rtm_errno = error;
19260Sstevel@tonic-gate 	rtm->rtm_flags |= RTF_DONE;
19270Sstevel@tonic-gate 	rtm->rtm_addrs = rtm_addrs;
19288485SPeter.Memishian@Sun.COM 	rts_queue_input(mp, NULL, AF_INET, RTSQ_ALL, ipst);
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate /*
19320Sstevel@tonic-gate  * This routine is called to generate a message to the routing
19330Sstevel@tonic-gate  * socket indicating that the status of a network interface has changed.
19340Sstevel@tonic-gate  * Message type generated RTM_IFINFO.
19350Sstevel@tonic-gate  */
19360Sstevel@tonic-gate void
ip_rts_ifmsg(const ipif_t * ipif,uint_t flags)19378485SPeter.Memishian@Sun.COM ip_rts_ifmsg(const ipif_t *ipif, uint_t flags)
19388485SPeter.Memishian@Sun.COM {
19398485SPeter.Memishian@Sun.COM 	ip_rts_xifmsg(ipif, 0, 0, flags);
19408485SPeter.Memishian@Sun.COM }
19418485SPeter.Memishian@Sun.COM 
19428485SPeter.Memishian@Sun.COM void
ip_rts_xifmsg(const ipif_t * ipif,uint64_t set,uint64_t clear,uint_t flags)19438485SPeter.Memishian@Sun.COM ip_rts_xifmsg(const ipif_t *ipif, uint64_t set, uint64_t clear, uint_t flags)
19440Sstevel@tonic-gate {
19450Sstevel@tonic-gate 	if_msghdr_t	*ifm;
19460Sstevel@tonic-gate 	mblk_t		*mp;
19470Sstevel@tonic-gate 	sa_family_t	af;
19483448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	/*
195111042SErik.Nordmark@Sun.COM 	 * This message should be generated only
195211042SErik.Nordmark@Sun.COM 	 * when the physical device is changing
195311042SErik.Nordmark@Sun.COM 	 * state.
19540Sstevel@tonic-gate 	 */
19550Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
19560Sstevel@tonic-gate 		return;
19570Sstevel@tonic-gate 	if (ipif->ipif_isv6) {
19580Sstevel@tonic-gate 		af = AF_INET6;
19591676Sjpk 		mp = rts_alloc_msg(RTM_IFINFO, RTA_IFP, af, 0);
19600Sstevel@tonic-gate 		if (mp == NULL)
19610Sstevel@tonic-gate 			return;
19620Sstevel@tonic-gate 		rts_fill_msg_v6(RTM_IFINFO, RTA_IFP, &ipv6_all_zeros,
19630Sstevel@tonic-gate 		    &ipv6_all_zeros, &ipv6_all_zeros, &ipv6_all_zeros,
196411042SErik.Nordmark@Sun.COM 		    &ipv6_all_zeros, &ipv6_all_zeros, &ipv6_all_zeros,
196511042SErik.Nordmark@Sun.COM 		    ipif->ipif_ill, mp, NULL);
19660Sstevel@tonic-gate 	} else {
19670Sstevel@tonic-gate 		af = AF_INET;
19681676Sjpk 		mp = rts_alloc_msg(RTM_IFINFO, RTA_IFP, af, 0);
19690Sstevel@tonic-gate 		if (mp == NULL)
19700Sstevel@tonic-gate 			return;
197111042SErik.Nordmark@Sun.COM 		rts_fill_msg(RTM_IFINFO, RTA_IFP, 0, 0, 0, 0, 0, 0, 0,
197211042SErik.Nordmark@Sun.COM 		    ipif->ipif_ill, mp, NULL);
19730Sstevel@tonic-gate 	}
19740Sstevel@tonic-gate 	ifm = (if_msghdr_t *)mp->b_rptr;
19750Sstevel@tonic-gate 	ifm->ifm_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
19768485SPeter.Memishian@Sun.COM 	ifm->ifm_flags = (ipif->ipif_flags | ipif->ipif_ill->ill_flags |
19778485SPeter.Memishian@Sun.COM 	    ipif->ipif_ill->ill_phyint->phyint_flags | set) & ~clear;
19780Sstevel@tonic-gate 	rts_getifdata(&ifm->ifm_data, ipif);
19790Sstevel@tonic-gate 	ifm->ifm_addrs = RTA_IFP;
19808485SPeter.Memishian@Sun.COM 
19818485SPeter.Memishian@Sun.COM 	if (flags & RTSQ_DEFAULT) {
19828485SPeter.Memishian@Sun.COM 		flags = RTSQ_ALL;
19838485SPeter.Memishian@Sun.COM 		/*
19848485SPeter.Memishian@Sun.COM 		 * If this message is for an underlying interface, prevent
19858485SPeter.Memishian@Sun.COM 		 * "normal" (IPMP-unaware) routing sockets from seeing it.
19868485SPeter.Memishian@Sun.COM 		 */
19878485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ipif->ipif_ill))
19888485SPeter.Memishian@Sun.COM 			flags &= ~RTSQ_NORMAL;
19898485SPeter.Memishian@Sun.COM 	}
19908485SPeter.Memishian@Sun.COM 
19918485SPeter.Memishian@Sun.COM 	rts_queue_input(mp, NULL, af, flags, ipst);
19920Sstevel@tonic-gate }
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate /*
199511076SCathy.Zhou@Sun.COM  * If cmd is RTM_ADD or RTM_DELETE, generate the rt_msghdr_t message;
199611076SCathy.Zhou@Sun.COM  * otherwise (RTM_NEWADDR, RTM_DELADDR, RTM_CHGADDR and RTM_FREEADDR)
199711076SCathy.Zhou@Sun.COM  * generate the ifa_msghdr_t message.
19980Sstevel@tonic-gate  */
199911076SCathy.Zhou@Sun.COM static void
rts_new_rtsmsg(int cmd,int error,const ipif_t * ipif,uint_t flags)200011076SCathy.Zhou@Sun.COM rts_new_rtsmsg(int cmd, int error, const ipif_t *ipif, uint_t flags)
20010Sstevel@tonic-gate {
20020Sstevel@tonic-gate 	int		rtm_addrs;
20030Sstevel@tonic-gate 	mblk_t		*mp;
20040Sstevel@tonic-gate 	ifa_msghdr_t	*ifam;
20050Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
20060Sstevel@tonic-gate 	sa_family_t	af;
20073448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20080Sstevel@tonic-gate 
200911042SErik.Nordmark@Sun.COM 	/*
201011076SCathy.Zhou@Sun.COM 	 * Do not report unspecified address if this is the RTM_CHGADDR or
201111076SCathy.Zhou@Sun.COM 	 * RTM_FREEADDR message.
201211042SErik.Nordmark@Sun.COM 	 */
201311076SCathy.Zhou@Sun.COM 	if (cmd == RTM_CHGADDR || cmd == RTM_FREEADDR) {
201411076SCathy.Zhou@Sun.COM 		if (!ipif->ipif_isv6) {
201511076SCathy.Zhou@Sun.COM 			if (ipif->ipif_lcl_addr == INADDR_ANY)
201611076SCathy.Zhou@Sun.COM 				return;
201711076SCathy.Zhou@Sun.COM 		} else if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
201811076SCathy.Zhou@Sun.COM 			return;
201911076SCathy.Zhou@Sun.COM 		}
202011076SCathy.Zhou@Sun.COM 	}
202111042SErik.Nordmark@Sun.COM 
20220Sstevel@tonic-gate 	if (ipif->ipif_isv6)
20230Sstevel@tonic-gate 		af = AF_INET6;
20240Sstevel@tonic-gate 	else
20250Sstevel@tonic-gate 		af = AF_INET;
20268485SPeter.Memishian@Sun.COM 
202711076SCathy.Zhou@Sun.COM 	if (cmd == RTM_ADD || cmd == RTM_DELETE)
202811076SCathy.Zhou@Sun.COM 		rtm_addrs = (RTA_DST | RTA_NETMASK);
202911076SCathy.Zhou@Sun.COM 	else
203011076SCathy.Zhou@Sun.COM 		rtm_addrs = (RTA_IFA | RTA_NETMASK | RTA_BRD | RTA_IFP);
203111076SCathy.Zhou@Sun.COM 
203211076SCathy.Zhou@Sun.COM 	mp = rts_alloc_msg(cmd, rtm_addrs, af, 0);
203311076SCathy.Zhou@Sun.COM 	if (mp == NULL)
203411076SCathy.Zhou@Sun.COM 		return;
203511076SCathy.Zhou@Sun.COM 
203611076SCathy.Zhou@Sun.COM 	if (cmd != RTM_ADD && cmd != RTM_DELETE) {
203711076SCathy.Zhou@Sun.COM 		switch (af) {
203811076SCathy.Zhou@Sun.COM 		case AF_INET:
203911076SCathy.Zhou@Sun.COM 			rts_fill_msg(cmd, rtm_addrs, 0,
204011076SCathy.Zhou@Sun.COM 			    ipif->ipif_net_mask, 0, ipif->ipif_lcl_addr,
204111076SCathy.Zhou@Sun.COM 			    ipif->ipif_pp_dst_addr, 0,
204211076SCathy.Zhou@Sun.COM 			    ipif->ipif_lcl_addr, ipif->ipif_ill,
204311076SCathy.Zhou@Sun.COM 			    mp, NULL);
204411076SCathy.Zhou@Sun.COM 			break;
204511076SCathy.Zhou@Sun.COM 		case AF_INET6:
204611076SCathy.Zhou@Sun.COM 			rts_fill_msg_v6(cmd, rtm_addrs,
204711076SCathy.Zhou@Sun.COM 			    &ipv6_all_zeros, &ipif->ipif_v6net_mask,
204811076SCathy.Zhou@Sun.COM 			    &ipv6_all_zeros, &ipif->ipif_v6lcl_addr,
204911076SCathy.Zhou@Sun.COM 			    &ipif->ipif_v6pp_dst_addr, &ipv6_all_zeros,
205011076SCathy.Zhou@Sun.COM 			    &ipif->ipif_v6lcl_addr, ipif->ipif_ill,
205111076SCathy.Zhou@Sun.COM 			    mp, NULL);
205211076SCathy.Zhou@Sun.COM 			break;
205311076SCathy.Zhou@Sun.COM 		}
205411076SCathy.Zhou@Sun.COM 		ifam = (ifa_msghdr_t *)mp->b_rptr;
205511076SCathy.Zhou@Sun.COM 		ifam->ifam_index =
205611076SCathy.Zhou@Sun.COM 		    ipif->ipif_ill->ill_phyint->phyint_ifindex;
205712016SGirish.Moodalbail@Sun.COM 		ifam->ifam_metric = ipif->ipif_ill->ill_metric;
205811076SCathy.Zhou@Sun.COM 		ifam->ifam_flags = ((cmd == RTM_NEWADDR) ? RTF_UP : 0);
205911076SCathy.Zhou@Sun.COM 		ifam->ifam_addrs = rtm_addrs;
206011076SCathy.Zhou@Sun.COM 	} else {
206111076SCathy.Zhou@Sun.COM 		switch (af) {
206211076SCathy.Zhou@Sun.COM 		case AF_INET:
206311076SCathy.Zhou@Sun.COM 			rts_fill_msg(cmd, rtm_addrs,
206411076SCathy.Zhou@Sun.COM 			    ipif->ipif_lcl_addr, ipif->ipif_net_mask, 0,
206511076SCathy.Zhou@Sun.COM 			    0, 0, 0, 0, NULL, mp, NULL);
206611076SCathy.Zhou@Sun.COM 			break;
206711076SCathy.Zhou@Sun.COM 		case AF_INET6:
206811076SCathy.Zhou@Sun.COM 			rts_fill_msg_v6(cmd, rtm_addrs,
206911076SCathy.Zhou@Sun.COM 			    &ipif->ipif_v6lcl_addr,
207011076SCathy.Zhou@Sun.COM 			    &ipif->ipif_v6net_mask, &ipv6_all_zeros,
207111076SCathy.Zhou@Sun.COM 			    &ipv6_all_zeros, &ipv6_all_zeros,
207211076SCathy.Zhou@Sun.COM 			    &ipv6_all_zeros, &ipv6_all_zeros,
207311076SCathy.Zhou@Sun.COM 			    NULL, mp, NULL);
207411076SCathy.Zhou@Sun.COM 			break;
207511076SCathy.Zhou@Sun.COM 		}
207611076SCathy.Zhou@Sun.COM 		rtm = (rt_msghdr_t *)mp->b_rptr;
207711076SCathy.Zhou@Sun.COM 		rtm->rtm_index =
207811076SCathy.Zhou@Sun.COM 		    ipif->ipif_ill->ill_phyint->phyint_ifindex;
207911076SCathy.Zhou@Sun.COM 		rtm->rtm_flags = ((cmd == RTM_ADD) ? RTF_UP : 0);
208011076SCathy.Zhou@Sun.COM 		rtm->rtm_errno = error;
208111076SCathy.Zhou@Sun.COM 		if (error == 0)
208211076SCathy.Zhou@Sun.COM 			rtm->rtm_flags |= RTF_DONE;
208311076SCathy.Zhou@Sun.COM 		rtm->rtm_addrs = rtm_addrs;
208411076SCathy.Zhou@Sun.COM 	}
208511076SCathy.Zhou@Sun.COM 	rts_queue_input(mp, NULL, af, flags, ipst);
208611076SCathy.Zhou@Sun.COM }
208711076SCathy.Zhou@Sun.COM 
208811076SCathy.Zhou@Sun.COM /*
208911076SCathy.Zhou@Sun.COM  * This is called to generate messages to the routing socket
209011076SCathy.Zhou@Sun.COM  * indicating a network interface has had addresses associated with it.
209111076SCathy.Zhou@Sun.COM  * The structure of the code is based on the 4.4BSD-Lite2 <net/rtsock.c>.
209211076SCathy.Zhou@Sun.COM  */
209311076SCathy.Zhou@Sun.COM void
ip_rts_newaddrmsg(int cmd,int error,const ipif_t * ipif,uint_t flags)209411076SCathy.Zhou@Sun.COM ip_rts_newaddrmsg(int cmd, int error, const ipif_t *ipif, uint_t flags)
209511076SCathy.Zhou@Sun.COM {
209611076SCathy.Zhou@Sun.COM 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
209711076SCathy.Zhou@Sun.COM 
20988485SPeter.Memishian@Sun.COM 	if (flags & RTSQ_DEFAULT) {
20998485SPeter.Memishian@Sun.COM 		flags = RTSQ_ALL;
21008485SPeter.Memishian@Sun.COM 		/*
21018485SPeter.Memishian@Sun.COM 		 * If this message is for an underlying interface, prevent
21028485SPeter.Memishian@Sun.COM 		 * "normal" (IPMP-unaware) routing sockets from seeing it.
21038485SPeter.Memishian@Sun.COM 		 */
21048485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ipif->ipif_ill))
21058485SPeter.Memishian@Sun.COM 			flags &= ~RTSQ_NORMAL;
21068485SPeter.Memishian@Sun.COM 	}
21078485SPeter.Memishian@Sun.COM 
21080Sstevel@tonic-gate 	/*
210911076SCathy.Zhou@Sun.COM 	 * Let conn_ixa caching know that source address selection
211011076SCathy.Zhou@Sun.COM 	 * changed
211111076SCathy.Zhou@Sun.COM 	 */
211211076SCathy.Zhou@Sun.COM 	if (cmd == RTM_ADD || cmd == RTM_DELETE)
211311076SCathy.Zhou@Sun.COM 		ip_update_source_selection(ipst);
211411076SCathy.Zhou@Sun.COM 
211511076SCathy.Zhou@Sun.COM 	/*
21160Sstevel@tonic-gate 	 * If the request is DELETE, send RTM_DELETE and RTM_DELADDR.
21170Sstevel@tonic-gate 	 * if the request is ADD, send RTM_NEWADDR and RTM_ADD.
211811076SCathy.Zhou@Sun.COM 	 * otherwise simply send the request.
21190Sstevel@tonic-gate 	 */
212011076SCathy.Zhou@Sun.COM 	switch (cmd) {
212111076SCathy.Zhou@Sun.COM 	case RTM_ADD:
212211076SCathy.Zhou@Sun.COM 		rts_new_rtsmsg(RTM_NEWADDR, error, ipif, flags);
212311076SCathy.Zhou@Sun.COM 		rts_new_rtsmsg(RTM_ADD, error, ipif, flags);
212411076SCathy.Zhou@Sun.COM 		break;
212511076SCathy.Zhou@Sun.COM 	case RTM_DELETE:
212611076SCathy.Zhou@Sun.COM 		rts_new_rtsmsg(RTM_DELETE, error, ipif, flags);
212711076SCathy.Zhou@Sun.COM 		rts_new_rtsmsg(RTM_DELADDR, error, ipif, flags);
212811076SCathy.Zhou@Sun.COM 		break;
212911076SCathy.Zhou@Sun.COM 	default:
213011076SCathy.Zhou@Sun.COM 		rts_new_rtsmsg(cmd, error, ipif, flags);
213111076SCathy.Zhou@Sun.COM 		break;
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate /*
21360Sstevel@tonic-gate  * Based on the address family specified in a sockaddr, copy the address field
21370Sstevel@tonic-gate  * into an in6_addr_t.
21380Sstevel@tonic-gate  *
21390Sstevel@tonic-gate  * In the case of AF_UNSPEC, we assume the family is actually AF_INET for
21400Sstevel@tonic-gate  * compatibility with programs that leave the family cleared in the sockaddr.
21410Sstevel@tonic-gate  * Callers of rts_copyfromsockaddr should check the family themselves if they
21420Sstevel@tonic-gate  * wish to verify its value.
21430Sstevel@tonic-gate  *
21440Sstevel@tonic-gate  * In the case of AF_INET6, a check is made to ensure that address is not an
21450Sstevel@tonic-gate  * IPv4-mapped address.
21460Sstevel@tonic-gate  */
21470Sstevel@tonic-gate size_t
rts_copyfromsockaddr(struct sockaddr * sa,in6_addr_t * addrp)21480Sstevel@tonic-gate rts_copyfromsockaddr(struct sockaddr *sa, in6_addr_t *addrp)
21490Sstevel@tonic-gate {
21500Sstevel@tonic-gate 	switch (sa->sa_family) {
21510Sstevel@tonic-gate 	case AF_INET:
21520Sstevel@tonic-gate 	case AF_UNSPEC:
21530Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(((sin_t *)sa)->sin_addr.s_addr, addrp);
21540Sstevel@tonic-gate 		return (sizeof (sin_t));
21550Sstevel@tonic-gate 	case AF_INET6:
21560Sstevel@tonic-gate 		*addrp = ((sin6_t *)sa)->sin6_addr;
21570Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(addrp))
21580Sstevel@tonic-gate 			return (0);
21590Sstevel@tonic-gate 		return (sizeof (sin6_t));
21600Sstevel@tonic-gate 	default:
21610Sstevel@tonic-gate 		return (0);
21620Sstevel@tonic-gate 	}
21630Sstevel@tonic-gate }
2164