xref: /illumos-gate/usr/src/uts/common/inet/ip/ip6_rts.c (revision bd670b35)
17c478bd9Sstevel@tonic-gate /*
2e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1991, 1993
87c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
117c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
127c478bd9Sstevel@tonic-gate  * are met:
137c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
147c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
157c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
167c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
177c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
187c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
197c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
207c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
217c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
227c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
237c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
247c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
277c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
287c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
297c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
307c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
317c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
327c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
337c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
347c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
357c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
367c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * This file contains routines that processes routing socket requests.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stream.h>
477c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
487c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
497c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
507c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/systm.h>
557c478bd9Sstevel@tonic-gate #include <sys/param.h>
567c478bd9Sstevel@tonic-gate #include <sys/socket.h>
577c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
587c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
597c478bd9Sstevel@tonic-gate #include <net/if.h>
607c478bd9Sstevel@tonic-gate #include <net/route.h>
617c478bd9Sstevel@tonic-gate #include <netinet/in.h>
627c478bd9Sstevel@tonic-gate #include <net/if_dl.h>
637c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <inet/common.h>
667c478bd9Sstevel@tonic-gate #include <inet/mi.h>
677c478bd9Sstevel@tonic-gate #include <inet/ip.h>
687c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
697c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
707c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
717c478bd9Sstevel@tonic-gate #include <inet/ip_rts.h>
727c478bd9Sstevel@tonic-gate #include <inet/ip_multi.h>
7345916cd2Sjpk #include <sys/tsol/tndb.h>
7445916cd2Sjpk #include <sys/tsol/tnet.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Fills the message with the given info.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate void
rts_fill_msg_v6(int type,int rtm_addrs,const in6_addr_t * dst,const in6_addr_t * mask,const in6_addr_t * gateway,const in6_addr_t * src_addr,const in6_addr_t * brd_addr,const in6_addr_t * author,const in6_addr_t * ifaddr,const ill_t * ill,mblk_t * mp,const tsol_gc_t * gc)807c478bd9Sstevel@tonic-gate rts_fill_msg_v6(int type, int rtm_addrs, const in6_addr_t *dst,
817c478bd9Sstevel@tonic-gate     const in6_addr_t *mask, const in6_addr_t *gateway,
827c478bd9Sstevel@tonic-gate     const in6_addr_t *src_addr, const in6_addr_t *brd_addr,
83*bd670b35SErik Nordmark     const in6_addr_t *author, const in6_addr_t *ifaddr, const ill_t *ill,
84*bd670b35SErik Nordmark     mblk_t *mp, const tsol_gc_t *gc)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
877c478bd9Sstevel@tonic-gate 	sin6_t		*sin6;
887c478bd9Sstevel@tonic-gate 	size_t		data_size, header_size;
897c478bd9Sstevel@tonic-gate 	uchar_t		*cp;
907c478bd9Sstevel@tonic-gate 	int		i;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * First find the type of the message
957c478bd9Sstevel@tonic-gate 	 * and its length.
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 	header_size = rts_header_msg_size(type);
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * Now find the size of the data
1007c478bd9Sstevel@tonic-gate 	 * that follows the message header.
1017c478bd9Sstevel@tonic-gate 	 */
102*bd670b35SErik Nordmark 	data_size = rts_data_msg_size(rtm_addrs, AF_INET6, gc != NULL ? 1 : 0);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
1057c478bd9Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[header_size];
1067c478bd9Sstevel@tonic-gate 	cp = mp->b_wptr;
1077c478bd9Sstevel@tonic-gate 	bzero(cp, data_size);
1087c478bd9Sstevel@tonic-gate 	for (i = 0; i < RTA_NUMBITS; i++) {
1097c478bd9Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
1107c478bd9Sstevel@tonic-gate 		switch (rtm_addrs & (1 << i)) {
1117c478bd9Sstevel@tonic-gate 		case RTA_DST:
1127c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = *dst;
1137c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1147c478bd9Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 		case RTA_GATEWAY:
1177c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = *gateway;
1187c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1197c478bd9Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 		case RTA_NETMASK:
1227c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = *mask;
1237c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1247c478bd9Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 		case RTA_IFA:
127*bd670b35SErik Nordmark 			sin6->sin6_addr = *ifaddr;
128*bd670b35SErik Nordmark 			sin6->sin6_family = AF_INET6;
129*bd670b35SErik Nordmark 			cp += sizeof (sin6_t);
130*bd670b35SErik Nordmark 			break;
1317c478bd9Sstevel@tonic-gate 		case RTA_SRC:
1327c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = *src_addr;
1337c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1347c478bd9Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		case RTA_IFP:
137*bd670b35SErik Nordmark 			cp += ill_dls_info((struct sockaddr_dl *)cp, ill);
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		case RTA_AUTHOR:
1407c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = *author;
1417c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1427c478bd9Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		case RTA_BRD:
1457c478bd9Sstevel@tonic-gate 			/*
1467c478bd9Sstevel@tonic-gate 			 * RTA_BRD is used typically to specify a point-to-point
1477c478bd9Sstevel@tonic-gate 			 * destination address.
1487c478bd9Sstevel@tonic-gate 			 */
1497c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = *brd_addr;
1507c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
1517c478bd9Sstevel@tonic-gate 			cp += sizeof (sin6_t);
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
15545916cd2Sjpk 
15645916cd2Sjpk 	if (gc != NULL) {
15745916cd2Sjpk 		rtm_ext_t *rtm_ext;
15845916cd2Sjpk 		struct rtsa_s *rp_dst;
15945916cd2Sjpk 		tsol_rtsecattr_t *rsap;
16045916cd2Sjpk 
16145916cd2Sjpk 		ASSERT(gc->gc_grp != NULL);
16245916cd2Sjpk 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
16345916cd2Sjpk 
16445916cd2Sjpk 		rtm_ext = (rtm_ext_t *)cp;
16545916cd2Sjpk 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
166*bd670b35SErik Nordmark 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(1);
16745916cd2Sjpk 
16845916cd2Sjpk 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
169*bd670b35SErik Nordmark 		rsap->rtsa_cnt = 1;
17045916cd2Sjpk 		rp_dst = rsap->rtsa_attr;
17145916cd2Sjpk 
172*bd670b35SErik Nordmark 		ASSERT(gc->gc_db != NULL);
173*bd670b35SErik Nordmark 		bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
17445916cd2Sjpk 		cp = (uchar_t *)rp_dst;
17545916cd2Sjpk 	}
17645916cd2Sjpk 
1777c478bd9Sstevel@tonic-gate 	mp->b_wptr = cp;
1787c478bd9Sstevel@tonic-gate 	mp->b_cont = NULL;
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * set the fields that are common to
1817c478bd9Sstevel@tonic-gate 	 * to different messages.
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	rtm->rtm_msglen = (short)(header_size + data_size);
1847c478bd9Sstevel@tonic-gate 	rtm->rtm_version = RTM_VERSION;
1857c478bd9Sstevel@tonic-gate 	rtm->rtm_type = (uchar_t)type;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * This routine is called to generate a message to the routing
1907c478bd9Sstevel@tonic-gate  * socket indicating that a redirect has occured, a routing lookup
1917c478bd9Sstevel@tonic-gate  * has failed, or that a protocol has detected timeouts to a particular
1927c478bd9Sstevel@tonic-gate  * destination. This routine is called for message types RTM_LOSING,
1937c478bd9Sstevel@tonic-gate  * RTM_REDIRECT, and RTM_MISS.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate void
ip_rts_change_v6(int type,const in6_addr_t * dst_addr,const in6_addr_t * gw_addr,const in6_addr_t * net_mask,const in6_addr_t * source,const in6_addr_t * author,int flags,int error,int rtm_addrs,ip_stack_t * ipst)1967c478bd9Sstevel@tonic-gate ip_rts_change_v6(int type, const in6_addr_t *dst_addr,
1977c478bd9Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *net_mask,
1987c478bd9Sstevel@tonic-gate     const in6_addr_t *source, const in6_addr_t *author,
199f4b3ec61Sdh     int flags, int error, int rtm_addrs, ip_stack_t *ipst)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	rt_msghdr_t	*rtm;
2027c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (rtm_addrs == 0)
2057c478bd9Sstevel@tonic-gate 		return;
20645916cd2Sjpk 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET6, 0);
2077c478bd9Sstevel@tonic-gate 	if (mp == NULL)
2087c478bd9Sstevel@tonic-gate 		return;
2097c478bd9Sstevel@tonic-gate 	rts_fill_msg_v6(type, rtm_addrs, dst_addr, net_mask, gw_addr, source,
210*bd670b35SErik Nordmark 	    &ipv6_all_zeros, &ipv6_all_zeros, author, NULL, mp, NULL);
2117c478bd9Sstevel@tonic-gate 	rtm = (rt_msghdr_t *)mp->b_rptr;
2127c478bd9Sstevel@tonic-gate 	rtm->rtm_flags = flags;
2137c478bd9Sstevel@tonic-gate 	rtm->rtm_errno = error;
2147c478bd9Sstevel@tonic-gate 	rtm->rtm_flags |= RTF_DONE;
2157c478bd9Sstevel@tonic-gate 	rtm->rtm_addrs = rtm_addrs;
216e11c3f44Smeem 	rts_queue_input(mp, NULL, AF_INET6, RTSQ_ALL, ipst);
2177c478bd9Sstevel@tonic-gate }
218