xref: /illumos-gate/usr/src/uts/common/inet/udp/udp_opt_data.c (revision 8159a906dbf7d5466ef599875f165fe944ed07e7)
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, OP_PASSNEXT, sizeof (int), 0 },
60 { SO_DONTROUTE,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
61 { SO_USELOOPBACK, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0
62 	},
63 { SO_BROADCAST,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
64 { SO_REUSEADDR, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
65 { SO_TYPE,	SOL_SOCKET, OA_R, OA_R, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
66 { SO_SNDBUF,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
67 { SO_RCVBUF,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
68 { SO_SNDTIMEO,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
69 	sizeof (struct timeval), 0 },
70 { SO_RCVTIMEO,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
71 	sizeof (struct timeval), 0 },
72 { SO_DGRAM_ERRIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
73 	0 },
74 { SO_RECVUCRED, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0
75 	},
76 { SO_ALLZONES, SOL_SOCKET, OA_R, OA_RW, OP_CONFIG, OP_PASSNEXT, sizeof (int),
77 	0 },
78 { SO_TIMESTAMP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0
79 	},
80 { SO_ANON_MLP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
81     0 },
82 { SO_MAC_EXEMPT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
83     0 },
84 { SCM_UCRED, SOL_SOCKET, OA_W, OA_W, OP_NP, OP_VARLEN|OP_NODEFAULT, 512, 0 },
85 { SO_EXCLBIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
86 { SO_DOMAIN,	SOL_SOCKET, OA_R, OA_R, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
87 { SO_PROTOTYPE,	SOL_SOCKET, OA_R, OA_R, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
88 
89 { IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
90 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
91 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
92 { T_IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
93 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
94 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
95 
96 { IP_TOS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
97 { T_IP_TOS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
98 { IP_TTL,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
99 { IP_RECVOPTS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
100 { IP_RECVDSTADDR, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0
101 	},
102 { IP_RECVIF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
103 { IP_RECVSLLA, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
104 { IP_RECVTTL,	IPPROTO_IP,  OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
105 	0 },
106 { IP_MULTICAST_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
107 	sizeof (struct in_addr),	0 /* INADDR_ANY */ },
108 
109 { IP_MULTICAST_LOOP, IPPROTO_IP, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_DEF_FN),
110 	sizeof (uchar_t), -1 /* not initialized */},
111 
112 { IP_MULTICAST_TTL, IPPROTO_IP, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_DEF_FN),
113 	sizeof (uchar_t), -1 /* not initialized */ },
114 
115 { IP_ADD_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
116 	sizeof (struct ip_mreq), -1 /* not initialized */ },
117 
118 { IP_DROP_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
119 	sizeof (struct ip_mreq), -1 /* not initialized */ },
120 
121 { IP_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
122 	sizeof (struct ip_mreq_source), -1 /* not initialized */ },
123 
124 { IP_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
125 	sizeof (struct ip_mreq_source), -1 /* not initialized */ },
126 
127 { IP_ADD_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
128 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct ip_mreq_source), -1 },
129 
130 { IP_DROP_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
131 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct ip_mreq_source), -1 },
132 
133 { IP_SEC_OPT, IPPROTO_IP, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
134 	sizeof (ipsec_req_t), -1 /* not initialized */ },
135 
136 { IP_BOUND_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
137 	sizeof (int),	0 /* no ifindex */ },
138 
139 { IP_DHCPINIT_IF, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, OP_PASSNEXT,
140 	sizeof (int), 0 },
141 
142 { IP_UNSPEC_SRC, IPPROTO_IP, OA_R, OA_RW, OP_RAW, OP_PASSNEXT,
143 	sizeof (int), 0 },
144 
145 { IP_BROADCAST_TTL, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0, sizeof (uchar_t),
146 	0 /* disabled */ },
147 
148 { IP_PKTINFO, IPPROTO_IP, OA_RW, OA_RW, OP_NP,
149 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
150 	sizeof (struct in_pktinfo), -1 /* not initialized */ },
151 { IP_NEXTHOP, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, OP_PASSNEXT,
152 	sizeof (in_addr_t),	-1 /* not initialized  */ },
153 
154 { MCAST_JOIN_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
155 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
156 	-1 /* not initialized */ },
157 { MCAST_LEAVE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
158 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
159 	-1 /* not initialized */ },
160 { MCAST_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
161 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
162 	-1 /* not initialized */ },
163 { MCAST_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
164 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
165 	-1 /* not initialized */ },
166 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
167 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
168 	-1 /* not initialized */ },
169 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
170 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
171 	-1 /* not initialized */ },
172 
173 { IPV6_MULTICAST_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
174 	sizeof (int), 0 },
175 
176 { IPV6_MULTICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
177 	(OP_PASSNEXT|OP_DEF_FN), sizeof (int), -1 /* not initialized */ },
178 
179 { IPV6_MULTICAST_LOOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
180 	(OP_PASSNEXT|OP_DEF_FN), sizeof (int), -1 /* not initialized */},
181 
182 { IPV6_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
183 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
184 
185 { IPV6_LEAVE_GROUP,	IPPROTO_IPV6, OA_X, OA_X, OP_NP,
186 	(OP_PASSNEXT|OP_NODEFAULT),
187 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
188 
189 { IPV6_UNICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_DEF_FN),
190 	sizeof (int), -1 /* not initialized */ },
191 
192 { IPV6_BOUND_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
193 	sizeof (int),	0 /* no ifindex */ },
194 
195 { IPV6_UNSPEC_SRC, IPPROTO_IPV6, OA_R, OA_RW, OP_RAW, OP_PASSNEXT,
196 	sizeof (int), 0 },
197 
198 { IPV6_PKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
199 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
200 	sizeof (struct in6_pktinfo), -1 /* not initialized */ },
201 { IPV6_HOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
202 	(OP_PASSNEXT|OP_NODEFAULT),
203 	sizeof (int), -1 /* not initialized */ },
204 { IPV6_NEXTHOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
205 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
206 	sizeof (sin6_t), -1 /* not initialized */ },
207 { IPV6_HOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
208 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
209 	MAX_EHDR_LEN, -1 /* not initialized */ },
210 { IPV6_DSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
211 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
212 	MAX_EHDR_LEN, -1 /* not initialized */ },
213 { IPV6_RTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
214 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
215 	MAX_EHDR_LEN, -1 /* not initialized */ },
216 { IPV6_RTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
217 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
218 	MAX_EHDR_LEN, -1 /* not initialized */ },
219 { IPV6_TCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
220 	(OP_PASSNEXT|OP_NODEFAULT),
221 	sizeof (int), -1 /* not initialized */ },
222 { IPV6_PATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
223 	(OP_PASSNEXT|OP_NODEFAULT),
224 	sizeof (int), -1 /* not initialized */ },
225 { IPV6_DONTFRAG, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
226 	sizeof (int), 0 },
227 { IPV6_USE_MIN_MTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
228 	sizeof (int), 0 },
229 { IPV6_V6ONLY, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
230 	sizeof (int), 0 },
231 
232 { IPV6_RECVPKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
233 	sizeof (int), 0 },
234 { IPV6_RECVHOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
235 	sizeof (int), 0 },
236 { IPV6_RECVHOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
237 	sizeof (int), 0 },
238 { _OLD_IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
239 	sizeof (int), 0 },
240 { IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
241 	sizeof (int), 0 },
242 { IPV6_RECVRTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
243 	sizeof (int), 0 },
244 { IPV6_RECVRTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
245 	sizeof (int), 0 },
246 { IPV6_RECVPATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
247 	OP_PASSNEXT, sizeof (int), 0 },
248 { IPV6_RECVTCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
249 	sizeof (int), 0 },
250 
251 { IPV6_SEC_OPT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
252 	sizeof (ipsec_req_t), -1 /* not initialized */ },
253 { IPV6_SRC_PREFERENCES, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
254 	sizeof (uint32_t), IPV6_PREFER_SRC_DEFAULT },
255 
256 { MCAST_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
257 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
258 	-1 /* not initialized */ },
259 { MCAST_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
260 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
261 	-1 /* not initialized */ },
262 { MCAST_BLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
263 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
264 	-1 /* not initialized */ },
265 { MCAST_UNBLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
266 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
267 	-1 /* not initialized */ },
268 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
269 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
270 	-1 /* not initialized */ },
271 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
272 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
273 	-1 /* not initialized */ },
274 
275 { UDP_ANONPRIVBIND, IPPROTO_UDP, OA_R, OA_RW, OP_PRIVPORT, OP_PASSNEXT,
276 	sizeof (int), 0 },
277 { UDP_EXCLBIND, IPPROTO_UDP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0
278 	},
279 { UDP_RCVHDR, IPPROTO_UDP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
280 	},
281 { UDP_NAT_T_ENDPOINT, IPPROTO_UDP, OA_RW, OA_RW, OP_PRIVPORT, 0, sizeof (int),
282 	0 },
283 };
284 
285 /*
286  * Table of all supported levels
287  * Note: Some levels (e.g. XTI_GENERIC) may be valid but may not have
288  * any supported options so we need this info separately.
289  *
290  * This is needed only for topmost tpi providers and is used only by
291  * XTI interfaces.
292  */
293 optlevel_t	udp_valid_levels_arr[] = {
294 	XTI_GENERIC,
295 	SOL_SOCKET,
296 	IPPROTO_UDP,
297 	IPPROTO_IP,
298 	IPPROTO_IPV6
299 };
300 
301 #define	UDP_VALID_LEVELS_CNT	A_CNT(udp_valid_levels_arr)
302 #define	UDP_OPT_ARR_CNT		A_CNT(udp_opt_arr)
303 
304 uint_t udp_max_optsize; /* initialized when UDP driver is loaded */
305 
306 /*
307  * Initialize option database object for UDP
308  *
309  * This object represents database of options to search passed to
310  * {sock,tpi}optcom_req() interface routine to take care of option
311  * management and associated methods.
312  */
313 
314 optdb_obj_t udp_opt_obj = {
315 	udp_opt_default,	/* UDP default value function pointer */
316 	udp_tpi_opt_get,	/* UDP get function pointer */
317 	udp_tpi_opt_set,	/* UDP set function pointer */
318 	B_TRUE,			/* UDP is tpi provider */
319 	UDP_OPT_ARR_CNT,	/* UDP option database count of entries */
320 	udp_opt_arr,		/* UDP option database */
321 	UDP_VALID_LEVELS_CNT,	/* UDP valid level count of entries */
322 	udp_valid_levels_arr	/* UDP valid level array */
323 };
324