1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/stream.h>
28 #define	_SUN_TPI_VERSION 2
29 #include <sys/tihdr.h>
30 #include <sys/socket.h>
31 #include <sys/xti_xtiopt.h>
32 #include <sys/xti_inet.h>
33 
34 #include <inet/common.h>
35 #include <netinet/ip6.h>
36 #include <inet/ip.h>
37 #include <inet/udp_impl.h>
38 /*
39  * MK_XXX Following 2 includes temporary to import ip6_rthdr_t
40  *        definition. May not be needed if we fix ip6_dg_snd_attrs_t
41  *        to do all extension headers in identical manner.
42  */
43 #include <net/if.h>
44 #include <inet/ip6.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/udp.h>
48 #include <inet/optcom.h>
49 
50 /*
51  * Table of all known options handled on a UDP protocol stack.
52  *
53  * Note: This table contains options processed by both UDP and IP levels
54  *       and is the superset of options that can be performed on a UDP over IP
55  *       stack.
56  */
57 opdes_t	udp_opt_arr[] = {
58 
59 { SO_DEBUG,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
60 { SO_DONTROUTE,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
61 { SO_USELOOPBACK, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
62 	},
63 { SO_BROADCAST,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
64 { SO_REUSEADDR, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
65 { SO_TYPE,	SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
66 { SO_SNDBUF,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
67 { SO_RCVBUF,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
68 { SO_SNDTIMEO,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0,
69 	sizeof (struct timeval), 0 },
70 { SO_RCVTIMEO,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0,
71 	sizeof (struct timeval), 0 },
72 { SO_DGRAM_ERRIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
73 	0 },
74 { SO_RECVUCRED, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
75 	},
76 { SO_ALLZONES, SOL_SOCKET, OA_R, OA_RW, OP_CONFIG, 0, sizeof (int),
77 	0 },
78 { SO_TIMESTAMP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
79 	},
80 { SO_ANON_MLP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
81     0 },
82 { SO_MAC_EXEMPT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
83     0 },
84 { SO_MAC_IMPLICIT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
85     0 },
86 { SCM_UCRED, SOL_SOCKET, OA_W, OA_W, OP_NP, OP_VARLEN|OP_NODEFAULT, 512, 0 },
87 { SO_EXCLBIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
88 { SO_DOMAIN,	SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
89 { SO_PROTOTYPE,	SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
90 
91 { IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
92 	(OP_VARLEN|OP_NODEFAULT),
93 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
94 { T_IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
95 	(OP_VARLEN|OP_NODEFAULT),
96 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
97 
98 { IP_TOS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
99 { T_IP_TOS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
100 { IP_TTL,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
101 { IP_RECVOPTS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
102 { IP_RECVDSTADDR, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
103 	},
104 { IP_RECVIF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
105 { IP_RECVSLLA, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
106 { IP_RECVTTL,	IPPROTO_IP,  OA_RW, OA_RW, OP_NP, 0, sizeof (int),
107 	0 },
108 { IP_MULTICAST_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0,
109 	sizeof (struct in_addr),	0 /* INADDR_ANY */ },
110 
111 { IP_MULTICAST_LOOP, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
112 	sizeof (uchar_t), -1 /* not initialized */},
113 
114 { IP_MULTICAST_TTL, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
115 	sizeof (uchar_t), -1 /* not initialized */ },
116 
117 { IP_ADD_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
118 	sizeof (struct ip_mreq), -1 /* not initialized */ },
119 
120 { IP_DROP_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
121 	sizeof (struct ip_mreq), -1 /* not initialized */ },
122 
123 { IP_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
124 	sizeof (struct ip_mreq_source), -1 /* not initialized */ },
125 
126 { IP_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
127 	sizeof (struct ip_mreq_source), -1 /* not initialized */ },
128 
129 { IP_ADD_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
130 	OP_NODEFAULT, sizeof (struct ip_mreq_source), -1 },
131 
132 { IP_DROP_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
133 	OP_NODEFAULT, sizeof (struct ip_mreq_source), -1 },
134 
135 { IP_SEC_OPT, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_NODEFAULT,
136 	sizeof (ipsec_req_t), -1 /* not initialized */ },
137 
138 { IP_BOUND_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0,
139 	sizeof (int),	0 /* no ifindex */ },
140 
141 { IP_DHCPINIT_IF, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, 0,
142 	sizeof (int), 0 },
143 
144 { IP_UNSPEC_SRC, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0,
145 	sizeof (int), 0 },
146 
147 { IP_BROADCAST_TTL, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0, sizeof (uchar_t),
148 	0 /* disabled */ },
149 
150 { IP_PKTINFO, IPPROTO_IP, OA_RW, OA_RW, OP_NP,
151 	(OP_NODEFAULT|OP_VARLEN),
152 	sizeof (struct in_pktinfo), -1 /* not initialized */ },
153 { IP_NEXTHOP, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, 0,
154 	sizeof (in_addr_t),	-1 /* not initialized  */ },
155 
156 { IP_DONTFRAG, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
157 
158 { MCAST_JOIN_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
159 	OP_NODEFAULT, sizeof (struct group_req),
160 	-1 /* not initialized */ },
161 { MCAST_LEAVE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
162 	OP_NODEFAULT, sizeof (struct group_req),
163 	-1 /* not initialized */ },
164 { MCAST_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
165 	OP_NODEFAULT, sizeof (struct group_source_req),
166 	-1 /* not initialized */ },
167 { MCAST_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
168 	OP_NODEFAULT, sizeof (struct group_source_req),
169 	-1 /* not initialized */ },
170 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
171 	OP_NODEFAULT, sizeof (struct group_source_req),
172 	-1 /* not initialized */ },
173 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
174 	OP_NODEFAULT, sizeof (struct group_source_req),
175 	-1 /* not initialized */ },
176 
177 { IPV6_MULTICAST_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
178 	sizeof (int), 0 },
179 
180 { IPV6_MULTICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
181 	OP_DEF_FN, sizeof (int), -1 /* not initialized */ },
182 
183 { IPV6_MULTICAST_LOOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
184 	OP_DEF_FN, sizeof (int), -1 /* not initialized */},
185 
186 { IPV6_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, OP_NODEFAULT,
187 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
188 
189 { IPV6_LEAVE_GROUP,	IPPROTO_IPV6, OA_X, OA_X, OP_NP,
190 	OP_NODEFAULT,
191 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
192 
193 { IPV6_UNICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
194 	sizeof (int), -1 /* not initialized */ },
195 
196 { IPV6_BOUND_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
197 	sizeof (int),	0 /* no ifindex */ },
198 
199 { IPV6_UNSPEC_SRC, IPPROTO_IPV6, OA_R, OA_RW, OP_RAW, 0,
200 	sizeof (int), 0 },
201 
202 { IPV6_PKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
203 	(OP_NODEFAULT|OP_VARLEN),
204 	sizeof (struct in6_pktinfo), -1 /* not initialized */ },
205 { IPV6_HOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
206 	OP_NODEFAULT,
207 	sizeof (int), -1 /* not initialized */ },
208 { IPV6_NEXTHOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
209 	(OP_NODEFAULT|OP_VARLEN),
210 	sizeof (sin6_t), -1 /* not initialized */ },
211 { IPV6_HOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
212 	(OP_VARLEN|OP_NODEFAULT),
213 	MAX_EHDR_LEN, -1 /* not initialized */ },
214 { IPV6_DSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
215 	(OP_VARLEN|OP_NODEFAULT),
216 	MAX_EHDR_LEN, -1 /* not initialized */ },
217 { IPV6_RTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
218 	(OP_VARLEN|OP_NODEFAULT),
219 	MAX_EHDR_LEN, -1 /* not initialized */ },
220 { IPV6_RTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
221 	(OP_VARLEN|OP_NODEFAULT),
222 	MAX_EHDR_LEN, -1 /* not initialized */ },
223 { IPV6_TCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
224 	OP_NODEFAULT,
225 	sizeof (int), -1 /* not initialized */ },
226 { IPV6_PATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
227 	OP_NODEFAULT,
228 	sizeof (struct ip6_mtuinfo), -1 },
229 { IPV6_DONTFRAG, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
230 	sizeof (int), 0 },
231 { IPV6_USE_MIN_MTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
232 	sizeof (int), 0 },
233 { IPV6_V6ONLY, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
234 	sizeof (int), 0 },
235 
236 { IPV6_RECVPKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
237 	sizeof (int), 0 },
238 { IPV6_RECVHOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
239 	sizeof (int), 0 },
240 { IPV6_RECVHOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
241 	sizeof (int), 0 },
242 { _OLD_IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
243 	sizeof (int), 0 },
244 { IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
245 	sizeof (int), 0 },
246 { IPV6_RECVRTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
247 	sizeof (int), 0 },
248 { IPV6_RECVRTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
249 	sizeof (int), 0 },
250 { IPV6_RECVPATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
251 	0, sizeof (int), 0 },
252 { IPV6_RECVTCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
253 	sizeof (int), 0 },
254 
255 { IPV6_SEC_OPT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_NODEFAULT,
256 	sizeof (ipsec_req_t), -1 /* not initialized */ },
257 { IPV6_SRC_PREFERENCES, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
258 	sizeof (uint32_t), IPV6_PREFER_SRC_DEFAULT },
259 
260 { MCAST_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
261 	OP_NODEFAULT, sizeof (struct group_req),
262 	-1 /* not initialized */ },
263 { MCAST_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
264 	OP_NODEFAULT, sizeof (struct group_req),
265 	-1 /* not initialized */ },
266 { MCAST_BLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
267 	OP_NODEFAULT, sizeof (struct group_source_req),
268 	-1 /* not initialized */ },
269 { MCAST_UNBLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
270 	OP_NODEFAULT, sizeof (struct group_source_req),
271 	-1 /* not initialized */ },
272 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
273 	OP_NODEFAULT, sizeof (struct group_source_req),
274 	-1 /* not initialized */ },
275 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
276 	OP_NODEFAULT, sizeof (struct group_source_req),
277 	-1 /* not initialized */ },
278 
279 { UDP_ANONPRIVBIND, IPPROTO_UDP, OA_R, OA_RW, OP_PRIVPORT, 0,
280 	sizeof (int), 0 },
281 { UDP_EXCLBIND, IPPROTO_UDP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
282 	},
283 { UDP_RCVHDR, IPPROTO_UDP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
284 	},
285 { UDP_NAT_T_ENDPOINT, IPPROTO_UDP, OA_RW, OA_RW, OP_PRIVPORT, 0, sizeof (int),
286 	0 },
287 };
288 
289 /*
290  * Table of all supported levels
291  * Note: Some levels (e.g. XTI_GENERIC) may be valid but may not have
292  * any supported options so we need this info separately.
293  *
294  * This is needed only for topmost tpi providers and is used only by
295  * XTI interfaces.
296  */
297 optlevel_t	udp_valid_levels_arr[] = {
298 	XTI_GENERIC,
299 	SOL_SOCKET,
300 	IPPROTO_UDP,
301 	IPPROTO_IP,
302 	IPPROTO_IPV6
303 };
304 
305 #define	UDP_VALID_LEVELS_CNT	A_CNT(udp_valid_levels_arr)
306 #define	UDP_OPT_ARR_CNT		A_CNT(udp_opt_arr)
307 
308 uint_t udp_max_optsize; /* initialized when UDP driver is loaded */
309 
310 /*
311  * Initialize option database object for UDP
312  *
313  * This object represents database of options to search passed to
314  * {sock,tpi}optcom_req() interface routine to take care of option
315  * management and associated methods.
316  */
317 
318 optdb_obj_t udp_opt_obj = {
319 	udp_opt_default,	/* UDP default value function pointer */
320 	udp_tpi_opt_get,	/* UDP get function pointer */
321 	udp_tpi_opt_set,	/* UDP set function pointer */
322 	UDP_OPT_ARR_CNT,	/* UDP option database count of entries */
323 	udp_opt_arr,		/* UDP option database */
324 	UDP_VALID_LEVELS_CNT,	/* UDP valid level count of entries */
325 	udp_valid_levels_arr	/* UDP valid level array */
326 };
327