xref: /titanic_44/usr/src/uts/common/inet/ip/icmp.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/stropts.h>
33 #include <sys/strlog.h>
34 #include <sys/strsun.h>
35 #define	_SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
37 #include <sys/timod.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/debug.h>
42 #include <sys/kmem.h>
43 #include <sys/policy.h>
44 #include <sys/zone.h>
45 
46 #include <sys/socket.h>
47 #include <sys/isa_defs.h>
48 #include <sys/suntpi.h>
49 #include <sys/xti_inet.h>
50 
51 #include <net/route.h>
52 #include <net/if.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/ip6.h>
56 #include <netinet/icmp6.h>
57 #include <inet/common.h>
58 #include <inet/ip.h>
59 #include <inet/ip6.h>
60 #include <inet/ip_ire.h>
61 #include <inet/mi.h>
62 #include <inet/nd.h>
63 #include <inet/optcom.h>
64 #include <inet/snmpcom.h>
65 #include <inet/kstatcom.h>
66 #include <inet/rawip_impl.h>
67 
68 #include <netinet/ip_mroute.h>
69 #include <inet/tcp.h>
70 #include <net/pfkeyv2.h>
71 #include <inet/ipsec_info.h>
72 #include <inet/ipclassifier.h>
73 
74 #define	ICMP6 "icmp6"
75 major_t	ICMP6_MAJ;
76 
77 /*
78  * Object to represent database of options to search passed to
79  * {sock,tpi}optcom_req() interface routine to take care of option
80  * management and associated methods.
81  * XXX These and other extern's should really move to a icmp header.
82  */
83 extern optdb_obj_t	icmp_opt_obj;
84 extern uint_t		icmp_max_optsize;
85 
86 /*
87  * Synchronization notes:
88  *
89  * At all points in this code where exclusive access is required, we
90  * pass a message to a subroutine by invoking qwriter(..., PERIM_OUTER)
91  * which will arrange to call the routine only after all threads have
92  * exited the shared resource.
93  */
94 
95 /* Named Dispatch Parameter Management Structure */
96 typedef struct icmpparam_s {
97 	uint_t	icmp_param_min;
98 	uint_t	icmp_param_max;
99 	uint_t	icmp_param_value;
100 	char	*icmp_param_name;
101 } icmpparam_t;
102 
103 static void	icmp_addr_req(queue_t *q, mblk_t *mp);
104 static void	icmp_bind(queue_t *q, mblk_t *mp);
105 static void	icmp_bind_proto(queue_t *q);
106 static int	icmp_build_hdrs(queue_t *q, icmp_t *icmp);
107 static void	icmp_capability_req(queue_t *q, mblk_t *mp);
108 static int	icmp_close(queue_t *q);
109 static void	icmp_connect(queue_t *q, mblk_t *mp);
110 static void	icmp_disconnect(queue_t *q, mblk_t *mp);
111 static void	icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
112 		    int sys_error);
113 static void	icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
114 		    t_scalar_t t_error, int sys_error);
115 static void	icmp_icmp_error(queue_t *q, mblk_t *mp);
116 static void	icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp);
117 static void	icmp_info_req(queue_t *q, mblk_t *mp);
118 static mblk_t	*icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim,
119 		    t_scalar_t addr_length, in_port_t);
120 static int	icmp_open(queue_t *q, dev_t *devp, int flag,
121 		    int sflag, cred_t *credp);
122 static int	icmp_unitdata_opt_process(queue_t *q, mblk_t *mp,
123 		    int *errorp, void *thisdg_attrs);
124 static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
125 int		icmp_opt_set(queue_t *q, uint_t optset_context,
126 		    int level, int name, uint_t inlen,
127 		    uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
128 		    void *thisdg_attrs, cred_t *cr, mblk_t *mblk);
129 int		icmp_opt_get(queue_t *q, int level, int name,
130 		    uchar_t *ptr);
131 static int	icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
132 static boolean_t icmp_param_register(icmpparam_t *icmppa, int cnt);
133 static int	icmp_param_set(queue_t *q, mblk_t *mp, char *value,
134 		    caddr_t cp, cred_t *cr);
135 static int	icmp_pkt_set(uchar_t *invalp, uint_t inlen, boolean_t sticky,
136 		    uchar_t **optbufp, uint_t *optlenp);
137 static void	icmp_rput(queue_t *q, mblk_t *mp);
138 static void	icmp_rput_bind_ack(queue_t *q, mblk_t *mp);
139 static int	icmp_snmp_get(queue_t *q, mblk_t *mpctl);
140 static int	icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
141 		    uchar_t *ptr, int len);
142 static int	icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp,
143 		    cred_t *cr);
144 static void	icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err);
145 static void	icmp_unbind(queue_t *q, mblk_t *mp);
146 static void	icmp_wput(queue_t *q, mblk_t *mp);
147 static void	icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6,
148 		    t_scalar_t tudr_optlen);
149 static void	icmp_wput_other(queue_t *q, mblk_t *mp);
150 static void	icmp_wput_iocdata(queue_t *q, mblk_t *mp);
151 static void	icmp_wput_restricted(queue_t *q, mblk_t *mp);
152 
153 static void	rawip_kstat_init(void);
154 static void	rawip_kstat_fini(void);
155 static int	rawip_kstat_update(kstat_t *kp, int rw);
156 
157 
158 static struct module_info info =  {
159 	5707, "icmp", 1, INFPSZ, 512, 128
160 };
161 
162 static struct qinit rinit = {
163 	(pfi_t)icmp_rput, NULL, icmp_open, icmp_close, NULL, &info
164 };
165 
166 static struct qinit winit = {
167 	(pfi_t)icmp_wput, NULL, NULL, NULL, NULL, &info
168 };
169 
170 struct streamtab icmpinfo = {
171 	&rinit, &winit
172 };
173 
174 static sin_t	sin_null;	/* Zero address for quick clears */
175 static sin6_t	sin6_null;	/* Zero address for quick clears */
176 static void	*icmp_g_head;	/* Head for list of open icmp streams. */
177 static IDP	icmp_g_nd;	/* Points to table of ICMP ND variables. */
178 
179 /* MIB-2 stuff for SNMP */
180 static mib2_rawip_t	rawip_mib;	/* SNMP fixed size info */
181 static kstat_t		*rawip_mibkp;	/* kstat exporting rawip_mib data */
182 
183 /* Default structure copied into T_INFO_ACK messages */
184 static struct T_info_ack icmp_g_t_info_ack = {
185 	T_INFO_ACK,
186 	IP_MAXPACKET,	 /* TSDU_size.  icmp allows maximum size messages. */
187 	T_INVALID,	/* ETSDU_size.  icmp does not support expedited data. */
188 	T_INVALID,	/* CDATA_size. icmp does not support connect data. */
189 	T_INVALID,	/* DDATA_size. icmp does not support disconnect data. */
190 	0,		/* ADDR_size - filled in later. */
191 	0,		/* OPT_size - not initialized here */
192 	IP_MAXPACKET,	/* TIDU_size.  icmp allows maximum size messages. */
193 	T_CLTS,		/* SERV_type.  icmp supports connection-less. */
194 	TS_UNBND,	/* CURRENT_state.  This is set from icmp_state. */
195 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
196 };
197 
198 /*
199  * Table of ND variables supported by icmp.  These are loaded into icmp_g_nd
200  * in icmp_open.
201  * All of these are alterable, within the min/max values given, at run time.
202  */
203 static icmpparam_t	icmp_param_arr[] = {
204 	/* min	max	value	name */
205 	{ 0,	128,	32,	"icmp_wroff_extra" },
206 	{ 1,	255,	255,	"icmp_ipv4_ttl" },
207 	{ 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS,	"icmp_ipv6_hoplimit"},
208 	{ 0,	1,	1,	"icmp_bsd_compat" },
209 	{ 4096,	65536,	8192,	"icmp_xmit_hiwat"},
210 	{ 0,	65536,	1024,	"icmp_xmit_lowat"},
211 	{ 4096,	65536,	8192,	"icmp_recv_hiwat"},
212 	{ 65536, 1024*1024*1024, 256*1024,	"icmp_max_buf"},
213 };
214 #define	icmp_wroff_extra		icmp_param_arr[0].icmp_param_value
215 #define	icmp_ipv4_ttl			icmp_param_arr[1].icmp_param_value
216 #define	icmp_ipv6_hoplimit		icmp_param_arr[2].icmp_param_value
217 #define	icmp_bsd_compat			icmp_param_arr[3].icmp_param_value
218 #define	icmp_xmit_hiwat			icmp_param_arr[4].icmp_param_value
219 #define	icmp_xmit_lowat			icmp_param_arr[5].icmp_param_value
220 #define	icmp_recv_hiwat			icmp_param_arr[6].icmp_param_value
221 #define	icmp_max_buf			icmp_param_arr[7].icmp_param_value
222 
223 /*
224  * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
225  * passed to icmp_wput.
226  * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP
227  * protocol type placed in the message following the address. A T_BIND_ACK
228  * message is passed upstream when ip acknowledges the request.
229  * (Called as writer.)
230  */
231 static void
232 icmp_bind(queue_t *q, mblk_t *mp)
233 {
234 	sin_t	*sin;
235 	sin6_t	*sin6;
236 	mblk_t	*mp1;
237 	struct T_bind_req	*tbr;
238 	icmp_t	*icmp;
239 
240 	icmp = (icmp_t *)q->q_ptr;
241 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
242 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
243 		    "icmp_bind: bad req, len %u",
244 		    (uint_t)(mp->b_wptr - mp->b_rptr));
245 		icmp_err_ack(q, mp, TPROTO, 0);
246 		return;
247 	}
248 	if (icmp->icmp_state != TS_UNBND) {
249 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
250 		    "icmp_bind: bad state, %d", icmp->icmp_state);
251 		icmp_err_ack(q, mp, TOUTSTATE, 0);
252 		return;
253 	}
254 	/*
255 	 * Reallocate the message to make sure we have enough room for an
256 	 * address and the protocol type.
257 	 */
258 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1);
259 	if (!mp1) {
260 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
261 		return;
262 	}
263 	mp = mp1;
264 	tbr = (struct T_bind_req *)mp->b_rptr;
265 	switch (tbr->ADDR_length) {
266 	case 0:			/* Generic request */
267 		tbr->ADDR_offset = sizeof (struct T_bind_req);
268 		if (icmp->icmp_family == AF_INET) {
269 			tbr->ADDR_length = sizeof (sin_t);
270 			sin = (sin_t *)&tbr[1];
271 			*sin = sin_null;
272 			sin->sin_family = AF_INET;
273 			mp->b_wptr = (uchar_t *)&sin[1];
274 		} else {
275 			ASSERT(icmp->icmp_family == AF_INET6);
276 			tbr->ADDR_length = sizeof (sin6_t);
277 			sin6 = (sin6_t *)&tbr[1];
278 			*sin6 = sin6_null;
279 			sin6->sin6_family = AF_INET6;
280 			mp->b_wptr = (uchar_t *)&sin6[1];
281 		}
282 		break;
283 	case sizeof (sin_t):	/* Complete IP address */
284 		sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset,
285 		    sizeof (sin_t));
286 		if (sin == NULL || !OK_32PTR((char *)sin)) {
287 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
288 			return;
289 		}
290 		if (icmp->icmp_family != AF_INET ||
291 		    sin->sin_family != AF_INET) {
292 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
293 			return;
294 		}
295 		break;
296 	case sizeof (sin6_t):	/* Complete IP address */
297 		sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset,
298 		    sizeof (sin6_t));
299 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
300 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
301 			return;
302 		}
303 		if (icmp->icmp_family != AF_INET6 ||
304 		    sin6->sin6_family != AF_INET6) {
305 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
306 			return;
307 		}
308 		/* No support for mapped addresses on raw sockets */
309 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
310 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
311 			return;
312 		}
313 		break;
314 	default:
315 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
316 		    "icmp_bind: bad ADDR_length %d", tbr->ADDR_length);
317 		icmp_err_ack(q, mp, TBADADDR, 0);
318 		return;
319 	}
320 	/*
321 	 * Copy the source address into our icmp structure.  This address
322 	 * may still be zero; if so, ip will fill in the correct address
323 	 * each time an outbound packet is passed to it.
324 	 * If we are binding to a broadcast or multicast address icmp_rput
325 	 * will clear the source address when it receives the T_BIND_ACK.
326 	 */
327 	icmp->icmp_state = TS_IDLE;
328 
329 	if (icmp->icmp_family == AF_INET) {
330 		ASSERT(sin != NULL);
331 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
332 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr,
333 		    &icmp->icmp_v6src);
334 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
335 		    icmp->icmp_ip_snd_options_len;
336 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
337 	} else {
338 		int error;
339 
340 		ASSERT(sin6 != NULL);
341 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
342 		icmp->icmp_v6src = sin6->sin6_addr;
343 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
344 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
345 
346 		/* Rebuild the header template */
347 		error = icmp_build_hdrs(q, icmp);
348 		if (error != 0) {
349 			icmp_err_ack(q, mp, TSYSERR, error);
350 			return;
351 		}
352 	}
353 	/*
354 	 * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following
355 	 * the address.
356 	 */
357 	*mp->b_wptr++ = icmp->icmp_proto;
358 	if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) {
359 		/*
360 		 * Append a request for an IRE if src not 0 (INADDR_ANY)
361 		 */
362 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
363 		if (!mp->b_cont) {
364 			icmp_err_ack(q, mp, TSYSERR, ENOMEM);
365 			return;
366 		}
367 		mp->b_cont->b_wptr += sizeof (ire_t);
368 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
369 	}
370 
371 	/* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */
372 	putnext(q, mp);
373 }
374 
375 /*
376  * Send message to IP to just bind to the protocol.
377  */
378 static void
379 icmp_bind_proto(queue_t *q)
380 {
381 	mblk_t	*mp;
382 	struct T_bind_req	*tbr;
383 	icmp_t	*icmp;
384 
385 	icmp = (icmp_t *)q->q_ptr;
386 	mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1,
387 	    BPRI_MED);
388 	if (!mp) {
389 		return;
390 	}
391 	mp->b_datap->db_type = M_PROTO;
392 	tbr = (struct T_bind_req *)mp->b_rptr;
393 	tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */
394 	tbr->ADDR_offset = sizeof (struct T_bind_req);
395 	if (icmp->icmp_ipversion == IPV4_VERSION) {
396 		sin_t	*sin;
397 
398 		tbr->ADDR_length = sizeof (sin_t);
399 		sin = (sin_t *)&tbr[1];
400 		*sin = sin_null;
401 		sin->sin_family = AF_INET;
402 		mp->b_wptr = (uchar_t *)&sin[1];
403 	} else {
404 		sin6_t	*sin6;
405 
406 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
407 		tbr->ADDR_length = sizeof (sin6_t);
408 		sin6 = (sin6_t *)&tbr[1];
409 		*sin6 = sin6_null;
410 		sin6->sin6_family = AF_INET6;
411 		mp->b_wptr = (uchar_t *)&sin6[1];
412 	}
413 
414 	/* Place protocol type in the O_T_BIND_REQ following the address. */
415 	*mp->b_wptr++ = icmp->icmp_proto;
416 
417 	/* Pass the O_T_BIND_REQ to ip. */
418 	putnext(q, mp);
419 }
420 
421 /*
422  * This routine handles each T_CONN_REQ message passed to icmp.  It
423  * associates a default destination address with the stream.
424  *
425  * This routine sends down a T_BIND_REQ to IP with the following mblks:
426  *	T_BIND_REQ	- specifying local and remote address.
427  *	IRE_DB_REQ_TYPE	- to get an IRE back containing ire_type and src
428  *	T_OK_ACK	- for the T_CONN_REQ
429  *	T_CONN_CON	- to keep the TPI user happy
430  *
431  * The connect completes in icmp_rput.
432  * When a T_BIND_ACK is received information is extracted from the IRE
433  * and the two appended messages are sent to the TPI user.
434  * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
435  * it to an error ack for the appropriate primitive.
436  */
437 static void
438 icmp_connect(queue_t *q, mblk_t *mp)
439 {
440 	sin_t	*sin;
441 	sin6_t	*sin6;
442 	mblk_t	*mp1, *mp2;
443 	struct T_conn_req	*tcr;
444 	icmp_t	*icmp;
445 	ipaddr_t	v4dst;
446 	in6_addr_t	v6dst;
447 	uint32_t	flowinfo;
448 
449 	icmp = (icmp_t *)q->q_ptr;
450 	tcr = (struct T_conn_req *)mp->b_rptr;
451 	/* Sanity checks */
452 	if ((mp->b_wptr - mp->b_rptr < sizeof (struct T_conn_req))) {
453 		icmp_err_ack(q, mp, TPROTO, 0);
454 		return;
455 	}
456 
457 	if (icmp->icmp_state == TS_DATA_XFER) {
458 		/* Already connected - clear out state */
459 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
460 		icmp->icmp_state = TS_IDLE;
461 	}
462 
463 
464 	if (tcr->OPT_length != 0) {
465 		icmp_err_ack(q, mp, TBADOPT, 0);
466 		return;
467 	}
468 	switch (tcr->DEST_length) {
469 	default:
470 		icmp_err_ack(q, mp, TBADADDR, 0);
471 		return;
472 
473 	case sizeof (sin_t):
474 		sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset,
475 		    sizeof (sin_t));
476 		if (sin == NULL || !OK_32PTR((char *)sin)) {
477 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
478 			return;
479 		}
480 		if (icmp->icmp_family != AF_INET ||
481 		    sin->sin_family != AF_INET) {
482 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
483 			return;
484 		}
485 		v4dst = sin->sin_addr.s_addr;
486 		IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
487 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
488 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
489 		    icmp->icmp_ip_snd_options_len;
490 		break;
491 
492 	case sizeof (sin6_t):
493 		sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset,
494 		    sizeof (sin6_t));
495 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
496 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
497 			return;
498 		}
499 		if (icmp->icmp_family != AF_INET6 ||
500 		    sin6->sin6_family != AF_INET6) {
501 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
502 			return;
503 		}
504 		/* No support for mapped addresses on raw sockets */
505 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
506 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
507 			return;
508 		}
509 		v6dst = sin6->sin6_addr;
510 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
511 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
512 		flowinfo = sin6->sin6_flowinfo;
513 		break;
514 	}
515 	if (icmp->icmp_ipversion == IPV4_VERSION) {
516 		/*
517 		 * Interpret a zero destination to mean loopback.
518 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
519 		 * generate the T_CONN_CON.
520 		 */
521 		if (v4dst == INADDR_ANY) {
522 			v4dst = htonl(INADDR_LOOPBACK);
523 			IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
524 			if (icmp->icmp_family == AF_INET) {
525 				sin->sin_addr.s_addr = v4dst;
526 			} else {
527 				sin6->sin6_addr = v6dst;
528 			}
529 		}
530 		icmp->icmp_v6dst = v6dst;
531 		icmp->icmp_flowinfo = 0;
532 
533 		/*
534 		 * If the destination address is multicast and
535 		 * an outgoing multicast interface has been set,
536 		 * use the address of that interface as our
537 		 * source address if no source address has been set.
538 		 */
539 		if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY &&
540 		    CLASSD(v4dst) &&
541 		    icmp->icmp_multicast_if_addr != INADDR_ANY) {
542 			IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr,
543 			    &icmp->icmp_v6src);
544 		}
545 	} else {
546 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
547 		/*
548 		 * Interpret a zero destination to mean loopback.
549 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
550 		 * generate the T_CONN_CON.
551 		 */
552 		if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
553 			v6dst = ipv6_loopback;
554 			sin6->sin6_addr = v6dst;
555 		}
556 		icmp->icmp_v6dst = v6dst;
557 		icmp->icmp_flowinfo = flowinfo;
558 		/*
559 		 * If the destination address is multicast and
560 		 * an outgoing multicast interface has been set,
561 		 * then the ip bind logic will pick the correct source
562 		 * address (i.e. matching the outgoing multicast interface).
563 		 */
564 	}
565 
566 	/*
567 	 * Send down bind to IP to verify that there is a route
568 	 * and to determine the source address.
569 	 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput.
570 	 */
571 	if (icmp->icmp_family == AF_INET) {
572 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t),
573 		    sin->sin_port);
574 	} else {
575 		ASSERT(icmp->icmp_family == AF_INET6);
576 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t),
577 		    sin6->sin6_port);
578 	}
579 	if (mp1 == NULL) {
580 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
581 		return;
582 	}
583 
584 	/*
585 	 * We also have to send a connection confirmation to
586 	 * keep TLI happy. Prepare it for icmp_rput.
587 	 */
588 	if (icmp->icmp_family == AF_INET) {
589 		mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL,
590 		    0);
591 	} else {
592 		ASSERT(icmp->icmp_family == AF_INET6);
593 		mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL,
594 		    0);
595 	}
596 	if (mp2 == NULL) {
597 		freemsg(mp1);
598 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
599 		return;
600 	}
601 
602 	mp = mi_tpi_ok_ack_alloc(mp);
603 	if (mp == NULL) {
604 		/* Unable to reuse the T_CONN_REQ for the ack. */
605 		freemsg(mp2);
606 		icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
607 		return;
608 	}
609 
610 	icmp->icmp_state = TS_DATA_XFER;
611 
612 	/* Hang onto the T_OK_ACK and T_CONN_CON for later. */
613 	linkb(mp1, mp);
614 	linkb(mp1, mp2);
615 
616 	putnext(q, mp1);
617 }
618 
619 static int
620 icmp_close(queue_t *q)
621 {
622 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
623 	int	i1;
624 
625 	qprocsoff(q);
626 
627 	/* If there are any options associated with the stream, free them. */
628 	if (icmp->icmp_ip_snd_options)
629 		mi_free((char *)icmp->icmp_ip_snd_options);
630 
631 	if (icmp->icmp_filter != NULL)
632 		kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
633 
634 	/* Free memory associated with sticky options */
635 	if (icmp->icmp_sticky_hdrs_len != 0) {
636 		kmem_free(icmp->icmp_sticky_hdrs,
637 		    icmp->icmp_sticky_hdrs_len);
638 		icmp->icmp_sticky_hdrs = NULL;
639 		icmp->icmp_sticky_hdrs_len = 0;
640 	}
641 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
642 		kmem_free(icmp->icmp_sticky_ipp.ipp_hopopts,
643 		    icmp->icmp_sticky_ipp.ipp_hopoptslen);
644 	}
645 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
646 		kmem_free(icmp->icmp_sticky_ipp.ipp_rtdstopts,
647 		    icmp->icmp_sticky_ipp.ipp_rtdstoptslen);
648 	}
649 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
650 		kmem_free(icmp->icmp_sticky_ipp.ipp_rthdr,
651 		    icmp->icmp_sticky_ipp.ipp_rthdrlen);
652 	}
653 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
654 		kmem_free(icmp->icmp_sticky_ipp.ipp_dstopts,
655 		    icmp->icmp_sticky_ipp.ipp_dstoptslen);
656 	}
657 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_PATHMTU) {
658 		kmem_free(icmp->icmp_sticky_ipp.ipp_pathmtu,
659 		    icmp->icmp_sticky_ipp.ipp_pathmtulen);
660 	}
661 	icmp->icmp_sticky_ipp.ipp_fields &=
662 	    ~(IPPF_HOPOPTS|IPPF_RTDSTOPTS|IPPF_RTHDR|IPPF_DSTOPTS);
663 
664 	crfree(icmp->icmp_credp);
665 
666 	/* Free the icmp structure and release the minor device number. */
667 	i1 = mi_close_comm(&icmp_g_head, q);
668 
669 	return (i1);
670 }
671 
672 /*
673  * This routine handles each T_DISCON_REQ message passed to icmp
674  * as an indicating that ICMP is no longer connected. This results
675  * in sending a T_BIND_REQ to IP to restore the binding to just
676  * the local address.
677  *
678  * This routine sends down a T_BIND_REQ to IP with the following mblks:
679  *	T_BIND_REQ	- specifying just the local address.
680  *	T_OK_ACK	- for the T_DISCON_REQ
681  *
682  * The disconnect completes in icmp_rput.
683  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
684  * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
685  * it to an error ack for the appropriate primitive.
686  */
687 static void
688 icmp_disconnect(queue_t *q, mblk_t *mp)
689 {
690 	icmp_t	*icmp;
691 	mblk_t	*mp1;
692 
693 	icmp = (icmp_t *)q->q_ptr;
694 
695 	if (icmp->icmp_state != TS_DATA_XFER) {
696 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
697 		    "icmp_disconnect: bad state, %d", icmp->icmp_state);
698 		icmp_err_ack(q, mp, TOUTSTATE, 0);
699 		return;
700 	}
701 	icmp->icmp_v6src = icmp->icmp_bound_v6src;
702 	icmp->icmp_state = TS_IDLE;
703 
704 	/*
705 	 * Send down bind to IP to remove the full binding and revert
706 	 * to the local address binding.
707 	 */
708 	if (icmp->icmp_family == AF_INET) {
709 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0);
710 	} else {
711 		ASSERT(icmp->icmp_family == AF_INET6);
712 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0);
713 	}
714 	if (mp1 == NULL) {
715 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
716 		return;
717 	}
718 	mp = mi_tpi_ok_ack_alloc(mp);
719 	if (mp == NULL) {
720 		/* Unable to reuse the T_DISCON_REQ for the ack. */
721 		icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
722 		return;
723 	}
724 
725 	if (icmp->icmp_family == AF_INET6) {
726 		int error;
727 
728 		/* Rebuild the header template */
729 		error = icmp_build_hdrs(q, icmp);
730 		if (error != 0) {
731 			icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
732 			freemsg(mp1);
733 			return;
734 		}
735 	}
736 	icmp->icmp_discon_pending = 1;
737 
738 	/* Append the T_OK_ACK to the T_BIND_REQ for icmp_rput */
739 	linkb(mp1, mp);
740 	putnext(q, mp1);
741 }
742 
743 /* This routine creates a T_ERROR_ACK message and passes it upstream. */
744 static void
745 icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
746 {
747 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
748 		qreply(q, mp);
749 }
750 
751 /* Shorthand to generate and send TPI error acks to our client */
752 static void
753 icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
754     t_scalar_t t_error, int sys_error)
755 {
756 	struct T_error_ack	*teackp;
757 
758 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
759 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
760 		teackp = (struct T_error_ack *)mp->b_rptr;
761 		teackp->ERROR_prim = primitive;
762 		teackp->TLI_error = t_error;
763 		teackp->UNIX_error = sys_error;
764 		qreply(q, mp);
765 	}
766 }
767 
768 /*
769  * icmp_icmp_error is called by icmp_rput to process ICMP
770  * messages passed up by IP.
771  * Generates the appropriate T_UDERROR_IND for permanent
772  * (non-transient) errors.
773  * Assumes that IP has pulled up everything up to and including
774  * the ICMP header.
775  */
776 static void
777 icmp_icmp_error(queue_t *q, mblk_t *mp)
778 {
779 	icmph_t *icmph;
780 	ipha_t	*ipha;
781 	int	iph_hdr_length;
782 	sin_t	sin;
783 	sin6_t	sin6;
784 	mblk_t	*mp1;
785 	int	error = 0;
786 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
787 
788 	/*
789 	 * Deliver T_UDERROR_IND when the application has asked for it.
790 	 * The socket layer enables this automatically when connected.
791 	 */
792 	if (!icmp->icmp_dgram_errind) {
793 		freemsg(mp);
794 		return;
795 	}
796 
797 	ipha = (ipha_t *)mp->b_rptr;
798 
799 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
800 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
801 		icmp_icmp_error_ipv6(q, mp);
802 		return;
803 	}
804 	ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
805 
806 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
807 	icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]);
808 	ipha = (ipha_t *)&icmph[1];
809 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
810 
811 	switch (icmph->icmph_type) {
812 	case ICMP_DEST_UNREACHABLE:
813 		switch (icmph->icmph_code) {
814 		case ICMP_FRAGMENTATION_NEEDED:
815 			/*
816 			 * IP has already adjusted the path MTU.
817 			 * XXX Somehow pass MTU indication to application?
818 			 */
819 			break;
820 		case ICMP_PORT_UNREACHABLE:
821 		case ICMP_PROTOCOL_UNREACHABLE:
822 			error = ECONNREFUSED;
823 			break;
824 		default:
825 			/* Transient errors */
826 			break;
827 		}
828 		break;
829 	default:
830 		/* Transient errors */
831 		break;
832 	}
833 	if (error == 0) {
834 		freemsg(mp);
835 		return;
836 	}
837 
838 	switch (icmp->icmp_family) {
839 	case AF_INET:
840 		sin = sin_null;
841 		sin.sin_family = AF_INET;
842 		sin.sin_addr.s_addr = ipha->ipha_dst;
843 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
844 		    error);
845 		break;
846 	case AF_INET6:
847 		sin6 = sin6_null;
848 		sin6.sin6_family = AF_INET6;
849 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
850 
851 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
852 		    NULL, 0, error);
853 		break;
854 	}
855 	if (mp1)
856 		putnext(q, mp1);
857 	freemsg(mp);
858 }
859 
860 /*
861  * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6
862  * for IPv6 packets.
863  * Send permanent (non-transient) errors upstream.
864  * Assumes that IP has pulled up all the extension headers as well
865  * as the ICMPv6 header.
866  */
867 static void
868 icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
869 {
870 	icmp6_t		*icmp6;
871 	ip6_t		*ip6h, *outer_ip6h;
872 	uint16_t	iph_hdr_length;
873 	uint8_t		*nexthdrp;
874 	sin6_t		sin6;
875 	mblk_t		*mp1;
876 	int		error = 0;
877 	icmp_t		*icmp = (icmp_t *)q->q_ptr;
878 
879 	outer_ip6h = (ip6_t *)mp->b_rptr;
880 	if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
881 		iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
882 	else
883 		iph_hdr_length = IPV6_HDR_LEN;
884 
885 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
886 	ip6h = (ip6_t *)&icmp6[1];
887 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
888 		freemsg(mp);
889 		return;
890 	}
891 	if (*nexthdrp != icmp->icmp_proto) {
892 		/*
893 		 * Could have switched icmp_proto after while ip did fanout of
894 		 * this message
895 		 */
896 		freemsg(mp);
897 		return;
898 	}
899 	switch (icmp6->icmp6_type) {
900 	case ICMP6_DST_UNREACH:
901 		switch (icmp6->icmp6_code) {
902 		case ICMP6_DST_UNREACH_NOPORT:
903 			error = ECONNREFUSED;
904 			break;
905 		case ICMP6_DST_UNREACH_ADMIN:
906 		case ICMP6_DST_UNREACH_NOROUTE:
907 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
908 		case ICMP6_DST_UNREACH_ADDR:
909 			/* Transient errors */
910 			break;
911 		default:
912 			break;
913 		}
914 		break;
915 	case ICMP6_PACKET_TOO_BIG: {
916 		struct T_unitdata_ind	*tudi;
917 		struct T_opthdr		*toh;
918 		size_t			udi_size;
919 		mblk_t			*newmp;
920 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
921 		    sizeof (struct ip6_mtuinfo);
922 		sin6_t			*sin6;
923 		struct ip6_mtuinfo	*mtuinfo;
924 
925 		/*
926 		 * If the application has requested to receive path mtu
927 		 * information, send up an empty message containing an
928 		 * IPV6_PATHMTU ancillary data item.
929 		 */
930 		if (!icmp->icmp_ipv6_recvpathmtu)
931 			break;
932 
933 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
934 		    opt_length;
935 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
936 			BUMP_MIB(&rawip_mib, rawipInErrors);
937 			break;
938 		}
939 
940 		/*
941 		 * newmp->b_cont is left to NULL on purpose.  This is an
942 		 * empty message containing only ancillary data.
943 		 */
944 		newmp->b_datap->db_type = M_PROTO;
945 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
946 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
947 		tudi->PRIM_type = T_UNITDATA_IND;
948 		tudi->SRC_length = sizeof (sin6_t);
949 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
950 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
951 		tudi->OPT_length = opt_length;
952 
953 		sin6 = (sin6_t *)&tudi[1];
954 		bzero(sin6, sizeof (sin6_t));
955 		sin6->sin6_family = AF_INET6;
956 		sin6->sin6_addr = icmp->icmp_v6dst;
957 
958 		toh = (struct T_opthdr *)&sin6[1];
959 		toh->level = IPPROTO_IPV6;
960 		toh->name = IPV6_PATHMTU;
961 		toh->len = opt_length;
962 		toh->status = 0;
963 
964 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
965 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
966 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
967 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
968 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
969 		/*
970 		 * We've consumed everything we need from the original
971 		 * message.  Free it, then send our empty message.
972 		 */
973 		freemsg(mp);
974 		putnext(q, newmp);
975 		return;
976 	}
977 	case ICMP6_TIME_EXCEEDED:
978 		/* Transient errors */
979 		break;
980 	case ICMP6_PARAM_PROB:
981 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
982 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
983 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
984 		    (uchar_t *)nexthdrp) {
985 			error = ECONNREFUSED;
986 			break;
987 		}
988 		break;
989 	}
990 	if (error == 0) {
991 		freemsg(mp);
992 		return;
993 	}
994 
995 	sin6 = sin6_null;
996 	sin6.sin6_family = AF_INET6;
997 	sin6.sin6_addr = ip6h->ip6_dst;
998 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
999 
1000 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
1001 	    error);
1002 	if (mp1)
1003 		putnext(q, mp1);
1004 	freemsg(mp);
1005 }
1006 
1007 /*
1008  * This routine responds to T_ADDR_REQ messages.  It is called by icmp_wput.
1009  * The local address is filled in if endpoint is bound. The remote address
1010  * is filled in if remote address has been precified ("connected endpoint")
1011  * (The concept of connected CLTS sockets is alien to published TPI
1012  *  but we support it anyway).
1013  */
1014 static void
1015 icmp_addr_req(queue_t *q, mblk_t *mp)
1016 {
1017 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
1018 	mblk_t	*ackmp;
1019 	struct T_addr_ack *taa;
1020 
1021 	/* Make it large enough for worst case */
1022 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
1023 	    2 * sizeof (sin6_t), 1);
1024 	if (ackmp == NULL) {
1025 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
1026 		return;
1027 	}
1028 	taa = (struct T_addr_ack *)ackmp->b_rptr;
1029 
1030 	bzero(taa, sizeof (struct T_addr_ack));
1031 	ackmp->b_wptr = (uchar_t *)&taa[1];
1032 
1033 	taa->PRIM_type = T_ADDR_ACK;
1034 	ackmp->b_datap->db_type = M_PCPROTO;
1035 
1036 	/*
1037 	 * Note: Following code assumes 32 bit alignment of basic
1038 	 * data structures like sin_t and struct T_addr_ack.
1039 	 */
1040 	if (icmp->icmp_state != TS_UNBND) {
1041 		/*
1042 		 * Fill in local address
1043 		 */
1044 		taa->LOCADDR_offset = sizeof (*taa);
1045 		if (icmp->icmp_family == AF_INET) {
1046 			sin_t	*sin;
1047 
1048 			taa->LOCADDR_length = sizeof (sin_t);
1049 			sin = (sin_t *)&taa[1];
1050 			/* Fill zeroes and then intialize non-zero fields */
1051 			*sin = sin_null;
1052 			sin->sin_family = AF_INET;
1053 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
1054 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
1055 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src,
1056 				    sin->sin_addr.s_addr);
1057 			} else {
1058 				/*
1059 				 * INADDR_ANY
1060 				 * icmp_v6src is not set, we might be bound to
1061 				 * broadcast/multicast. Use icmp_bound_v6src as
1062 				 * local address instead (that could
1063 				 * also still be INADDR_ANY)
1064 				 */
1065 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src,
1066 				    sin->sin_addr.s_addr);
1067 			}
1068 			ackmp->b_wptr = (uchar_t *)&sin[1];
1069 		} else {
1070 			sin6_t	*sin6;
1071 
1072 			ASSERT(icmp->icmp_family == AF_INET6);
1073 			taa->LOCADDR_length = sizeof (sin6_t);
1074 			sin6 = (sin6_t *)&taa[1];
1075 			/* Fill zeroes and then intialize non-zero fields */
1076 			*sin6 = sin6_null;
1077 			sin6->sin6_family = AF_INET6;
1078 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
1079 				sin6->sin6_addr = icmp->icmp_v6src;
1080 			} else {
1081 				/*
1082 				 * UNSPECIFIED
1083 				 * icmp_v6src is not set, we might be bound to
1084 				 * broadcast/multicast. Use icmp_bound_v6src as
1085 				 * local address instead (that could
1086 				 * also still be UNSPECIFIED)
1087 				 */
1088 				sin6->sin6_addr = icmp->icmp_bound_v6src;
1089 			}
1090 			ackmp->b_wptr = (uchar_t *)&sin6[1];
1091 		}
1092 	}
1093 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
1094 	qreply(q, ackmp);
1095 }
1096 
1097 static void
1098 icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
1099 {
1100 	*tap = icmp_g_t_info_ack;
1101 
1102 	if (icmp->icmp_family == AF_INET6)
1103 		tap->ADDR_size = sizeof (sin6_t);
1104 	else
1105 		tap->ADDR_size = sizeof (sin_t);
1106 	tap->CURRENT_state = icmp->icmp_state;
1107 	tap->OPT_size = icmp_max_optsize;
1108 }
1109 
1110 /*
1111  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
1112  * icmp_wput.  Much of the T_CAPABILITY_ACK information is copied from
1113  * icmp_g_t_info_ack.  The current state of the stream is copied from
1114  * icmp_state.
1115  */
1116 static void
1117 icmp_capability_req(queue_t *q, mblk_t *mp)
1118 {
1119 	icmp_t			*icmp = (icmp_t *)q->q_ptr;
1120 	t_uscalar_t		cap_bits1;
1121 	struct T_capability_ack	*tcap;
1122 
1123 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
1124 
1125 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
1126 		mp->b_datap->db_type, T_CAPABILITY_ACK);
1127 	if (!mp)
1128 		return;
1129 
1130 	tcap = (struct T_capability_ack *)mp->b_rptr;
1131 	tcap->CAP_bits1 = 0;
1132 
1133 	if (cap_bits1 & TC1_INFO) {
1134 		icmp_copy_info(&tcap->INFO_ack, icmp);
1135 		tcap->CAP_bits1 |= TC1_INFO;
1136 	}
1137 
1138 	qreply(q, mp);
1139 }
1140 
1141 /*
1142  * This routine responds to T_INFO_REQ messages.  It is called by icmp_wput.
1143  * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
1144  * The current state of the stream is copied from icmp_state.
1145  */
1146 static void
1147 icmp_info_req(queue_t *q, mblk_t *mp)
1148 {
1149 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
1150 
1151 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
1152 	    T_INFO_ACK);
1153 	if (!mp)
1154 		return;
1155 	icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
1156 	qreply(q, mp);
1157 }
1158 
1159 /*
1160  * IP recognizes seven kinds of bind requests:
1161  *
1162  * - A zero-length address binds only to the protocol number.
1163  *
1164  * - A 4-byte address is treated as a request to
1165  * validate that the address is a valid local IPv4
1166  * address, appropriate for an application to bind to.
1167  * IP does the verification, but does not make any note
1168  * of the address at this time.
1169  *
1170  * - A 16-byte address contains is treated as a request
1171  * to validate a local IPv6 address, as the 4-byte
1172  * address case above.
1173  *
1174  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
1175  * use it for the inbound fanout of packets.
1176  *
1177  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
1178  * use it for the inbound fanout of packets.
1179  *
1180  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
1181  * information consisting of local and remote addresses
1182  * and ports (unused for raw sockets).  In this case, the addresses are both
1183  * validated as appropriate for this operation, and, if
1184  * so, the information is retained for use in the
1185  * inbound fanout.
1186  *
1187  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
1188  * fanout information, like the 12-byte case above.
1189  *
1190  * IP will also fill in the IRE request mblk with information
1191  * regarding our peer.  In all cases, we notify IP of our protocol
1192  * type by appending a single protocol byte to the bind request.
1193  */
1194 static mblk_t *
1195 icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length,
1196     in_port_t fport)
1197 {
1198 	char	*cp;
1199 	mblk_t	*mp;
1200 	struct T_bind_req *tbr;
1201 	ipa_conn_t	*ac;
1202 	ipa6_conn_t	*ac6;
1203 	sin_t		*sin;
1204 	sin6_t		*sin6;
1205 
1206 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
1207 
1208 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
1209 	if (mp == NULL)
1210 		return (NULL);
1211 	mp->b_datap->db_type = M_PROTO;
1212 	tbr = (struct T_bind_req *)mp->b_rptr;
1213 	tbr->PRIM_type = bind_prim;
1214 	tbr->ADDR_offset = sizeof (*tbr);
1215 	tbr->CONIND_number = 0;
1216 	tbr->ADDR_length = addr_length;
1217 	cp = (char *)&tbr[1];
1218 	switch (addr_length) {
1219 	case sizeof (ipa_conn_t):
1220 		ASSERT(icmp->icmp_family == AF_INET);
1221 		/* Append a request for an IRE */
1222 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
1223 		if (mp->b_cont == NULL) {
1224 			freemsg(mp);
1225 			return (NULL);
1226 		}
1227 		mp->b_cont->b_wptr += sizeof (ire_t);
1228 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
1229 
1230 		/* cp known to be 32 bit aligned */
1231 		ac = (ipa_conn_t *)cp;
1232 		ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src);
1233 		ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst);
1234 		ac->ac_fport = fport;
1235 		ac->ac_lport = 0;
1236 		break;
1237 
1238 	case sizeof (ipa6_conn_t):
1239 		ASSERT(icmp->icmp_family == AF_INET6);
1240 		/* Append a request for an IRE */
1241 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
1242 		if (mp->b_cont == NULL) {
1243 			freemsg(mp);
1244 			return (NULL);
1245 		}
1246 		mp->b_cont->b_wptr += sizeof (ire_t);
1247 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
1248 
1249 		/* cp known to be 32 bit aligned */
1250 		ac6 = (ipa6_conn_t *)cp;
1251 		ac6->ac6_laddr = icmp->icmp_v6src;
1252 		ac6->ac6_faddr = icmp->icmp_v6dst;
1253 		ac6->ac6_fport = fport;
1254 		ac6->ac6_lport = 0;
1255 		break;
1256 
1257 	case sizeof (sin_t):
1258 		ASSERT(icmp->icmp_family == AF_INET);
1259 		/* Append a request for an IRE */
1260 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
1261 		if (!mp->b_cont) {
1262 			freemsg(mp);
1263 			return (NULL);
1264 		}
1265 		mp->b_cont->b_wptr += sizeof (ire_t);
1266 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
1267 
1268 		sin = (sin_t *)cp;
1269 		*sin = sin_null;
1270 		sin->sin_family = AF_INET;
1271 		sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
1272 		break;
1273 
1274 	case sizeof (sin6_t):
1275 		ASSERT(icmp->icmp_family == AF_INET6);
1276 		/* Append a request for an IRE */
1277 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
1278 		if (!mp->b_cont) {
1279 			freemsg(mp);
1280 			return (NULL);
1281 		}
1282 		mp->b_cont->b_wptr += sizeof (ire_t);
1283 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
1284 
1285 		sin6 = (sin6_t *)cp;
1286 		*sin6 = sin6_null;
1287 		sin6->sin6_family = AF_INET6;
1288 		sin6->sin6_addr = icmp->icmp_bound_v6src;
1289 		break;
1290 	}
1291 	/* Add protocol number to end */
1292 	cp[addr_length] = icmp->icmp_proto;
1293 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
1294 	return (mp);
1295 }
1296 
1297 /*
1298  * This is the open routine for icmp.  It allocates a icmp_t structure for
1299  * the stream and, on the first open of the module, creates an ND table.
1300  */
1301 static int
1302 icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
1303 {
1304 	int	err;
1305 	icmp_t	*icmp;
1306 
1307 	/* If the stream is already open, return immediately. */
1308 	if (q->q_ptr != NULL)
1309 		return (0);
1310 
1311 	/* If this is not a push of icmp as a module, fail. */
1312 	if (sflag != MODOPEN)
1313 		return (EINVAL);
1314 
1315 	/*
1316 	 * Defer the qprocson until everything is initialized since
1317 	 * we are D_MTPERQ and after qprocson the rput routine can
1318 	 * run. (Could do qprocson earlier since icmp currently
1319 	 * has an outer perimeter.)
1320 	 */
1321 
1322 	/*
1323 	 * Create a icmp_t structure for this stream and link into the
1324 	 * list of open streams.
1325 	 */
1326 	err = mi_open_comm(&icmp_g_head, sizeof (icmp_t), q, devp,
1327 	    flag, sflag, credp);
1328 	if (err)
1329 		return (err);
1330 
1331 	/*
1332 	 * The receive hiwat is only looked at on the stream head queue.
1333 	 * Store in q_hiwat in order to return on SO_RCVBUF getsockopts.
1334 	 */
1335 	q->q_hiwat = icmp_recv_hiwat;
1336 
1337 	/* Set the initial state of the stream and the privilege status. */
1338 	icmp = (icmp_t *)q->q_ptr;
1339 	icmp->icmp_state = TS_UNBND;
1340 	icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1341 	icmp->icmp_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1342 	icmp->icmp_filter = NULL;
1343 
1344 	icmp->icmp_credp = credp;
1345 	crhold(credp);
1346 
1347 	icmp->icmp_zoneid = getzoneid();
1348 
1349 	if (getmajor(*devp) == (major_t)ICMP6_MAJ) {
1350 		icmp->icmp_ipversion = IPV6_VERSION;
1351 		icmp->icmp_family = AF_INET6;
1352 		/* May be changed by a SO_PROTOTYPE socket option. */
1353 		icmp->icmp_proto = IPPROTO_ICMPV6;
1354 		icmp->icmp_checksum_off = 2;	/* Offset for icmp6_cksum */
1355 		icmp->icmp_max_hdr_len = IPV6_HDR_LEN;
1356 		icmp->icmp_ttl = (uint8_t)icmp_ipv6_hoplimit;
1357 	} else {
1358 		icmp->icmp_ipversion = IPV4_VERSION;
1359 		icmp->icmp_family = AF_INET;
1360 		/* May be changed by a SO_PROTOTYPE socket option. */
1361 		icmp->icmp_proto = IPPROTO_ICMP;
1362 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH;
1363 		icmp->icmp_ttl = (uint8_t)icmp_ipv4_ttl;
1364 	}
1365 	qprocson(q);
1366 
1367 	/*
1368 	 * Check if icmp is being I_PUSHed by a non-privileged user.
1369 	 * If so, we set icmp_restricted to indicate that only MIB
1370 	 * traffic may pass.
1371 	 */
1372 	if (secpolicy_net_icmpaccess(credp) != 0) {
1373 		icmp->icmp_restricted = 1;
1374 	}
1375 
1376 	/*
1377 	 * The transmit hiwat is only looked at on IP's queue.
1378 	 * Store in q_hiwat in order to return on SO_SNDBUF
1379 	 * getsockopts.
1380 	 */
1381 	WR(q)->q_hiwat = icmp_xmit_hiwat;
1382 	WR(q)->q_next->q_hiwat = WR(q)->q_hiwat;
1383 	WR(q)->q_lowat = icmp_xmit_lowat;
1384 	WR(q)->q_next->q_lowat = WR(q)->q_lowat;
1385 
1386 	if (icmp->icmp_family == AF_INET6) {
1387 		/* Build initial header template for transmit */
1388 		int error;
1389 
1390 		error = icmp_build_hdrs(q, icmp);
1391 		if (error != 0) {
1392 			(void) icmp_close(q);
1393 			return (error);
1394 		}
1395 	}
1396 	/* Set the Stream head write offset. */
1397 	(void) mi_set_sth_wroff(q, icmp->icmp_max_hdr_len + icmp_wroff_extra);
1398 	(void) mi_set_sth_hiwat(q, q->q_hiwat);
1399 
1400 	return (0);
1401 }
1402 
1403 /*
1404  * Which ICMP options OK to set through T_UNITDATA_REQ...
1405  */
1406 /* ARGSUSED */
1407 static boolean_t
1408 icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
1409 {
1410 	return (B_TRUE);
1411 }
1412 
1413 /*
1414  * This routine gets default values of certain options whose default
1415  * values are maintained by protcol specific code
1416  */
1417 /* ARGSUSED */
1418 int
1419 icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr)
1420 {
1421 	int *i1 = (int *)ptr;
1422 
1423 	switch (level) {
1424 	case IPPROTO_IP:
1425 		switch (name) {
1426 		case IP_MULTICAST_TTL:
1427 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
1428 			return (sizeof (uchar_t));
1429 		case IP_MULTICAST_LOOP:
1430 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
1431 			return (sizeof (uchar_t));
1432 		}
1433 		break;
1434 	case IPPROTO_IPV6:
1435 		switch (name) {
1436 		case IPV6_MULTICAST_HOPS:
1437 			*i1 = IP_DEFAULT_MULTICAST_TTL;
1438 			return (sizeof (int));
1439 		case IPV6_MULTICAST_LOOP:
1440 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
1441 			return (sizeof (int));
1442 		case IPV6_UNICAST_HOPS:
1443 			*i1 = icmp_ipv6_hoplimit;
1444 			return (sizeof (int));
1445 		}
1446 		break;
1447 	case IPPROTO_ICMPV6:
1448 		switch (name) {
1449 		case ICMP6_FILTER:
1450 			/* Make it look like "pass all" */
1451 			ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
1452 			return (sizeof (icmp6_filter_t));
1453 		}
1454 		break;
1455 	}
1456 	return (-1);
1457 }
1458 
1459 /*
1460  * This routine retrieves the current status of socket options.
1461  * It returns the size of the option retrieved.
1462  */
1463 int
1464 icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
1465 {
1466 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
1467 	int	*i1 = (int *)ptr;
1468 	ip6_pkt_t	*ipp = &icmp->icmp_sticky_ipp;
1469 
1470 	switch (level) {
1471 	case SOL_SOCKET:
1472 		switch (name) {
1473 		case SO_DEBUG:
1474 			*i1 = icmp->icmp_debug;
1475 			break;
1476 		case SO_TYPE:
1477 			*i1 = SOCK_RAW;
1478 			break;
1479 		case SO_PROTOTYPE:
1480 			*i1 = icmp->icmp_proto;
1481 			break;
1482 		case SO_REUSEADDR:
1483 			*i1 = icmp->icmp_reuseaddr;
1484 			break;
1485 
1486 		/*
1487 		 * The following three items are available here,
1488 		 * but are only meaningful to IP.
1489 		 */
1490 		case SO_DONTROUTE:
1491 			*i1 = icmp->icmp_dontroute;
1492 			break;
1493 		case SO_USELOOPBACK:
1494 			*i1 = icmp->icmp_useloopback;
1495 			break;
1496 		case SO_BROADCAST:
1497 			*i1 = icmp->icmp_broadcast;
1498 			break;
1499 
1500 		case SO_SNDBUF:
1501 			ASSERT(q->q_hiwat <= INT_MAX);
1502 			*i1 = (int)q->q_hiwat;
1503 			break;
1504 		case SO_RCVBUF:
1505 			ASSERT(RD(q)->q_hiwat <= INT_MAX);
1506 			*i1 = (int)RD(q)->q_hiwat;
1507 			break;
1508 		case SO_DGRAM_ERRIND:
1509 			*i1 = icmp->icmp_dgram_errind;
1510 			break;
1511 		/*
1512 		 * Following three not meaningful for icmp
1513 		 * Action is same as "default" to which we fallthrough
1514 		 * so we keep them in comments.
1515 		 * case SO_LINGER:
1516 		 * case SO_KEEPALIVE:
1517 		 * case SO_OOBINLINE:
1518 		 */
1519 		default:
1520 			return (-1);
1521 		}
1522 		break;
1523 	case IPPROTO_IP:
1524 		/*
1525 		 * Only allow IPv4 option processing on IPv4 sockets.
1526 		 */
1527 		if (icmp->icmp_family != AF_INET)
1528 			return (-1);
1529 
1530 		switch (name) {
1531 		case IP_OPTIONS:
1532 		case T_IP_OPTIONS:
1533 			/* Options are passed up with each packet */
1534 			return (0);
1535 		case IP_HDRINCL:
1536 			*i1 = (int)icmp->icmp_hdrincl;
1537 			break;
1538 		case IP_TOS:
1539 		case T_IP_TOS:
1540 			*i1 = (int)icmp->icmp_type_of_service;
1541 			break;
1542 		case IP_TTL:
1543 			*i1 = (int)icmp->icmp_ttl;
1544 			break;
1545 		case IP_MULTICAST_IF:
1546 			/* 0 address if not set */
1547 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
1548 			return (sizeof (ipaddr_t));
1549 		case IP_MULTICAST_TTL:
1550 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
1551 			return (sizeof (uchar_t));
1552 		case IP_MULTICAST_LOOP:
1553 			*ptr = icmp->icmp_multicast_loop;
1554 			return (sizeof (uint8_t));
1555 		case IP_BOUND_IF:
1556 			/* Zero if not set */
1557 			*i1 = icmp->icmp_bound_if;
1558 			break;	/* goto sizeof (int) option return */
1559 		case IP_UNSPEC_SRC:
1560 			*ptr = icmp->icmp_unspec_source;
1561 			break;	/* goto sizeof (int) option return */
1562 		case IP_XMIT_IF:
1563 			*i1 = icmp->icmp_xmit_if;
1564 			break;	/* goto sizeof (int) option return */
1565 		case IP_RECVIF:
1566 			*ptr = icmp->icmp_recvif;
1567 			break;	/* goto sizeof (int) option return */
1568 		/*
1569 		 * Cannot "get" the value of following options
1570 		 * at this level. Action is same as "default" to
1571 		 * which we fallthrough so we keep them in comments.
1572 		 *
1573 		 * case IP_ADD_MEMBERSHIP:
1574 		 * case IP_DROP_MEMBERSHIP:
1575 		 * case IP_BLOCK_SOURCE:
1576 		 * case IP_UNBLOCK_SOURCE:
1577 		 * case IP_ADD_SOURCE_MEMBERSHIP:
1578 		 * case IP_DROP_SOURCE_MEMBERSHIP:
1579 		 * case MCAST_JOIN_GROUP:
1580 		 * case MCAST_LEAVE_GROUP:
1581 		 * case MCAST_BLOCK_SOURCE:
1582 		 * case MCAST_UNBLOCK_SOURCE:
1583 		 * case MCAST_JOIN_SOURCE_GROUP:
1584 		 * case MCAST_LEAVE_SOURCE_GROUP:
1585 		 * case MRT_INIT:
1586 		 * case MRT_DONE:
1587 		 * case MRT_ADD_VIF:
1588 		 * case MRT_DEL_VIF:
1589 		 * case MRT_ADD_MFC:
1590 		 * case MRT_DEL_MFC:
1591 		 * case MRT_VERSION:
1592 		 * case MRT_ASSERT:
1593 		 * case IP_SEC_OPT:
1594 		 * case IP_DONTFAILOVER_IF:
1595 		 */
1596 		default:
1597 			return (-1);
1598 		}
1599 		break;
1600 	case IPPROTO_IPV6:
1601 		/*
1602 		 * Only allow IPv6 option processing on native IPv6 sockets.
1603 		 */
1604 		if (icmp->icmp_family != AF_INET6)
1605 			return (-1);
1606 		switch (name) {
1607 		case IPV6_UNICAST_HOPS:
1608 			*i1 = (unsigned int)icmp->icmp_ttl;
1609 			break;
1610 		case IPV6_MULTICAST_IF:
1611 			/* 0 index if not set */
1612 			*i1 = icmp->icmp_multicast_if_index;
1613 			break;
1614 		case IPV6_MULTICAST_HOPS:
1615 			*i1 = icmp->icmp_multicast_ttl;
1616 			break;
1617 		case IPV6_MULTICAST_LOOP:
1618 			*i1 = icmp->icmp_multicast_loop;
1619 			break;
1620 		case IPV6_BOUND_IF:
1621 			/* Zero if not set */
1622 			*i1 = icmp->icmp_bound_if;
1623 			break;
1624 		case IPV6_UNSPEC_SRC:
1625 			*i1 = icmp->icmp_unspec_source;
1626 			break;
1627 		case IPV6_CHECKSUM:
1628 			/*
1629 			 * Return offset or -1 if no checksum offset.
1630 			 * Does not apply to IPPROTO_ICMPV6
1631 			 */
1632 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
1633 				return (-1);
1634 
1635 			if (icmp->icmp_raw_checksum) {
1636 				*i1 = icmp->icmp_checksum_off;
1637 			} else {
1638 				*i1 = -1;
1639 			}
1640 			break;
1641 		case IPV6_JOIN_GROUP:
1642 		case IPV6_LEAVE_GROUP:
1643 		case MCAST_JOIN_GROUP:
1644 		case MCAST_LEAVE_GROUP:
1645 		case MCAST_BLOCK_SOURCE:
1646 		case MCAST_UNBLOCK_SOURCE:
1647 		case MCAST_JOIN_SOURCE_GROUP:
1648 		case MCAST_LEAVE_SOURCE_GROUP:
1649 			/* cannot "get" the value for these */
1650 			return (-1);
1651 		case IPV6_RECVPKTINFO:
1652 			*i1 = icmp->icmp_ipv6_recvpktinfo;
1653 			break;
1654 		case IPV6_RECVTCLASS:
1655 			*i1 = icmp->icmp_ipv6_recvtclass;
1656 			break;
1657 		case IPV6_RECVPATHMTU:
1658 			*i1 = icmp->icmp_ipv6_recvpathmtu;
1659 			break;
1660 		case IPV6_V6ONLY:
1661 			*i1 = 1;
1662 			break;
1663 		case IPV6_RECVHOPLIMIT:
1664 			*i1 = icmp->icmp_ipv6_recvhoplimit;
1665 			break;
1666 		case IPV6_RECVHOPOPTS:
1667 			*i1 = icmp->icmp_ipv6_recvhopopts;
1668 			break;
1669 		case IPV6_RECVDSTOPTS:
1670 			*i1 = icmp->icmp_ipv6_recvdstopts;
1671 			break;
1672 		case _OLD_IPV6_RECVDSTOPTS:
1673 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
1674 			break;
1675 		case IPV6_RECVRTHDRDSTOPTS:
1676 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
1677 			break;
1678 		case IPV6_RECVRTHDR:
1679 			*i1 = icmp->icmp_ipv6_recvrthdr;
1680 			break;
1681 		case IPV6_PKTINFO: {
1682 			/* XXX assumes that caller has room for max size! */
1683 			struct in6_pktinfo *pkti;
1684 
1685 			pkti = (struct in6_pktinfo *)ptr;
1686 			if (ipp->ipp_fields & IPPF_IFINDEX)
1687 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
1688 			else
1689 				pkti->ipi6_ifindex = 0;
1690 			if (ipp->ipp_fields & IPPF_ADDR)
1691 				pkti->ipi6_addr = ipp->ipp_addr;
1692 			else
1693 				pkti->ipi6_addr = ipv6_all_zeros;
1694 			return (sizeof (struct in6_pktinfo));
1695 		}
1696 		case IPV6_NEXTHOP: {
1697 			sin6_t *sin6 = (sin6_t *)ptr;
1698 
1699 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
1700 				return (0);
1701 			*sin6 = sin6_null;
1702 			sin6->sin6_family = AF_INET6;
1703 			sin6->sin6_addr = ipp->ipp_nexthop;
1704 			return (sizeof (sin6_t));
1705 		}
1706 		case IPV6_HOPOPTS:
1707 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
1708 				return (0);
1709 			bcopy(ipp->ipp_hopopts, ptr, ipp->ipp_hopoptslen);
1710 			return (ipp->ipp_hopoptslen);
1711 		case IPV6_RTHDRDSTOPTS:
1712 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
1713 				return (0);
1714 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
1715 			return (ipp->ipp_rtdstoptslen);
1716 		case IPV6_RTHDR:
1717 			if (!(ipp->ipp_fields & IPPF_RTHDR))
1718 				return (0);
1719 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
1720 			return (ipp->ipp_rthdrlen);
1721 		case IPV6_DSTOPTS:
1722 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
1723 				return (0);
1724 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
1725 			return (ipp->ipp_dstoptslen);
1726 		case IPV6_PATHMTU:
1727 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
1728 				return (0);
1729 
1730 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
1731 				(struct ip6_mtuinfo *)ptr));
1732 		case IPV6_TCLASS:
1733 			if (ipp->ipp_fields & IPPF_TCLASS)
1734 				*i1 = ipp->ipp_tclass;
1735 			else
1736 				*i1 = IPV6_FLOW_TCLASS(
1737 				    IPV6_DEFAULT_VERS_AND_FLOW);
1738 			break;
1739 		default:
1740 			return (-1);
1741 		}
1742 		break;
1743 	case IPPROTO_ICMPV6:
1744 		/*
1745 		 * Only allow IPv6 option processing on native IPv6 sockets.
1746 		 */
1747 		if (icmp->icmp_family != AF_INET6)
1748 			return (-1);
1749 
1750 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
1751 			return (-1);
1752 
1753 		switch (name) {
1754 		case ICMP6_FILTER:
1755 			if (icmp->icmp_filter == NULL) {
1756 				/* Make it look like "pass all" */
1757 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
1758 			} else {
1759 				(void) bcopy(icmp->icmp_filter, ptr,
1760 				    sizeof (icmp6_filter_t));
1761 			}
1762 			return (sizeof (icmp6_filter_t));
1763 		default:
1764 			return (-1);
1765 		}
1766 	default:
1767 		return (-1);
1768 	}
1769 	return (sizeof (int));
1770 }
1771 
1772 /* This routine sets socket options. */
1773 /* ARGSUSED */
1774 int
1775 icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
1776     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
1777     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
1778 {
1779 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
1780 	int	*i1 = (int *)invalp;
1781 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
1782 	boolean_t checkonly;
1783 	int	error;
1784 
1785 	switch (optset_context) {
1786 	case SETFN_OPTCOM_CHECKONLY:
1787 		checkonly = B_TRUE;
1788 		/*
1789 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
1790 		 * inlen != 0 implies value supplied and
1791 		 * 	we have to "pretend" to set it.
1792 		 * inlen == 0 implies that there is no
1793 		 * 	value part in T_CHECK request and just validation
1794 		 * done elsewhere should be enough, we just return here.
1795 		 */
1796 		if (inlen == 0) {
1797 			*outlenp = 0;
1798 			return (0);
1799 		}
1800 		break;
1801 	case SETFN_OPTCOM_NEGOTIATE:
1802 		checkonly = B_FALSE;
1803 		break;
1804 	case SETFN_UD_NEGOTIATE:
1805 	case SETFN_CONN_NEGOTIATE:
1806 		checkonly = B_FALSE;
1807 		/*
1808 		 * Negotiating local and "association-related" options
1809 		 * through T_UNITDATA_REQ.
1810 		 *
1811 		 * Following routine can filter out ones we do not
1812 		 * want to be "set" this way.
1813 		 */
1814 		if (!icmp_opt_allow_udr_set(level, name)) {
1815 			*outlenp = 0;
1816 			return (EINVAL);
1817 		}
1818 		break;
1819 	default:
1820 		/*
1821 		 * We should never get here
1822 		 */
1823 		*outlenp = 0;
1824 		return (EINVAL);
1825 	}
1826 
1827 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
1828 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
1829 
1830 	/*
1831 	 * For fixed length options, no sanity check
1832 	 * of passed in length is done. It is assumed *_optcom_req()
1833 	 * routines do the right thing.
1834 	 */
1835 
1836 	switch (level) {
1837 	case SOL_SOCKET:
1838 		switch (name) {
1839 		case SO_DEBUG:
1840 			if (!checkonly)
1841 				icmp->icmp_debug = onoff;
1842 			break;
1843 		case SO_PROTOTYPE:
1844 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
1845 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
1846 			    secpolicy_net_rawaccess(cr) != 0) {
1847 				*outlenp = 0;
1848 				return (EACCES);
1849 			}
1850 			/* Can't use IPPROTO_RAW with IPv6 */
1851 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
1852 			    icmp->icmp_family == AF_INET6) {
1853 				*outlenp = 0;
1854 				return (EPROTONOSUPPORT);
1855 			}
1856 			if (checkonly) {
1857 				/* T_CHECK case */
1858 				*(int *)outvalp = (*i1 & 0xFF);
1859 				break;
1860 			}
1861 			icmp->icmp_proto = *i1 & 0xFF;
1862 			if ((icmp->icmp_proto == IPPROTO_RAW ||
1863 			    icmp->icmp_proto == IPPROTO_IGMP) &&
1864 			    icmp->icmp_family == AF_INET)
1865 				icmp->icmp_hdrincl = 1;
1866 			else
1867 				icmp->icmp_hdrincl = 0;
1868 
1869 			if (icmp->icmp_family == AF_INET6 &&
1870 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
1871 				/* Set offset for icmp6_cksum */
1872 				icmp->icmp_raw_checksum = 0;
1873 				icmp->icmp_checksum_off = 2;
1874 			}
1875 			if (icmp->icmp_proto == IPPROTO_UDP ||
1876 			    icmp->icmp_proto == IPPROTO_TCP ||
1877 			    icmp->icmp_proto == IPPROTO_SCTP) {
1878 				icmp->icmp_no_tp_cksum = 1;
1879 				icmp->icmp_sticky_ipp.ipp_fields |=
1880 				    IPPF_NO_CKSUM;
1881 			} else {
1882 				icmp->icmp_no_tp_cksum = 0;
1883 				icmp->icmp_sticky_ipp.ipp_fields &=
1884 				    ~IPPF_NO_CKSUM;
1885 			}
1886 
1887 			if (icmp->icmp_filter != NULL &&
1888 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
1889 				kmem_free(icmp->icmp_filter,
1890 				    sizeof (icmp6_filter_t));
1891 				icmp->icmp_filter = NULL;
1892 			}
1893 
1894 			/* Rebuild the header template */
1895 			error = icmp_build_hdrs(q, icmp);
1896 			if (error != 0) {
1897 				*outlenp = 0;
1898 				return (error);
1899 			}
1900 
1901 			/*
1902 			 * For SCTP, we don't use icmp_bind_proto() for
1903 			 * raw socket binding.  Note that we do not need
1904 			 * to set *outlenp.
1905 			 */
1906 			if (icmp->icmp_proto == IPPROTO_SCTP)
1907 				return (0);
1908 
1909 			icmp_bind_proto(q);
1910 			*outlenp = sizeof (int);
1911 			*(int *)outvalp = *i1 & 0xFF;
1912 			return (0);
1913 		case SO_REUSEADDR:
1914 			if (!checkonly)
1915 				icmp->icmp_reuseaddr = onoff;
1916 			break;
1917 
1918 		/*
1919 		 * The following three items are available here,
1920 		 * but are only meaningful to IP.
1921 		 */
1922 		case SO_DONTROUTE:
1923 			if (!checkonly)
1924 				icmp->icmp_dontroute = onoff;
1925 			break;
1926 		case SO_USELOOPBACK:
1927 			if (!checkonly)
1928 				icmp->icmp_useloopback = onoff;
1929 			break;
1930 		case SO_BROADCAST:
1931 			if (!checkonly)
1932 				icmp->icmp_broadcast = onoff;
1933 			break;
1934 
1935 		case SO_SNDBUF:
1936 			if (*i1 > icmp_max_buf) {
1937 				*outlenp = 0;
1938 				return (ENOBUFS);
1939 			}
1940 			if (!checkonly) {
1941 				q->q_hiwat = *i1;
1942 				q->q_next->q_hiwat = *i1;
1943 			}
1944 			break;
1945 		case SO_RCVBUF:
1946 			if (*i1 > icmp_max_buf) {
1947 				*outlenp = 0;
1948 				return (ENOBUFS);
1949 			}
1950 			if (!checkonly) {
1951 				RD(q)->q_hiwat = *i1;
1952 				(void) mi_set_sth_hiwat(RD(q), *i1);
1953 			}
1954 			break;
1955 		case SO_DGRAM_ERRIND:
1956 			if (!checkonly)
1957 				icmp->icmp_dgram_errind = onoff;
1958 			break;
1959 		/*
1960 		 * Following three not meaningful for icmp
1961 		 * Action is same as "default" so we keep them
1962 		 * in comments.
1963 		 * case SO_LINGER:
1964 		 * case SO_KEEPALIVE:
1965 		 * case SO_OOBINLINE:
1966 		 */
1967 		default:
1968 			*outlenp = 0;
1969 			return (EINVAL);
1970 		}
1971 		break;
1972 	case IPPROTO_IP:
1973 		/*
1974 		 * Only allow IPv4 option processing on IPv4 sockets.
1975 		 */
1976 		if (icmp->icmp_family != AF_INET) {
1977 			*outlenp = 0;
1978 			return (ENOPROTOOPT);
1979 		}
1980 		switch (name) {
1981 		case IP_OPTIONS:
1982 		case T_IP_OPTIONS:
1983 			/* Save options for use by IP. */
1984 			if (inlen & 0x3) {
1985 				*outlenp = 0;
1986 				return (EINVAL);
1987 			}
1988 			if (checkonly)
1989 				break;
1990 
1991 			if (icmp->icmp_ip_snd_options) {
1992 				mi_free((char *)icmp->icmp_ip_snd_options);
1993 				icmp->icmp_ip_snd_options_len = 0;
1994 				icmp->icmp_ip_snd_options = NULL;
1995 			}
1996 			if (inlen) {
1997 				icmp->icmp_ip_snd_options =
1998 				    (uchar_t *)mi_alloc(inlen, BPRI_HI);
1999 				if (icmp->icmp_ip_snd_options) {
2000 					bcopy(invalp,
2001 					    icmp->icmp_ip_snd_options, inlen);
2002 					icmp->icmp_ip_snd_options_len = inlen;
2003 				}
2004 			}
2005 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
2006 			    icmp->icmp_ip_snd_options_len;
2007 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
2008 						icmp_wroff_extra);
2009 			break;
2010 		case IP_HDRINCL:
2011 			if (!checkonly)
2012 				icmp->icmp_hdrincl = onoff;
2013 			break;
2014 		case IP_TOS:
2015 		case T_IP_TOS:
2016 			if (!checkonly) {
2017 				icmp->icmp_type_of_service = (uint8_t)*i1;
2018 			}
2019 			break;
2020 		case IP_TTL:
2021 			if (!checkonly) {
2022 				icmp->icmp_ttl = (uint8_t)*i1;
2023 			}
2024 			break;
2025 		case IP_MULTICAST_IF:
2026 			/*
2027 			 * TODO should check OPTMGMT reply and undo this if
2028 			 * there is an error.
2029 			 */
2030 			if (!checkonly)
2031 				icmp->icmp_multicast_if_addr = *i1;
2032 			break;
2033 		case IP_MULTICAST_TTL:
2034 			if (!checkonly)
2035 				icmp->icmp_multicast_ttl = *invalp;
2036 			break;
2037 		case IP_MULTICAST_LOOP:
2038 			if (!checkonly) {
2039 				icmp->icmp_multicast_loop =
2040 				    (*invalp == 0) ? 0 : 1;
2041 			}
2042 			break;
2043 		case IP_BOUND_IF:
2044 			if (!checkonly)
2045 				icmp->icmp_bound_if = *i1;
2046 			break;
2047 		case IP_UNSPEC_SRC:
2048 			if (!checkonly)
2049 				icmp->icmp_unspec_source = onoff;
2050 			break;
2051 		case IP_XMIT_IF:
2052 			if (!checkonly)
2053 				icmp->icmp_xmit_if = *i1;
2054 			break;
2055 		case IP_RECVIF:
2056 			if (!checkonly)
2057 				icmp->icmp_recvif = onoff;
2058 			break;
2059 		case IP_ADD_MEMBERSHIP:
2060 		case IP_DROP_MEMBERSHIP:
2061 		case IP_BLOCK_SOURCE:
2062 		case IP_UNBLOCK_SOURCE:
2063 		case IP_ADD_SOURCE_MEMBERSHIP:
2064 		case IP_DROP_SOURCE_MEMBERSHIP:
2065 		case MCAST_JOIN_GROUP:
2066 		case MCAST_LEAVE_GROUP:
2067 		case MCAST_BLOCK_SOURCE:
2068 		case MCAST_UNBLOCK_SOURCE:
2069 		case MCAST_JOIN_SOURCE_GROUP:
2070 		case MCAST_LEAVE_SOURCE_GROUP:
2071 		case MRT_INIT:
2072 		case MRT_DONE:
2073 		case MRT_ADD_VIF:
2074 		case MRT_DEL_VIF:
2075 		case MRT_ADD_MFC:
2076 		case MRT_DEL_MFC:
2077 		case MRT_VERSION:
2078 		case MRT_ASSERT:
2079 		case IP_SEC_OPT:
2080 		case IP_DONTFAILOVER_IF:
2081 			/*
2082 			 * "soft" error (negative)
2083 			 * option not handled at this level
2084 			 * Note: Do not modify *outlenp
2085 			 */
2086 			return (-EINVAL);
2087 		default:
2088 			*outlenp = 0;
2089 			return (EINVAL);
2090 		}
2091 		break;
2092 	case IPPROTO_IPV6: {
2093 		ip6_pkt_t		*ipp;
2094 		boolean_t		sticky;
2095 
2096 		if (icmp->icmp_family != AF_INET6) {
2097 			*outlenp = 0;
2098 			return (ENOPROTOOPT);
2099 		}
2100 		/*
2101 		 * Deal with both sticky options and ancillary data
2102 		 */
2103 		if (thisdg_attrs == NULL) {
2104 			/* sticky options, or none */
2105 			ipp = &icmp->icmp_sticky_ipp;
2106 			sticky = B_TRUE;
2107 		} else {
2108 			/* ancillary data */
2109 			ipp = (ip6_pkt_t *)thisdg_attrs;
2110 			sticky = B_FALSE;
2111 		}
2112 
2113 		switch (name) {
2114 		case IPV6_MULTICAST_IF:
2115 			if (!checkonly)
2116 				icmp->icmp_multicast_if_index = *i1;
2117 			break;
2118 		case IPV6_UNICAST_HOPS:
2119 			/* -1 means use default */
2120 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
2121 				*outlenp = 0;
2122 				return (EINVAL);
2123 			}
2124 			if (!checkonly) {
2125 				if (*i1 == -1) {
2126 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
2127 					    icmp_ipv6_hoplimit;
2128 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
2129 					/* Pass modified value to IP. */
2130 					*i1 = ipp->ipp_hoplimit;
2131 				} else {
2132 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
2133 					    (uint8_t)*i1;
2134 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
2135 				}
2136 				/* Rebuild the header template */
2137 				error = icmp_build_hdrs(q, icmp);
2138 				if (error != 0) {
2139 					*outlenp = 0;
2140 					return (error);
2141 				}
2142 			}
2143 			break;
2144 		case IPV6_MULTICAST_HOPS:
2145 			/* -1 means use default */
2146 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
2147 				*outlenp = 0;
2148 				return (EINVAL);
2149 			}
2150 			if (!checkonly) {
2151 				if (*i1 == -1) {
2152 					icmp->icmp_multicast_ttl =
2153 					    ipp->ipp_multicast_hops =
2154 					    IP_DEFAULT_MULTICAST_TTL;
2155 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
2156 					/* Pass modified value to IP. */
2157 					*i1 = icmp->icmp_multicast_ttl;
2158 				} else {
2159 					icmp->icmp_multicast_ttl =
2160 					    ipp->ipp_multicast_hops =
2161 					    (uint8_t)*i1;
2162 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
2163 				}
2164 			}
2165 			break;
2166 		case IPV6_MULTICAST_LOOP:
2167 			if (*i1 != 0 && *i1 != 1) {
2168 				*outlenp = 0;
2169 				return (EINVAL);
2170 			}
2171 			if (!checkonly)
2172 				icmp->icmp_multicast_loop = *i1;
2173 			break;
2174 		case IPV6_CHECKSUM:
2175 			/*
2176 			 * Integer offset into the user data of where the
2177 			 * checksum is located.
2178 			 * Offset of -1 disables option.
2179 			 * Does not apply to IPPROTO_ICMPV6.
2180 			 */
2181 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
2182 				*outlenp = 0;
2183 				return (EINVAL);
2184 			}
2185 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
2186 				/* Negative or not 16 bit aligned offset */
2187 				*outlenp = 0;
2188 				return (EINVAL);
2189 			}
2190 			if (checkonly)
2191 				break;
2192 
2193 			if (*i1 == -1) {
2194 				icmp->icmp_raw_checksum = 0;
2195 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
2196 			} else {
2197 				icmp->icmp_raw_checksum = 1;
2198 				icmp->icmp_checksum_off = *i1;
2199 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
2200 			}
2201 			/* Rebuild the header template */
2202 			error = icmp_build_hdrs(q, icmp);
2203 			if (error != 0) {
2204 				*outlenp = 0;
2205 				return (error);
2206 			}
2207 			break;
2208 		case IPV6_JOIN_GROUP:
2209 		case IPV6_LEAVE_GROUP:
2210 		case MCAST_JOIN_GROUP:
2211 		case MCAST_LEAVE_GROUP:
2212 		case MCAST_BLOCK_SOURCE:
2213 		case MCAST_UNBLOCK_SOURCE:
2214 		case MCAST_JOIN_SOURCE_GROUP:
2215 		case MCAST_LEAVE_SOURCE_GROUP:
2216 			/*
2217 			 * "soft" error (negative)
2218 			 * option not handled at this level
2219 			 * Note: Do not modify *outlenp
2220 			 */
2221 			return (-EINVAL);
2222 		case IPV6_BOUND_IF:
2223 			if (!checkonly)
2224 				icmp->icmp_bound_if = *i1;
2225 			break;
2226 		case IPV6_UNSPEC_SRC:
2227 			if (!checkonly)
2228 				icmp->icmp_unspec_source = onoff;
2229 			break;
2230 		case IPV6_RECVTCLASS:
2231 			if (!checkonly)
2232 				icmp->icmp_ipv6_recvtclass = onoff;
2233 			break;
2234 		/*
2235 		 * Set boolean switches for ancillary data delivery
2236 		 */
2237 		case IPV6_RECVPKTINFO:
2238 			if (!checkonly)
2239 				icmp->icmp_ipv6_recvpktinfo = onoff;
2240 			break;
2241 		case IPV6_RECVPATHMTU:
2242 			if (!checkonly)
2243 				icmp->icmp_ipv6_recvpathmtu = onoff;
2244 			break;
2245 		case IPV6_RECVHOPLIMIT:
2246 			if (!checkonly)
2247 				icmp->icmp_ipv6_recvhoplimit = onoff;
2248 			break;
2249 		case IPV6_RECVHOPOPTS:
2250 			if (!checkonly)
2251 				icmp->icmp_ipv6_recvhopopts = onoff;
2252 			break;
2253 		case IPV6_RECVDSTOPTS:
2254 			if (!checkonly)
2255 				icmp->icmp_ipv6_recvdstopts = onoff;
2256 			break;
2257 		case _OLD_IPV6_RECVDSTOPTS:
2258 			if (!checkonly)
2259 				icmp->icmp_old_ipv6_recvdstopts = onoff;
2260 			break;
2261 		case IPV6_RECVRTHDRDSTOPTS:
2262 			if (!checkonly)
2263 				icmp->icmp_ipv6_recvrtdstopts = onoff;
2264 			break;
2265 		case IPV6_RECVRTHDR:
2266 			if (!checkonly)
2267 				icmp->icmp_ipv6_recvrthdr = onoff;
2268 			break;
2269 		/*
2270 		 * Set sticky options or ancillary data.
2271 		 * If sticky options, (re)build any extension headers
2272 		 * that might be needed as a result.
2273 		 */
2274 		case IPV6_PKTINFO:
2275 			/*
2276 			 * The source address and ifindex are verified
2277 			 * in ip_opt_set(). For ancillary data the
2278 			 * source address is checked in ip_wput_v6.
2279 			 */
2280 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
2281 				return (EINVAL);
2282 			if (checkonly)
2283 				break;
2284 
2285 			if (inlen == 0) {
2286 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
2287 				ipp->ipp_sticky_ignored |=
2288 				    (IPPF_IFINDEX|IPPF_ADDR);
2289 			} else {
2290 				struct in6_pktinfo *pkti;
2291 
2292 				pkti = (struct in6_pktinfo *)invalp;
2293 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
2294 				ipp->ipp_addr = pkti->ipi6_addr;
2295 				if (ipp->ipp_ifindex != 0)
2296 					ipp->ipp_fields |= IPPF_IFINDEX;
2297 				else
2298 					ipp->ipp_fields &= ~IPPF_IFINDEX;
2299 				if (!IN6_IS_ADDR_UNSPECIFIED(
2300 				    &ipp->ipp_addr))
2301 					ipp->ipp_fields |= IPPF_ADDR;
2302 				else
2303 					ipp->ipp_fields &= ~IPPF_ADDR;
2304 			}
2305 			if (sticky) {
2306 				error = icmp_build_hdrs(q, icmp);
2307 				if (error != 0)
2308 					return (error);
2309 			}
2310 			break;
2311 		case IPV6_HOPLIMIT:
2312 			/* This option can only be used as ancillary data. */
2313 			if (sticky)
2314 				return (EINVAL);
2315 			if (inlen != 0 && inlen != sizeof (int))
2316 				return (EINVAL);
2317 			if (checkonly)
2318 				break;
2319 
2320 			if (inlen == 0) {
2321 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
2322 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
2323 			} else {
2324 				if (*i1 > 255 || *i1 < -1)
2325 					return (EINVAL);
2326 				if (*i1 == -1)
2327 					ipp->ipp_hoplimit = icmp_ipv6_hoplimit;
2328 				else
2329 					ipp->ipp_hoplimit = *i1;
2330 				ipp->ipp_fields |= IPPF_HOPLIMIT;
2331 			}
2332 			break;
2333 		case IPV6_TCLASS:
2334 			/*
2335 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
2336 			 * and [0, 255] as the actualy traffic class.
2337 			 */
2338 			if (inlen != 0 && inlen != sizeof (int))
2339 				return (EINVAL);
2340 			if (checkonly)
2341 				break;
2342 
2343 			if (inlen == 0) {
2344 				ipp->ipp_fields &= ~IPPF_TCLASS;
2345 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
2346 			} else {
2347 				if (*i1 >= 256 || *i1 < -1)
2348 					return (EINVAL);
2349 				if (*i1 == -1) {
2350 					ipp->ipp_tclass =
2351 					    IPV6_FLOW_TCLASS(
2352 					    IPV6_DEFAULT_VERS_AND_FLOW);
2353 				} else {
2354 					ipp->ipp_tclass = *i1;
2355 				}
2356 				ipp->ipp_fields |= IPPF_TCLASS;
2357 			}
2358 			if (sticky) {
2359 				error = icmp_build_hdrs(q, icmp);
2360 				if (error != 0)
2361 					return (error);
2362 			}
2363 			break;
2364 		case IPV6_NEXTHOP:
2365 			/*
2366 			 * IP will verify that the nexthop is reachable
2367 			 * and fail for sticky options.
2368 			 */
2369 			if (inlen != 0 && inlen != sizeof (sin6_t))
2370 				return (EINVAL);
2371 			if (checkonly)
2372 				break;
2373 
2374 			if (inlen == 0) {
2375 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
2376 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
2377 			} else {
2378 				sin6_t *sin6 = (sin6_t *)invalp;
2379 
2380 				if (sin6->sin6_family != AF_INET6)
2381 					return (EAFNOSUPPORT);
2382 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2383 					return (EADDRNOTAVAIL);
2384 				ipp->ipp_nexthop = sin6->sin6_addr;
2385 				if (!IN6_IS_ADDR_UNSPECIFIED(
2386 				    &ipp->ipp_nexthop))
2387 					ipp->ipp_fields |= IPPF_NEXTHOP;
2388 				else
2389 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
2390 			}
2391 			if (sticky) {
2392 				error = icmp_build_hdrs(q, icmp);
2393 				if (error != 0)
2394 					return (error);
2395 			}
2396 			break;
2397 		case IPV6_HOPOPTS: {
2398 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
2399 			/*
2400 			 * Sanity checks - minimum size, size a multiple of
2401 			 * eight bytes, and matching size passed in.
2402 			 */
2403 			if (inlen != 0 &&
2404 			    inlen != (8 * (hopts->ip6h_len + 1)))
2405 				return (EINVAL);
2406 
2407 			if (checkonly)
2408 				break;
2409 			if (inlen == 0) {
2410 				if (sticky &&
2411 				    (ipp->ipp_fields & IPPF_HOPOPTS) != 0) {
2412 					kmem_free(ipp->ipp_hopopts,
2413 					    ipp->ipp_hopoptslen);
2414 					ipp->ipp_hopopts = NULL;
2415 					ipp->ipp_hopoptslen = 0;
2416 				}
2417 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
2418 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
2419 			} else {
2420 				error = icmp_pkt_set(invalp, inlen, sticky,
2421 				    (uchar_t **)&ipp->ipp_hopopts,
2422 				    &ipp->ipp_hopoptslen);
2423 				if (error != 0)
2424 					return (error);
2425 				ipp->ipp_fields |= IPPF_HOPOPTS;
2426 			}
2427 			if (sticky) {
2428 				error = icmp_build_hdrs(q, icmp);
2429 				if (error != 0)
2430 					return (error);
2431 			}
2432 			break;
2433 		}
2434 		case IPV6_RTHDRDSTOPTS: {
2435 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
2436 
2437 			/*
2438 			 * Sanity checks - minimum size, size a multiple of
2439 			 * eight bytes, and matching size passed in.
2440 			 */
2441 			if (inlen != 0 &&
2442 			    inlen != (8 * (dopts->ip6d_len + 1)))
2443 				return (EINVAL);
2444 
2445 			if (checkonly)
2446 				break;
2447 
2448 			if (inlen == 0) {
2449 				if (sticky &&
2450 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
2451 					kmem_free(ipp->ipp_rtdstopts,
2452 					    ipp->ipp_rtdstoptslen);
2453 					ipp->ipp_rtdstopts = NULL;
2454 					ipp->ipp_rtdstoptslen = 0;
2455 				}
2456 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
2457 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
2458 			} else {
2459 				error = icmp_pkt_set(invalp, inlen, sticky,
2460 				    (uchar_t **)&ipp->ipp_rtdstopts,
2461 				    &ipp->ipp_rtdstoptslen);
2462 				if (error != 0)
2463 					return (error);
2464 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
2465 			}
2466 			if (sticky) {
2467 				error = icmp_build_hdrs(q, icmp);
2468 				if (error != 0)
2469 					return (error);
2470 			}
2471 			break;
2472 		}
2473 		case IPV6_DSTOPTS: {
2474 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
2475 
2476 			/*
2477 			 * Sanity checks - minimum size, size a multiple of
2478 			 * eight bytes, and matching size passed in.
2479 			 */
2480 			if (inlen != 0 &&
2481 			    inlen != (8 * (dopts->ip6d_len + 1)))
2482 				return (EINVAL);
2483 
2484 			if (checkonly)
2485 				break;
2486 
2487 			if (inlen == 0) {
2488 				if (sticky &&
2489 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
2490 					kmem_free(ipp->ipp_dstopts,
2491 					    ipp->ipp_dstoptslen);
2492 					ipp->ipp_dstopts = NULL;
2493 					ipp->ipp_dstoptslen = 0;
2494 				}
2495 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
2496 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
2497 			} else {
2498 				error = icmp_pkt_set(invalp, inlen, sticky,
2499 				    (uchar_t **)&ipp->ipp_dstopts,
2500 				    &ipp->ipp_dstoptslen);
2501 				if (error != 0)
2502 					return (error);
2503 				ipp->ipp_fields |= IPPF_DSTOPTS;
2504 			}
2505 			if (sticky) {
2506 				error = icmp_build_hdrs(q, icmp);
2507 				if (error != 0)
2508 					return (error);
2509 			}
2510 			break;
2511 		}
2512 		case IPV6_RTHDR: {
2513 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
2514 
2515 			/*
2516 			 * Sanity checks - minimum size, size a multiple of
2517 			 * eight bytes, and matching size passed in.
2518 			 */
2519 			if (inlen != 0 &&
2520 			    inlen != (8 * (rt->ip6r_len + 1)))
2521 				return (EINVAL);
2522 
2523 			if (checkonly)
2524 				break;
2525 
2526 			if (inlen == 0) {
2527 				if (sticky &&
2528 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
2529 					kmem_free(ipp->ipp_rthdr,
2530 					    ipp->ipp_rthdrlen);
2531 					ipp->ipp_rthdr = NULL;
2532 					ipp->ipp_rthdrlen = 0;
2533 				}
2534 				ipp->ipp_fields &= ~IPPF_RTHDR;
2535 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
2536 			} else {
2537 				error = icmp_pkt_set(invalp, inlen, sticky,
2538 				    (uchar_t **)&ipp->ipp_rthdr,
2539 				    &ipp->ipp_rthdrlen);
2540 				if (error != 0)
2541 					return (error);
2542 				ipp->ipp_fields |= IPPF_RTHDR;
2543 			}
2544 			if (sticky) {
2545 				error = icmp_build_hdrs(q, icmp);
2546 				if (error != 0)
2547 					return (error);
2548 			}
2549 			break;
2550 		}
2551 
2552 		case IPV6_DONTFRAG:
2553 			if (checkonly)
2554 				break;
2555 
2556 			if (onoff) {
2557 				ipp->ipp_fields |= IPPF_DONTFRAG;
2558 			} else {
2559 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
2560 			}
2561 			break;
2562 
2563 		case IPV6_USE_MIN_MTU:
2564 			if (inlen != sizeof (int))
2565 				return (EINVAL);
2566 
2567 			if (*i1 < -1 || *i1 > 1)
2568 				return (EINVAL);
2569 
2570 			if (checkonly)
2571 				break;
2572 
2573 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
2574 			ipp->ipp_use_min_mtu = *i1;
2575 			break;
2576 
2577 		/*
2578 		 * This option can't be set.  Its only returned via
2579 		 * getsockopt() or ancillary data.
2580 		 */
2581 		case IPV6_PATHMTU:
2582 			return (EINVAL);
2583 
2584 		case IPV6_BOUND_PIF:
2585 		case IPV6_SEC_OPT:
2586 		case IPV6_DONTFAILOVER_IF:
2587 		case IPV6_SRC_PREFERENCES:
2588 		case IPV6_V6ONLY:
2589 			/* Handled at IP level */
2590 			return (-EINVAL);
2591 		default:
2592 			*outlenp = 0;
2593 			return (EINVAL);
2594 		}
2595 		break;
2596 	}		/* end IPPROTO_IPV6 */
2597 
2598 	case IPPROTO_ICMPV6:
2599 		/*
2600 		 * Only allow IPv6 option processing on IPv6 sockets.
2601 		 */
2602 		if (icmp->icmp_family != AF_INET6) {
2603 			*outlenp = 0;
2604 			return (ENOPROTOOPT);
2605 		}
2606 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
2607 			*outlenp = 0;
2608 			return (ENOPROTOOPT);
2609 		}
2610 		switch (name) {
2611 		case ICMP6_FILTER:
2612 			if (!checkonly) {
2613 				if ((inlen != 0) &&
2614 				    (inlen != sizeof (icmp6_filter_t)))
2615 					return (EINVAL);
2616 
2617 				if (inlen == 0) {
2618 					if (icmp->icmp_filter != NULL) {
2619 						kmem_free(icmp->icmp_filter,
2620 						    sizeof (icmp6_filter_t));
2621 						icmp->icmp_filter = NULL;
2622 					}
2623 				} else {
2624 					if (icmp->icmp_filter == NULL) {
2625 						icmp->icmp_filter = kmem_alloc(
2626 						    sizeof (icmp6_filter_t),
2627 						    KM_NOSLEEP);
2628 						if (icmp->icmp_filter == NULL) {
2629 							*outlenp = 0;
2630 							return (ENOBUFS);
2631 						}
2632 					}
2633 					(void) bcopy(invalp, icmp->icmp_filter,
2634 					    inlen);
2635 				}
2636 			}
2637 			break;
2638 
2639 		default:
2640 			*outlenp = 0;
2641 			return (EINVAL);
2642 		}
2643 		break;
2644 	default:
2645 		*outlenp = 0;
2646 		return (EINVAL);
2647 	}
2648 	/*
2649 	 * Common case of OK return with outval same as inval.
2650 	 */
2651 	if (invalp != outvalp) {
2652 		/* don't trust bcopy for identical src/dst */
2653 		(void) bcopy(invalp, outvalp, inlen);
2654 	}
2655 	*outlenp = inlen;
2656 	return (0);
2657 }
2658 
2659 /*
2660  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
2661  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
2662  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
2663  * headers.
2664  * Returns failure if can't allocate memory.
2665  */
2666 static int
2667 icmp_build_hdrs(queue_t *q, icmp_t *icmp)
2668 {
2669 	uchar_t	*hdrs;
2670 	uint_t	hdrs_len;
2671 	ip6_t	*ip6h;
2672 	ip6i_t	*ip6i;
2673 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
2674 
2675 	hdrs_len = ip_total_hdrs_len_v6(ipp);
2676 	ASSERT(hdrs_len != 0);
2677 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
2678 		/* Need to reallocate */
2679 		if (hdrs_len != 0) {
2680 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
2681 			if (hdrs == NULL)
2682 				return (ENOMEM);
2683 		} else {
2684 			hdrs = NULL;
2685 		}
2686 		if (icmp->icmp_sticky_hdrs_len != 0) {
2687 			kmem_free(icmp->icmp_sticky_hdrs,
2688 			    icmp->icmp_sticky_hdrs_len);
2689 		}
2690 		icmp->icmp_sticky_hdrs = hdrs;
2691 		icmp->icmp_sticky_hdrs_len = hdrs_len;
2692 	}
2693 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
2694 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
2695 
2696 	/* Set header fields not in ipp */
2697 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
2698 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
2699 		ip6h = (ip6_t *)&ip6i[1];
2700 
2701 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
2702 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
2703 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
2704 		}
2705 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
2706 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
2707 		}
2708 	} else {
2709 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
2710 	}
2711 
2712 	if (!(ipp->ipp_fields & IPPF_ADDR))
2713 		ip6h->ip6_src = icmp->icmp_v6src;
2714 
2715 	/* Try to get everything in a single mblk */
2716 	if (hdrs_len > icmp->icmp_max_hdr_len) {
2717 		icmp->icmp_max_hdr_len = hdrs_len;
2718 		(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
2719 		    icmp_wroff_extra);
2720 	}
2721 	return (0);
2722 }
2723 
2724 /*
2725  * Set optbuf and optlen for the option.
2726  * If sticky is set allocate memory (if not already present).
2727  * Otherwise just point optbuf and optlen at invalp and inlen.
2728  * Returns failure if memory can not be allocated.
2729  */
2730 static int
2731 icmp_pkt_set(uchar_t *invalp, uint_t inlen, boolean_t sticky,
2732     uchar_t **optbufp, uint_t *optlenp)
2733 {
2734 	uchar_t *optbuf;
2735 
2736 	if (!sticky) {
2737 		*optbufp = invalp;
2738 		*optlenp = inlen;
2739 		return (0);
2740 	}
2741 	if (inlen == *optlenp) {
2742 		/* Unchanged length - no need to realocate */
2743 		bcopy(invalp, *optbufp, inlen);
2744 		return (0);
2745 	}
2746 	if (inlen != 0) {
2747 		/* Allocate new buffer before free */
2748 		optbuf = kmem_alloc(inlen, KM_NOSLEEP);
2749 		if (optbuf == NULL)
2750 			return (ENOMEM);
2751 	} else {
2752 		optbuf = NULL;
2753 	}
2754 	/* Free old buffer */
2755 	if (*optlenp != 0)
2756 		kmem_free(*optbufp, *optlenp);
2757 
2758 	bcopy(invalp, optbuf, inlen);
2759 	*optbufp = optbuf;
2760 	*optlenp = inlen;
2761 	return (0);
2762 }
2763 
2764 /*
2765  * This routine retrieves the value of an ND variable in a icmpparam_t
2766  * structure.  It is called through nd_getset when a user reads the
2767  * variable.
2768  */
2769 /* ARGSUSED */
2770 static int
2771 icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
2772 {
2773 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
2774 
2775 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
2776 	return (0);
2777 }
2778 
2779 /*
2780  * Walk through the param array specified registering each element with the
2781  * named dispatch (ND) handler.
2782  */
2783 static boolean_t
2784 icmp_param_register(icmpparam_t *icmppa, int cnt)
2785 {
2786 	for (; cnt-- > 0; icmppa++) {
2787 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
2788 			if (!nd_load(&icmp_g_nd, icmppa->icmp_param_name,
2789 			    icmp_param_get, icmp_param_set,
2790 			    (caddr_t)icmppa)) {
2791 				nd_free(&icmp_g_nd);
2792 				return (B_FALSE);
2793 			}
2794 		}
2795 	}
2796 	if (!nd_load(&icmp_g_nd, "icmp_status", icmp_status_report, NULL,
2797 	    NULL)) {
2798 		nd_free(&icmp_g_nd);
2799 		return (B_FALSE);
2800 	}
2801 	return (B_TRUE);
2802 }
2803 
2804 /* This routine sets an ND variable in a icmpparam_t structure. */
2805 /* ARGSUSED */
2806 static int
2807 icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
2808 {
2809 	long		new_value;
2810 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
2811 
2812 	/*
2813 	 * Fail the request if the new value does not lie within the
2814 	 * required bounds.
2815 	 */
2816 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
2817 	    new_value < icmppa->icmp_param_min ||
2818 	    new_value > icmppa->icmp_param_max) {
2819 		return (EINVAL);
2820 	}
2821 	/* Set the new value */
2822 	icmppa->icmp_param_value = new_value;
2823 	return (0);
2824 }
2825 
2826 static void
2827 icmp_rput(queue_t *q, mblk_t *mp)
2828 {
2829 	struct T_unitdata_ind	*tudi;
2830 	uchar_t			*rptr;
2831 	struct T_error_ack	*tea;
2832 	icmp_t			*icmp;
2833 	sin_t			*sin;
2834 	sin6_t			*sin6;
2835 	ip6_t			*ip6h;
2836 	ip6i_t			*ip6i;
2837 	mblk_t			*mp1;
2838 	int			hdr_len;
2839 	ipha_t			*ipha;
2840 	int			udi_size;	/* Size of T_unitdata_ind */
2841 	uint_t			ipvers;
2842 	ip6_pkt_t		ipp;
2843 	uint8_t			nexthdr;
2844 	boolean_t		recvif = B_FALSE;
2845 	in_pktinfo_t		*pinfo;
2846 	mblk_t			*options_mp = NULL;
2847 	uint_t			icmp_opt = 0;
2848 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
2849 
2850 	icmp = (icmp_t *)q->q_ptr;
2851 	if (icmp->icmp_restricted) {
2852 		putnext(q, mp);
2853 		return;
2854 	}
2855 
2856 	if (mp->b_datap->db_type == M_CTL) {
2857 		/*
2858 		 * IP sends up the IPSEC_IN message for handling IPSEC
2859 		 * policy at the TCP level. We don't need it here.
2860 		 */
2861 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
2862 			mp1 = mp->b_cont;
2863 			freeb(mp);
2864 			mp = mp1;
2865 		} else {
2866 			pinfo = (in_pktinfo_t *)mp->b_rptr;
2867 			if ((icmp->icmp_recvif != 0) &&
2868 			    (pinfo->in_pkt_ulp_type == IN_PKTINFO)) {
2869 				/*
2870 				 * IP has passed the options in mp and the
2871 				 * actual data is in b_cont.
2872 				 */
2873 				recvif = B_TRUE;
2874 				/*
2875 				 * We are here bcos IP_RECVIF is set so we need
2876 				 * to extract the options mblk and adjust the
2877 				 * rptr
2878 				 */
2879 				options_mp = mp;
2880 				mp = mp->b_cont;
2881 			}
2882 		}
2883 	}
2884 
2885 	rptr = mp->b_rptr;
2886 	switch (mp->b_datap->db_type) {
2887 	case M_DATA:
2888 		/*
2889 		 * M_DATA messages contain IP packets.  They are handled
2890 		 * following the switch.
2891 		 */
2892 		break;
2893 	case M_PROTO:
2894 	case M_PCPROTO:
2895 		/* M_PROTO messages contain some type of TPI message. */
2896 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
2897 			freemsg(mp);
2898 			return;
2899 		}
2900 		tea = (struct T_error_ack *)rptr;
2901 		switch (tea->PRIM_type) {
2902 		case T_ERROR_ACK:
2903 			switch (tea->ERROR_prim) {
2904 			case O_T_BIND_REQ:
2905 			case T_BIND_REQ:
2906 				/*
2907 				 * If our O_T_BIND_REQ/T_BIND_REQ fails,
2908 				 * clear out the source address before
2909 				 * passing the message upstream.
2910 				 * If this was caused by a T_CONN_REQ
2911 				 * revert back to bound state.
2912 				 */
2913 				if (icmp->icmp_state == TS_UNBND) {
2914 					/*
2915 					 * TPI has not yet bound - bind sent by
2916 					 * icmp_bind_proto.
2917 					 */
2918 					freemsg(mp);
2919 					return;
2920 				}
2921 				if (icmp->icmp_state == TS_DATA_XFER) {
2922 					/* Connect failed */
2923 					tea->ERROR_prim = T_CONN_REQ;
2924 					icmp->icmp_v6src =
2925 					    icmp->icmp_bound_v6src;
2926 					icmp->icmp_state = TS_IDLE;
2927 					if (icmp->icmp_family == AF_INET6)
2928 						(void) icmp_build_hdrs(q, icmp);
2929 					break;
2930 				}
2931 
2932 				if (icmp->icmp_discon_pending) {
2933 					tea->ERROR_prim = T_DISCON_REQ;
2934 					icmp->icmp_discon_pending = 0;
2935 				}
2936 				V6_SET_ZERO(icmp->icmp_v6src);
2937 				V6_SET_ZERO(icmp->icmp_bound_v6src);
2938 				icmp->icmp_state = TS_UNBND;
2939 				if (icmp->icmp_family == AF_INET6)
2940 					(void) icmp_build_hdrs(q, icmp);
2941 				break;
2942 			default:
2943 				break;
2944 			}
2945 			break;
2946 		case T_BIND_ACK:
2947 			icmp_rput_bind_ack(q, mp);
2948 			return;
2949 
2950 		case T_OPTMGMT_ACK:
2951 		case T_OK_ACK:
2952 			if (tea->PRIM_type == T_OK_ACK) {
2953 				struct T_ok_ack *toa;
2954 				toa = (struct T_ok_ack *)rptr;
2955 				if (toa->CORRECT_prim == T_UNBIND_REQ) {
2956 					/*
2957 					 * If somebody sets IPSEC options, IP
2958 					 * sends some IPSEC info which is used
2959 					 * by the TCP for detached connections.
2960 					 * We don't need it here.
2961 					 */
2962 					if ((mp1 = mp->b_cont) != NULL) {
2963 						freemsg(mp1);
2964 						mp->b_cont = NULL;
2965 					}
2966 				}
2967 			}
2968 			break;
2969 		default:
2970 			freemsg(mp);
2971 			return;
2972 		}
2973 		putnext(q, mp);
2974 		return;
2975 	case M_CTL:
2976 		if (recvif) {
2977 			/*
2978 			 * IP has passed the options in mp and the actual data
2979 			 * is in b_cont. Jump to normal data processing.
2980 			 */
2981 			break;
2982 		}
2983 
2984 		/* Contains ICMP packet from IP */
2985 		icmp_icmp_error(q, mp);
2986 		return;
2987 	default:
2988 		putnext(q, mp);
2989 		return;
2990 	}
2991 
2992 	/*
2993 	 * Discard message if it is misaligned or smaller than the IP header.
2994 	 */
2995 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
2996 		freemsg(mp);
2997 		if (options_mp != NULL)
2998 			freeb(options_mp);
2999 		BUMP_MIB(&rawip_mib, rawipInErrors);
3000 		return;
3001 	}
3002 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
3003 
3004 	/* Handle M_DATA messages containing IP packets messages */
3005 	if (ipvers == IPV4_VERSION) {
3006 		/*
3007 		 * Special case where IP attaches
3008 		 * the IRE needs to be handled so that we don't send up
3009 		 * IRE to the user land.
3010 		 */
3011 		ipha = (ipha_t *)rptr;
3012 		hdr_len = IPH_HDR_LENGTH(ipha);
3013 
3014 		if (ipha->ipha_protocol == IPPROTO_TCP) {
3015 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
3016 
3017 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
3018 			    TH_SYN) && mp->b_cont != NULL) {
3019 				mp1 = mp->b_cont;
3020 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
3021 					freeb(mp1);
3022 					mp->b_cont = NULL;
3023 				}
3024 			}
3025 		}
3026 		if (icmp_bsd_compat) {
3027 			ushort_t len;
3028 			len = ntohs(ipha->ipha_length);
3029 
3030 			if (mp->b_datap->db_ref > 1) {
3031 				/*
3032 				 * Allocate a new IP header so that we can
3033 				 * modify ipha_length.
3034 				 */
3035 				mblk_t	*mp1;
3036 
3037 				mp1 = allocb(hdr_len, BPRI_MED);
3038 				if (!mp1) {
3039 					freemsg(mp);
3040 					if (options_mp != NULL)
3041 						freeb(options_mp);
3042 					BUMP_MIB(&rawip_mib, rawipInErrors);
3043 					return;
3044 				}
3045 				bcopy(rptr, mp1->b_rptr, hdr_len);
3046 				mp->b_rptr = rptr + hdr_len;
3047 				rptr = mp1->b_rptr;
3048 				ipha = (ipha_t *)rptr;
3049 				mp1->b_cont = mp;
3050 				mp1->b_wptr = rptr + hdr_len;
3051 				mp = mp1;
3052 			}
3053 			len -= hdr_len;
3054 			ipha->ipha_length = htons(len);
3055 		}
3056 	}
3057 
3058 	/*
3059 	 * This is the inbound data path.  Packets are passed upstream as
3060 	 * T_UNITDATA_IND messages with full IP headers still attached.
3061 	 */
3062 	if (icmp->icmp_family == AF_INET) {
3063 		ASSERT(ipvers == IPV4_VERSION);
3064 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
3065 		if (recvif) {
3066 			udi_size += sizeof (struct T_opthdr) +
3067 			    sizeof (uint_t);
3068 		}
3069 		mp1 = allocb(udi_size, BPRI_MED);
3070 		if (mp1 == NULL) {
3071 			freemsg(mp);
3072 			if (options_mp != NULL)
3073 				freeb(options_mp);
3074 			BUMP_MIB(&rawip_mib, rawipInErrors);
3075 			return;
3076 		}
3077 		mp1->b_cont = mp;
3078 		mp = mp1;
3079 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
3080 		mp->b_datap->db_type = M_PROTO;
3081 		mp->b_wptr = (uchar_t *)tudi + udi_size;
3082 		tudi->PRIM_type = T_UNITDATA_IND;
3083 		tudi->SRC_length = sizeof (sin_t);
3084 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
3085 		sin = (sin_t *)&tudi[1];
3086 		*sin = sin_null;
3087 		sin->sin_family = AF_INET;
3088 		sin->sin_addr.s_addr = ipha->ipha_src;
3089 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
3090 		    sizeof (sin_t);
3091 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
3092 		tudi->OPT_length = udi_size;
3093 
3094 		/*
3095 		 * Add options if IP_RECVIF is set
3096 		 */
3097 		if (udi_size != 0) {
3098 			char *dstopt;
3099 
3100 			dstopt = (char *)&sin[1];
3101 			if (recvif) {
3102 
3103 				struct T_opthdr *toh;
3104 				uint_t		*dstptr;
3105 
3106 				toh = (struct T_opthdr *)dstopt;
3107 				toh->level = IPPROTO_IP;
3108 				toh->name = IP_RECVIF;
3109 				toh->len = sizeof (struct T_opthdr) +
3110 					sizeof (uint_t);
3111 				toh->status = 0;
3112 				dstopt += sizeof (struct T_opthdr);
3113 				dstptr = (uint_t *)dstopt;
3114 				*dstptr = pinfo->in_pkt_ifindex;
3115 				dstopt += sizeof (uint_t);
3116 				freeb(options_mp);
3117 				udi_size -= toh->len;
3118 			}
3119 
3120 			/* Consumed all of allocated space */
3121 			ASSERT(udi_size == 0);
3122 		}
3123 
3124 		BUMP_MIB(&rawip_mib, rawipInDatagrams);
3125 		putnext(q, mp);
3126 		return;
3127 	}
3128 
3129 	/*
3130 	 * We don't need options_mp in the IPv6 path.
3131 	 */
3132 	if (options_mp != NULL) {
3133 		freeb(options_mp);
3134 		options_mp = NULL;
3135 	}
3136 
3137 	/*
3138 	 * Discard message if it is smaller than the IPv6 header
3139 	 * or if the header is malformed.
3140 	 */
3141 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
3142 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
3143 	    icmp->icmp_family != AF_INET6) {
3144 		freemsg(mp);
3145 		BUMP_MIB(&rawip_mib, rawipInErrors);
3146 		return;
3147 	}
3148 
3149 	/* Initialize */
3150 	ipp.ipp_fields = 0;
3151 
3152 	ip6h = (ip6_t *)rptr;
3153 	/*
3154 	 * Call on ip_find_hdr_v6 which gets the total hdr len
3155 	 * as well as individual lenghts of ext hdrs (and ptrs to
3156 	 * them).
3157 	 */
3158 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
3159 		/* Look for ifindex information */
3160 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
3161 			ip6i = (ip6i_t *)ip6h;
3162 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
3163 				ASSERT(ip6i->ip6i_ifindex != 0);
3164 				ipp.ipp_fields |= IPPF_IFINDEX;
3165 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
3166 			}
3167 			rptr = (uchar_t *)&ip6i[1];
3168 			mp->b_rptr = rptr;
3169 			if (rptr == mp->b_wptr) {
3170 				mp1 = mp->b_cont;
3171 				freeb(mp);
3172 				mp = mp1;
3173 				rptr = mp->b_rptr;
3174 			}
3175 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
3176 			ip6h = (ip6_t *)rptr;
3177 		}
3178 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
3179 	} else {
3180 		hdr_len = IPV6_HDR_LEN;
3181 		ip6i = NULL;
3182 		nexthdr = ip6h->ip6_nxt;
3183 	}
3184 	/*
3185 	 * One special case where IP attaches the IRE needs to
3186 	 * be handled so that we don't send up IRE to the user land.
3187 	 */
3188 	if (nexthdr == IPPROTO_TCP) {
3189 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
3190 
3191 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
3192 		    mp->b_cont != NULL) {
3193 			mp1 = mp->b_cont;
3194 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
3195 				freeb(mp1);
3196 				mp->b_cont = NULL;
3197 			}
3198 		}
3199 	}
3200 	/*
3201 	 * Check a filter for ICMPv6 types if needed.
3202 	 * Verify raw checksums if needed.
3203 	 */
3204 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
3205 		if (icmp->icmp_filter != NULL) {
3206 			int type;
3207 
3208 			/* Assumes that IP has done the pullupmsg */
3209 			type = mp->b_rptr[hdr_len];
3210 
3211 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
3212 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
3213 				freemsg(mp);
3214 				return;
3215 			}
3216 		} else {
3217 			/* Checksum */
3218 			uint16_t	*up;
3219 			uint32_t	sum;
3220 			int		remlen;
3221 
3222 			up = (uint16_t *)&ip6h->ip6_src;
3223 
3224 			remlen = msgdsize(mp) - hdr_len;
3225 			sum = htons(icmp->icmp_proto + remlen)
3226 			    + up[0] + up[1] + up[2] + up[3]
3227 			    + up[4] + up[5] + up[6] + up[7]
3228 			    + up[8] + up[9] + up[10] + up[11]
3229 			    + up[12] + up[13] + up[14] + up[15];
3230 			sum = (sum & 0xffff) + (sum >> 16);
3231 			sum = IP_CSUM(mp, hdr_len, sum);
3232 			if (sum != 0) {
3233 				/* IPv6 RAW checksum failed */
3234 				ip0dbg(("icmp_rput: RAW checksum "
3235 				    "failed %x\n", sum));
3236 				freemsg(mp);
3237 				BUMP_MIB(&rawip_mib, rawipInCksumErrs);
3238 				return;
3239 			}
3240 		}
3241 	}
3242 	/* Skip all the IPv6 headers per API */
3243 	mp->b_rptr += hdr_len;
3244 
3245 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
3246 
3247 	/*
3248 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
3249 	 * maintain state information, instead of relying on icmp_t
3250 	 * structure, since there arent any locks protecting these members
3251 	 * and there is a window where there might be a race between a
3252 	 * thread setting options on the write side and a thread reading
3253 	 * these options on the read size.
3254 	 */
3255 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
3256 	    IPPF_RTHDR|IPPF_IFINDEX)) {
3257 		if (icmp->icmp_ipv6_recvhopopts &&
3258 		    (ipp.ipp_fields & IPPF_HOPOPTS)) {
3259 			udi_size += sizeof (struct T_opthdr) +
3260 			    ipp.ipp_hopoptslen;
3261 			icmp_opt |= IPPF_HOPOPTS;
3262 		}
3263 		if ((icmp->icmp_ipv6_recvdstopts ||
3264 			icmp->icmp_old_ipv6_recvdstopts) &&
3265 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
3266 			udi_size += sizeof (struct T_opthdr) +
3267 			    ipp.ipp_dstoptslen;
3268 			icmp_opt |= IPPF_DSTOPTS;
3269 		}
3270 		if (((icmp->icmp_ipv6_recvdstopts &&
3271 		    icmp->icmp_ipv6_recvrthdr &&
3272 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
3273 		    icmp->icmp_ipv6_recvrtdstopts) &&
3274 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
3275 			udi_size += sizeof (struct T_opthdr) +
3276 			    ipp.ipp_rtdstoptslen;
3277 			icmp_opt |= IPPF_RTDSTOPTS;
3278 		}
3279 		if (icmp->icmp_ipv6_recvrthdr &&
3280 		    (ipp.ipp_fields & IPPF_RTHDR)) {
3281 			udi_size += sizeof (struct T_opthdr) +
3282 			    ipp.ipp_rthdrlen;
3283 			icmp_opt |= IPPF_RTHDR;
3284 		}
3285 		if (icmp->icmp_ipv6_recvpktinfo &&
3286 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
3287 			udi_size += sizeof (struct T_opthdr) +
3288 			    sizeof (struct in6_pktinfo);
3289 			icmp_opt |= IPPF_IFINDEX;
3290 		}
3291 	}
3292 	if (icmp->icmp_ipv6_recvhoplimit) {
3293 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
3294 		icmp_ipv6_recvhoplimit = B_TRUE;
3295 	}
3296 
3297 	if (icmp->icmp_ipv6_recvtclass)
3298 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
3299 
3300 	mp1 = allocb(udi_size, BPRI_MED);
3301 	if (mp1 == NULL) {
3302 		freemsg(mp);
3303 		BUMP_MIB(&rawip_mib, rawipInErrors);
3304 		return;
3305 	}
3306 	mp1->b_cont = mp;
3307 	mp = mp1;
3308 	mp->b_datap->db_type = M_PROTO;
3309 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
3310 	mp->b_wptr = (uchar_t *)tudi + udi_size;
3311 	tudi->PRIM_type = T_UNITDATA_IND;
3312 	tudi->SRC_length = sizeof (sin6_t);
3313 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
3314 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
3315 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
3316 	tudi->OPT_length = udi_size;
3317 	sin6 = (sin6_t *)&tudi[1];
3318 	sin6->sin6_port = 0;
3319 	sin6->sin6_family = AF_INET6;
3320 
3321 	sin6->sin6_addr = ip6h->ip6_src;
3322 	/* No sin6_flowinfo per API */
3323 	sin6->sin6_flowinfo = 0;
3324 	/* For link-scope source pass up scope id */
3325 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
3326 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
3327 		sin6->sin6_scope_id = ipp.ipp_ifindex;
3328 	else
3329 		sin6->sin6_scope_id = 0;
3330 
3331 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
3332 	    icmp->icmp_zoneid);
3333 
3334 	if (udi_size != 0) {
3335 		uchar_t *dstopt;
3336 
3337 		dstopt = (uchar_t *)&sin6[1];
3338 		if (icmp_opt & IPPF_IFINDEX) {
3339 			struct T_opthdr *toh;
3340 			struct in6_pktinfo *pkti;
3341 
3342 			toh = (struct T_opthdr *)dstopt;
3343 			toh->level = IPPROTO_IPV6;
3344 			toh->name = IPV6_PKTINFO;
3345 			toh->len = sizeof (struct T_opthdr) +
3346 			    sizeof (*pkti);
3347 			toh->status = 0;
3348 			dstopt += sizeof (struct T_opthdr);
3349 			pkti = (struct in6_pktinfo *)dstopt;
3350 			pkti->ipi6_addr = ip6h->ip6_dst;
3351 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
3352 			dstopt += sizeof (*pkti);
3353 			udi_size -= toh->len;
3354 		}
3355 		if (icmp_ipv6_recvhoplimit) {
3356 			struct T_opthdr *toh;
3357 
3358 			toh = (struct T_opthdr *)dstopt;
3359 			toh->level = IPPROTO_IPV6;
3360 			toh->name = IPV6_HOPLIMIT;
3361 			toh->len = sizeof (struct T_opthdr) +
3362 			    sizeof (uint_t);
3363 			toh->status = 0;
3364 			dstopt += sizeof (struct T_opthdr);
3365 			*(uint_t *)dstopt = ip6h->ip6_hops;
3366 			dstopt += sizeof (uint_t);
3367 			udi_size -= toh->len;
3368 		}
3369 		if (icmp->icmp_ipv6_recvtclass) {
3370 			struct T_opthdr *toh;
3371 
3372 			toh = (struct T_opthdr *)dstopt;
3373 			toh->level = IPPROTO_IPV6;
3374 			toh->name = IPV6_TCLASS;
3375 			toh->len = sizeof (struct T_opthdr) +
3376 			    sizeof (uint_t);
3377 			toh->status = 0;
3378 			dstopt += sizeof (struct T_opthdr);
3379 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
3380 			dstopt += sizeof (uint_t);
3381 			udi_size -= toh->len;
3382 		}
3383 		if (icmp_opt & IPPF_HOPOPTS) {
3384 			struct T_opthdr *toh;
3385 
3386 			toh = (struct T_opthdr *)dstopt;
3387 			toh->level = IPPROTO_IPV6;
3388 			toh->name = IPV6_HOPOPTS;
3389 			toh->len = sizeof (struct T_opthdr) +
3390 			    ipp.ipp_hopoptslen;
3391 			toh->status = 0;
3392 			dstopt += sizeof (struct T_opthdr);
3393 			bcopy(ipp.ipp_hopopts, dstopt,
3394 			    ipp.ipp_hopoptslen);
3395 			dstopt += ipp.ipp_hopoptslen;
3396 			udi_size -= toh->len;
3397 		}
3398 		if (icmp_opt & IPPF_RTDSTOPTS) {
3399 			struct T_opthdr *toh;
3400 
3401 			toh = (struct T_opthdr *)dstopt;
3402 			toh->level = IPPROTO_IPV6;
3403 			toh->name = IPV6_DSTOPTS;
3404 			toh->len = sizeof (struct T_opthdr) +
3405 			    ipp.ipp_rtdstoptslen;
3406 			toh->status = 0;
3407 			dstopt += sizeof (struct T_opthdr);
3408 			bcopy(ipp.ipp_rtdstopts, dstopt,
3409 			    ipp.ipp_rtdstoptslen);
3410 			dstopt += ipp.ipp_rtdstoptslen;
3411 			udi_size -= toh->len;
3412 		}
3413 		if (icmp_opt & IPPF_RTHDR) {
3414 			struct T_opthdr *toh;
3415 
3416 			toh = (struct T_opthdr *)dstopt;
3417 			toh->level = IPPROTO_IPV6;
3418 			toh->name = IPV6_RTHDR;
3419 			toh->len = sizeof (struct T_opthdr) +
3420 			    ipp.ipp_rthdrlen;
3421 			toh->status = 0;
3422 			dstopt += sizeof (struct T_opthdr);
3423 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
3424 			dstopt += ipp.ipp_rthdrlen;
3425 			udi_size -= toh->len;
3426 		}
3427 		if (icmp_opt & IPPF_DSTOPTS) {
3428 			struct T_opthdr *toh;
3429 
3430 			toh = (struct T_opthdr *)dstopt;
3431 			toh->level = IPPROTO_IPV6;
3432 			toh->name = IPV6_DSTOPTS;
3433 			toh->len = sizeof (struct T_opthdr) +
3434 			    ipp.ipp_dstoptslen;
3435 			toh->status = 0;
3436 			dstopt += sizeof (struct T_opthdr);
3437 			bcopy(ipp.ipp_dstopts, dstopt,
3438 			    ipp.ipp_dstoptslen);
3439 			dstopt += ipp.ipp_dstoptslen;
3440 			udi_size -= toh->len;
3441 		}
3442 		/* Consumed all of allocated space */
3443 		ASSERT(udi_size == 0);
3444 	}
3445 	BUMP_MIB(&rawip_mib, rawipInDatagrams);
3446 	putnext(q, mp);
3447 }
3448 
3449 /*
3450  * Process a T_BIND_ACK
3451  */
3452 static void
3453 icmp_rput_bind_ack(queue_t *q, mblk_t *mp)
3454 {
3455 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
3456 	mblk_t	*mp1;
3457 	ire_t	*ire;
3458 	struct T_bind_ack *tba;
3459 	uchar_t *addrp;
3460 	ipa_conn_t	*ac;
3461 	ipa6_conn_t	*ac6;
3462 
3463 	/*
3464 	 * We know if headers are included or not so we can
3465 	 * safely do this.
3466 	 */
3467 	if (icmp->icmp_state == TS_UNBND) {
3468 		/*
3469 		 * TPI has not yet bound - bind sent by
3470 		 * icmp_bind_proto.
3471 		 */
3472 		freemsg(mp);
3473 		return;
3474 	}
3475 	if (icmp->icmp_discon_pending)
3476 		icmp->icmp_discon_pending = 0;
3477 
3478 	/*
3479 	 * If a broadcast/multicast address was bound set
3480 	 * the source address to 0.
3481 	 * This ensures no datagrams with broadcast address
3482 	 * as source address are emitted (which would violate
3483 	 * RFC1122 - Hosts requirements)
3484 	 *
3485 	 * Note that when connecting the returned IRE is
3486 	 * for the destination address and we only perform
3487 	 * the broadcast check for the source address (it
3488 	 * is OK to connect to a broadcast/multicast address.)
3489 	 */
3490 	mp1 = mp->b_cont;
3491 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
3492 		ire = (ire_t *)mp1->b_rptr;
3493 
3494 		/*
3495 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
3496 		 * local address.
3497 		 */
3498 		if (ire->ire_type == IRE_BROADCAST &&
3499 		    icmp->icmp_state != TS_DATA_XFER) {
3500 			/* This was just a local bind to a MC/broadcast addr */
3501 			V6_SET_ZERO(icmp->icmp_v6src);
3502 			if (icmp->icmp_family == AF_INET6)
3503 				(void) icmp_build_hdrs(q, icmp);
3504 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
3505 			/*
3506 			 * Local address not yet set - pick it from the
3507 			 * T_bind_ack
3508 			 */
3509 			tba = (struct T_bind_ack *)mp->b_rptr;
3510 			addrp = &mp->b_rptr[tba->ADDR_offset];
3511 			switch (icmp->icmp_family) {
3512 			case AF_INET:
3513 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
3514 					ac = (ipa_conn_t *)addrp;
3515 				} else {
3516 					ASSERT(tba->ADDR_length ==
3517 					    sizeof (ipa_conn_x_t));
3518 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
3519 				}
3520 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
3521 				    &icmp->icmp_v6src);
3522 				break;
3523 			case AF_INET6:
3524 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
3525 					ac6 = (ipa6_conn_t *)addrp;
3526 				} else {
3527 					ASSERT(tba->ADDR_length ==
3528 					    sizeof (ipa6_conn_x_t));
3529 					ac6 = &((ipa6_conn_x_t *)
3530 					    addrp)->ac6x_conn;
3531 				}
3532 				icmp->icmp_v6src = ac6->ac6_laddr;
3533 				(void) icmp_build_hdrs(q, icmp);
3534 			}
3535 		}
3536 		mp1 = mp1->b_cont;
3537 	}
3538 	/*
3539 	 * Look for one or more appended ACK message added by
3540 	 * icmp_connect or icmp_disconnect.
3541 	 * If none found just send up the T_BIND_ACK.
3542 	 * icmp_connect has appended a T_OK_ACK and a
3543 	 * T_CONN_CON.
3544 	 * icmp_disconnect has appended a T_OK_ACK.
3545 	 */
3546 	if (mp1 != NULL) {
3547 		if (mp->b_cont == mp1)
3548 			mp->b_cont = NULL;
3549 		else {
3550 			ASSERT(mp->b_cont->b_cont == mp1);
3551 			mp->b_cont->b_cont = NULL;
3552 		}
3553 		freemsg(mp);
3554 		mp = mp1;
3555 		while (mp != NULL) {
3556 			mp1 = mp->b_cont;
3557 			mp->b_cont = NULL;
3558 			putnext(q, mp);
3559 			mp = mp1;
3560 		}
3561 		return;
3562 	}
3563 	freemsg(mp->b_cont);
3564 	mp->b_cont = NULL;
3565 	putnext(q, mp);
3566 }
3567 
3568 /*
3569  * return SNMP stuff in buffer in mpdata
3570  */
3571 static int
3572 icmp_snmp_get(queue_t *q, mblk_t *mpctl)
3573 {
3574 	mblk_t			*mpdata;
3575 	struct opthdr		*optp;
3576 
3577 	if (mpctl == NULL ||
3578 	    (mpdata = mpctl->b_cont) == NULL) {
3579 		return (0);
3580 	}
3581 
3582 	/* fixed length structure for IPv4 and IPv6 counters */
3583 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
3584 	optp->level = EXPER_RAWIP;
3585 	optp->name = 0;
3586 	(void) snmp_append_data(mpdata, (char *)&rawip_mib, sizeof (rawip_mib));
3587 	optp->len = msgdsize(mpdata);
3588 	qreply(q, mpctl);
3589 
3590 	return (1);
3591 }
3592 
3593 /*
3594  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
3595  * TODO:  If this ever actually tries to set anything, it needs to be
3596  * to do the appropriate locking.
3597  */
3598 /* ARGSUSED */
3599 static int
3600 icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
3601     uchar_t *ptr, int len)
3602 {
3603 	switch (level) {
3604 	case EXPER_RAWIP:
3605 		return (0);
3606 	default:
3607 		return (1);
3608 	}
3609 }
3610 
3611 /* Report for ndd "icmp_status" */
3612 /* ARGSUSED */
3613 static int
3614 icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
3615 {
3616 	IDP	idp;
3617 	icmp_t	*icmp;
3618 	char	*state;
3619 	char	laddrbuf[INET6_ADDRSTRLEN];
3620 	char	faddrbuf[INET6_ADDRSTRLEN];
3621 
3622 	(void) mi_mpprintf(mp,
3623 	    "RAWIP    " MI_COL_HDRPAD_STR
3624 	/*   01234567[89ABCDEF] */
3625 	    "  src addr        dest addr       state");
3626 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
3627 
3628 
3629 	for (idp = mi_first_ptr(&icmp_g_head);
3630 	    (icmp = (icmp_t *)idp) != NULL;
3631 	    idp = mi_next_ptr(&icmp_g_head, idp)) {
3632 		if (icmp->icmp_state == TS_UNBND)
3633 			state = "UNBOUND";
3634 		else if (icmp->icmp_state == TS_IDLE)
3635 			state = "IDLE";
3636 		else if (icmp->icmp_state == TS_DATA_XFER)
3637 			state = "CONNECTED";
3638 		else
3639 			state = "UnkState";
3640 
3641 		(void) mi_mpprintf(mp,
3642 		    MI_COL_PTRFMT_STR "%s %s %s",
3643 		    (void *)icmp,
3644 		    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
3645 		    sizeof (faddrbuf)),
3646 		    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
3647 		    sizeof (laddrbuf)),
3648 		    state);
3649 	}
3650 	return (0);
3651 }
3652 
3653 /*
3654  * This routine creates a T_UDERROR_IND message and passes it upstream.
3655  * The address and options are copied from the T_UNITDATA_REQ message
3656  * passed in mp.  This message is freed.
3657  */
3658 static void
3659 icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
3660 {
3661 	mblk_t	*mp1;
3662 	uchar_t	*rptr = mp->b_rptr;
3663 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
3664 
3665 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
3666 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
3667 	    tudr->OPT_length, err);
3668 	if (mp1)
3669 		qreply(q, mp1);
3670 	freemsg(mp);
3671 }
3672 
3673 /*
3674  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
3675  * After some error checking, the message is passed downstream to ip.
3676  */
3677 static void
3678 icmp_unbind(queue_t *q, mblk_t *mp)
3679 {
3680 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
3681 
3682 	/* If a bind has not been done, we can't unbind. */
3683 	if (icmp->icmp_state == TS_UNBND) {
3684 		icmp_err_ack(q, mp, TOUTSTATE, 0);
3685 		return;
3686 	}
3687 	V6_SET_ZERO(icmp->icmp_v6src);
3688 	V6_SET_ZERO(icmp->icmp_bound_v6src);
3689 	icmp->icmp_state = TS_UNBND;
3690 
3691 	if (icmp->icmp_family == AF_INET6) {
3692 		int error;
3693 
3694 		/* Rebuild the header template */
3695 		error = icmp_build_hdrs(q, icmp);
3696 		if (error != 0) {
3697 			icmp_err_ack(q, mp, TSYSERR, error);
3698 			return;
3699 		}
3700 	}
3701 	/* Pass the unbind to IP. */
3702 	putnext(q, mp);
3703 }
3704 
3705 /*
3706  * Process IPv4 packets that already include an IP header.
3707  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
3708  * IPPROTO_IGMP).
3709  */
3710 static void
3711 icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp)
3712 {
3713 	ipha_t	*ipha;
3714 	int	ip_hdr_length;
3715 	int	tp_hdr_len;
3716 	mblk_t	*mp1;
3717 	uint_t	pkt_len;
3718 
3719 	ipha = (ipha_t *)mp->b_rptr;
3720 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
3721 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
3722 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
3723 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3724 			freemsg(mp);
3725 			return;
3726 		}
3727 		ipha = (ipha_t *)mp->b_rptr;
3728 	}
3729 	ipha->ipha_version_and_hdr_length =
3730 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
3731 
3732 	/*
3733 	 * For the socket of SOCK_RAW type, the checksum is provided in the
3734 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
3735 	 * tell IP that the application has sent a complete IP header and not
3736 	 * to compute the transport checksum nor change the DF flag.
3737 	 */
3738 	ipha->ipha_ident = IP_HDR_INCLUDED;
3739 	ipha->ipha_hdr_checksum = 0;
3740 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
3741 	/* Insert options if any */
3742 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
3743 		/*
3744 		 * Put the IP header plus any transport header that is
3745 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
3746 		 * that at least the checksum field is in the first mblk.)
3747 		 */
3748 		switch (ipha->ipha_protocol) {
3749 		case IPPROTO_UDP:
3750 			tp_hdr_len = 8;
3751 			break;
3752 		case IPPROTO_TCP:
3753 			tp_hdr_len = 20;
3754 			break;
3755 		default:
3756 			tp_hdr_len = 0;
3757 			break;
3758 		}
3759 		/*
3760 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
3761 		 * tp_hdr_len bytes will be in a single mblk.
3762 		 */
3763 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
3764 		    tp_hdr_len)) {
3765 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
3766 			    tp_hdr_len)) {
3767 				BUMP_MIB(&rawip_mib, rawipOutErrors);
3768 				freemsg(mp);
3769 				return;
3770 			}
3771 			ipha = (ipha_t *)mp->b_rptr;
3772 		}
3773 
3774 		/*
3775 		 * if the length is larger then the max allowed IP packet,
3776 		 * then send an error and abort the processing.
3777 		 */
3778 		pkt_len = ntohs(ipha->ipha_length)
3779 		    + icmp->icmp_ip_snd_options_len;
3780 		if (pkt_len > IP_MAXPACKET) {
3781 			icmp_ud_err(q, mp, EMSGSIZE);
3782 			return;
3783 		}
3784 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra +
3785 		    tp_hdr_len, BPRI_LO))) {
3786 			icmp_ud_err(q, mp, ENOMEM);
3787 			return;
3788 		}
3789 		mp1->b_rptr += icmp_wroff_extra;
3790 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
3791 
3792 		ipha->ipha_length = htons((uint16_t)pkt_len);
3793 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
3794 
3795 		/* Copy transport header if any */
3796 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
3797 		mp1->b_wptr += tp_hdr_len;
3798 
3799 		/* Add options */
3800 		ipha = (ipha_t *)mp1->b_rptr;
3801 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
3802 		    icmp->icmp_ip_snd_options_len);
3803 
3804 		/* Drop IP header and transport header from original */
3805 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
3806 
3807 		mp1->b_cont = mp;
3808 		mp = mp1;
3809 		/*
3810 		 * Massage source route putting first source
3811 		 * route in ipha_dst.
3812 		 */
3813 		(void) ip_massage_options(ipha);
3814 	}
3815 	putnext(q, mp);
3816 }
3817 
3818 /*
3819  * This routine handles all messages passed downstream.  It either
3820  * consumes the message or passes it downstream; it never queues a
3821  * a message.
3822  */
3823 static void
3824 icmp_wput(queue_t *q, mblk_t *mp)
3825 {
3826 	uchar_t	*rptr = mp->b_rptr;
3827 	ipha_t	*ipha;
3828 	mblk_t	*mp1;
3829 	int	ip_hdr_length;
3830 #define	tudr ((struct T_unitdata_req *)rptr)
3831 	size_t	ip_len;
3832 	icmp_t	*icmp;
3833 	sin6_t	*sin6;
3834 	sin_t	*sin;
3835 	ipaddr_t	v4dst;
3836 
3837 	icmp = (icmp_t *)q->q_ptr;
3838 	if (icmp->icmp_restricted) {
3839 		icmp_wput_restricted(q, mp);
3840 		return;
3841 	}
3842 
3843 	switch (mp->b_datap->db_type) {
3844 	case M_DATA:
3845 		if (icmp->icmp_hdrincl) {
3846 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3847 			icmp_wput_hdrincl(q, mp, icmp);
3848 			return;
3849 		}
3850 		freemsg(mp);
3851 		return;
3852 	case M_PROTO:
3853 	case M_PCPROTO:
3854 		ip_len = mp->b_wptr - rptr;
3855 		if (ip_len >= sizeof (struct T_unitdata_req)) {
3856 			/* Expedite valid T_UNITDATA_REQ to below the switch */
3857 			if (((union T_primitives *)rptr)->type
3858 			    == T_UNITDATA_REQ)
3859 				break;
3860 		}
3861 		/* FALLTHRU */
3862 	default:
3863 		icmp_wput_other(q, mp);
3864 		return;
3865 	}
3866 
3867 	/* Handle T_UNITDATA_REQ messages here. */
3868 
3869 	if (icmp->icmp_state == TS_UNBND) {
3870 		/* If a port has not been bound to the stream, fail. */
3871 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3872 		icmp_ud_err(q, mp, EPROTO);
3873 		return;
3874 	}
3875 	mp1 = mp->b_cont;
3876 	if (mp1 == NULL) {
3877 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3878 		icmp_ud_err(q, mp, EPROTO);
3879 		return;
3880 	}
3881 
3882 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
3883 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3884 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
3885 		return;
3886 	}
3887 
3888 	switch (icmp->icmp_family) {
3889 	case AF_INET6:
3890 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
3891 		if (!OK_32PTR((char *)sin6) ||
3892 		    tudr->DEST_length != sizeof (sin6_t) ||
3893 		    sin6->sin6_family != AF_INET6) {
3894 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3895 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
3896 			return;
3897 		}
3898 
3899 		/* No support for mapped addresses on raw sockets */
3900 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3901 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3902 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
3903 			return;
3904 		}
3905 
3906 		/*
3907 		 * Destination is a native IPv6 address.
3908 		 * Send out an IPv6 format packet.
3909 		 */
3910 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
3911 		return;
3912 
3913 	case AF_INET:
3914 		sin = (sin_t *)&rptr[tudr->DEST_offset];
3915 		if (!OK_32PTR((char *)sin) ||
3916 		    tudr->DEST_length != sizeof (sin_t) ||
3917 		    sin->sin_family != AF_INET) {
3918 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3919 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
3920 			return;
3921 		}
3922 		/* Extract and ipaddr */
3923 		v4dst = sin->sin_addr.s_addr;
3924 		break;
3925 	}
3926 
3927 	/*
3928 	 * If options passed in, feed it for verification and handling
3929 	 */
3930 	if (tudr->OPT_length != 0) {
3931 		int error;
3932 
3933 		if (icmp_unitdata_opt_process(q, mp, &error,
3934 		    (uchar_t *)0) < 0) {
3935 			/* failure */
3936 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3937 			icmp_ud_err(q, mp, error);
3938 			return;
3939 		}
3940 		/*
3941 		 * Note: Success in processing options.
3942 		 * mp option buffer represented by
3943 		 * OPT_length/offset now potentially modified
3944 		 * and contain option setting results
3945 		 */
3946 	}
3947 
3948 	/* Protocol 255 contains full IP headers */
3949 	if (icmp->icmp_hdrincl) {
3950 		freeb(mp);
3951 		icmp_wput_hdrincl(q, mp1, icmp);
3952 		return;
3953 	}
3954 	/* Add an IP header */
3955 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
3956 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
3957 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
3958 	    mp1->b_datap->db_ref != 1 ||
3959 	    !OK_32PTR(ipha)) {
3960 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra,
3961 		    BPRI_LO))) {
3962 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3963 			icmp_ud_err(q, mp, ENOMEM);
3964 			return;
3965 		}
3966 		mp1->b_cont = mp->b_cont;
3967 		ipha = (ipha_t *)mp1->b_datap->db_lim;
3968 		mp1->b_wptr = (uchar_t *)ipha;
3969 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
3970 	}
3971 #ifdef	_BIG_ENDIAN
3972 	/* Set version, header length, and tos */
3973 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
3974 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
3975 		icmp->icmp_type_of_service);
3976 	/* Set ttl and protocol */
3977 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
3978 #else
3979 	/* Set version, header length, and tos */
3980 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
3981 	    ((icmp->icmp_type_of_service << 8) |
3982 		((IP_VERSION << 4) | (ip_hdr_length>>2)));
3983 	/* Set ttl and protocol */
3984 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
3985 #endif
3986 	/*
3987 	 * Copy our address into the packet.  If this is zero,
3988 	 * ip will fill in the real source address.
3989 	 */
3990 	IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
3991 	ipha->ipha_fragment_offset_and_flags = 0;
3992 
3993 	/*
3994 	 * For the socket of SOCK_RAW type, the checksum is provided in the
3995 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
3996 	 * tell IP that the application has sent a complete IP header and not
3997 	 * to compute the transport checksum nor change the DF flag.
3998 	 */
3999 	ipha->ipha_ident = IP_HDR_INCLUDED;
4000 
4001 	/* Finish common formatting of the packet. */
4002 	mp1->b_rptr = (uchar_t *)ipha;
4003 
4004 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
4005 	if (mp1->b_cont != NULL)
4006 		ip_len += msgdsize(mp1->b_cont);
4007 
4008 	/*
4009 	 * Set the length into the IP header.
4010 	 * If the length is greater than the maximum allowed by IP,
4011 	 * then free the message and return. Do not try and send it
4012 	 * as this can cause problems in layers below.
4013 	 */
4014 	if (ip_len > IP_MAXPACKET) {
4015 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4016 		icmp_ud_err(q, mp, EMSGSIZE);
4017 		return;
4018 	}
4019 	ipha->ipha_length = htons((uint16_t)ip_len);
4020 	/*
4021 	 * Copy in the destination address from the T_UNITDATA
4022 	 * request
4023 	 */
4024 	if (v4dst == INADDR_ANY)
4025 		ipha->ipha_dst = htonl(INADDR_LOOPBACK);
4026 	else
4027 		ipha->ipha_dst = v4dst;
4028 
4029 	/*
4030 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
4031 	 */
4032 	if (CLASSD(v4dst))
4033 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
4034 
4035 	/* Copy in options if any */
4036 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
4037 		bcopy(icmp->icmp_ip_snd_options,
4038 		    &ipha[1], icmp->icmp_ip_snd_options_len);
4039 		/*
4040 		 * Massage source route putting first source route in ipha_dst.
4041 		 * Ignore the destination in the T_unitdata_req.
4042 		 */
4043 		(void) ip_massage_options(ipha);
4044 	}
4045 	freeb(mp);
4046 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
4047 	putnext(q, mp1);
4048 #undef	ipha
4049 #undef tudr
4050 }
4051 
4052 /*
4053  * icmp_wput_ipv6():
4054  * Assumes that icmp_wput did some sanity checking on the destination
4055  * address.
4056  */
4057 void
4058 icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
4059 {
4060 	ip6_t			*ip6h;
4061 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
4062 	mblk_t			*mp1;
4063 	int			ip_hdr_len = IPV6_HDR_LEN;
4064 	size_t			ip_len;
4065 	icmp_t			*icmp;
4066 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
4067 	ip6_pkt_t		*ipp = &ipp_s;
4068 	ip6_pkt_t		*tipp;
4069 	uint32_t		csum = 0;
4070 	uint_t			ignore = 0;
4071 	uint_t			option_exists = 0, is_sticky = 0;
4072 	uint8_t			*cp;
4073 	uint8_t			*nxthdr_ptr;
4074 
4075 	icmp = (icmp_t *)q->q_ptr;
4076 
4077 	/*
4078 	 * If the local address is a mapped address return
4079 	 * an error.
4080 	 * It would be possible to send an IPv6 packet but the
4081 	 * response would never make it back to the application
4082 	 * since it is bound to a mapped address.
4083 	 */
4084 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
4085 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4086 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
4087 		return;
4088 	}
4089 
4090 	ipp->ipp_fields = 0;
4091 	ipp->ipp_sticky_ignored = 0;
4092 
4093 	/*
4094 	 * If TPI options passed in, feed it for verification and handling
4095 	 */
4096 	if (tudr_optlen != 0) {
4097 		int error;
4098 
4099 		if (icmp_unitdata_opt_process(q, mp, &error,
4100 		    (void *)ipp) < 0) {
4101 			/* failure */
4102 			BUMP_MIB(&rawip_mib, rawipOutErrors);
4103 			icmp_ud_err(q, mp, error);
4104 			return;
4105 		}
4106 		ignore = ipp->ipp_sticky_ignored;
4107 		ASSERT(error == 0);
4108 	}
4109 
4110 	if (sin6->sin6_scope_id != 0 &&
4111 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
4112 		/*
4113 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
4114 		 * option nor ancillary data.  It needs to be
4115 		 * explicitly set in options_exists.
4116 		 */
4117 		option_exists |= IPPF_SCOPE_ID;
4118 	}
4119 
4120 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
4121 	    (ipp->ipp_fields == 0)) {
4122 		/* No sticky options nor ancillary data. */
4123 		goto no_options;
4124 	}
4125 
4126 	/*
4127 	 * Go through the options figuring out where each is going to
4128 	 * come from and build two masks.  The first mask indicates if
4129 	 * the option exists at all.  The second mask indicates if the
4130 	 * option is sticky or ancillary.
4131 	 */
4132 	if (!(ignore & IPPF_HOPOPTS)) {
4133 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
4134 			option_exists |= IPPF_HOPOPTS;
4135 			ip_hdr_len += ipp->ipp_hopoptslen;
4136 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
4137 			option_exists |= IPPF_HOPOPTS;
4138 			is_sticky |= IPPF_HOPOPTS;
4139 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
4140 		}
4141 	}
4142 
4143 	if (!(ignore & IPPF_RTHDR)) {
4144 		if (ipp->ipp_fields & IPPF_RTHDR) {
4145 			option_exists |= IPPF_RTHDR;
4146 			ip_hdr_len += ipp->ipp_rthdrlen;
4147 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
4148 			option_exists |= IPPF_RTHDR;
4149 			is_sticky |= IPPF_RTHDR;
4150 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
4151 		}
4152 	}
4153 
4154 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
4155 		/*
4156 		 * Need to have a router header to use these.
4157 		 */
4158 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
4159 			option_exists |= IPPF_RTDSTOPTS;
4160 			ip_hdr_len += ipp->ipp_rtdstoptslen;
4161 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
4162 			option_exists |= IPPF_RTDSTOPTS;
4163 			is_sticky |= IPPF_RTDSTOPTS;
4164 			ip_hdr_len +=
4165 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
4166 		}
4167 	}
4168 
4169 	if (!(ignore & IPPF_DSTOPTS)) {
4170 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
4171 			option_exists |= IPPF_DSTOPTS;
4172 			ip_hdr_len += ipp->ipp_dstoptslen;
4173 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
4174 			option_exists |= IPPF_DSTOPTS;
4175 			is_sticky |= IPPF_DSTOPTS;
4176 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
4177 		}
4178 	}
4179 
4180 	if (!(ignore & IPPF_IFINDEX)) {
4181 		if (ipp->ipp_fields & IPPF_IFINDEX) {
4182 			option_exists |= IPPF_IFINDEX;
4183 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
4184 			option_exists |= IPPF_IFINDEX;
4185 			is_sticky |= IPPF_IFINDEX;
4186 		}
4187 	}
4188 
4189 	if (!(ignore & IPPF_ADDR)) {
4190 		if (ipp->ipp_fields & IPPF_ADDR) {
4191 			option_exists |= IPPF_ADDR;
4192 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
4193 			option_exists |= IPPF_ADDR;
4194 			is_sticky |= IPPF_ADDR;
4195 		}
4196 	}
4197 
4198 	if (!(ignore & IPPF_DONTFRAG)) {
4199 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
4200 			option_exists |= IPPF_DONTFRAG;
4201 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
4202 			option_exists |= IPPF_DONTFRAG;
4203 			is_sticky |= IPPF_DONTFRAG;
4204 		}
4205 	}
4206 
4207 	if (!(ignore & IPPF_USE_MIN_MTU)) {
4208 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
4209 			option_exists |= IPPF_USE_MIN_MTU;
4210 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
4211 		    IPPF_USE_MIN_MTU) {
4212 			option_exists |= IPPF_USE_MIN_MTU;
4213 			is_sticky |= IPPF_USE_MIN_MTU;
4214 		}
4215 	}
4216 
4217 	if (!(ignore & IPPF_NEXTHOP)) {
4218 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
4219 			option_exists |= IPPF_NEXTHOP;
4220 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
4221 			option_exists |= IPPF_NEXTHOP;
4222 			is_sticky |= IPPF_NEXTHOP;
4223 		}
4224 	}
4225 
4226 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4227 		option_exists |= IPPF_HOPLIMIT;
4228 	/* IPV6_HOPLIMIT can never be sticky */
4229 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4230 
4231 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4232 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4233 		option_exists |= IPPF_UNICAST_HOPS;
4234 		is_sticky |= IPPF_UNICAST_HOPS;
4235 	}
4236 
4237 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4238 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4239 		option_exists |= IPPF_MULTICAST_HOPS;
4240 		is_sticky |= IPPF_MULTICAST_HOPS;
4241 	}
4242 
4243 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
4244 		/* This is a sticky socket option only */
4245 		option_exists |= IPPF_NO_CKSUM;
4246 		is_sticky |= IPPF_NO_CKSUM;
4247 	}
4248 
4249 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
4250 		/* This is a sticky socket option only */
4251 		option_exists |= IPPF_RAW_CKSUM;
4252 		is_sticky |= IPPF_RAW_CKSUM;
4253 	}
4254 
4255 	if (!(ignore & IPPF_TCLASS)) {
4256 		if (ipp->ipp_fields & IPPF_TCLASS) {
4257 			option_exists |= IPPF_TCLASS;
4258 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
4259 			option_exists |= IPPF_TCLASS;
4260 			is_sticky |= IPPF_TCLASS;
4261 		}
4262 	}
4263 
4264 no_options:
4265 
4266 	/*
4267 	 * If any options carried in the ip6i_t were specified, we
4268 	 * need to account for the ip6i_t in the data we'll be sending
4269 	 * down.
4270 	 */
4271 	if (option_exists & IPPF_HAS_IP6I)
4272 		ip_hdr_len += sizeof (ip6i_t);
4273 
4274 	/* check/fix buffer config, setup pointers into it */
4275 	mp1 = mp->b_cont;
4276 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
4277 	if ((mp1->b_datap->db_ref != 1) ||
4278 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
4279 	    !OK_32PTR(ip6h)) {
4280 		/* Try to get everything in a single mblk next time */
4281 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
4282 			icmp->icmp_max_hdr_len = ip_hdr_len;
4283 			(void) mi_set_sth_wroff(RD(q),
4284 			    icmp->icmp_max_hdr_len + icmp_wroff_extra);
4285 		}
4286 		mp1 = allocb(ip_hdr_len + icmp_wroff_extra, BPRI_LO);
4287 		if (!mp1) {
4288 			BUMP_MIB(&rawip_mib, rawipOutErrors);
4289 			icmp_ud_err(q, mp, ENOMEM);
4290 			return;
4291 		}
4292 		mp1->b_cont = mp->b_cont;
4293 		mp1->b_wptr = mp1->b_datap->db_lim;
4294 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
4295 	}
4296 	mp1->b_rptr = (unsigned char *)ip6h;
4297 	ip6i = (ip6i_t *)ip6h;
4298 
4299 #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
4300 	if (option_exists & IPPF_HAS_IP6I) {
4301 		ip6h = (ip6_t *)&ip6i[1];
4302 		ip6i->ip6i_flags = 0;
4303 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
4304 
4305 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
4306 		if (option_exists & IPPF_SCOPE_ID) {
4307 			ip6i->ip6i_flags |= IP6I_IFINDEX;
4308 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
4309 		} else if (option_exists & IPPF_IFINDEX) {
4310 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
4311 			ASSERT(tipp->ipp_ifindex != 0);
4312 			ip6i->ip6i_flags |= IP6I_IFINDEX;
4313 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
4314 		}
4315 
4316 		if (option_exists & IPPF_RAW_CKSUM) {
4317 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
4318 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
4319 		}
4320 
4321 		if (option_exists & IPPF_NO_CKSUM) {
4322 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
4323 		}
4324 
4325 		if (option_exists & IPPF_ADDR) {
4326 			/*
4327 			 * Enable per-packet source address verification if
4328 			 * IPV6_PKTINFO specified the source address.
4329 			 * ip6_src is set in the transport's _wput function.
4330 			 */
4331 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
4332 		}
4333 
4334 		if (option_exists & IPPF_DONTFRAG) {
4335 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
4336 		}
4337 
4338 		if (option_exists & IPPF_USE_MIN_MTU) {
4339 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
4340 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
4341 		}
4342 
4343 		if (option_exists & IPPF_NEXTHOP) {
4344 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
4345 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
4346 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
4347 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
4348 		}
4349 
4350 		/*
4351 		 * tell IP this is an ip6i_t private header
4352 		 */
4353 		ip6i->ip6i_nxt = IPPROTO_RAW;
4354 	}
4355 
4356 	/* Initialize IPv6 header */
4357 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
4358 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
4359 
4360 	/* Set the hoplimit of the outgoing packet. */
4361 	if (option_exists & IPPF_HOPLIMIT) {
4362 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4363 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4364 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4365 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4366 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4367 		if (option_exists & IPPF_MULTICAST_HOPS)
4368 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4369 	} else {
4370 		ip6h->ip6_hops = icmp->icmp_ttl;
4371 		if (option_exists & IPPF_UNICAST_HOPS)
4372 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4373 	}
4374 
4375 	if (option_exists & IPPF_ADDR) {
4376 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
4377 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
4378 		ip6h->ip6_src = tipp->ipp_addr;
4379 	} else {
4380 		/*
4381 		 * The source address was not set using IPV6_PKTINFO.
4382 		 * First look at the bound source.
4383 		 * If unspecified fallback to __sin6_src_id.
4384 		 */
4385 		ip6h->ip6_src = icmp->icmp_v6src;
4386 		if (sin6->__sin6_src_id != 0 &&
4387 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
4388 			ip_srcid_find_id(sin6->__sin6_src_id,
4389 			    &ip6h->ip6_src, icmp->icmp_zoneid);
4390 		}
4391 	}
4392 
4393 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
4394 	cp = (uint8_t *)&ip6h[1];
4395 
4396 	/*
4397 	 * Here's where we have to start stringing together
4398 	 * any extension headers in the right order:
4399 	 * Hop-by-hop, destination, routing, and final destination opts.
4400 	 */
4401 	if (option_exists & IPPF_HOPOPTS) {
4402 		/* Hop-by-hop options */
4403 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
4404 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
4405 
4406 		*nxthdr_ptr = IPPROTO_HOPOPTS;
4407 		nxthdr_ptr = &hbh->ip6h_nxt;
4408 
4409 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
4410 		cp += tipp->ipp_hopoptslen;
4411 	}
4412 	/*
4413 	 * En-route destination options
4414 	 * Only do them if there's a routing header as well
4415 	 */
4416 	if (option_exists & IPPF_RTDSTOPTS) {
4417 		ip6_dest_t *dst = (ip6_dest_t *)cp;
4418 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
4419 
4420 		*nxthdr_ptr = IPPROTO_DSTOPTS;
4421 		nxthdr_ptr = &dst->ip6d_nxt;
4422 
4423 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
4424 		cp += tipp->ipp_rtdstoptslen;
4425 	}
4426 	/*
4427 	 * Routing header next
4428 	 */
4429 	if (option_exists & IPPF_RTHDR) {
4430 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
4431 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
4432 
4433 		*nxthdr_ptr = IPPROTO_ROUTING;
4434 		nxthdr_ptr = &rt->ip6r_nxt;
4435 
4436 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
4437 		cp += tipp->ipp_rthdrlen;
4438 	}
4439 	/*
4440 	 * Do ultimate destination options
4441 	 */
4442 	if (option_exists & IPPF_DSTOPTS) {
4443 		ip6_dest_t *dest = (ip6_dest_t *)cp;
4444 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
4445 
4446 		*nxthdr_ptr = IPPROTO_DSTOPTS;
4447 		nxthdr_ptr = &dest->ip6d_nxt;
4448 
4449 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
4450 		cp += tipp->ipp_dstoptslen;
4451 	}
4452 
4453 	/*
4454 	 * Now set the last header pointer to the proto passed in
4455 	 */
4456 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
4457 	*nxthdr_ptr = icmp->icmp_proto;
4458 
4459 	/*
4460 	 * Copy in the destination address
4461 	 */
4462 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
4463 		ip6h->ip6_dst = ipv6_loopback;
4464 	else
4465 		ip6h->ip6_dst = sin6->sin6_addr;
4466 
4467 	ip6h->ip6_vcf =
4468 		(IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
4469 		(sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
4470 
4471 	if (option_exists & IPPF_TCLASS) {
4472 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
4473 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
4474 		    tipp->ipp_tclass);
4475 	}
4476 	if (option_exists & IPPF_RTHDR) {
4477 		ip6_rthdr_t	*rth;
4478 
4479 		/*
4480 		 * Perform any processing needed for source routing.
4481 		 * We know that all extension headers will be in the same mblk
4482 		 * as the IPv6 header.
4483 		 */
4484 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
4485 		if (rth != NULL && rth->ip6r_segleft != 0) {
4486 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
4487 				/*
4488 				 * Drop packet - only support Type 0 routing.
4489 				 * Notify the application as well.
4490 				 */
4491 				icmp_ud_err(q, mp, EPROTO);
4492 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4493 				return;
4494 			}
4495 			/*
4496 			 * rth->ip6r_len is twice the number of
4497 			 * addresses in the header
4498 			 */
4499 			if (rth->ip6r_len & 0x1) {
4500 				icmp_ud_err(q, mp, EPROTO);
4501 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4502 				return;
4503 			}
4504 			/*
4505 			 * Shuffle the routing header and ip6_dst
4506 			 * addresses, and get the checksum difference
4507 			 * between the first hop (in ip6_dst) and
4508 			 * the destination (in the last routing hdr entry).
4509 			 */
4510 			csum = ip_massage_options_v6(ip6h, rth);
4511 			/*
4512 			 * Verify that the first hop isn't a mapped address.
4513 			 * Routers along the path need to do this verification
4514 			 * for subsequent hops.
4515 			 */
4516 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
4517 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
4518 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4519 				return;
4520 			}
4521 		}
4522 	}
4523 
4524 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
4525 	if (mp1->b_cont != NULL)
4526 		ip_len += msgdsize(mp1->b_cont);
4527 
4528 	/*
4529 	 * Set the length into the IP header.
4530 	 * If the length is greater than the maximum allowed by IP,
4531 	 * then free the message and return. Do not try and send it
4532 	 * as this can cause problems in layers below.
4533 	 */
4534 	if (ip_len > IP_MAXPACKET) {
4535 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4536 		icmp_ud_err(q, mp, EMSGSIZE);
4537 		return;
4538 	}
4539 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
4540 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
4541 		uint16_t *cksum_ptr;
4542 		uint_t	ext_hdrs_len;
4543 
4544 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
4545 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
4546 		    icmp->icmp_checksum_off == 2);
4547 
4548 		/*
4549 		 * We make it easy for IP to include our pseudo header
4550 		 * by putting our length in uh_checksum, modified (if
4551 		 * we have a routing header) by the checksum difference
4552 		 * between the ultimate destination and first hop addresses.
4553 		 * Note: ICMPv6 must always checksum the packet.
4554 		 */
4555 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
4556 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
4557 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
4558 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4559 				freemsg(mp);
4560 				return;
4561 			}
4562 			ip6i = (ip6i_t *)mp1->b_rptr;
4563 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
4564 				ip6h = (ip6_t *)&ip6i[1];
4565 			else
4566 				ip6h = (ip6_t *)ip6i;
4567 		}
4568 		/* Add payload length to checksum */
4569 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
4570 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
4571 		csum += htons(ip_len - ext_hdrs_len);
4572 
4573 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
4574 		csum = (csum & 0xFFFF) + (csum >> 16);
4575 		*cksum_ptr = (uint16_t)csum;
4576 	}
4577 
4578 #ifdef _LITTLE_ENDIAN
4579 	ip_len = htons(ip_len);
4580 #endif
4581 	ip6h->ip6_plen = (uint16_t)ip_len;
4582 
4583 	freeb(mp);
4584 
4585 	/* We're done. Pass the packet to IP */
4586 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
4587 	putnext(q, mp1);
4588 }
4589 
4590 static void
4591 icmp_wput_other(queue_t *q, mblk_t *mp)
4592 {
4593 	uchar_t	*rptr = mp->b_rptr;
4594 	struct iocblk *iocp;
4595 #define	tudr ((struct T_unitdata_req *)rptr)
4596 	icmp_t	*icmp;
4597 	cred_t *cr;
4598 
4599 	icmp = (icmp_t *)q->q_ptr;
4600 
4601 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
4602 
4603 	switch (mp->b_datap->db_type) {
4604 	case M_PROTO:
4605 	case M_PCPROTO:
4606 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
4607 			/*
4608 			 * If the message does not contain a PRIM_type,
4609 			 * throw it away.
4610 			 */
4611 			freemsg(mp);
4612 			return;
4613 		}
4614 		switch (((union T_primitives *)rptr)->type) {
4615 		case T_ADDR_REQ:
4616 			icmp_addr_req(q, mp);
4617 			return;
4618 		case O_T_BIND_REQ:
4619 		case T_BIND_REQ:
4620 			qwriter(q, mp, icmp_bind, PERIM_OUTER);
4621 			return;
4622 		case T_CONN_REQ:
4623 			icmp_connect(q, mp);
4624 			return;
4625 		case T_CAPABILITY_REQ:
4626 			icmp_capability_req(q, mp);
4627 			return;
4628 		case T_INFO_REQ:
4629 			icmp_info_req(q, mp);
4630 			return;
4631 		case T_UNITDATA_REQ:
4632 			/*
4633 			 * If a T_UNITDATA_REQ gets here, the address must
4634 			 * be bad.  Valid T_UNITDATA_REQs are found above
4635 			 * and break to below this switch.
4636 			 */
4637 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
4638 			return;
4639 		case T_UNBIND_REQ:
4640 			icmp_unbind(q, mp);
4641 			return;
4642 
4643 		case T_SVR4_OPTMGMT_REQ:
4644 			if (!snmpcom_req(q, mp, icmp_snmp_set, icmp_snmp_get,
4645 			    cr))
4646 				/* Only IP can return anything meaningful */
4647 				(void) svr4_optcom_req(q, mp, cr,
4648 				    &icmp_opt_obj);
4649 			return;
4650 
4651 		case T_OPTMGMT_REQ:
4652 			/* Only IP can return anything meaningful */
4653 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
4654 			return;
4655 
4656 		case T_DISCON_REQ:
4657 			icmp_disconnect(q, mp);
4658 			return;
4659 
4660 		/* The following TPI message is not supported by icmp. */
4661 		case O_T_CONN_RES:
4662 		case T_CONN_RES:
4663 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
4664 			return;
4665 
4666 		/* The following 3 TPI requests are illegal for icmp. */
4667 		case T_DATA_REQ:
4668 		case T_EXDATA_REQ:
4669 		case T_ORDREL_REQ:
4670 			freemsg(mp);
4671 			(void) putctl1(RD(q), M_ERROR, EPROTO);
4672 			return;
4673 		default:
4674 			break;
4675 		}
4676 		break;
4677 	case M_IOCTL:
4678 		iocp = (struct iocblk *)mp->b_rptr;
4679 		switch (iocp->ioc_cmd) {
4680 		case TI_GETPEERNAME:
4681 			if (icmp->icmp_state != TS_DATA_XFER) {
4682 				/*
4683 				 * If a default destination address has not
4684 				 * been associated with the stream, then we
4685 				 * don't know the peer's name.
4686 				 */
4687 				iocp->ioc_error = ENOTCONN;
4688 			    err_ret:;
4689 				iocp->ioc_count = 0;
4690 				mp->b_datap->db_type = M_IOCACK;
4691 				qreply(q, mp);
4692 				return;
4693 			}
4694 			/* FALLTHRU */
4695 		case TI_GETMYNAME:
4696 			/*
4697 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
4698 			 * need to copyin the user's strbuf structure.
4699 			 * Processing will continue in the M_IOCDATA case
4700 			 * below.
4701 			 */
4702 			mi_copyin(q, mp, NULL,
4703 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
4704 			return;
4705 		case ND_SET:
4706 			/* nd_getset performs the necessary error checking */
4707 		case ND_GET:
4708 			if (nd_getset(q, icmp_g_nd, mp)) {
4709 				qreply(q, mp);
4710 				return;
4711 			}
4712 			break;
4713 		default:
4714 			break;
4715 		}
4716 		break;
4717 	case M_IOCDATA:
4718 		icmp_wput_iocdata(q, mp);
4719 		return;
4720 	default:
4721 		break;
4722 	}
4723 	putnext(q, mp);
4724 }
4725 
4726 /*
4727  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
4728  * messages.
4729  */
4730 static void
4731 icmp_wput_iocdata(queue_t *q, mblk_t *mp)
4732 {
4733 	mblk_t	*mp1;
4734 	STRUCT_HANDLE(strbuf, sb);
4735 	icmp_t	*icmp;
4736 	in6_addr_t	v6addr;
4737 	ipaddr_t	v4addr;
4738 	uint32_t	flowinfo = 0;
4739 	int		addrlen;
4740 
4741 	/* Make sure it is one of ours. */
4742 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4743 	case TI_GETMYNAME:
4744 	case TI_GETPEERNAME:
4745 		break;
4746 	default:
4747 		putnext(q, mp);
4748 		return;
4749 	}
4750 	switch (mi_copy_state(q, mp, &mp1)) {
4751 	case -1:
4752 		return;
4753 	case MI_COPY_CASE(MI_COPY_IN, 1):
4754 		break;
4755 	case MI_COPY_CASE(MI_COPY_OUT, 1):
4756 		/*
4757 		 * The address has been copied out, so now
4758 		 * copyout the strbuf.
4759 		 */
4760 		mi_copyout(q, mp);
4761 		return;
4762 	case MI_COPY_CASE(MI_COPY_OUT, 2):
4763 		/*
4764 		 * The address and strbuf have been copied out.
4765 		 * We're done, so just acknowledge the original
4766 		 * M_IOCTL.
4767 		 */
4768 		mi_copy_done(q, mp, 0);
4769 		return;
4770 	default:
4771 		/*
4772 		 * Something strange has happened, so acknowledge
4773 		 * the original M_IOCTL with an EPROTO error.
4774 		 */
4775 		mi_copy_done(q, mp, EPROTO);
4776 		return;
4777 	}
4778 	/*
4779 	 * Now we have the strbuf structure for TI_GETMYNAME
4780 	 * and TI_GETPEERNAME.  Next we copyout the requested
4781 	 * address and then we'll copyout the strbuf.
4782 	 */
4783 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
4784 	    (void *)mp1->b_rptr);
4785 	icmp = (icmp_t *)q->q_ptr;
4786 	if (icmp->icmp_family == AF_INET)
4787 		addrlen = sizeof (sin_t);
4788 	else
4789 		addrlen = sizeof (sin6_t);
4790 
4791 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
4792 		mi_copy_done(q, mp, EINVAL);
4793 		return;
4794 	}
4795 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4796 	case TI_GETMYNAME:
4797 		if (icmp->icmp_family == AF_INET) {
4798 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
4799 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
4800 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
4801 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
4802 			} else {
4803 				/*
4804 				 * INADDR_ANY
4805 				 * icmp_v6src is not set, we might be bound to
4806 				 * broadcast/multicast. Use icmp_bound_v6src as
4807 				 * local address instead (that could
4808 				 * also still be INADDR_ANY)
4809 				 */
4810 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
4811 			}
4812 		} else {
4813 			/* icmp->icmp_family == AF_INET6 */
4814 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
4815 				v6addr = icmp->icmp_v6src;
4816 			} else {
4817 				/*
4818 				 * UNSPECIFIED
4819 				 * icmp_v6src is not set, we might be bound to
4820 				 * broadcast/multicast. Use icmp_bound_v6src as
4821 				 * local address instead (that could
4822 				 * also still be UNSPECIFIED)
4823 				 */
4824 				v6addr = icmp->icmp_bound_v6src;
4825 			}
4826 		}
4827 		break;
4828 	case TI_GETPEERNAME:
4829 		if (icmp->icmp_family == AF_INET) {
4830 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
4831 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
4832 		} else {
4833 			/* icmp->icmp_family == AF_INET6) */
4834 			v6addr = icmp->icmp_v6dst;
4835 			flowinfo = icmp->icmp_flowinfo;
4836 		}
4837 		break;
4838 	default:
4839 		mi_copy_done(q, mp, EPROTO);
4840 		return;
4841 	}
4842 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
4843 	if (!mp1)
4844 		return;
4845 
4846 	if (icmp->icmp_family == AF_INET) {
4847 		sin_t *sin;
4848 
4849 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
4850 		sin = (sin_t *)mp1->b_rptr;
4851 		mp1->b_wptr = (uchar_t *)&sin[1];
4852 		*sin = sin_null;
4853 		sin->sin_family = AF_INET;
4854 		sin->sin_addr.s_addr = v4addr;
4855 	} else {
4856 		/* icmp->icmp_family == AF_INET6 */
4857 		sin6_t *sin6;
4858 
4859 		ASSERT(icmp->icmp_family == AF_INET6);
4860 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
4861 		sin6 = (sin6_t *)mp1->b_rptr;
4862 		mp1->b_wptr = (uchar_t *)&sin6[1];
4863 		*sin6 = sin6_null;
4864 		sin6->sin6_family = AF_INET6;
4865 		sin6->sin6_flowinfo = flowinfo;
4866 		sin6->sin6_addr = v6addr;
4867 	}
4868 	/* Copy out the address */
4869 	mi_copyout(q, mp);
4870 }
4871 
4872 /*
4873  * Only allow MIB requests and M_FLUSHes to pass.
4874  * All other messages are nacked or dropped.
4875  */
4876 static void
4877 icmp_wput_restricted(queue_t *q, mblk_t *mp)
4878 {
4879 	cred_t	*cr;
4880 	icmp_t	*icmp;
4881 
4882 	switch (DB_TYPE(mp)) {
4883 	case M_PROTO:
4884 	case M_PCPROTO:
4885 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
4886 			freemsg(mp);
4887 			return;
4888 		}
4889 		icmp = (icmp_t *)q->q_ptr;
4890 		cr = DB_CREDDEF(mp, icmp->icmp_credp);
4891 
4892 		switch (((union T_primitives *)mp->b_rptr)->type) {
4893 		case T_SVR4_OPTMGMT_REQ:
4894 			if (!snmpcom_req(q, mp,
4895 			    icmp_snmp_set, icmp_snmp_get, cr))
4896 				(void) svr4_optcom_req(q, mp, cr,
4897 				    &icmp_opt_obj);
4898 			return;
4899 		case T_OPTMGMT_REQ:
4900 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
4901 			return;
4902 		default:
4903 			icmp_err_ack(q, mp, TSYSERR, ENOTSUP);
4904 			return;
4905 		}
4906 		/* NOTREACHED */
4907 	case M_IOCTL:
4908 		miocnak(q, mp, 0, ENOTSUP);
4909 		break;
4910 	case M_FLUSH:
4911 		putnext(q, mp);
4912 		break;
4913 	default:
4914 		freemsg(mp);
4915 		break;
4916 	}
4917 }
4918 
4919 static int
4920 icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
4921     void *thisdg_attrs)
4922 {
4923 	icmp_t	*icmp;
4924 	struct T_unitdata_req *udreqp;
4925 	int is_absreq_failure;
4926 	cred_t *cr;
4927 
4928 	icmp = (icmp_t *)q->q_ptr;
4929 
4930 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
4931 	*errorp = 0;
4932 
4933 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
4934 
4935 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
4936 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
4937 	    thisdg_attrs, &is_absreq_failure);
4938 
4939 	if (*errorp != 0) {
4940 		/*
4941 		 * Note: No special action needed in this
4942 		 * module for "is_absreq_failure"
4943 		 */
4944 		return (-1);		/* failure */
4945 	}
4946 	ASSERT(is_absreq_failure == 0);
4947 	return (0);	/* success */
4948 }
4949 
4950 void
4951 icmp_ddi_init(void)
4952 {
4953 	ICMP6_MAJ = ddi_name_to_major(ICMP6);
4954 	icmp_max_optsize =
4955 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
4956 		icmp_opt_obj.odb_opt_arr_cnt);
4957 
4958 	(void) icmp_param_register(icmp_param_arr, A_CNT(icmp_param_arr));
4959 
4960 	rawip_kstat_init();
4961 }
4962 
4963 void
4964 icmp_ddi_destroy(void)
4965 {
4966 	nd_free(&icmp_g_nd);
4967 
4968 	rawip_kstat_fini();
4969 }
4970 
4971 static void
4972 rawip_kstat_init(void) {
4973 
4974 	rawip_named_kstat_t template = {
4975 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
4976 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
4977 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
4978 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
4979 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
4980 	};
4981 
4982 	rawip_mibkp = kstat_create("icmp", 0, "rawip", "mib2",
4983 					KSTAT_TYPE_NAMED,
4984 					NUM_OF_FIELDS(rawip_named_kstat_t),
4985 					0);
4986 	if (rawip_mibkp == NULL)
4987 		return;
4988 
4989 	bcopy(&template, rawip_mibkp->ks_data, sizeof (template));
4990 
4991 	rawip_mibkp->ks_update = rawip_kstat_update;
4992 
4993 	kstat_install(rawip_mibkp);
4994 }
4995 
4996 static void
4997 rawip_kstat_fini(void) {
4998 	if (rawip_mibkp) {
4999 		kstat_delete(rawip_mibkp);
5000 		rawip_mibkp = NULL;
5001 	}
5002 }
5003 
5004 static int
5005 rawip_kstat_update(kstat_t *kp, int rw) {
5006 	rawip_named_kstat_t *rawipkp;
5007 
5008 	if ((kp == NULL) || (kp->ks_data == NULL))
5009 		return (EIO);
5010 
5011 	if (rw == KSTAT_WRITE)
5012 		return (EACCES);
5013 
5014 	rawipkp = (rawip_named_kstat_t *)kp->ks_data;
5015 
5016 	rawipkp->inDatagrams.value.ui32 =	rawip_mib.rawipInDatagrams;
5017 	rawipkp->inCksumErrs.value.ui32 =	rawip_mib.rawipInCksumErrs;
5018 	rawipkp->inErrors.value.ui32 =		rawip_mib.rawipInErrors;
5019 	rawipkp->outDatagrams.value.ui32 =	rawip_mib.rawipOutDatagrams;
5020 	rawipkp->outErrors.value.ui32 =		rawip_mib.rawipOutErrors;
5021 
5022 	return (0);
5023 }
5024