xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_rts.c (revision 8485:633e5b5eb268)
10Sstevel@tonic-gate /*
2*8485SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1988, 1991, 1993
80Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate  * are met:
130Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
140Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
170Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
180Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
190Sstevel@tonic-gate  *    must display the following acknowledgement:
200Sstevel@tonic-gate  *	This product includes software developed by the University of
210Sstevel@tonic-gate  *	California, Berkeley and its contributors.
220Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
230Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
240Sstevel@tonic-gate  *    without specific prior written permission.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
270Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
280Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
290Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
300Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
310Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
320Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
330Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
340Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
350Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
360Sstevel@tonic-gate  * SUCH DAMAGE.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * This file contains routines that processes routing socket requests.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/stream.h>
470Sstevel@tonic-gate #include <sys/stropts.h>
480Sstevel@tonic-gate #include <sys/strlog.h>
490Sstevel@tonic-gate #include <sys/dlpi.h>
500Sstevel@tonic-gate #include <sys/ddi.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/debug.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/systm.h>
550Sstevel@tonic-gate #include <sys/param.h>
560Sstevel@tonic-gate #include <sys/socket.h>
570Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
580Sstevel@tonic-gate #include <sys/tihdr.h>
590Sstevel@tonic-gate #include <net/if.h>
600Sstevel@tonic-gate #include <net/route.h>
610Sstevel@tonic-gate #include <netinet/in.h>
620Sstevel@tonic-gate #include <net/if_dl.h>
630Sstevel@tonic-gate #include <netinet/ip6.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <inet/common.h>
660Sstevel@tonic-gate #include <inet/mi.h>
670Sstevel@tonic-gate #include <inet/ip.h>
680Sstevel@tonic-gate #include <inet/ip6.h>
690Sstevel@tonic-gate #include <inet/ip_if.h>
700Sstevel@tonic-gate #include <inet/ip_ire.h>
710Sstevel@tonic-gate #include <inet/ip_rts.h>
720Sstevel@tonic-gate #include <inet/ip_multi.h>
731676Sjpk #include <sys/tsol/tndb.h>
741676Sjpk #include <sys/tsol/tnet.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Fills the message with the given info.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate void
800Sstevel@tonic-gate rts_fill_msg_v6(int type, int rtm_addrs, const in6_addr_t *dst,
810Sstevel@tonic-gate     const in6_addr_t *mask, const in6_addr_t *gateway,
820Sstevel@tonic-gate     const in6_addr_t *src_addr, const in6_addr_t *brd_addr,
831676Sjpk     const in6_addr_t *author, const ipif_t *ipif, mblk_t *mp,
841676Sjpk     uint_t sacnt, const tsol_gc_t *gc)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
870Sstevel@tonic-gate 	sin6_t		*sin6;
880Sstevel@tonic-gate 	size_t		data_size, header_size;
890Sstevel@tonic-gate 	uchar_t		*cp;
900Sstevel@tonic-gate 	int		i;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	ASSERT(mp != NULL);
931676Sjpk 	ASSERT(sacnt == 0 || gc != NULL);
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * First find the type of the message
960Sstevel@tonic-gate 	 * and its length.
970Sstevel@tonic-gate 	 */
980Sstevel@tonic-gate 	header_size = rts_header_msg_size(type);
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * Now find the size of the data
1010Sstevel@tonic-gate 	 * that follows the message header.
1020Sstevel@tonic-gate 	 */
1031676Sjpk 	data_size = rts_data_msg_size(rtm_addrs, AF_INET6, sacnt);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
1060Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[header_size];
1070Sstevel@tonic-gate 	cp = mp->b_wptr;
1080Sstevel@tonic-gate 	bzero(cp, data_size);
1090Sstevel@tonic-gate 	for (i = 0; i < RTA_NUMBITS; i++) {
1100Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
1110Sstevel@tonic-gate 		switch (rtm_addrs & (1 << i)) {
1120Sstevel@tonic-gate 		case RTA_DST:
1130Sstevel@tonic-gate 			sin6->sin6_addr = *dst;
1140Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1150Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1160Sstevel@tonic-gate 			break;
1170Sstevel@tonic-gate 		case RTA_GATEWAY:
1180Sstevel@tonic-gate 			sin6->sin6_addr = *gateway;
1190Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1200Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1210Sstevel@tonic-gate 			break;
1220Sstevel@tonic-gate 		case RTA_NETMASK:
1230Sstevel@tonic-gate 			sin6->sin6_addr = *mask;
1240Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1250Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1260Sstevel@tonic-gate 			break;
1270Sstevel@tonic-gate 		case RTA_IFA:
1280Sstevel@tonic-gate 		case RTA_SRC:
1290Sstevel@tonic-gate 			sin6->sin6_addr = *src_addr;
1300Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1310Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 		case RTA_IFP:
1340Sstevel@tonic-gate 			cp += ill_dls_info((struct sockaddr_dl *)cp, ipif);
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 		case RTA_AUTHOR:
1370Sstevel@tonic-gate 			sin6->sin6_addr = *author;
1380Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1390Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		case RTA_BRD:
1420Sstevel@tonic-gate 			/*
1430Sstevel@tonic-gate 			 * RTA_BRD is used typically to specify a point-to-point
1440Sstevel@tonic-gate 			 * destination address.
1450Sstevel@tonic-gate 			 */
1460Sstevel@tonic-gate 			sin6->sin6_addr = *brd_addr;
1470Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1480Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	}
1521676Sjpk 
1531676Sjpk 	if (gc != NULL) {
1541676Sjpk 		rtm_ext_t *rtm_ext;
1551676Sjpk 		struct rtsa_s *rp_dst;
1561676Sjpk 		tsol_rtsecattr_t *rsap;
1571676Sjpk 		int i;
1581676Sjpk 
1591676Sjpk 		ASSERT(gc->gc_grp != NULL);
1601676Sjpk 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
1611676Sjpk 		ASSERT(sacnt > 0);
1621676Sjpk 
1631676Sjpk 		rtm_ext = (rtm_ext_t *)cp;
1641676Sjpk 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
1651676Sjpk 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(sacnt);
1661676Sjpk 
1671676Sjpk 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
1681676Sjpk 		rsap->rtsa_cnt = sacnt;
1691676Sjpk 		rp_dst = rsap->rtsa_attr;
1701676Sjpk 
1711676Sjpk 		for (i = 0; i < sacnt; i++, gc = gc->gc_next, rp_dst++) {
1721676Sjpk 			ASSERT(gc->gc_db != NULL);
1731676Sjpk 			bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
1741676Sjpk 		}
1751676Sjpk 		cp = (uchar_t *)rp_dst;
1761676Sjpk 	}
1771676Sjpk 
1780Sstevel@tonic-gate 	mp->b_wptr = cp;
1790Sstevel@tonic-gate 	mp->b_cont = NULL;
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * set the fields that are common to
1820Sstevel@tonic-gate 	 * to different messages.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	rtm->rtm_msglen = (short)(header_size + data_size);
1850Sstevel@tonic-gate 	rtm->rtm_version = RTM_VERSION;
1860Sstevel@tonic-gate 	rtm->rtm_type = (uchar_t)type;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * This routine is called to generate a message to the routing
1910Sstevel@tonic-gate  * socket indicating that a redirect has occured, a routing lookup
1920Sstevel@tonic-gate  * has failed, or that a protocol has detected timeouts to a particular
1930Sstevel@tonic-gate  * destination. This routine is called for message types RTM_LOSING,
1940Sstevel@tonic-gate  * RTM_REDIRECT, and RTM_MISS.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate void
1970Sstevel@tonic-gate ip_rts_change_v6(int type, const in6_addr_t *dst_addr,
1980Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *net_mask,
1990Sstevel@tonic-gate     const in6_addr_t *source, const in6_addr_t *author,
2003448Sdh155122     int flags, int error, int rtm_addrs, ip_stack_t *ipst)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
2030Sstevel@tonic-gate 	mblk_t		*mp;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (rtm_addrs == 0)
2060Sstevel@tonic-gate 		return;
2071676Sjpk 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET6, 0);
2080Sstevel@tonic-gate 	if (mp == NULL)
2090Sstevel@tonic-gate 		return;
2100Sstevel@tonic-gate 	rts_fill_msg_v6(type, rtm_addrs, dst_addr, net_mask, gw_addr, source,
2111676Sjpk 	    &ipv6_all_zeros, author, NULL, mp, 0, NULL);
2120Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
2130Sstevel@tonic-gate 	rtm->rtm_flags = flags;
2140Sstevel@tonic-gate 	rtm->rtm_errno = error;
2150Sstevel@tonic-gate 	rtm->rtm_flags |= RTF_DONE;
2160Sstevel@tonic-gate 	rtm->rtm_addrs = rtm_addrs;
217*8485SPeter.Memishian@Sun.COM 	rts_queue_input(mp, NULL, AF_INET6, RTSQ_ALL, ipst);
2180Sstevel@tonic-gate }
219