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 <netinet/in.h>
35 #include <netinet/icmp6.h>
36 #include <inet/common.h>
37 #include <netinet/ip6.h>
38 #include <inet/ip.h>
39 
40 #include <netinet/tcp.h>
41 #include <netinet/ip_mroute.h>
42 #include <inet/optcom.h>
43 #include <inet/rawip_impl.h>
44 
45 /*
46  * Table of all known options handled on a ICMP protocol stack.
47  *
48  * Note: This table contains options processed by both ICMP and IP levels
49  *       and is the superset of options that can be performed on a ICMP over IP
50  *       stack.
51  */
52 opdes_t	icmp_opt_arr[] = {
53 
54 { SO_DEBUG,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
55 { SO_DONTROUTE,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
56 { SO_USELOOPBACK, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
57 	},
58 { SO_BROADCAST,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
59 { SO_REUSEADDR, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
60 
61 #ifdef	SO_PROTOTYPE
62 	/*
63 	 * icmp will only allow IPPROTO_ICMP for non-privileged streams
64 	 * that check is made on an adhoc basis.
65 	 */
66 { SO_PROTOTYPE, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
67 #endif
68 
69 { SO_TYPE,	SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
70 { SO_SNDBUF,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
71 { SO_RCVBUF,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
72 { SO_SNDTIMEO,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0,
73 	sizeof (struct timeval), 0 },
74 { SO_RCVTIMEO,	SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0,
75 	sizeof (struct timeval), 0 },
76 { SO_DGRAM_ERRIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
77 	0 },
78 { SO_TIMESTAMP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
79 	},
80 { SO_MAC_EXEMPT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
81 	0 },
82 { SO_MAC_IMPLICIT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
83 	0 },
84 
85 { SO_ALLZONES, SOL_SOCKET, OA_R, OA_RW, OP_CONFIG, 0, sizeof (int),
86 	0 },
87 { SO_DOMAIN,	SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
88 
89 { IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
90 	(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_VARLEN|OP_NODEFAULT),
94 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
95 
96 { IP_HDRINCL,	IPPROTO_IP, OA_R,  OA_RW, OP_RAW, 0,
97 	sizeof (int), 0 },
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 
102 { IP_MULTICAST_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0,
103 	sizeof (struct in_addr), 0 /* INADDR_ANY */ },
104 
105 { IP_MULTICAST_LOOP, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
106 	sizeof (uchar_t), -1 /* not initialized */},
107 
108 { IP_MULTICAST_TTL, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
109 	sizeof (uchar_t), -1 /* not initialized */ },
110 
111 { IP_ADD_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
112 	sizeof (struct ip_mreq), -1 /* not initialized */ },
113 
114 { IP_DROP_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
115 	sizeof (struct ip_mreq), 0 },
116 
117 { IP_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
118 	sizeof (struct ip_mreq_source), -1 },
119 
120 { IP_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
121 	sizeof (struct ip_mreq_source), -1 },
122 
123 { IP_ADD_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
124 	OP_NODEFAULT, sizeof (struct ip_mreq_source), -1 },
125 
126 { IP_DROP_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
127 	OP_NODEFAULT, sizeof (struct ip_mreq_source), -1 },
128 
129 { IP_SEC_OPT, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_NODEFAULT,
130 	sizeof (ipsec_req_t), -1 /* not initialized */ },
131 
132 { IP_BOUND_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0,
133 	sizeof (int),	0 /* no ifindex */ },
134 
135 { IP_UNSPEC_SRC, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0,
136 	sizeof (int), 0 },
137 
138 { IP_BROADCAST_TTL, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0, sizeof (uchar_t),
139 	0 /* disabled */ },
140 
141 { IP_RECVIF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
142 
143 { IP_PKTINFO, IPPROTO_IP, OA_RW, OA_RW, OP_NP,
144 	(OP_NODEFAULT|OP_VARLEN),
145 	sizeof (struct in_pktinfo), -1 /* not initialized */ },
146 
147 { IP_DONTFRAG, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
148 
149 { IP_NEXTHOP, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, 0,
150 	sizeof (in_addr_t), -1 /* not initialized */ },
151 
152 { MRT_INIT, IPPROTO_IP, 0, OA_X, OP_CONFIG,
153 	OP_NODEFAULT, sizeof (int),
154 	-1 /* not initialized */ },
155 
156 { MRT_DONE, IPPROTO_IP, 0, OA_X, OP_CONFIG,
157 	OP_NODEFAULT, 0, -1 /* not initialized */ },
158 
159 { MRT_ADD_VIF, IPPROTO_IP, 0, OA_X, OP_CONFIG, OP_NODEFAULT,
160 	sizeof (struct vifctl), -1 /* not initialized */ },
161 
162 { MRT_DEL_VIF, 	IPPROTO_IP, 0, OA_X, OP_CONFIG, OP_NODEFAULT,
163 	sizeof (vifi_t), -1 /* not initialized */ },
164 
165 { MRT_ADD_MFC, 	IPPROTO_IP, 0, OA_X, OP_CONFIG, OP_NODEFAULT,
166 	sizeof (struct mfcctl), -1 /* not initialized */ },
167 
168 { MRT_DEL_MFC, 	IPPROTO_IP, 0, OA_X, OP_CONFIG, OP_NODEFAULT,
169 	sizeof (struct mfcctl), -1 /* not initialized */ },
170 
171 { MRT_VERSION, 	IPPROTO_IP, OA_R, OA_R, OP_NP, OP_NODEFAULT,
172 	sizeof (int), -1 /* not initialized */ },
173 
174 { MRT_ASSERT, 	IPPROTO_IP, 0, OA_RW, OP_CONFIG,
175 	OP_NODEFAULT,
176 	sizeof (int), -1 /* not initialized */ },
177 
178 { MCAST_JOIN_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
179 	OP_NODEFAULT, sizeof (struct group_req),
180 	-1 /* not initialized */ },
181 { MCAST_LEAVE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
182 	OP_NODEFAULT, sizeof (struct group_req),
183 	-1 /* not initialized */ },
184 { MCAST_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
185 	OP_NODEFAULT, sizeof (struct group_source_req),
186 	-1 /* not initialized */ },
187 { MCAST_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
188 	OP_NODEFAULT, sizeof (struct group_source_req),
189 	-1 /* not initialized */ },
190 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
191 	OP_NODEFAULT, sizeof (struct group_source_req),
192 	-1 /* not initialized */ },
193 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
194 	OP_NODEFAULT, sizeof (struct group_source_req),
195 	-1 /* not initialized */ },
196 
197 { IPV6_MULTICAST_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
198 	sizeof (int), 0 },
199 
200 { IPV6_MULTICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
201 	OP_DEF_FN, sizeof (int), -1 /* not initialized */ },
202 
203 { IPV6_MULTICAST_LOOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
204 	OP_DEF_FN, sizeof (int), -1 /* not initialized */},
205 
206 { IPV6_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, OP_NODEFAULT,
207 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
208 
209 { IPV6_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, OP_NODEFAULT,
210 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
211 
212 { IPV6_UNICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
213 	sizeof (int), -1 /* not initialized */ },
214 
215 { IPV6_BOUND_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
216 	sizeof (int),	0 /* no ifindex */ },
217 
218 { IPV6_UNSPEC_SRC, IPPROTO_IPV6, OA_R, OA_RW, OP_RAW, 0,
219 	sizeof (int), 0 },
220 
221 { IPV6_CHECKSUM, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
222 	-1 },
223 
224 { ICMP6_FILTER, IPPROTO_ICMPV6, OA_RW, OA_RW, OP_NP, OP_DEF_FN|OP_VARLEN,
225 	sizeof (icmp6_filter_t), 0 },
226 { IPV6_PKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
227 	(OP_NODEFAULT|OP_VARLEN),
228 	sizeof (struct in6_pktinfo), -1 /* not initialized */ },
229 { IPV6_HOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
230 	(OP_NODEFAULT|OP_VARLEN),
231 	sizeof (int), -1 /* not initialized */ },
232 { IPV6_NEXTHOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
233 	(OP_NODEFAULT|OP_VARLEN),
234 	sizeof (sin6_t), -1 /* not initialized */ },
235 { IPV6_HOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
236 	(OP_VARLEN|OP_NODEFAULT),
237 	MAX_EHDR_LEN, -1 /* not initialized */ },
238 { IPV6_DSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
239 	(OP_VARLEN|OP_NODEFAULT),
240 	MAX_EHDR_LEN, -1 /* not initialized */ },
241 { IPV6_RTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
242 	(OP_VARLEN|OP_NODEFAULT),
243 	MAX_EHDR_LEN, -1 /* not initialized */ },
244 { IPV6_RTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
245 	(OP_VARLEN|OP_NODEFAULT),
246 	MAX_EHDR_LEN, -1 /* not initialized */ },
247 { IPV6_TCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
248 	(OP_NODEFAULT|OP_VARLEN),
249 	sizeof (int), -1 /* not initialized */ },
250 { IPV6_PATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
251 	sizeof (struct ip6_mtuinfo), -1 },
252 { IPV6_DONTFRAG, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
253 	sizeof (int), 0 },
254 { IPV6_USE_MIN_MTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
255 	sizeof (int), 0 },
256 { IPV6_V6ONLY, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
257 	sizeof (int), 0 },
258 
259 { IPV6_RECVPKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
260 	sizeof (int), 0 },
261 { IPV6_RECVHOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
262 	sizeof (int), 0 },
263 { IPV6_RECVHOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
264 	sizeof (int), 0 },
265 { _OLD_IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
266 	sizeof (int), 0 },
267 { IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
268 	sizeof (int), 0 },
269 { IPV6_RECVRTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
270 	sizeof (int), 0 },
271 { IPV6_RECVRTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
272 	sizeof (int), 0 },
273 { IPV6_RECVPATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
274 	sizeof (int), 0 },
275 { IPV6_RECVTCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
276 	sizeof (int), 0 },
277 
278 { IPV6_SEC_OPT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_NODEFAULT,
279 	sizeof (ipsec_req_t), -1 /* not initialized */ },
280 { IPV6_SRC_PREFERENCES, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
281 	sizeof (uint32_t), IPV6_PREFER_SRC_DEFAULT },
282 
283 { MCAST_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
284 	OP_NODEFAULT, sizeof (struct group_req),
285 	-1 /* not initialized */ },
286 { MCAST_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
287 	OP_NODEFAULT, sizeof (struct group_req),
288 	-1 /* not initialized */ },
289 { MCAST_BLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
290 	OP_NODEFAULT, sizeof (struct group_source_req),
291 	-1 /* not initialized */ },
292 { MCAST_UNBLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
293 	OP_NODEFAULT, sizeof (struct group_source_req),
294 	-1 /* not initialized */ },
295 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
296 	OP_NODEFAULT, sizeof (struct group_source_req),
297 	-1 /* not initialized */ },
298 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
299 	OP_NODEFAULT, sizeof (struct group_source_req),
300 	-1 /* not initialized */ },
301 };
302 
303 /*
304  * Table of all supported levels
305  * Note: Some levels (e.g. XTI_GENERIC) may be valid but may not have
306  * any supported options so we need this info separately.
307  *
308  * This is needed only for topmost tpi providers and is used only by
309  * XTI interfaces.
310  */
311 optlevel_t	icmp_valid_levels_arr[] = {
312 	XTI_GENERIC,
313 	SOL_SOCKET,
314 	IPPROTO_ICMP,
315 	IPPROTO_IP,
316 	IPPROTO_IPV6,
317 	IPPROTO_ICMPV6
318 };
319 
320 #define	ICMP_VALID_LEVELS_CNT	A_CNT(icmp_valid_levels_arr)
321 #define	ICMP_OPT_ARR_CNT		A_CNT(icmp_opt_arr)
322 
323 uint_t	icmp_max_optsize; /* initialized when ICMP driver is loaded */
324 
325 /*
326  * Initialize option database object for ICMP
327  *
328  * This object represents database of options to search passed to
329  * {sock,tpi}optcom_req() interface routine to take care of option
330  * management and associated methods.
331  */
332 
333 optdb_obj_t icmp_opt_obj = {
334 	icmp_opt_default,	/* ICMP default value function pointer */
335 	icmp_tpi_opt_get,	/* ICMP get function pointer */
336 	icmp_tpi_opt_set,	/* ICMP set function pointer */
337 	ICMP_OPT_ARR_CNT,	/* ICMP option database count of entries */
338 	icmp_opt_arr,		/* ICMP option database */
339 	ICMP_VALID_LEVELS_CNT,	/* ICMP valid level count of entries */
340 	icmp_valid_levels_arr	/* ICMP valid level array */
341 };
342