xref: /illumos-gate/usr/src/uts/common/inet/ip/ip6_rts.c (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
1 /*
2  * Copyright 2009 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 /*
42  * This file contains routines that processes routing socket requests.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/stream.h>
47 #include <sys/stropts.h>
48 #include <sys/strlog.h>
49 #include <sys/dlpi.h>
50 #include <sys/ddi.h>
51 #include <sys/cmn_err.h>
52 #include <sys/debug.h>
53 
54 #include <sys/systm.h>
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #define	_SUN_TPI_VERSION	2
58 #include <sys/tihdr.h>
59 #include <net/if.h>
60 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <net/if_dl.h>
63 #include <netinet/ip6.h>
64 
65 #include <inet/common.h>
66 #include <inet/mi.h>
67 #include <inet/ip.h>
68 #include <inet/ip6.h>
69 #include <inet/ip_if.h>
70 #include <inet/ip_ire.h>
71 #include <inet/ip_rts.h>
72 #include <inet/ip_multi.h>
73 #include <sys/tsol/tndb.h>
74 #include <sys/tsol/tnet.h>
75 
76 /*
77  * Fills the message with the given info.
78  */
79 void
80 rts_fill_msg_v6(int type, int rtm_addrs, const in6_addr_t *dst,
81     const in6_addr_t *mask, const in6_addr_t *gateway,
82     const in6_addr_t *src_addr, const in6_addr_t *brd_addr,
83     const in6_addr_t *author, const in6_addr_t *ifaddr, const ill_t *ill,
84     mblk_t *mp, const tsol_gc_t *gc)
85 {
86 	rt_msghdr_t	*rtm;
87 	sin6_t		*sin6;
88 	size_t		data_size, header_size;
89 	uchar_t		*cp;
90 	int		i;
91 
92 	ASSERT(mp != NULL);
93 	/*
94 	 * First find the type of the message
95 	 * and its length.
96 	 */
97 	header_size = rts_header_msg_size(type);
98 	/*
99 	 * Now find the size of the data
100 	 * that follows the message header.
101 	 */
102 	data_size = rts_data_msg_size(rtm_addrs, AF_INET6, gc != NULL ? 1 : 0);
103 
104 	rtm = (rt_msghdr_t *)mp->b_rptr;
105 	mp->b_wptr = &mp->b_rptr[header_size];
106 	cp = mp->b_wptr;
107 	bzero(cp, data_size);
108 	for (i = 0; i < RTA_NUMBITS; i++) {
109 		sin6 = (sin6_t *)cp;
110 		switch (rtm_addrs & (1 << i)) {
111 		case RTA_DST:
112 			sin6->sin6_addr = *dst;
113 			sin6->sin6_family = AF_INET6;
114 			cp += sizeof (sin6_t);
115 			break;
116 		case RTA_GATEWAY:
117 			sin6->sin6_addr = *gateway;
118 			sin6->sin6_family = AF_INET6;
119 			cp += sizeof (sin6_t);
120 			break;
121 		case RTA_NETMASK:
122 			sin6->sin6_addr = *mask;
123 			sin6->sin6_family = AF_INET6;
124 			cp += sizeof (sin6_t);
125 			break;
126 		case RTA_IFA:
127 			sin6->sin6_addr = *ifaddr;
128 			sin6->sin6_family = AF_INET6;
129 			cp += sizeof (sin6_t);
130 			break;
131 		case RTA_SRC:
132 			sin6->sin6_addr = *src_addr;
133 			sin6->sin6_family = AF_INET6;
134 			cp += sizeof (sin6_t);
135 			break;
136 		case RTA_IFP:
137 			cp += ill_dls_info((struct sockaddr_dl *)cp, ill);
138 			break;
139 		case RTA_AUTHOR:
140 			sin6->sin6_addr = *author;
141 			sin6->sin6_family = AF_INET6;
142 			cp += sizeof (sin6_t);
143 			break;
144 		case RTA_BRD:
145 			/*
146 			 * RTA_BRD is used typically to specify a point-to-point
147 			 * destination address.
148 			 */
149 			sin6->sin6_addr = *brd_addr;
150 			sin6->sin6_family = AF_INET6;
151 			cp += sizeof (sin6_t);
152 			break;
153 		}
154 	}
155 
156 	if (gc != NULL) {
157 		rtm_ext_t *rtm_ext;
158 		struct rtsa_s *rp_dst;
159 		tsol_rtsecattr_t *rsap;
160 
161 		ASSERT(gc->gc_grp != NULL);
162 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
163 
164 		rtm_ext = (rtm_ext_t *)cp;
165 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
166 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(1);
167 
168 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
169 		rsap->rtsa_cnt = 1;
170 		rp_dst = rsap->rtsa_attr;
171 
172 		ASSERT(gc->gc_db != NULL);
173 		bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
174 		cp = (uchar_t *)rp_dst;
175 	}
176 
177 	mp->b_wptr = cp;
178 	mp->b_cont = NULL;
179 	/*
180 	 * set the fields that are common to
181 	 * to different messages.
182 	 */
183 	rtm->rtm_msglen = (short)(header_size + data_size);
184 	rtm->rtm_version = RTM_VERSION;
185 	rtm->rtm_type = (uchar_t)type;
186 }
187 
188 /*
189  * This routine is called to generate a message to the routing
190  * socket indicating that a redirect has occured, a routing lookup
191  * has failed, or that a protocol has detected timeouts to a particular
192  * destination. This routine is called for message types RTM_LOSING,
193  * RTM_REDIRECT, and RTM_MISS.
194  */
195 void
196 ip_rts_change_v6(int type, const in6_addr_t *dst_addr,
197     const in6_addr_t *gw_addr, const in6_addr_t *net_mask,
198     const in6_addr_t *source, const in6_addr_t *author,
199     int flags, int error, int rtm_addrs, ip_stack_t *ipst)
200 {
201 	rt_msghdr_t	*rtm;
202 	mblk_t		*mp;
203 
204 	if (rtm_addrs == 0)
205 		return;
206 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET6, 0);
207 	if (mp == NULL)
208 		return;
209 	rts_fill_msg_v6(type, rtm_addrs, dst_addr, net_mask, gw_addr, source,
210 	    &ipv6_all_zeros, &ipv6_all_zeros, author, NULL, mp, NULL);
211 	rtm = (rt_msghdr_t *)mp->b_rptr;
212 	rtm->rtm_flags = flags;
213 	rtm->rtm_errno = error;
214 	rtm->rtm_flags |= RTF_DONE;
215 	rtm->rtm_addrs = rtm_addrs;
216 	rts_queue_input(mp, NULL, AF_INET6, RTSQ_ALL, ipst);
217 }
218