xref: /titanic_41/usr/src/uts/common/inet/ip/ip6_rts.c (revision 46736d35df047bb400483364f76bfcb08cdcbb25)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1988, 1991, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
39  */
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 /*
44  * This file contains routines that processes routing socket requests.
45  */
46 
47 #include <sys/types.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/strlog.h>
51 #include <sys/dlpi.h>
52 #include <sys/ddi.h>
53 #include <sys/cmn_err.h>
54 #include <sys/debug.h>
55 
56 #include <sys/systm.h>
57 #include <sys/param.h>
58 #include <sys/socket.h>
59 #define	_SUN_TPI_VERSION	2
60 #include <sys/tihdr.h>
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <net/if_dl.h>
65 #include <netinet/ip6.h>
66 
67 #include <inet/common.h>
68 #include <inet/mi.h>
69 #include <inet/ip.h>
70 #include <inet/ip6.h>
71 #include <inet/ip_if.h>
72 #include <inet/ip_ire.h>
73 #include <inet/ip_rts.h>
74 #include <inet/ip_multi.h>
75 #include <sys/tsol/tndb.h>
76 #include <sys/tsol/tnet.h>
77 
78 /*
79  * Fills the message with the given info.
80  */
81 void
82 rts_fill_msg_v6(int type, int rtm_addrs, const in6_addr_t *dst,
83     const in6_addr_t *mask, const in6_addr_t *gateway,
84     const in6_addr_t *src_addr, const in6_addr_t *brd_addr,
85     const in6_addr_t *author, const ipif_t *ipif, mblk_t *mp,
86     uint_t sacnt, const tsol_gc_t *gc)
87 {
88 	rt_msghdr_t	*rtm;
89 	sin6_t		*sin6;
90 	size_t		data_size, header_size;
91 	uchar_t		*cp;
92 	int		i;
93 
94 	ASSERT(mp != NULL);
95 	ASSERT(sacnt == 0 || gc != NULL);
96 	/*
97 	 * First find the type of the message
98 	 * and its length.
99 	 */
100 	header_size = rts_header_msg_size(type);
101 	/*
102 	 * Now find the size of the data
103 	 * that follows the message header.
104 	 */
105 	data_size = rts_data_msg_size(rtm_addrs, AF_INET6, sacnt);
106 
107 	rtm = (rt_msghdr_t *)mp->b_rptr;
108 	mp->b_wptr = &mp->b_rptr[header_size];
109 	cp = mp->b_wptr;
110 	bzero(cp, data_size);
111 	for (i = 0; i < RTA_NUMBITS; i++) {
112 		sin6 = (sin6_t *)cp;
113 		switch (rtm_addrs & (1 << i)) {
114 		case RTA_DST:
115 			sin6->sin6_addr = *dst;
116 			sin6->sin6_family = AF_INET6;
117 			cp += sizeof (sin6_t);
118 			break;
119 		case RTA_GATEWAY:
120 			sin6->sin6_addr = *gateway;
121 			sin6->sin6_family = AF_INET6;
122 			cp += sizeof (sin6_t);
123 			break;
124 		case RTA_NETMASK:
125 			sin6->sin6_addr = *mask;
126 			sin6->sin6_family = AF_INET6;
127 			cp += sizeof (sin6_t);
128 			break;
129 		case RTA_IFA:
130 		case RTA_SRC:
131 			sin6->sin6_addr = *src_addr;
132 			sin6->sin6_family = AF_INET6;
133 			cp += sizeof (sin6_t);
134 			break;
135 		case RTA_IFP:
136 			cp += ill_dls_info((struct sockaddr_dl *)cp, ipif);
137 			break;
138 		case RTA_AUTHOR:
139 			sin6->sin6_addr = *author;
140 			sin6->sin6_family = AF_INET6;
141 			cp += sizeof (sin6_t);
142 			break;
143 		case RTA_BRD:
144 			/*
145 			 * RTA_BRD is used typically to specify a point-to-point
146 			 * destination address.
147 			 */
148 			sin6->sin6_addr = *brd_addr;
149 			sin6->sin6_family = AF_INET6;
150 			cp += sizeof (sin6_t);
151 			break;
152 		}
153 	}
154 
155 	if (gc != NULL) {
156 		rtm_ext_t *rtm_ext;
157 		struct rtsa_s *rp_dst;
158 		tsol_rtsecattr_t *rsap;
159 		int i;
160 
161 		ASSERT(gc->gc_grp != NULL);
162 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
163 		ASSERT(sacnt > 0);
164 
165 		rtm_ext = (rtm_ext_t *)cp;
166 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
167 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(sacnt);
168 
169 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
170 		rsap->rtsa_cnt = sacnt;
171 		rp_dst = rsap->rtsa_attr;
172 
173 		for (i = 0; i < sacnt; i++, gc = gc->gc_next, rp_dst++) {
174 			ASSERT(gc->gc_db != NULL);
175 			bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
176 		}
177 		cp = (uchar_t *)rp_dst;
178 	}
179 
180 	mp->b_wptr = cp;
181 	mp->b_cont = NULL;
182 	/*
183 	 * set the fields that are common to
184 	 * to different messages.
185 	 */
186 	rtm->rtm_msglen = (short)(header_size + data_size);
187 	rtm->rtm_version = RTM_VERSION;
188 	rtm->rtm_type = (uchar_t)type;
189 }
190 
191 /*
192  * This routine is called to generate a message to the routing
193  * socket indicating that a redirect has occured, a routing lookup
194  * has failed, or that a protocol has detected timeouts to a particular
195  * destination. This routine is called for message types RTM_LOSING,
196  * RTM_REDIRECT, and RTM_MISS.
197  */
198 void
199 ip_rts_change_v6(int type, const in6_addr_t *dst_addr,
200     const in6_addr_t *gw_addr, const in6_addr_t *net_mask,
201     const in6_addr_t *source, const in6_addr_t *author,
202     int flags, int error, int rtm_addrs, ip_stack_t *ipst)
203 {
204 	rt_msghdr_t	*rtm;
205 	mblk_t		*mp;
206 
207 	if (rtm_addrs == 0)
208 		return;
209 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET6, 0);
210 	if (mp == NULL)
211 		return;
212 	rts_fill_msg_v6(type, rtm_addrs, dst_addr, net_mask, gw_addr, source,
213 	    &ipv6_all_zeros, author, NULL, mp, 0, NULL);
214 	rtm = (rt_msghdr_t *)mp->b_rptr;
215 	rtm->rtm_flags = flags;
216 	rtm->rtm_errno = error;
217 	rtm->rtm_flags |= RTF_DONE;
218 	rtm->rtm_addrs = rtm_addrs;
219 	rts_queue_input(mp, NULL, AF_INET6, ipst);
220 }
221