xref: /illumos-gate/usr/src/uts/common/inet/ip/icmp_opt_data.c (revision f5fc5c043551e3eab595da7d8b5a53479207738f)
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 2008 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_DGRAM_ERRIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
85 	0 },
86 { SO_TIMESTAMP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0
87 	},
88 { SO_MAC_EXEMPT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
89 	0 },
90 
91 { SO_ALLZONES, SOL_SOCKET, OA_R, OA_RW, OP_CONFIG, OP_PASSNEXT, sizeof (int),
92 	0 },
93 { SO_DOMAIN,	SOL_SOCKET, OA_R, OA_R, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
94 
95 { IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
96 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
97 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
98 { T_IP_OPTIONS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP,
99 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
100 	IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
101 
102 { IP_HDRINCL,	IPPROTO_IP, OA_R,  OA_RW, OP_RAW, OP_PASSNEXT,
103 	sizeof (int), 0 },
104 { IP_TOS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
105 { T_IP_TOS,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
106 { IP_TTL,	IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
107 
108 { IP_MULTICAST_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
109 	sizeof (struct in_addr), 0 /* INADDR_ANY */ },
110 
111 { IP_MULTICAST_LOOP, IPPROTO_IP, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_DEF_FN),
112 	sizeof (uchar_t), -1 /* not initialized */},
113 
114 { IP_MULTICAST_TTL, IPPROTO_IP, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_DEF_FN),
115 	sizeof (uchar_t), -1 /* not initialized */ },
116 
117 { IP_ADD_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
118 	sizeof (struct ip_mreq), -1 /* not initialized */ },
119 
120 { IP_DROP_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
121 	sizeof (struct ip_mreq), 0 },
122 
123 { IP_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
124 	sizeof (struct ip_mreq_source), -1 },
125 
126 { IP_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
127 	sizeof (struct ip_mreq_source), -1 },
128 
129 { IP_ADD_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
130 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct ip_mreq_source), -1 },
131 
132 { IP_DROP_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
133 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct ip_mreq_source), -1 },
134 
135 { IP_SEC_OPT, IPPROTO_IP, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
136 	sizeof (ipsec_req_t), -1 /* not initialized */ },
137 
138 { IP_BOUND_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
139 	sizeof (int),	0 /* no ifindex */ },
140 
141 { IP_DONTFAILOVER_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
142 	sizeof (struct in_addr), 0 /* not initialized */ },
143 
144 { IP_UNSPEC_SRC, IPPROTO_IP, OA_R, OA_RW, OP_RAW, OP_PASSNEXT,
145 	sizeof (int), 0 },
146 
147 { IP_BROADCAST_TTL, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0, sizeof (uchar_t),
148 	0 /* disabled */ },
149 
150 { IP_RECVIF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int), 0 },
151 
152 { IP_PKTINFO, IPPROTO_IP, OA_RW, OA_RW, OP_NP,
153 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
154 	sizeof (struct in_pktinfo), -1 /* not initialized */ },
155 
156 { IP_NEXTHOP, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, OP_PASSNEXT,
157 	sizeof (in_addr_t), -1 /* not initialized */ },
158 
159 { MRT_INIT, IPPROTO_IP, 0, OA_X, OP_CONFIG,
160 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (int),
161 	-1 /* not initialized */ },
162 
163 { MRT_DONE, IPPROTO_IP, 0, OA_X, OP_CONFIG,
164 	(OP_PASSNEXT|OP_NODEFAULT), 0, -1 /* not initialized */ },
165 
166 { MRT_ADD_VIF, IPPROTO_IP, 0, OA_X, OP_CONFIG, (OP_PASSNEXT|OP_NODEFAULT),
167 	sizeof (struct vifctl), -1 /* not initialized */ },
168 
169 { MRT_DEL_VIF, 	IPPROTO_IP, 0, OA_X, OP_CONFIG, (OP_PASSNEXT|OP_NODEFAULT),
170 	sizeof (vifi_t), -1 /* not initialized */ },
171 
172 { MRT_ADD_MFC, 	IPPROTO_IP, 0, OA_X, OP_CONFIG, (OP_PASSNEXT|OP_NODEFAULT),
173 	sizeof (struct mfcctl), -1 /* not initialized */ },
174 
175 { MRT_DEL_MFC, 	IPPROTO_IP, 0, OA_X, OP_CONFIG, (OP_PASSNEXT|OP_NODEFAULT),
176 	sizeof (struct mfcctl), -1 /* not initialized */ },
177 
178 { MRT_VERSION, 	IPPROTO_IP, OA_R, OA_R, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
179 	sizeof (int), -1 /* not initialized */ },
180 
181 { MRT_ASSERT, 	IPPROTO_IP, 0, OA_RW, OP_CONFIG,
182 	(OP_PASSNEXT|OP_NODEFAULT),
183 	sizeof (int), -1 /* not initialized */ },
184 
185 { MCAST_JOIN_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
186 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
187 	-1 /* not initialized */ },
188 { MCAST_LEAVE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
189 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
190 	-1 /* not initialized */ },
191 { MCAST_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
192 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
193 	-1 /* not initialized */ },
194 { MCAST_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
195 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
196 	-1 /* not initialized */ },
197 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
198 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
199 	-1 /* not initialized */ },
200 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
201 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
202 	-1 /* not initialized */ },
203 
204 { IPV6_MULTICAST_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
205 	sizeof (int), 0 },
206 
207 { IPV6_MULTICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
208 	(OP_PASSNEXT|OP_DEF_FN), sizeof (int), -1 /* not initialized */ },
209 
210 { IPV6_MULTICAST_LOOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
211 	(OP_PASSNEXT|OP_DEF_FN), sizeof (int), -1 /* not initialized */},
212 
213 { IPV6_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
214 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
215 
216 { IPV6_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
217 	sizeof (struct ipv6_mreq), -1 /* not initialized */ },
218 
219 { IPV6_UNICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_DEF_FN),
220 	sizeof (int), -1 /* not initialized */ },
221 
222 { IPV6_BOUND_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
223 	sizeof (int),	0 /* no ifindex */ },
224 
225 { IPV6_BOUND_PIF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
226 	sizeof (int),	0 /* no ifindex */ },
227 
228 { IPV6_DONTFAILOVER_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
229 	sizeof (int),	0 /* no ifindex */ },
230 
231 { IPV6_UNSPEC_SRC, IPPROTO_IPV6, OA_R, OA_RW, OP_RAW, OP_PASSNEXT,
232 	sizeof (int), 0 },
233 
234 { IPV6_CHECKSUM, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT, sizeof (int),
235 	-1 },
236 
237 { ICMP6_FILTER, IPPROTO_ICMPV6, OA_RW, OA_RW, OP_NP, OP_DEF_FN|OP_VARLEN,
238 	sizeof (icmp6_filter_t), 0 },
239 { IPV6_PKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
240 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
241 	sizeof (struct in6_pktinfo), -1 /* not initialized */ },
242 { IPV6_HOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
243 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
244 	sizeof (int), -1 /* not initialized */ },
245 { IPV6_NEXTHOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
246 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
247 	sizeof (sin6_t), -1 /* not initialized */ },
248 { IPV6_HOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
249 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
250 	MAX_EHDR_LEN, -1 /* not initialized */ },
251 { IPV6_DSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
252 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
253 	MAX_EHDR_LEN, -1 /* not initialized */ },
254 { IPV6_RTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
255 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
256 	MAX_EHDR_LEN, -1 /* not initialized */ },
257 { IPV6_RTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
258 	(OP_PASSNEXT|OP_VARLEN|OP_NODEFAULT),
259 	MAX_EHDR_LEN, -1 /* not initialized */ },
260 { IPV6_TCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
261 	(OP_PASSNEXT|OP_NODEFAULT|OP_VARLEN),
262 	sizeof (int), -1 /* not initialized */ },
263 { IPV6_PATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
264 	sizeof (struct ip6_mtuinfo), -1 },
265 { IPV6_DONTFRAG, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
266 	sizeof (int), 0 },
267 { IPV6_USE_MIN_MTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
268 	sizeof (int), 0 },
269 { IPV6_V6ONLY, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
270 	sizeof (int), 0 },
271 
272 { IPV6_RECVPKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
273 	sizeof (int), 0 },
274 { IPV6_RECVHOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
275 	sizeof (int), 0 },
276 { IPV6_RECVHOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
277 	sizeof (int), 0 },
278 { _OLD_IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
279 	sizeof (int), 0 },
280 { IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
281 	sizeof (int), 0 },
282 { IPV6_RECVRTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
283 	sizeof (int), 0 },
284 { IPV6_RECVRTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
285 	sizeof (int), 0 },
286 { IPV6_RECVPATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
287 	sizeof (int), 0 },
288 { IPV6_RECVTCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
289 	sizeof (int), 0 },
290 
291 { IPV6_SEC_OPT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, (OP_PASSNEXT|OP_NODEFAULT),
292 	sizeof (ipsec_req_t), -1 /* not initialized */ },
293 { IPV6_SRC_PREFERENCES, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_PASSNEXT,
294 	sizeof (uint32_t), IPV6_PREFER_SRC_DEFAULT },
295 
296 { MCAST_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
297 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
298 	-1 /* not initialized */ },
299 { MCAST_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
300 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_req),
301 	-1 /* not initialized */ },
302 { MCAST_BLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
303 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
304 	-1 /* not initialized */ },
305 { MCAST_UNBLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
306 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
307 	-1 /* not initialized */ },
308 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
309 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
310 	-1 /* not initialized */ },
311 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
312 	(OP_PASSNEXT|OP_NODEFAULT), sizeof (struct group_source_req),
313 	-1 /* not initialized */ },
314 };
315 
316 /*
317  * Table of all supported levels
318  * Note: Some levels (e.g. XTI_GENERIC) may be valid but may not have
319  * any supported options so we need this info separately.
320  *
321  * This is needed only for topmost tpi providers and is used only by
322  * XTI interfaces.
323  */
324 optlevel_t	icmp_valid_levels_arr[] = {
325 	XTI_GENERIC,
326 	SOL_SOCKET,
327 	IPPROTO_ICMP,
328 	IPPROTO_IP,
329 	IPPROTO_IPV6,
330 	IPPROTO_ICMPV6
331 };
332 
333 #define	ICMP_VALID_LEVELS_CNT	A_CNT(icmp_valid_levels_arr)
334 #define	ICMP_OPT_ARR_CNT		A_CNT(icmp_opt_arr)
335 
336 uint_t	icmp_max_optsize; /* initialized when ICMP driver is loaded */
337 
338 /*
339  * Initialize option database object for ICMP
340  *
341  * This object represents database of options to search passed to
342  * {sock,tpi}optcom_req() interface routine to take care of option
343  * management and associated methods.
344  */
345 
346 optdb_obj_t icmp_opt_obj = {
347 	icmp_opt_default,	/* ICMP default value function pointer */
348 	icmp_tpi_opt_get,		/* ICMP get function pointer */
349 	icmp_tpi_opt_set,		/* ICMP set function pointer */
350 	B_TRUE,			/* ICMP is tpi provider */
351 	ICMP_OPT_ARR_CNT,	/* ICMP option database count of entries */
352 	icmp_opt_arr,		/* ICMP option database */
353 	ICMP_VALID_LEVELS_CNT,	/* ICMP valid level count of entries */
354 	icmp_valid_levels_arr	/* ICMP valid level array */
355 };
356