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