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