xref: /illumos-gate/usr/src/uts/common/inet/ip/ip6_rts.c (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
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 ipif_t *ipif, mblk_t *mp,
84     uint_t sacnt, 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 	ASSERT(sacnt == 0 || gc != NULL);
94 	/*
95 	 * First find the type of the message
96 	 * and its length.
97 	 */
98 	header_size = rts_header_msg_size(type);
99 	/*
100 	 * Now find the size of the data
101 	 * that follows the message header.
102 	 */
103 	data_size = rts_data_msg_size(rtm_addrs, AF_INET6, sacnt);
104 
105 	rtm = (rt_msghdr_t *)mp->b_rptr;
106 	mp->b_wptr = &mp->b_rptr[header_size];
107 	cp = mp->b_wptr;
108 	bzero(cp, data_size);
109 	for (i = 0; i < RTA_NUMBITS; i++) {
110 		sin6 = (sin6_t *)cp;
111 		switch (rtm_addrs & (1 << i)) {
112 		case RTA_DST:
113 			sin6->sin6_addr = *dst;
114 			sin6->sin6_family = AF_INET6;
115 			cp += sizeof (sin6_t);
116 			break;
117 		case RTA_GATEWAY:
118 			sin6->sin6_addr = *gateway;
119 			sin6->sin6_family = AF_INET6;
120 			cp += sizeof (sin6_t);
121 			break;
122 		case RTA_NETMASK:
123 			sin6->sin6_addr = *mask;
124 			sin6->sin6_family = AF_INET6;
125 			cp += sizeof (sin6_t);
126 			break;
127 		case RTA_IFA:
128 		case RTA_SRC:
129 			sin6->sin6_addr = *src_addr;
130 			sin6->sin6_family = AF_INET6;
131 			cp += sizeof (sin6_t);
132 			break;
133 		case RTA_IFP:
134 			cp += ill_dls_info((struct sockaddr_dl *)cp, ipif);
135 			break;
136 		case RTA_AUTHOR:
137 			sin6->sin6_addr = *author;
138 			sin6->sin6_family = AF_INET6;
139 			cp += sizeof (sin6_t);
140 			break;
141 		case RTA_BRD:
142 			/*
143 			 * RTA_BRD is used typically to specify a point-to-point
144 			 * destination address.
145 			 */
146 			sin6->sin6_addr = *brd_addr;
147 			sin6->sin6_family = AF_INET6;
148 			cp += sizeof (sin6_t);
149 			break;
150 		}
151 	}
152 
153 	if (gc != NULL) {
154 		rtm_ext_t *rtm_ext;
155 		struct rtsa_s *rp_dst;
156 		tsol_rtsecattr_t *rsap;
157 		int i;
158 
159 		ASSERT(gc->gc_grp != NULL);
160 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
161 		ASSERT(sacnt > 0);
162 
163 		rtm_ext = (rtm_ext_t *)cp;
164 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
165 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(sacnt);
166 
167 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
168 		rsap->rtsa_cnt = sacnt;
169 		rp_dst = rsap->rtsa_attr;
170 
171 		for (i = 0; i < sacnt; i++, gc = gc->gc_next, rp_dst++) {
172 			ASSERT(gc->gc_db != NULL);
173 			bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
174 		}
175 		cp = (uchar_t *)rp_dst;
176 	}
177 
178 	mp->b_wptr = cp;
179 	mp->b_cont = NULL;
180 	/*
181 	 * set the fields that are common to
182 	 * to different messages.
183 	 */
184 	rtm->rtm_msglen = (short)(header_size + data_size);
185 	rtm->rtm_version = RTM_VERSION;
186 	rtm->rtm_type = (uchar_t)type;
187 }
188 
189 /*
190  * This routine is called to generate a message to the routing
191  * socket indicating that a redirect has occured, a routing lookup
192  * has failed, or that a protocol has detected timeouts to a particular
193  * destination. This routine is called for message types RTM_LOSING,
194  * RTM_REDIRECT, and RTM_MISS.
195  */
196 void
197 ip_rts_change_v6(int type, const in6_addr_t *dst_addr,
198     const in6_addr_t *gw_addr, const in6_addr_t *net_mask,
199     const in6_addr_t *source, const in6_addr_t *author,
200     int flags, int error, int rtm_addrs, ip_stack_t *ipst)
201 {
202 	rt_msghdr_t	*rtm;
203 	mblk_t		*mp;
204 
205 	if (rtm_addrs == 0)
206 		return;
207 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET6, 0);
208 	if (mp == NULL)
209 		return;
210 	rts_fill_msg_v6(type, rtm_addrs, dst_addr, net_mask, gw_addr, source,
211 	    &ipv6_all_zeros, author, NULL, mp, 0, NULL);
212 	rtm = (rt_msghdr_t *)mp->b_rptr;
213 	rtm->rtm_flags = flags;
214 	rtm->rtm_errno = error;
215 	rtm->rtm_flags |= RTF_DONE;
216 	rtm->rtm_addrs = rtm_addrs;
217 	rts_queue_input(mp, NULL, AF_INET6, RTSQ_ALL, ipst);
218 }
219