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