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