xref: /titanic_41/usr/src/uts/common/inet/ip/icmp.c (revision c138f478d2bc94e73ab8f6a084e323bec25e62f5)
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_NEXTHOP: {
1698 			sin6_t *sin6 = (sin6_t *)ptr;
1699 
1700 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
1701 				return (0);
1702 			*sin6 = sin6_null;
1703 			sin6->sin6_family = AF_INET6;
1704 			sin6->sin6_addr = ipp->ipp_nexthop;
1705 			return (sizeof (sin6_t));
1706 		}
1707 		case IPV6_HOPOPTS:
1708 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
1709 				return (0);
1710 			bcopy(ipp->ipp_hopopts, ptr, ipp->ipp_hopoptslen);
1711 			return (ipp->ipp_hopoptslen);
1712 		case IPV6_RTHDRDSTOPTS:
1713 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
1714 				return (0);
1715 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
1716 			return (ipp->ipp_rtdstoptslen);
1717 		case IPV6_RTHDR:
1718 			if (!(ipp->ipp_fields & IPPF_RTHDR))
1719 				return (0);
1720 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
1721 			return (ipp->ipp_rthdrlen);
1722 		case IPV6_DSTOPTS:
1723 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
1724 				return (0);
1725 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
1726 			return (ipp->ipp_dstoptslen);
1727 		case IPV6_PATHMTU:
1728 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
1729 				return (0);
1730 
1731 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
1732 				(struct ip6_mtuinfo *)ptr));
1733 		case IPV6_TCLASS:
1734 			if (ipp->ipp_fields & IPPF_TCLASS)
1735 				*i1 = ipp->ipp_tclass;
1736 			else
1737 				*i1 = IPV6_FLOW_TCLASS(
1738 				    IPV6_DEFAULT_VERS_AND_FLOW);
1739 			break;
1740 		default:
1741 			return (-1);
1742 		}
1743 		break;
1744 	case IPPROTO_ICMPV6:
1745 		/*
1746 		 * Only allow IPv6 option processing on native IPv6 sockets.
1747 		 */
1748 		if (icmp->icmp_family != AF_INET6)
1749 			return (-1);
1750 
1751 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
1752 			return (-1);
1753 
1754 		switch (name) {
1755 		case ICMP6_FILTER:
1756 			if (icmp->icmp_filter == NULL) {
1757 				/* Make it look like "pass all" */
1758 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
1759 			} else {
1760 				(void) bcopy(icmp->icmp_filter, ptr,
1761 				    sizeof (icmp6_filter_t));
1762 			}
1763 			return (sizeof (icmp6_filter_t));
1764 		default:
1765 			return (-1);
1766 		}
1767 	default:
1768 		return (-1);
1769 	}
1770 	return (sizeof (int));
1771 }
1772 
1773 /* This routine sets socket options. */
1774 /* ARGSUSED */
1775 int
1776 icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
1777     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
1778     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
1779 {
1780 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
1781 	int	*i1 = (int *)invalp;
1782 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
1783 	boolean_t checkonly;
1784 	int	error;
1785 
1786 	switch (optset_context) {
1787 	case SETFN_OPTCOM_CHECKONLY:
1788 		checkonly = B_TRUE;
1789 		/*
1790 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
1791 		 * inlen != 0 implies value supplied and
1792 		 * 	we have to "pretend" to set it.
1793 		 * inlen == 0 implies that there is no
1794 		 * 	value part in T_CHECK request and just validation
1795 		 * done elsewhere should be enough, we just return here.
1796 		 */
1797 		if (inlen == 0) {
1798 			*outlenp = 0;
1799 			return (0);
1800 		}
1801 		break;
1802 	case SETFN_OPTCOM_NEGOTIATE:
1803 		checkonly = B_FALSE;
1804 		break;
1805 	case SETFN_UD_NEGOTIATE:
1806 	case SETFN_CONN_NEGOTIATE:
1807 		checkonly = B_FALSE;
1808 		/*
1809 		 * Negotiating local and "association-related" options
1810 		 * through T_UNITDATA_REQ.
1811 		 *
1812 		 * Following routine can filter out ones we do not
1813 		 * want to be "set" this way.
1814 		 */
1815 		if (!icmp_opt_allow_udr_set(level, name)) {
1816 			*outlenp = 0;
1817 			return (EINVAL);
1818 		}
1819 		break;
1820 	default:
1821 		/*
1822 		 * We should never get here
1823 		 */
1824 		*outlenp = 0;
1825 		return (EINVAL);
1826 	}
1827 
1828 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
1829 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
1830 
1831 	/*
1832 	 * For fixed length options, no sanity check
1833 	 * of passed in length is done. It is assumed *_optcom_req()
1834 	 * routines do the right thing.
1835 	 */
1836 
1837 	switch (level) {
1838 	case SOL_SOCKET:
1839 		switch (name) {
1840 		case SO_DEBUG:
1841 			if (!checkonly)
1842 				icmp->icmp_debug = onoff;
1843 			break;
1844 		case SO_PROTOTYPE:
1845 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
1846 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
1847 			    secpolicy_net_rawaccess(cr) != 0) {
1848 				*outlenp = 0;
1849 				return (EACCES);
1850 			}
1851 			/* Can't use IPPROTO_RAW with IPv6 */
1852 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
1853 			    icmp->icmp_family == AF_INET6) {
1854 				*outlenp = 0;
1855 				return (EPROTONOSUPPORT);
1856 			}
1857 			if (checkonly) {
1858 				/* T_CHECK case */
1859 				*(int *)outvalp = (*i1 & 0xFF);
1860 				break;
1861 			}
1862 			icmp->icmp_proto = *i1 & 0xFF;
1863 			if ((icmp->icmp_proto == IPPROTO_RAW ||
1864 			    icmp->icmp_proto == IPPROTO_IGMP) &&
1865 			    icmp->icmp_family == AF_INET)
1866 				icmp->icmp_hdrincl = 1;
1867 			else
1868 				icmp->icmp_hdrincl = 0;
1869 
1870 			if (icmp->icmp_family == AF_INET6 &&
1871 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
1872 				/* Set offset for icmp6_cksum */
1873 				icmp->icmp_raw_checksum = 0;
1874 				icmp->icmp_checksum_off = 2;
1875 			}
1876 			if (icmp->icmp_proto == IPPROTO_UDP ||
1877 			    icmp->icmp_proto == IPPROTO_TCP ||
1878 			    icmp->icmp_proto == IPPROTO_SCTP) {
1879 				icmp->icmp_no_tp_cksum = 1;
1880 				icmp->icmp_sticky_ipp.ipp_fields |=
1881 				    IPPF_NO_CKSUM;
1882 			} else {
1883 				icmp->icmp_no_tp_cksum = 0;
1884 				icmp->icmp_sticky_ipp.ipp_fields &=
1885 				    ~IPPF_NO_CKSUM;
1886 			}
1887 
1888 			if (icmp->icmp_filter != NULL &&
1889 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
1890 				kmem_free(icmp->icmp_filter,
1891 				    sizeof (icmp6_filter_t));
1892 				icmp->icmp_filter = NULL;
1893 			}
1894 
1895 			/* Rebuild the header template */
1896 			error = icmp_build_hdrs(q, icmp);
1897 			if (error != 0) {
1898 				*outlenp = 0;
1899 				return (error);
1900 			}
1901 
1902 			/*
1903 			 * For SCTP, we don't use icmp_bind_proto() for
1904 			 * raw socket binding.  Note that we do not need
1905 			 * to set *outlenp.
1906 			 */
1907 			if (icmp->icmp_proto == IPPROTO_SCTP)
1908 				return (0);
1909 
1910 			icmp_bind_proto(q);
1911 			*outlenp = sizeof (int);
1912 			*(int *)outvalp = *i1 & 0xFF;
1913 			return (0);
1914 		case SO_REUSEADDR:
1915 			if (!checkonly)
1916 				icmp->icmp_reuseaddr = onoff;
1917 			break;
1918 
1919 		/*
1920 		 * The following three items are available here,
1921 		 * but are only meaningful to IP.
1922 		 */
1923 		case SO_DONTROUTE:
1924 			if (!checkonly)
1925 				icmp->icmp_dontroute = onoff;
1926 			break;
1927 		case SO_USELOOPBACK:
1928 			if (!checkonly)
1929 				icmp->icmp_useloopback = onoff;
1930 			break;
1931 		case SO_BROADCAST:
1932 			if (!checkonly)
1933 				icmp->icmp_broadcast = onoff;
1934 			break;
1935 
1936 		case SO_SNDBUF:
1937 			if (*i1 > icmp_max_buf) {
1938 				*outlenp = 0;
1939 				return (ENOBUFS);
1940 			}
1941 			if (!checkonly) {
1942 				q->q_hiwat = *i1;
1943 				q->q_next->q_hiwat = *i1;
1944 			}
1945 			break;
1946 		case SO_RCVBUF:
1947 			if (*i1 > icmp_max_buf) {
1948 				*outlenp = 0;
1949 				return (ENOBUFS);
1950 			}
1951 			if (!checkonly) {
1952 				RD(q)->q_hiwat = *i1;
1953 				(void) mi_set_sth_hiwat(RD(q), *i1);
1954 			}
1955 			break;
1956 		case SO_DGRAM_ERRIND:
1957 			if (!checkonly)
1958 				icmp->icmp_dgram_errind = onoff;
1959 			break;
1960 		/*
1961 		 * Following three not meaningful for icmp
1962 		 * Action is same as "default" so we keep them
1963 		 * in comments.
1964 		 * case SO_LINGER:
1965 		 * case SO_KEEPALIVE:
1966 		 * case SO_OOBINLINE:
1967 		 */
1968 		default:
1969 			*outlenp = 0;
1970 			return (EINVAL);
1971 		}
1972 		break;
1973 	case IPPROTO_IP:
1974 		/*
1975 		 * Only allow IPv4 option processing on IPv4 sockets.
1976 		 */
1977 		if (icmp->icmp_family != AF_INET) {
1978 			*outlenp = 0;
1979 			return (ENOPROTOOPT);
1980 		}
1981 		switch (name) {
1982 		case IP_OPTIONS:
1983 		case T_IP_OPTIONS:
1984 			/* Save options for use by IP. */
1985 			if (inlen & 0x3) {
1986 				*outlenp = 0;
1987 				return (EINVAL);
1988 			}
1989 			if (checkonly)
1990 				break;
1991 
1992 			if (icmp->icmp_ip_snd_options) {
1993 				mi_free((char *)icmp->icmp_ip_snd_options);
1994 				icmp->icmp_ip_snd_options_len = 0;
1995 				icmp->icmp_ip_snd_options = NULL;
1996 			}
1997 			if (inlen) {
1998 				icmp->icmp_ip_snd_options =
1999 				    (uchar_t *)mi_alloc(inlen, BPRI_HI);
2000 				if (icmp->icmp_ip_snd_options) {
2001 					bcopy(invalp,
2002 					    icmp->icmp_ip_snd_options, inlen);
2003 					icmp->icmp_ip_snd_options_len = inlen;
2004 				}
2005 			}
2006 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
2007 			    icmp->icmp_ip_snd_options_len;
2008 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
2009 						icmp_wroff_extra);
2010 			break;
2011 		case IP_HDRINCL:
2012 			if (!checkonly)
2013 				icmp->icmp_hdrincl = onoff;
2014 			break;
2015 		case IP_TOS:
2016 		case T_IP_TOS:
2017 			if (!checkonly) {
2018 				icmp->icmp_type_of_service = (uint8_t)*i1;
2019 			}
2020 			break;
2021 		case IP_TTL:
2022 			if (!checkonly) {
2023 				icmp->icmp_ttl = (uint8_t)*i1;
2024 			}
2025 			break;
2026 		case IP_MULTICAST_IF:
2027 			/*
2028 			 * TODO should check OPTMGMT reply and undo this if
2029 			 * there is an error.
2030 			 */
2031 			if (!checkonly)
2032 				icmp->icmp_multicast_if_addr = *i1;
2033 			break;
2034 		case IP_MULTICAST_TTL:
2035 			if (!checkonly)
2036 				icmp->icmp_multicast_ttl = *invalp;
2037 			break;
2038 		case IP_MULTICAST_LOOP:
2039 			if (!checkonly) {
2040 				icmp->icmp_multicast_loop =
2041 				    (*invalp == 0) ? 0 : 1;
2042 			}
2043 			break;
2044 		case IP_BOUND_IF:
2045 			if (!checkonly)
2046 				icmp->icmp_bound_if = *i1;
2047 			break;
2048 		case IP_UNSPEC_SRC:
2049 			if (!checkonly)
2050 				icmp->icmp_unspec_source = onoff;
2051 			break;
2052 		case IP_XMIT_IF:
2053 			if (!checkonly)
2054 				icmp->icmp_xmit_if = *i1;
2055 			break;
2056 		case IP_RECVIF:
2057 			if (!checkonly)
2058 				icmp->icmp_recvif = onoff;
2059 			break;
2060 		case IP_ADD_MEMBERSHIP:
2061 		case IP_DROP_MEMBERSHIP:
2062 		case IP_BLOCK_SOURCE:
2063 		case IP_UNBLOCK_SOURCE:
2064 		case IP_ADD_SOURCE_MEMBERSHIP:
2065 		case IP_DROP_SOURCE_MEMBERSHIP:
2066 		case MCAST_JOIN_GROUP:
2067 		case MCAST_LEAVE_GROUP:
2068 		case MCAST_BLOCK_SOURCE:
2069 		case MCAST_UNBLOCK_SOURCE:
2070 		case MCAST_JOIN_SOURCE_GROUP:
2071 		case MCAST_LEAVE_SOURCE_GROUP:
2072 		case MRT_INIT:
2073 		case MRT_DONE:
2074 		case MRT_ADD_VIF:
2075 		case MRT_DEL_VIF:
2076 		case MRT_ADD_MFC:
2077 		case MRT_DEL_MFC:
2078 		case MRT_VERSION:
2079 		case MRT_ASSERT:
2080 		case IP_SEC_OPT:
2081 		case IP_DONTFAILOVER_IF:
2082 			/*
2083 			 * "soft" error (negative)
2084 			 * option not handled at this level
2085 			 * Note: Do not modify *outlenp
2086 			 */
2087 			return (-EINVAL);
2088 		default:
2089 			*outlenp = 0;
2090 			return (EINVAL);
2091 		}
2092 		break;
2093 	case IPPROTO_IPV6: {
2094 		ip6_pkt_t		*ipp;
2095 		boolean_t		sticky;
2096 
2097 		if (icmp->icmp_family != AF_INET6) {
2098 			*outlenp = 0;
2099 			return (ENOPROTOOPT);
2100 		}
2101 		/*
2102 		 * Deal with both sticky options and ancillary data
2103 		 */
2104 		if (thisdg_attrs == NULL) {
2105 			/* sticky options, or none */
2106 			ipp = &icmp->icmp_sticky_ipp;
2107 			sticky = B_TRUE;
2108 		} else {
2109 			/* ancillary data */
2110 			ipp = (ip6_pkt_t *)thisdg_attrs;
2111 			sticky = B_FALSE;
2112 		}
2113 
2114 		switch (name) {
2115 		case IPV6_MULTICAST_IF:
2116 			if (!checkonly)
2117 				icmp->icmp_multicast_if_index = *i1;
2118 			break;
2119 		case IPV6_UNICAST_HOPS:
2120 			/* -1 means use default */
2121 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
2122 				*outlenp = 0;
2123 				return (EINVAL);
2124 			}
2125 			if (!checkonly) {
2126 				if (*i1 == -1) {
2127 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
2128 					    icmp_ipv6_hoplimit;
2129 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
2130 					/* Pass modified value to IP. */
2131 					*i1 = ipp->ipp_hoplimit;
2132 				} else {
2133 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
2134 					    (uint8_t)*i1;
2135 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
2136 				}
2137 				/* Rebuild the header template */
2138 				error = icmp_build_hdrs(q, icmp);
2139 				if (error != 0) {
2140 					*outlenp = 0;
2141 					return (error);
2142 				}
2143 			}
2144 			break;
2145 		case IPV6_MULTICAST_HOPS:
2146 			/* -1 means use default */
2147 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
2148 				*outlenp = 0;
2149 				return (EINVAL);
2150 			}
2151 			if (!checkonly) {
2152 				if (*i1 == -1) {
2153 					icmp->icmp_multicast_ttl =
2154 					    ipp->ipp_multicast_hops =
2155 					    IP_DEFAULT_MULTICAST_TTL;
2156 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
2157 					/* Pass modified value to IP. */
2158 					*i1 = icmp->icmp_multicast_ttl;
2159 				} else {
2160 					icmp->icmp_multicast_ttl =
2161 					    ipp->ipp_multicast_hops =
2162 					    (uint8_t)*i1;
2163 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
2164 				}
2165 			}
2166 			break;
2167 		case IPV6_MULTICAST_LOOP:
2168 			if (*i1 != 0 && *i1 != 1) {
2169 				*outlenp = 0;
2170 				return (EINVAL);
2171 			}
2172 			if (!checkonly)
2173 				icmp->icmp_multicast_loop = *i1;
2174 			break;
2175 		case IPV6_CHECKSUM:
2176 			/*
2177 			 * Integer offset into the user data of where the
2178 			 * checksum is located.
2179 			 * Offset of -1 disables option.
2180 			 * Does not apply to IPPROTO_ICMPV6.
2181 			 */
2182 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
2183 				*outlenp = 0;
2184 				return (EINVAL);
2185 			}
2186 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
2187 				/* Negative or not 16 bit aligned offset */
2188 				*outlenp = 0;
2189 				return (EINVAL);
2190 			}
2191 			if (checkonly)
2192 				break;
2193 
2194 			if (*i1 == -1) {
2195 				icmp->icmp_raw_checksum = 0;
2196 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
2197 			} else {
2198 				icmp->icmp_raw_checksum = 1;
2199 				icmp->icmp_checksum_off = *i1;
2200 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
2201 			}
2202 			/* Rebuild the header template */
2203 			error = icmp_build_hdrs(q, icmp);
2204 			if (error != 0) {
2205 				*outlenp = 0;
2206 				return (error);
2207 			}
2208 			break;
2209 		case IPV6_JOIN_GROUP:
2210 		case IPV6_LEAVE_GROUP:
2211 		case MCAST_JOIN_GROUP:
2212 		case MCAST_LEAVE_GROUP:
2213 		case MCAST_BLOCK_SOURCE:
2214 		case MCAST_UNBLOCK_SOURCE:
2215 		case MCAST_JOIN_SOURCE_GROUP:
2216 		case MCAST_LEAVE_SOURCE_GROUP:
2217 			/*
2218 			 * "soft" error (negative)
2219 			 * option not handled at this level
2220 			 * Note: Do not modify *outlenp
2221 			 */
2222 			return (-EINVAL);
2223 		case IPV6_BOUND_IF:
2224 			if (!checkonly)
2225 				icmp->icmp_bound_if = *i1;
2226 			break;
2227 		case IPV6_UNSPEC_SRC:
2228 			if (!checkonly)
2229 				icmp->icmp_unspec_source = onoff;
2230 			break;
2231 		case IPV6_RECVTCLASS:
2232 			if (!checkonly)
2233 				icmp->icmp_ipv6_recvtclass = onoff;
2234 			break;
2235 		/*
2236 		 * Set boolean switches for ancillary data delivery
2237 		 */
2238 		case IPV6_RECVPKTINFO:
2239 			if (!checkonly)
2240 				icmp->icmp_ipv6_recvpktinfo = onoff;
2241 			break;
2242 		case IPV6_RECVPATHMTU:
2243 			if (!checkonly)
2244 				icmp->icmp_ipv6_recvpathmtu = onoff;
2245 			break;
2246 		case IPV6_RECVHOPLIMIT:
2247 			if (!checkonly)
2248 				icmp->icmp_ipv6_recvhoplimit = onoff;
2249 			break;
2250 		case IPV6_RECVHOPOPTS:
2251 			if (!checkonly)
2252 				icmp->icmp_ipv6_recvhopopts = onoff;
2253 			break;
2254 		case IPV6_RECVDSTOPTS:
2255 			if (!checkonly)
2256 				icmp->icmp_ipv6_recvdstopts = onoff;
2257 			break;
2258 		case _OLD_IPV6_RECVDSTOPTS:
2259 			if (!checkonly)
2260 				icmp->icmp_old_ipv6_recvdstopts = onoff;
2261 			break;
2262 		case IPV6_RECVRTHDRDSTOPTS:
2263 			if (!checkonly)
2264 				icmp->icmp_ipv6_recvrtdstopts = onoff;
2265 			break;
2266 		case IPV6_RECVRTHDR:
2267 			if (!checkonly)
2268 				icmp->icmp_ipv6_recvrthdr = onoff;
2269 			break;
2270 		/*
2271 		 * Set sticky options or ancillary data.
2272 		 * If sticky options, (re)build any extension headers
2273 		 * that might be needed as a result.
2274 		 */
2275 		case IPV6_PKTINFO:
2276 			/*
2277 			 * The source address and ifindex are verified
2278 			 * in ip_opt_set(). For ancillary data the
2279 			 * source address is checked in ip_wput_v6.
2280 			 */
2281 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
2282 				return (EINVAL);
2283 			if (checkonly)
2284 				break;
2285 
2286 			if (inlen == 0) {
2287 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
2288 				ipp->ipp_sticky_ignored |=
2289 				    (IPPF_IFINDEX|IPPF_ADDR);
2290 			} else {
2291 				struct in6_pktinfo *pkti;
2292 
2293 				pkti = (struct in6_pktinfo *)invalp;
2294 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
2295 				ipp->ipp_addr = pkti->ipi6_addr;
2296 				if (ipp->ipp_ifindex != 0)
2297 					ipp->ipp_fields |= IPPF_IFINDEX;
2298 				else
2299 					ipp->ipp_fields &= ~IPPF_IFINDEX;
2300 				if (!IN6_IS_ADDR_UNSPECIFIED(
2301 				    &ipp->ipp_addr))
2302 					ipp->ipp_fields |= IPPF_ADDR;
2303 				else
2304 					ipp->ipp_fields &= ~IPPF_ADDR;
2305 			}
2306 			if (sticky) {
2307 				error = icmp_build_hdrs(q, icmp);
2308 				if (error != 0)
2309 					return (error);
2310 			}
2311 			break;
2312 		case IPV6_HOPLIMIT:
2313 			/* This option can only be used as ancillary data. */
2314 			if (sticky)
2315 				return (EINVAL);
2316 			if (inlen != 0 && inlen != sizeof (int))
2317 				return (EINVAL);
2318 			if (checkonly)
2319 				break;
2320 
2321 			if (inlen == 0) {
2322 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
2323 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
2324 			} else {
2325 				if (*i1 > 255 || *i1 < -1)
2326 					return (EINVAL);
2327 				if (*i1 == -1)
2328 					ipp->ipp_hoplimit = icmp_ipv6_hoplimit;
2329 				else
2330 					ipp->ipp_hoplimit = *i1;
2331 				ipp->ipp_fields |= IPPF_HOPLIMIT;
2332 			}
2333 			break;
2334 		case IPV6_TCLASS:
2335 			/*
2336 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
2337 			 * and [0, 255] as the actualy traffic class.
2338 			 */
2339 			if (inlen != 0 && inlen != sizeof (int))
2340 				return (EINVAL);
2341 			if (checkonly)
2342 				break;
2343 
2344 			if (inlen == 0) {
2345 				ipp->ipp_fields &= ~IPPF_TCLASS;
2346 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
2347 			} else {
2348 				if (*i1 >= 256 || *i1 < -1)
2349 					return (EINVAL);
2350 				if (*i1 == -1) {
2351 					ipp->ipp_tclass =
2352 					    IPV6_FLOW_TCLASS(
2353 					    IPV6_DEFAULT_VERS_AND_FLOW);
2354 				} else {
2355 					ipp->ipp_tclass = *i1;
2356 				}
2357 				ipp->ipp_fields |= IPPF_TCLASS;
2358 			}
2359 			if (sticky) {
2360 				error = icmp_build_hdrs(q, icmp);
2361 				if (error != 0)
2362 					return (error);
2363 			}
2364 			break;
2365 		case IPV6_NEXTHOP:
2366 			/*
2367 			 * IP will verify that the nexthop is reachable
2368 			 * and fail for sticky options.
2369 			 */
2370 			if (inlen != 0 && inlen != sizeof (sin6_t))
2371 				return (EINVAL);
2372 			if (checkonly)
2373 				break;
2374 
2375 			if (inlen == 0) {
2376 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
2377 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
2378 			} else {
2379 				sin6_t *sin6 = (sin6_t *)invalp;
2380 
2381 				if (sin6->sin6_family != AF_INET6)
2382 					return (EAFNOSUPPORT);
2383 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2384 					return (EADDRNOTAVAIL);
2385 				ipp->ipp_nexthop = sin6->sin6_addr;
2386 				if (!IN6_IS_ADDR_UNSPECIFIED(
2387 				    &ipp->ipp_nexthop))
2388 					ipp->ipp_fields |= IPPF_NEXTHOP;
2389 				else
2390 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
2391 			}
2392 			if (sticky) {
2393 				error = icmp_build_hdrs(q, icmp);
2394 				if (error != 0)
2395 					return (error);
2396 			}
2397 			break;
2398 		case IPV6_HOPOPTS: {
2399 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
2400 			/*
2401 			 * Sanity checks - minimum size, size a multiple of
2402 			 * eight bytes, and matching size passed in.
2403 			 */
2404 			if (inlen != 0 &&
2405 			    inlen != (8 * (hopts->ip6h_len + 1)))
2406 				return (EINVAL);
2407 
2408 			if (checkonly)
2409 				break;
2410 			if (inlen == 0) {
2411 				if (sticky &&
2412 				    (ipp->ipp_fields & IPPF_HOPOPTS) != 0) {
2413 					kmem_free(ipp->ipp_hopopts,
2414 					    ipp->ipp_hopoptslen);
2415 					ipp->ipp_hopopts = NULL;
2416 					ipp->ipp_hopoptslen = 0;
2417 				}
2418 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
2419 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
2420 			} else {
2421 				error = icmp_pkt_set(invalp, inlen, sticky,
2422 				    (uchar_t **)&ipp->ipp_hopopts,
2423 				    &ipp->ipp_hopoptslen);
2424 				if (error != 0)
2425 					return (error);
2426 				ipp->ipp_fields |= IPPF_HOPOPTS;
2427 			}
2428 			if (sticky) {
2429 				error = icmp_build_hdrs(q, icmp);
2430 				if (error != 0)
2431 					return (error);
2432 			}
2433 			break;
2434 		}
2435 		case IPV6_RTHDRDSTOPTS: {
2436 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
2437 
2438 			/*
2439 			 * Sanity checks - minimum size, size a multiple of
2440 			 * eight bytes, and matching size passed in.
2441 			 */
2442 			if (inlen != 0 &&
2443 			    inlen != (8 * (dopts->ip6d_len + 1)))
2444 				return (EINVAL);
2445 
2446 			if (checkonly)
2447 				break;
2448 
2449 			if (inlen == 0) {
2450 				if (sticky &&
2451 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
2452 					kmem_free(ipp->ipp_rtdstopts,
2453 					    ipp->ipp_rtdstoptslen);
2454 					ipp->ipp_rtdstopts = NULL;
2455 					ipp->ipp_rtdstoptslen = 0;
2456 				}
2457 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
2458 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
2459 			} else {
2460 				error = icmp_pkt_set(invalp, inlen, sticky,
2461 				    (uchar_t **)&ipp->ipp_rtdstopts,
2462 				    &ipp->ipp_rtdstoptslen);
2463 				if (error != 0)
2464 					return (error);
2465 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
2466 			}
2467 			if (sticky) {
2468 				error = icmp_build_hdrs(q, icmp);
2469 				if (error != 0)
2470 					return (error);
2471 			}
2472 			break;
2473 		}
2474 		case IPV6_DSTOPTS: {
2475 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
2476 
2477 			/*
2478 			 * Sanity checks - minimum size, size a multiple of
2479 			 * eight bytes, and matching size passed in.
2480 			 */
2481 			if (inlen != 0 &&
2482 			    inlen != (8 * (dopts->ip6d_len + 1)))
2483 				return (EINVAL);
2484 
2485 			if (checkonly)
2486 				break;
2487 
2488 			if (inlen == 0) {
2489 				if (sticky &&
2490 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
2491 					kmem_free(ipp->ipp_dstopts,
2492 					    ipp->ipp_dstoptslen);
2493 					ipp->ipp_dstopts = NULL;
2494 					ipp->ipp_dstoptslen = 0;
2495 				}
2496 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
2497 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
2498 			} else {
2499 				error = icmp_pkt_set(invalp, inlen, sticky,
2500 				    (uchar_t **)&ipp->ipp_dstopts,
2501 				    &ipp->ipp_dstoptslen);
2502 				if (error != 0)
2503 					return (error);
2504 				ipp->ipp_fields |= IPPF_DSTOPTS;
2505 			}
2506 			if (sticky) {
2507 				error = icmp_build_hdrs(q, icmp);
2508 				if (error != 0)
2509 					return (error);
2510 			}
2511 			break;
2512 		}
2513 		case IPV6_RTHDR: {
2514 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
2515 
2516 			/*
2517 			 * Sanity checks - minimum size, size a multiple of
2518 			 * eight bytes, and matching size passed in.
2519 			 */
2520 			if (inlen != 0 &&
2521 			    inlen != (8 * (rt->ip6r_len + 1)))
2522 				return (EINVAL);
2523 
2524 			if (checkonly)
2525 				break;
2526 
2527 			if (inlen == 0) {
2528 				if (sticky &&
2529 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
2530 					kmem_free(ipp->ipp_rthdr,
2531 					    ipp->ipp_rthdrlen);
2532 					ipp->ipp_rthdr = NULL;
2533 					ipp->ipp_rthdrlen = 0;
2534 				}
2535 				ipp->ipp_fields &= ~IPPF_RTHDR;
2536 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
2537 			} else {
2538 				error = icmp_pkt_set(invalp, inlen, sticky,
2539 				    (uchar_t **)&ipp->ipp_rthdr,
2540 				    &ipp->ipp_rthdrlen);
2541 				if (error != 0)
2542 					return (error);
2543 				ipp->ipp_fields |= IPPF_RTHDR;
2544 			}
2545 			if (sticky) {
2546 				error = icmp_build_hdrs(q, icmp);
2547 				if (error != 0)
2548 					return (error);
2549 			}
2550 			break;
2551 		}
2552 
2553 		case IPV6_DONTFRAG:
2554 			if (checkonly)
2555 				break;
2556 
2557 			if (onoff) {
2558 				ipp->ipp_fields |= IPPF_DONTFRAG;
2559 			} else {
2560 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
2561 			}
2562 			break;
2563 
2564 		case IPV6_USE_MIN_MTU:
2565 			if (inlen != sizeof (int))
2566 				return (EINVAL);
2567 
2568 			if (*i1 < -1 || *i1 > 1)
2569 				return (EINVAL);
2570 
2571 			if (checkonly)
2572 				break;
2573 
2574 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
2575 			ipp->ipp_use_min_mtu = *i1;
2576 			break;
2577 
2578 		/*
2579 		 * This option can't be set.  Its only returned via
2580 		 * getsockopt() or ancillary data.
2581 		 */
2582 		case IPV6_PATHMTU:
2583 			return (EINVAL);
2584 
2585 		case IPV6_BOUND_PIF:
2586 		case IPV6_SEC_OPT:
2587 		case IPV6_DONTFAILOVER_IF:
2588 		case IPV6_SRC_PREFERENCES:
2589 		case IPV6_V6ONLY:
2590 			/* Handled at IP level */
2591 			return (-EINVAL);
2592 		default:
2593 			*outlenp = 0;
2594 			return (EINVAL);
2595 		}
2596 		break;
2597 	}		/* end IPPROTO_IPV6 */
2598 
2599 	case IPPROTO_ICMPV6:
2600 		/*
2601 		 * Only allow IPv6 option processing on IPv6 sockets.
2602 		 */
2603 		if (icmp->icmp_family != AF_INET6) {
2604 			*outlenp = 0;
2605 			return (ENOPROTOOPT);
2606 		}
2607 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
2608 			*outlenp = 0;
2609 			return (ENOPROTOOPT);
2610 		}
2611 		switch (name) {
2612 		case ICMP6_FILTER:
2613 			if (!checkonly) {
2614 				if ((inlen != 0) &&
2615 				    (inlen != sizeof (icmp6_filter_t)))
2616 					return (EINVAL);
2617 
2618 				if (inlen == 0) {
2619 					if (icmp->icmp_filter != NULL) {
2620 						kmem_free(icmp->icmp_filter,
2621 						    sizeof (icmp6_filter_t));
2622 						icmp->icmp_filter = NULL;
2623 					}
2624 				} else {
2625 					if (icmp->icmp_filter == NULL) {
2626 						icmp->icmp_filter = kmem_alloc(
2627 						    sizeof (icmp6_filter_t),
2628 						    KM_NOSLEEP);
2629 						if (icmp->icmp_filter == NULL) {
2630 							*outlenp = 0;
2631 							return (ENOBUFS);
2632 						}
2633 					}
2634 					(void) bcopy(invalp, icmp->icmp_filter,
2635 					    inlen);
2636 				}
2637 			}
2638 			break;
2639 
2640 		default:
2641 			*outlenp = 0;
2642 			return (EINVAL);
2643 		}
2644 		break;
2645 	default:
2646 		*outlenp = 0;
2647 		return (EINVAL);
2648 	}
2649 	/*
2650 	 * Common case of OK return with outval same as inval.
2651 	 */
2652 	if (invalp != outvalp) {
2653 		/* don't trust bcopy for identical src/dst */
2654 		(void) bcopy(invalp, outvalp, inlen);
2655 	}
2656 	*outlenp = inlen;
2657 	return (0);
2658 }
2659 
2660 /*
2661  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
2662  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
2663  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
2664  * headers.
2665  * Returns failure if can't allocate memory.
2666  */
2667 static int
2668 icmp_build_hdrs(queue_t *q, icmp_t *icmp)
2669 {
2670 	uchar_t	*hdrs;
2671 	uint_t	hdrs_len;
2672 	ip6_t	*ip6h;
2673 	ip6i_t	*ip6i;
2674 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
2675 
2676 	hdrs_len = ip_total_hdrs_len_v6(ipp);
2677 	ASSERT(hdrs_len != 0);
2678 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
2679 		/* Need to reallocate */
2680 		if (hdrs_len != 0) {
2681 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
2682 			if (hdrs == NULL)
2683 				return (ENOMEM);
2684 		} else {
2685 			hdrs = NULL;
2686 		}
2687 		if (icmp->icmp_sticky_hdrs_len != 0) {
2688 			kmem_free(icmp->icmp_sticky_hdrs,
2689 			    icmp->icmp_sticky_hdrs_len);
2690 		}
2691 		icmp->icmp_sticky_hdrs = hdrs;
2692 		icmp->icmp_sticky_hdrs_len = hdrs_len;
2693 	}
2694 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
2695 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
2696 
2697 	/* Set header fields not in ipp */
2698 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
2699 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
2700 		ip6h = (ip6_t *)&ip6i[1];
2701 
2702 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
2703 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
2704 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
2705 		}
2706 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
2707 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
2708 		}
2709 	} else {
2710 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
2711 	}
2712 
2713 	if (!(ipp->ipp_fields & IPPF_ADDR))
2714 		ip6h->ip6_src = icmp->icmp_v6src;
2715 
2716 	/* Try to get everything in a single mblk */
2717 	if (hdrs_len > icmp->icmp_max_hdr_len) {
2718 		icmp->icmp_max_hdr_len = hdrs_len;
2719 		(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
2720 		    icmp_wroff_extra);
2721 	}
2722 	return (0);
2723 }
2724 
2725 /*
2726  * Set optbuf and optlen for the option.
2727  * If sticky is set allocate memory (if not already present).
2728  * Otherwise just point optbuf and optlen at invalp and inlen.
2729  * Returns failure if memory can not be allocated.
2730  */
2731 static int
2732 icmp_pkt_set(uchar_t *invalp, uint_t inlen, boolean_t sticky,
2733     uchar_t **optbufp, uint_t *optlenp)
2734 {
2735 	uchar_t *optbuf;
2736 
2737 	if (!sticky) {
2738 		*optbufp = invalp;
2739 		*optlenp = inlen;
2740 		return (0);
2741 	}
2742 	if (inlen == *optlenp) {
2743 		/* Unchanged length - no need to realocate */
2744 		bcopy(invalp, *optbufp, inlen);
2745 		return (0);
2746 	}
2747 	if (inlen != 0) {
2748 		/* Allocate new buffer before free */
2749 		optbuf = kmem_alloc(inlen, KM_NOSLEEP);
2750 		if (optbuf == NULL)
2751 			return (ENOMEM);
2752 	} else {
2753 		optbuf = NULL;
2754 	}
2755 	/* Free old buffer */
2756 	if (*optlenp != 0)
2757 		kmem_free(*optbufp, *optlenp);
2758 
2759 	bcopy(invalp, optbuf, inlen);
2760 	*optbufp = optbuf;
2761 	*optlenp = inlen;
2762 	return (0);
2763 }
2764 
2765 /*
2766  * This routine retrieves the value of an ND variable in a icmpparam_t
2767  * structure.  It is called through nd_getset when a user reads the
2768  * variable.
2769  */
2770 /* ARGSUSED */
2771 static int
2772 icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
2773 {
2774 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
2775 
2776 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
2777 	return (0);
2778 }
2779 
2780 /*
2781  * Walk through the param array specified registering each element with the
2782  * named dispatch (ND) handler.
2783  */
2784 static boolean_t
2785 icmp_param_register(icmpparam_t *icmppa, int cnt)
2786 {
2787 	for (; cnt-- > 0; icmppa++) {
2788 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
2789 			if (!nd_load(&icmp_g_nd, icmppa->icmp_param_name,
2790 			    icmp_param_get, icmp_param_set,
2791 			    (caddr_t)icmppa)) {
2792 				nd_free(&icmp_g_nd);
2793 				return (B_FALSE);
2794 			}
2795 		}
2796 	}
2797 	if (!nd_load(&icmp_g_nd, "icmp_status", icmp_status_report, NULL,
2798 	    NULL)) {
2799 		nd_free(&icmp_g_nd);
2800 		return (B_FALSE);
2801 	}
2802 	return (B_TRUE);
2803 }
2804 
2805 /* This routine sets an ND variable in a icmpparam_t structure. */
2806 /* ARGSUSED */
2807 static int
2808 icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
2809 {
2810 	long		new_value;
2811 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
2812 
2813 	/*
2814 	 * Fail the request if the new value does not lie within the
2815 	 * required bounds.
2816 	 */
2817 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
2818 	    new_value < icmppa->icmp_param_min ||
2819 	    new_value > icmppa->icmp_param_max) {
2820 		return (EINVAL);
2821 	}
2822 	/* Set the new value */
2823 	icmppa->icmp_param_value = new_value;
2824 	return (0);
2825 }
2826 
2827 static void
2828 icmp_rput(queue_t *q, mblk_t *mp)
2829 {
2830 	struct T_unitdata_ind	*tudi;
2831 	uchar_t			*rptr;
2832 	struct T_error_ack	*tea;
2833 	icmp_t			*icmp;
2834 	sin_t			*sin;
2835 	sin6_t			*sin6;
2836 	ip6_t			*ip6h;
2837 	ip6i_t			*ip6i;
2838 	mblk_t			*mp1;
2839 	int			hdr_len;
2840 	ipha_t			*ipha;
2841 	int			udi_size;	/* Size of T_unitdata_ind */
2842 	uint_t			ipvers;
2843 	ip6_pkt_t		ipp;
2844 	uint8_t			nexthdr;
2845 	boolean_t		recvif = B_FALSE;
2846 	in_pktinfo_t		*pinfo;
2847 	mblk_t			*options_mp = NULL;
2848 	uint_t			icmp_opt = 0;
2849 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
2850 
2851 	icmp = (icmp_t *)q->q_ptr;
2852 	if (icmp->icmp_restricted) {
2853 		putnext(q, mp);
2854 		return;
2855 	}
2856 
2857 	if (mp->b_datap->db_type == M_CTL) {
2858 		/*
2859 		 * IP sends up the IPSEC_IN message for handling IPSEC
2860 		 * policy at the TCP level. We don't need it here.
2861 		 */
2862 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
2863 			mp1 = mp->b_cont;
2864 			freeb(mp);
2865 			mp = mp1;
2866 		} else {
2867 			pinfo = (in_pktinfo_t *)mp->b_rptr;
2868 			if ((icmp->icmp_recvif != 0) &&
2869 			    (pinfo->in_pkt_ulp_type == IN_PKTINFO)) {
2870 				/*
2871 				 * IP has passed the options in mp and the
2872 				 * actual data is in b_cont.
2873 				 */
2874 				recvif = B_TRUE;
2875 				/*
2876 				 * We are here bcos IP_RECVIF is set so we need
2877 				 * to extract the options mblk and adjust the
2878 				 * rptr
2879 				 */
2880 				options_mp = mp;
2881 				mp = mp->b_cont;
2882 			}
2883 		}
2884 	}
2885 
2886 	rptr = mp->b_rptr;
2887 	switch (mp->b_datap->db_type) {
2888 	case M_DATA:
2889 		/*
2890 		 * M_DATA messages contain IP packets.  They are handled
2891 		 * following the switch.
2892 		 */
2893 		break;
2894 	case M_PROTO:
2895 	case M_PCPROTO:
2896 		/* M_PROTO messages contain some type of TPI message. */
2897 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
2898 			freemsg(mp);
2899 			return;
2900 		}
2901 		tea = (struct T_error_ack *)rptr;
2902 		switch (tea->PRIM_type) {
2903 		case T_ERROR_ACK:
2904 			switch (tea->ERROR_prim) {
2905 			case O_T_BIND_REQ:
2906 			case T_BIND_REQ:
2907 				/*
2908 				 * If our O_T_BIND_REQ/T_BIND_REQ fails,
2909 				 * clear out the source address before
2910 				 * passing the message upstream.
2911 				 * If this was caused by a T_CONN_REQ
2912 				 * revert back to bound state.
2913 				 */
2914 				if (icmp->icmp_state == TS_UNBND) {
2915 					/*
2916 					 * TPI has not yet bound - bind sent by
2917 					 * icmp_bind_proto.
2918 					 */
2919 					freemsg(mp);
2920 					return;
2921 				}
2922 				if (icmp->icmp_state == TS_DATA_XFER) {
2923 					/* Connect failed */
2924 					tea->ERROR_prim = T_CONN_REQ;
2925 					icmp->icmp_v6src =
2926 					    icmp->icmp_bound_v6src;
2927 					icmp->icmp_state = TS_IDLE;
2928 					if (icmp->icmp_family == AF_INET6)
2929 						(void) icmp_build_hdrs(q, icmp);
2930 					break;
2931 				}
2932 
2933 				if (icmp->icmp_discon_pending) {
2934 					tea->ERROR_prim = T_DISCON_REQ;
2935 					icmp->icmp_discon_pending = 0;
2936 				}
2937 				V6_SET_ZERO(icmp->icmp_v6src);
2938 				V6_SET_ZERO(icmp->icmp_bound_v6src);
2939 				icmp->icmp_state = TS_UNBND;
2940 				if (icmp->icmp_family == AF_INET6)
2941 					(void) icmp_build_hdrs(q, icmp);
2942 				break;
2943 			default:
2944 				break;
2945 			}
2946 			break;
2947 		case T_BIND_ACK:
2948 			icmp_rput_bind_ack(q, mp);
2949 			return;
2950 
2951 		case T_OPTMGMT_ACK:
2952 		case T_OK_ACK:
2953 			if (tea->PRIM_type == T_OK_ACK) {
2954 				struct T_ok_ack *toa;
2955 				toa = (struct T_ok_ack *)rptr;
2956 				if (toa->CORRECT_prim == T_UNBIND_REQ) {
2957 					/*
2958 					 * If somebody sets IPSEC options, IP
2959 					 * sends some IPSEC info which is used
2960 					 * by the TCP for detached connections.
2961 					 * We don't need it here.
2962 					 */
2963 					if ((mp1 = mp->b_cont) != NULL) {
2964 						freemsg(mp1);
2965 						mp->b_cont = NULL;
2966 					}
2967 				}
2968 			}
2969 			break;
2970 		default:
2971 			freemsg(mp);
2972 			return;
2973 		}
2974 		putnext(q, mp);
2975 		return;
2976 	case M_CTL:
2977 		if (recvif) {
2978 			/*
2979 			 * IP has passed the options in mp and the actual data
2980 			 * is in b_cont. Jump to normal data processing.
2981 			 */
2982 			break;
2983 		}
2984 
2985 		/* Contains ICMP packet from IP */
2986 		icmp_icmp_error(q, mp);
2987 		return;
2988 	default:
2989 		putnext(q, mp);
2990 		return;
2991 	}
2992 
2993 	/*
2994 	 * Discard message if it is misaligned or smaller than the IP header.
2995 	 */
2996 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
2997 		freemsg(mp);
2998 		if (options_mp != NULL)
2999 			freeb(options_mp);
3000 		BUMP_MIB(&rawip_mib, rawipInErrors);
3001 		return;
3002 	}
3003 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
3004 
3005 	/* Handle M_DATA messages containing IP packets messages */
3006 	if (ipvers == IPV4_VERSION) {
3007 		/*
3008 		 * Special case where IP attaches
3009 		 * the IRE needs to be handled so that we don't send up
3010 		 * IRE to the user land.
3011 		 */
3012 		ipha = (ipha_t *)rptr;
3013 		hdr_len = IPH_HDR_LENGTH(ipha);
3014 
3015 		if (ipha->ipha_protocol == IPPROTO_TCP) {
3016 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
3017 
3018 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
3019 			    TH_SYN) && mp->b_cont != NULL) {
3020 				mp1 = mp->b_cont;
3021 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
3022 					freeb(mp1);
3023 					mp->b_cont = NULL;
3024 				}
3025 			}
3026 		}
3027 		if (icmp_bsd_compat) {
3028 			ushort_t len;
3029 			len = ntohs(ipha->ipha_length);
3030 
3031 			if (mp->b_datap->db_ref > 1) {
3032 				/*
3033 				 * Allocate a new IP header so that we can
3034 				 * modify ipha_length.
3035 				 */
3036 				mblk_t	*mp1;
3037 
3038 				mp1 = allocb(hdr_len, BPRI_MED);
3039 				if (!mp1) {
3040 					freemsg(mp);
3041 					if (options_mp != NULL)
3042 						freeb(options_mp);
3043 					BUMP_MIB(&rawip_mib, rawipInErrors);
3044 					return;
3045 				}
3046 				bcopy(rptr, mp1->b_rptr, hdr_len);
3047 				mp->b_rptr = rptr + hdr_len;
3048 				rptr = mp1->b_rptr;
3049 				ipha = (ipha_t *)rptr;
3050 				mp1->b_cont = mp;
3051 				mp1->b_wptr = rptr + hdr_len;
3052 				mp = mp1;
3053 			}
3054 			len -= hdr_len;
3055 			ipha->ipha_length = htons(len);
3056 		}
3057 	}
3058 
3059 	/*
3060 	 * This is the inbound data path.  Packets are passed upstream as
3061 	 * T_UNITDATA_IND messages with full IP headers still attached.
3062 	 */
3063 	if (icmp->icmp_family == AF_INET) {
3064 		ASSERT(ipvers == IPV4_VERSION);
3065 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
3066 		if (recvif) {
3067 			udi_size += sizeof (struct T_opthdr) +
3068 			    sizeof (uint_t);
3069 		}
3070 		mp1 = allocb(udi_size, BPRI_MED);
3071 		if (mp1 == NULL) {
3072 			freemsg(mp);
3073 			if (options_mp != NULL)
3074 				freeb(options_mp);
3075 			BUMP_MIB(&rawip_mib, rawipInErrors);
3076 			return;
3077 		}
3078 		mp1->b_cont = mp;
3079 		mp = mp1;
3080 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
3081 		mp->b_datap->db_type = M_PROTO;
3082 		mp->b_wptr = (uchar_t *)tudi + udi_size;
3083 		tudi->PRIM_type = T_UNITDATA_IND;
3084 		tudi->SRC_length = sizeof (sin_t);
3085 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
3086 		sin = (sin_t *)&tudi[1];
3087 		*sin = sin_null;
3088 		sin->sin_family = AF_INET;
3089 		sin->sin_addr.s_addr = ipha->ipha_src;
3090 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
3091 		    sizeof (sin_t);
3092 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
3093 		tudi->OPT_length = udi_size;
3094 
3095 		/*
3096 		 * Add options if IP_RECVIF is set
3097 		 */
3098 		if (udi_size != 0) {
3099 			char *dstopt;
3100 
3101 			dstopt = (char *)&sin[1];
3102 			if (recvif) {
3103 
3104 				struct T_opthdr *toh;
3105 				uint_t		*dstptr;
3106 
3107 				toh = (struct T_opthdr *)dstopt;
3108 				toh->level = IPPROTO_IP;
3109 				toh->name = IP_RECVIF;
3110 				toh->len = sizeof (struct T_opthdr) +
3111 					sizeof (uint_t);
3112 				toh->status = 0;
3113 				dstopt += sizeof (struct T_opthdr);
3114 				dstptr = (uint_t *)dstopt;
3115 				*dstptr = pinfo->in_pkt_ifindex;
3116 				dstopt += sizeof (uint_t);
3117 				freeb(options_mp);
3118 				udi_size -= toh->len;
3119 			}
3120 
3121 			/* Consumed all of allocated space */
3122 			ASSERT(udi_size == 0);
3123 		}
3124 
3125 		BUMP_MIB(&rawip_mib, rawipInDatagrams);
3126 		putnext(q, mp);
3127 		return;
3128 	}
3129 
3130 	/*
3131 	 * We don't need options_mp in the IPv6 path.
3132 	 */
3133 	if (options_mp != NULL) {
3134 		freeb(options_mp);
3135 		options_mp = NULL;
3136 	}
3137 
3138 	/*
3139 	 * Discard message if it is smaller than the IPv6 header
3140 	 * or if the header is malformed.
3141 	 */
3142 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
3143 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
3144 	    icmp->icmp_family != AF_INET6) {
3145 		freemsg(mp);
3146 		BUMP_MIB(&rawip_mib, rawipInErrors);
3147 		return;
3148 	}
3149 
3150 	/* Initialize */
3151 	ipp.ipp_fields = 0;
3152 
3153 	ip6h = (ip6_t *)rptr;
3154 	/*
3155 	 * Call on ip_find_hdr_v6 which gets the total hdr len
3156 	 * as well as individual lenghts of ext hdrs (and ptrs to
3157 	 * them).
3158 	 */
3159 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
3160 		/* Look for ifindex information */
3161 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
3162 			ip6i = (ip6i_t *)ip6h;
3163 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
3164 				ASSERT(ip6i->ip6i_ifindex != 0);
3165 				ipp.ipp_fields |= IPPF_IFINDEX;
3166 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
3167 			}
3168 			rptr = (uchar_t *)&ip6i[1];
3169 			mp->b_rptr = rptr;
3170 			if (rptr == mp->b_wptr) {
3171 				mp1 = mp->b_cont;
3172 				freeb(mp);
3173 				mp = mp1;
3174 				rptr = mp->b_rptr;
3175 			}
3176 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
3177 			ip6h = (ip6_t *)rptr;
3178 		}
3179 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
3180 	} else {
3181 		hdr_len = IPV6_HDR_LEN;
3182 		ip6i = NULL;
3183 		nexthdr = ip6h->ip6_nxt;
3184 	}
3185 	/*
3186 	 * One special case where IP attaches the IRE needs to
3187 	 * be handled so that we don't send up IRE to the user land.
3188 	 */
3189 	if (nexthdr == IPPROTO_TCP) {
3190 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
3191 
3192 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
3193 		    mp->b_cont != NULL) {
3194 			mp1 = mp->b_cont;
3195 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
3196 				freeb(mp1);
3197 				mp->b_cont = NULL;
3198 			}
3199 		}
3200 	}
3201 	/*
3202 	 * Check a filter for ICMPv6 types if needed.
3203 	 * Verify raw checksums if needed.
3204 	 */
3205 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
3206 		if (icmp->icmp_filter != NULL) {
3207 			int type;
3208 
3209 			/* Assumes that IP has done the pullupmsg */
3210 			type = mp->b_rptr[hdr_len];
3211 
3212 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
3213 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
3214 				freemsg(mp);
3215 				return;
3216 			}
3217 		} else {
3218 			/* Checksum */
3219 			uint16_t	*up;
3220 			uint32_t	sum;
3221 			int		remlen;
3222 
3223 			up = (uint16_t *)&ip6h->ip6_src;
3224 
3225 			remlen = msgdsize(mp) - hdr_len;
3226 			sum = htons(icmp->icmp_proto + remlen)
3227 			    + up[0] + up[1] + up[2] + up[3]
3228 			    + up[4] + up[5] + up[6] + up[7]
3229 			    + up[8] + up[9] + up[10] + up[11]
3230 			    + up[12] + up[13] + up[14] + up[15];
3231 			sum = (sum & 0xffff) + (sum >> 16);
3232 			sum = IP_CSUM(mp, hdr_len, sum);
3233 			if (sum != 0) {
3234 				/* IPv6 RAW checksum failed */
3235 				ip0dbg(("icmp_rput: RAW checksum "
3236 				    "failed %x\n", sum));
3237 				freemsg(mp);
3238 				BUMP_MIB(&rawip_mib, rawipInCksumErrs);
3239 				return;
3240 			}
3241 		}
3242 	}
3243 	/* Skip all the IPv6 headers per API */
3244 	mp->b_rptr += hdr_len;
3245 
3246 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
3247 
3248 	/*
3249 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
3250 	 * maintain state information, instead of relying on icmp_t
3251 	 * structure, since there arent any locks protecting these members
3252 	 * and there is a window where there might be a race between a
3253 	 * thread setting options on the write side and a thread reading
3254 	 * these options on the read size.
3255 	 */
3256 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
3257 	    IPPF_RTHDR|IPPF_IFINDEX)) {
3258 		if (icmp->icmp_ipv6_recvhopopts &&
3259 		    (ipp.ipp_fields & IPPF_HOPOPTS)) {
3260 			udi_size += sizeof (struct T_opthdr) +
3261 			    ipp.ipp_hopoptslen;
3262 			icmp_opt |= IPPF_HOPOPTS;
3263 		}
3264 		if ((icmp->icmp_ipv6_recvdstopts ||
3265 			icmp->icmp_old_ipv6_recvdstopts) &&
3266 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
3267 			udi_size += sizeof (struct T_opthdr) +
3268 			    ipp.ipp_dstoptslen;
3269 			icmp_opt |= IPPF_DSTOPTS;
3270 		}
3271 		if (((icmp->icmp_ipv6_recvdstopts &&
3272 		    icmp->icmp_ipv6_recvrthdr &&
3273 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
3274 		    icmp->icmp_ipv6_recvrtdstopts) &&
3275 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
3276 			udi_size += sizeof (struct T_opthdr) +
3277 			    ipp.ipp_rtdstoptslen;
3278 			icmp_opt |= IPPF_RTDSTOPTS;
3279 		}
3280 		if (icmp->icmp_ipv6_recvrthdr &&
3281 		    (ipp.ipp_fields & IPPF_RTHDR)) {
3282 			udi_size += sizeof (struct T_opthdr) +
3283 			    ipp.ipp_rthdrlen;
3284 			icmp_opt |= IPPF_RTHDR;
3285 		}
3286 		if (icmp->icmp_ipv6_recvpktinfo &&
3287 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
3288 			udi_size += sizeof (struct T_opthdr) +
3289 			    sizeof (struct in6_pktinfo);
3290 			icmp_opt |= IPPF_IFINDEX;
3291 		}
3292 	}
3293 	if (icmp->icmp_ipv6_recvhoplimit) {
3294 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
3295 		icmp_ipv6_recvhoplimit = B_TRUE;
3296 	}
3297 
3298 	if (icmp->icmp_ipv6_recvtclass)
3299 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
3300 
3301 	mp1 = allocb(udi_size, BPRI_MED);
3302 	if (mp1 == NULL) {
3303 		freemsg(mp);
3304 		BUMP_MIB(&rawip_mib, rawipInErrors);
3305 		return;
3306 	}
3307 	mp1->b_cont = mp;
3308 	mp = mp1;
3309 	mp->b_datap->db_type = M_PROTO;
3310 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
3311 	mp->b_wptr = (uchar_t *)tudi + udi_size;
3312 	tudi->PRIM_type = T_UNITDATA_IND;
3313 	tudi->SRC_length = sizeof (sin6_t);
3314 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
3315 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
3316 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
3317 	tudi->OPT_length = udi_size;
3318 	sin6 = (sin6_t *)&tudi[1];
3319 	sin6->sin6_port = 0;
3320 	sin6->sin6_family = AF_INET6;
3321 
3322 	sin6->sin6_addr = ip6h->ip6_src;
3323 	/* No sin6_flowinfo per API */
3324 	sin6->sin6_flowinfo = 0;
3325 	/* For link-scope source pass up scope id */
3326 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
3327 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
3328 		sin6->sin6_scope_id = ipp.ipp_ifindex;
3329 	else
3330 		sin6->sin6_scope_id = 0;
3331 
3332 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
3333 	    icmp->icmp_zoneid);
3334 
3335 	if (udi_size != 0) {
3336 		uchar_t *dstopt;
3337 
3338 		dstopt = (uchar_t *)&sin6[1];
3339 		if (icmp_opt & IPPF_IFINDEX) {
3340 			struct T_opthdr *toh;
3341 			struct in6_pktinfo *pkti;
3342 
3343 			toh = (struct T_opthdr *)dstopt;
3344 			toh->level = IPPROTO_IPV6;
3345 			toh->name = IPV6_PKTINFO;
3346 			toh->len = sizeof (struct T_opthdr) +
3347 			    sizeof (*pkti);
3348 			toh->status = 0;
3349 			dstopt += sizeof (struct T_opthdr);
3350 			pkti = (struct in6_pktinfo *)dstopt;
3351 			pkti->ipi6_addr = ip6h->ip6_dst;
3352 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
3353 			dstopt += sizeof (*pkti);
3354 			udi_size -= toh->len;
3355 		}
3356 		if (icmp_ipv6_recvhoplimit) {
3357 			struct T_opthdr *toh;
3358 
3359 			toh = (struct T_opthdr *)dstopt;
3360 			toh->level = IPPROTO_IPV6;
3361 			toh->name = IPV6_HOPLIMIT;
3362 			toh->len = sizeof (struct T_opthdr) +
3363 			    sizeof (uint_t);
3364 			toh->status = 0;
3365 			dstopt += sizeof (struct T_opthdr);
3366 			*(uint_t *)dstopt = ip6h->ip6_hops;
3367 			dstopt += sizeof (uint_t);
3368 			udi_size -= toh->len;
3369 		}
3370 		if (icmp->icmp_ipv6_recvtclass) {
3371 			struct T_opthdr *toh;
3372 
3373 			toh = (struct T_opthdr *)dstopt;
3374 			toh->level = IPPROTO_IPV6;
3375 			toh->name = IPV6_TCLASS;
3376 			toh->len = sizeof (struct T_opthdr) +
3377 			    sizeof (uint_t);
3378 			toh->status = 0;
3379 			dstopt += sizeof (struct T_opthdr);
3380 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
3381 			dstopt += sizeof (uint_t);
3382 			udi_size -= toh->len;
3383 		}
3384 		if (icmp_opt & IPPF_HOPOPTS) {
3385 			struct T_opthdr *toh;
3386 
3387 			toh = (struct T_opthdr *)dstopt;
3388 			toh->level = IPPROTO_IPV6;
3389 			toh->name = IPV6_HOPOPTS;
3390 			toh->len = sizeof (struct T_opthdr) +
3391 			    ipp.ipp_hopoptslen;
3392 			toh->status = 0;
3393 			dstopt += sizeof (struct T_opthdr);
3394 			bcopy(ipp.ipp_hopopts, dstopt,
3395 			    ipp.ipp_hopoptslen);
3396 			dstopt += ipp.ipp_hopoptslen;
3397 			udi_size -= toh->len;
3398 		}
3399 		if (icmp_opt & IPPF_RTDSTOPTS) {
3400 			struct T_opthdr *toh;
3401 
3402 			toh = (struct T_opthdr *)dstopt;
3403 			toh->level = IPPROTO_IPV6;
3404 			toh->name = IPV6_DSTOPTS;
3405 			toh->len = sizeof (struct T_opthdr) +
3406 			    ipp.ipp_rtdstoptslen;
3407 			toh->status = 0;
3408 			dstopt += sizeof (struct T_opthdr);
3409 			bcopy(ipp.ipp_rtdstopts, dstopt,
3410 			    ipp.ipp_rtdstoptslen);
3411 			dstopt += ipp.ipp_rtdstoptslen;
3412 			udi_size -= toh->len;
3413 		}
3414 		if (icmp_opt & IPPF_RTHDR) {
3415 			struct T_opthdr *toh;
3416 
3417 			toh = (struct T_opthdr *)dstopt;
3418 			toh->level = IPPROTO_IPV6;
3419 			toh->name = IPV6_RTHDR;
3420 			toh->len = sizeof (struct T_opthdr) +
3421 			    ipp.ipp_rthdrlen;
3422 			toh->status = 0;
3423 			dstopt += sizeof (struct T_opthdr);
3424 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
3425 			dstopt += ipp.ipp_rthdrlen;
3426 			udi_size -= toh->len;
3427 		}
3428 		if (icmp_opt & IPPF_DSTOPTS) {
3429 			struct T_opthdr *toh;
3430 
3431 			toh = (struct T_opthdr *)dstopt;
3432 			toh->level = IPPROTO_IPV6;
3433 			toh->name = IPV6_DSTOPTS;
3434 			toh->len = sizeof (struct T_opthdr) +
3435 			    ipp.ipp_dstoptslen;
3436 			toh->status = 0;
3437 			dstopt += sizeof (struct T_opthdr);
3438 			bcopy(ipp.ipp_dstopts, dstopt,
3439 			    ipp.ipp_dstoptslen);
3440 			dstopt += ipp.ipp_dstoptslen;
3441 			udi_size -= toh->len;
3442 		}
3443 		/* Consumed all of allocated space */
3444 		ASSERT(udi_size == 0);
3445 	}
3446 	BUMP_MIB(&rawip_mib, rawipInDatagrams);
3447 	putnext(q, mp);
3448 }
3449 
3450 /*
3451  * Process a T_BIND_ACK
3452  */
3453 static void
3454 icmp_rput_bind_ack(queue_t *q, mblk_t *mp)
3455 {
3456 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
3457 	mblk_t	*mp1;
3458 	ire_t	*ire;
3459 	struct T_bind_ack *tba;
3460 	uchar_t *addrp;
3461 	ipa_conn_t	*ac;
3462 	ipa6_conn_t	*ac6;
3463 
3464 	/*
3465 	 * We know if headers are included or not so we can
3466 	 * safely do this.
3467 	 */
3468 	if (icmp->icmp_state == TS_UNBND) {
3469 		/*
3470 		 * TPI has not yet bound - bind sent by
3471 		 * icmp_bind_proto.
3472 		 */
3473 		freemsg(mp);
3474 		return;
3475 	}
3476 	if (icmp->icmp_discon_pending)
3477 		icmp->icmp_discon_pending = 0;
3478 
3479 	/*
3480 	 * If a broadcast/multicast address was bound set
3481 	 * the source address to 0.
3482 	 * This ensures no datagrams with broadcast address
3483 	 * as source address are emitted (which would violate
3484 	 * RFC1122 - Hosts requirements)
3485 	 *
3486 	 * Note that when connecting the returned IRE is
3487 	 * for the destination address and we only perform
3488 	 * the broadcast check for the source address (it
3489 	 * is OK to connect to a broadcast/multicast address.)
3490 	 */
3491 	mp1 = mp->b_cont;
3492 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
3493 		ire = (ire_t *)mp1->b_rptr;
3494 
3495 		/*
3496 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
3497 		 * local address.
3498 		 */
3499 		if (ire->ire_type == IRE_BROADCAST &&
3500 		    icmp->icmp_state != TS_DATA_XFER) {
3501 			/* This was just a local bind to a MC/broadcast addr */
3502 			V6_SET_ZERO(icmp->icmp_v6src);
3503 			if (icmp->icmp_family == AF_INET6)
3504 				(void) icmp_build_hdrs(q, icmp);
3505 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
3506 			/*
3507 			 * Local address not yet set - pick it from the
3508 			 * T_bind_ack
3509 			 */
3510 			tba = (struct T_bind_ack *)mp->b_rptr;
3511 			addrp = &mp->b_rptr[tba->ADDR_offset];
3512 			switch (icmp->icmp_family) {
3513 			case AF_INET:
3514 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
3515 					ac = (ipa_conn_t *)addrp;
3516 				} else {
3517 					ASSERT(tba->ADDR_length ==
3518 					    sizeof (ipa_conn_x_t));
3519 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
3520 				}
3521 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
3522 				    &icmp->icmp_v6src);
3523 				break;
3524 			case AF_INET6:
3525 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
3526 					ac6 = (ipa6_conn_t *)addrp;
3527 				} else {
3528 					ASSERT(tba->ADDR_length ==
3529 					    sizeof (ipa6_conn_x_t));
3530 					ac6 = &((ipa6_conn_x_t *)
3531 					    addrp)->ac6x_conn;
3532 				}
3533 				icmp->icmp_v6src = ac6->ac6_laddr;
3534 				(void) icmp_build_hdrs(q, icmp);
3535 			}
3536 		}
3537 		mp1 = mp1->b_cont;
3538 	}
3539 	/*
3540 	 * Look for one or more appended ACK message added by
3541 	 * icmp_connect or icmp_disconnect.
3542 	 * If none found just send up the T_BIND_ACK.
3543 	 * icmp_connect has appended a T_OK_ACK and a
3544 	 * T_CONN_CON.
3545 	 * icmp_disconnect has appended a T_OK_ACK.
3546 	 */
3547 	if (mp1 != NULL) {
3548 		if (mp->b_cont == mp1)
3549 			mp->b_cont = NULL;
3550 		else {
3551 			ASSERT(mp->b_cont->b_cont == mp1);
3552 			mp->b_cont->b_cont = NULL;
3553 		}
3554 		freemsg(mp);
3555 		mp = mp1;
3556 		while (mp != NULL) {
3557 			mp1 = mp->b_cont;
3558 			mp->b_cont = NULL;
3559 			putnext(q, mp);
3560 			mp = mp1;
3561 		}
3562 		return;
3563 	}
3564 	freemsg(mp->b_cont);
3565 	mp->b_cont = NULL;
3566 	putnext(q, mp);
3567 }
3568 
3569 /*
3570  * return SNMP stuff in buffer in mpdata
3571  */
3572 static int
3573 icmp_snmp_get(queue_t *q, mblk_t *mpctl)
3574 {
3575 	mblk_t			*mpdata;
3576 	struct opthdr		*optp;
3577 
3578 	if (mpctl == NULL ||
3579 	    (mpdata = mpctl->b_cont) == NULL) {
3580 		return (0);
3581 	}
3582 
3583 	/* fixed length structure for IPv4 and IPv6 counters */
3584 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
3585 	optp->level = EXPER_RAWIP;
3586 	optp->name = 0;
3587 	(void) snmp_append_data(mpdata, (char *)&rawip_mib, sizeof (rawip_mib));
3588 	optp->len = msgdsize(mpdata);
3589 	qreply(q, mpctl);
3590 
3591 	return (1);
3592 }
3593 
3594 /*
3595  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
3596  * TODO:  If this ever actually tries to set anything, it needs to be
3597  * to do the appropriate locking.
3598  */
3599 /* ARGSUSED */
3600 static int
3601 icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
3602     uchar_t *ptr, int len)
3603 {
3604 	switch (level) {
3605 	case EXPER_RAWIP:
3606 		return (0);
3607 	default:
3608 		return (1);
3609 	}
3610 }
3611 
3612 /* Report for ndd "icmp_status" */
3613 /* ARGSUSED */
3614 static int
3615 icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
3616 {
3617 	IDP	idp;
3618 	icmp_t	*icmp;
3619 	char	*state;
3620 	char	laddrbuf[INET6_ADDRSTRLEN];
3621 	char	faddrbuf[INET6_ADDRSTRLEN];
3622 
3623 	(void) mi_mpprintf(mp,
3624 	    "RAWIP    " MI_COL_HDRPAD_STR
3625 	/*   01234567[89ABCDEF] */
3626 	    "  src addr        dest addr       state");
3627 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
3628 
3629 
3630 	for (idp = mi_first_ptr(&icmp_g_head);
3631 	    (icmp = (icmp_t *)idp) != NULL;
3632 	    idp = mi_next_ptr(&icmp_g_head, idp)) {
3633 		if (icmp->icmp_state == TS_UNBND)
3634 			state = "UNBOUND";
3635 		else if (icmp->icmp_state == TS_IDLE)
3636 			state = "IDLE";
3637 		else if (icmp->icmp_state == TS_DATA_XFER)
3638 			state = "CONNECTED";
3639 		else
3640 			state = "UnkState";
3641 
3642 		(void) mi_mpprintf(mp,
3643 		    MI_COL_PTRFMT_STR "%s %s %s",
3644 		    (void *)icmp,
3645 		    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
3646 		    sizeof (faddrbuf)),
3647 		    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
3648 		    sizeof (laddrbuf)),
3649 		    state);
3650 	}
3651 	return (0);
3652 }
3653 
3654 /*
3655  * This routine creates a T_UDERROR_IND message and passes it upstream.
3656  * The address and options are copied from the T_UNITDATA_REQ message
3657  * passed in mp.  This message is freed.
3658  */
3659 static void
3660 icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
3661 {
3662 	mblk_t	*mp1;
3663 	uchar_t	*rptr = mp->b_rptr;
3664 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
3665 
3666 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
3667 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
3668 	    tudr->OPT_length, err);
3669 	if (mp1)
3670 		qreply(q, mp1);
3671 	freemsg(mp);
3672 }
3673 
3674 /*
3675  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
3676  * After some error checking, the message is passed downstream to ip.
3677  */
3678 static void
3679 icmp_unbind(queue_t *q, mblk_t *mp)
3680 {
3681 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
3682 
3683 	/* If a bind has not been done, we can't unbind. */
3684 	if (icmp->icmp_state == TS_UNBND) {
3685 		icmp_err_ack(q, mp, TOUTSTATE, 0);
3686 		return;
3687 	}
3688 	V6_SET_ZERO(icmp->icmp_v6src);
3689 	V6_SET_ZERO(icmp->icmp_bound_v6src);
3690 	icmp->icmp_state = TS_UNBND;
3691 
3692 	if (icmp->icmp_family == AF_INET6) {
3693 		int error;
3694 
3695 		/* Rebuild the header template */
3696 		error = icmp_build_hdrs(q, icmp);
3697 		if (error != 0) {
3698 			icmp_err_ack(q, mp, TSYSERR, error);
3699 			return;
3700 		}
3701 	}
3702 	/* Pass the unbind to IP. */
3703 	putnext(q, mp);
3704 }
3705 
3706 /*
3707  * Process IPv4 packets that already include an IP header.
3708  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
3709  * IPPROTO_IGMP).
3710  */
3711 static void
3712 icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp)
3713 {
3714 	ipha_t	*ipha;
3715 	int	ip_hdr_length;
3716 	int	tp_hdr_len;
3717 	mblk_t	*mp1;
3718 	uint_t	pkt_len;
3719 
3720 	ipha = (ipha_t *)mp->b_rptr;
3721 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
3722 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
3723 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
3724 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3725 			freemsg(mp);
3726 			return;
3727 		}
3728 		ipha = (ipha_t *)mp->b_rptr;
3729 	}
3730 	ipha->ipha_version_and_hdr_length =
3731 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
3732 
3733 	/*
3734 	 * For the socket of SOCK_RAW type, the checksum is provided in the
3735 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
3736 	 * tell IP that the application has sent a complete IP header and not
3737 	 * to compute the transport checksum nor change the DF flag.
3738 	 */
3739 	ipha->ipha_ident = IP_HDR_INCLUDED;
3740 	ipha->ipha_hdr_checksum = 0;
3741 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
3742 	/* Insert options if any */
3743 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
3744 		/*
3745 		 * Put the IP header plus any transport header that is
3746 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
3747 		 * that at least the checksum field is in the first mblk.)
3748 		 */
3749 		switch (ipha->ipha_protocol) {
3750 		case IPPROTO_UDP:
3751 			tp_hdr_len = 8;
3752 			break;
3753 		case IPPROTO_TCP:
3754 			tp_hdr_len = 20;
3755 			break;
3756 		default:
3757 			tp_hdr_len = 0;
3758 			break;
3759 		}
3760 		/*
3761 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
3762 		 * tp_hdr_len bytes will be in a single mblk.
3763 		 */
3764 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
3765 		    tp_hdr_len)) {
3766 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
3767 			    tp_hdr_len)) {
3768 				BUMP_MIB(&rawip_mib, rawipOutErrors);
3769 				freemsg(mp);
3770 				return;
3771 			}
3772 			ipha = (ipha_t *)mp->b_rptr;
3773 		}
3774 
3775 		/*
3776 		 * if the length is larger then the max allowed IP packet,
3777 		 * then send an error and abort the processing.
3778 		 */
3779 		pkt_len = ntohs(ipha->ipha_length)
3780 		    + icmp->icmp_ip_snd_options_len;
3781 		if (pkt_len > IP_MAXPACKET) {
3782 			icmp_ud_err(q, mp, EMSGSIZE);
3783 			return;
3784 		}
3785 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra +
3786 		    tp_hdr_len, BPRI_LO))) {
3787 			icmp_ud_err(q, mp, ENOMEM);
3788 			return;
3789 		}
3790 		mp1->b_rptr += icmp_wroff_extra;
3791 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
3792 
3793 		ipha->ipha_length = htons((uint16_t)pkt_len);
3794 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
3795 
3796 		/* Copy transport header if any */
3797 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
3798 		mp1->b_wptr += tp_hdr_len;
3799 
3800 		/* Add options */
3801 		ipha = (ipha_t *)mp1->b_rptr;
3802 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
3803 		    icmp->icmp_ip_snd_options_len);
3804 
3805 		/* Drop IP header and transport header from original */
3806 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
3807 
3808 		mp1->b_cont = mp;
3809 		mp = mp1;
3810 		/*
3811 		 * Massage source route putting first source
3812 		 * route in ipha_dst.
3813 		 */
3814 		(void) ip_massage_options(ipha);
3815 	}
3816 	putnext(q, mp);
3817 }
3818 
3819 /*
3820  * This routine handles all messages passed downstream.  It either
3821  * consumes the message or passes it downstream; it never queues a
3822  * a message.
3823  */
3824 static void
3825 icmp_wput(queue_t *q, mblk_t *mp)
3826 {
3827 	uchar_t	*rptr = mp->b_rptr;
3828 	ipha_t	*ipha;
3829 	mblk_t	*mp1;
3830 	int	ip_hdr_length;
3831 #define	tudr ((struct T_unitdata_req *)rptr)
3832 	size_t	ip_len;
3833 	icmp_t	*icmp;
3834 	sin6_t	*sin6;
3835 	sin_t	*sin;
3836 	ipaddr_t	v4dst;
3837 
3838 	icmp = (icmp_t *)q->q_ptr;
3839 	if (icmp->icmp_restricted) {
3840 		icmp_wput_restricted(q, mp);
3841 		return;
3842 	}
3843 
3844 	switch (mp->b_datap->db_type) {
3845 	case M_DATA:
3846 		if (icmp->icmp_hdrincl) {
3847 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3848 			icmp_wput_hdrincl(q, mp, icmp);
3849 			return;
3850 		}
3851 		freemsg(mp);
3852 		return;
3853 	case M_PROTO:
3854 	case M_PCPROTO:
3855 		ip_len = mp->b_wptr - rptr;
3856 		if (ip_len >= sizeof (struct T_unitdata_req)) {
3857 			/* Expedite valid T_UNITDATA_REQ to below the switch */
3858 			if (((union T_primitives *)rptr)->type
3859 			    == T_UNITDATA_REQ)
3860 				break;
3861 		}
3862 		/* FALLTHRU */
3863 	default:
3864 		icmp_wput_other(q, mp);
3865 		return;
3866 	}
3867 
3868 	/* Handle T_UNITDATA_REQ messages here. */
3869 
3870 	if (icmp->icmp_state == TS_UNBND) {
3871 		/* If a port has not been bound to the stream, fail. */
3872 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3873 		icmp_ud_err(q, mp, EPROTO);
3874 		return;
3875 	}
3876 	mp1 = mp->b_cont;
3877 	if (mp1 == NULL) {
3878 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3879 		icmp_ud_err(q, mp, EPROTO);
3880 		return;
3881 	}
3882 
3883 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
3884 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3885 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
3886 		return;
3887 	}
3888 
3889 	switch (icmp->icmp_family) {
3890 	case AF_INET6:
3891 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
3892 		if (!OK_32PTR((char *)sin6) ||
3893 		    tudr->DEST_length != sizeof (sin6_t) ||
3894 		    sin6->sin6_family != AF_INET6) {
3895 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3896 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
3897 			return;
3898 		}
3899 
3900 		/* No support for mapped addresses on raw sockets */
3901 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3902 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3903 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
3904 			return;
3905 		}
3906 
3907 		/*
3908 		 * Destination is a native IPv6 address.
3909 		 * Send out an IPv6 format packet.
3910 		 */
3911 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
3912 		return;
3913 
3914 	case AF_INET:
3915 		sin = (sin_t *)&rptr[tudr->DEST_offset];
3916 		if (!OK_32PTR((char *)sin) ||
3917 		    tudr->DEST_length != sizeof (sin_t) ||
3918 		    sin->sin_family != AF_INET) {
3919 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3920 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
3921 			return;
3922 		}
3923 		/* Extract and ipaddr */
3924 		v4dst = sin->sin_addr.s_addr;
3925 		break;
3926 	}
3927 
3928 	/*
3929 	 * If options passed in, feed it for verification and handling
3930 	 */
3931 	if (tudr->OPT_length != 0) {
3932 		int error;
3933 
3934 		if (icmp_unitdata_opt_process(q, mp, &error,
3935 		    (uchar_t *)0) < 0) {
3936 			/* failure */
3937 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3938 			icmp_ud_err(q, mp, error);
3939 			return;
3940 		}
3941 		/*
3942 		 * Note: Success in processing options.
3943 		 * mp option buffer represented by
3944 		 * OPT_length/offset now potentially modified
3945 		 * and contain option setting results
3946 		 */
3947 	}
3948 
3949 	/* Protocol 255 contains full IP headers */
3950 	if (icmp->icmp_hdrincl) {
3951 		freeb(mp);
3952 		icmp_wput_hdrincl(q, mp1, icmp);
3953 		return;
3954 	}
3955 	/* Add an IP header */
3956 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
3957 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
3958 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
3959 	    mp1->b_datap->db_ref != 1 ||
3960 	    !OK_32PTR(ipha)) {
3961 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra,
3962 		    BPRI_LO))) {
3963 			BUMP_MIB(&rawip_mib, rawipOutErrors);
3964 			icmp_ud_err(q, mp1, ENOMEM);
3965 			return;
3966 		}
3967 		mp1->b_cont = mp->b_cont;
3968 		ipha = (ipha_t *)mp1->b_datap->db_lim;
3969 		mp1->b_wptr = (uchar_t *)ipha;
3970 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
3971 	}
3972 #ifdef	_BIG_ENDIAN
3973 	/* Set version, header length, and tos */
3974 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
3975 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
3976 		icmp->icmp_type_of_service);
3977 	/* Set ttl and protocol */
3978 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
3979 #else
3980 	/* Set version, header length, and tos */
3981 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
3982 	    ((icmp->icmp_type_of_service << 8) |
3983 		((IP_VERSION << 4) | (ip_hdr_length>>2)));
3984 	/* Set ttl and protocol */
3985 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
3986 #endif
3987 	/*
3988 	 * Copy our address into the packet.  If this is zero,
3989 	 * ip will fill in the real source address.
3990 	 */
3991 	IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
3992 	ipha->ipha_fragment_offset_and_flags = 0;
3993 
3994 	/*
3995 	 * For the socket of SOCK_RAW type, the checksum is provided in the
3996 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
3997 	 * tell IP that the application has sent a complete IP header and not
3998 	 * to compute the transport checksum nor change the DF flag.
3999 	 */
4000 	ipha->ipha_ident = IP_HDR_INCLUDED;
4001 
4002 	/* Finish common formatting of the packet. */
4003 	mp1->b_rptr = (uchar_t *)ipha;
4004 
4005 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
4006 	if (mp1->b_cont != NULL)
4007 		ip_len += msgdsize(mp1->b_cont);
4008 
4009 	/*
4010 	 * Set the length into the IP header.
4011 	 * If the length is greater than the maximum allowed by IP,
4012 	 * then free the message and return. Do not try and send it
4013 	 * as this can cause problems in layers below.
4014 	 */
4015 	if (ip_len > IP_MAXPACKET) {
4016 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4017 		icmp_ud_err(q, mp, EMSGSIZE);
4018 		return;
4019 	}
4020 	ipha->ipha_length = htons((uint16_t)ip_len);
4021 	/*
4022 	 * Copy in the destination address from the T_UNITDATA
4023 	 * request
4024 	 */
4025 	if (v4dst == INADDR_ANY)
4026 		ipha->ipha_dst = htonl(INADDR_LOOPBACK);
4027 	else
4028 		ipha->ipha_dst = v4dst;
4029 
4030 	/*
4031 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
4032 	 */
4033 	if (CLASSD(v4dst))
4034 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
4035 
4036 	/* Copy in options if any */
4037 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
4038 		bcopy(icmp->icmp_ip_snd_options,
4039 		    &ipha[1], icmp->icmp_ip_snd_options_len);
4040 		/*
4041 		 * Massage source route putting first source route in ipha_dst.
4042 		 * Ignore the destination in the T_unitdata_req.
4043 		 */
4044 		(void) ip_massage_options(ipha);
4045 	}
4046 	freeb(mp);
4047 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
4048 	putnext(q, mp1);
4049 #undef	ipha
4050 #undef tudr
4051 }
4052 
4053 /*
4054  * icmp_wput_ipv6():
4055  * Assumes that icmp_wput did some sanity checking on the destination
4056  * address.
4057  */
4058 void
4059 icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
4060 {
4061 	ip6_t			*ip6h;
4062 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
4063 	mblk_t			*mp1;
4064 	int			ip_hdr_len = IPV6_HDR_LEN;
4065 	size_t			ip_len;
4066 	icmp_t			*icmp;
4067 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
4068 	ip6_pkt_t		*ipp = &ipp_s;
4069 	ip6_pkt_t		*tipp;
4070 	uint32_t		csum = 0;
4071 	uint_t			ignore = 0;
4072 	uint_t			option_exists = 0, is_sticky = 0;
4073 	uint8_t			*cp;
4074 	uint8_t			*nxthdr_ptr;
4075 
4076 	icmp = (icmp_t *)q->q_ptr;
4077 
4078 	/*
4079 	 * If the local address is a mapped address return
4080 	 * an error.
4081 	 * It would be possible to send an IPv6 packet but the
4082 	 * response would never make it back to the application
4083 	 * since it is bound to a mapped address.
4084 	 */
4085 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
4086 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4087 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
4088 		return;
4089 	}
4090 
4091 	ipp->ipp_fields = 0;
4092 	ipp->ipp_sticky_ignored = 0;
4093 
4094 	/*
4095 	 * If TPI options passed in, feed it for verification and handling
4096 	 */
4097 	if (tudr_optlen != 0) {
4098 		int error;
4099 
4100 		if (icmp_unitdata_opt_process(q, mp, &error,
4101 		    (void *)ipp) < 0) {
4102 			/* failure */
4103 			BUMP_MIB(&rawip_mib, rawipOutErrors);
4104 			icmp_ud_err(q, mp, error);
4105 			return;
4106 		}
4107 		ignore = ipp->ipp_sticky_ignored;
4108 		ASSERT(error == 0);
4109 	}
4110 
4111 	if (sin6->sin6_scope_id != 0 &&
4112 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
4113 		/*
4114 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
4115 		 * option nor ancillary data.  It needs to be
4116 		 * explicitly set in options_exists.
4117 		 */
4118 		option_exists |= IPPF_SCOPE_ID;
4119 	}
4120 
4121 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
4122 	    (ipp->ipp_fields == 0)) {
4123 		/* No sticky options nor ancillary data. */
4124 		goto no_options;
4125 	}
4126 
4127 	/*
4128 	 * Go through the options figuring out where each is going to
4129 	 * come from and build two masks.  The first mask indicates if
4130 	 * the option exists at all.  The second mask indicates if the
4131 	 * option is sticky or ancillary.
4132 	 */
4133 	if (!(ignore & IPPF_HOPOPTS)) {
4134 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
4135 			option_exists |= IPPF_HOPOPTS;
4136 			ip_hdr_len += ipp->ipp_hopoptslen;
4137 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
4138 			option_exists |= IPPF_HOPOPTS;
4139 			is_sticky |= IPPF_HOPOPTS;
4140 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
4141 		}
4142 	}
4143 
4144 	if (!(ignore & IPPF_RTHDR)) {
4145 		if (ipp->ipp_fields & IPPF_RTHDR) {
4146 			option_exists |= IPPF_RTHDR;
4147 			ip_hdr_len += ipp->ipp_rthdrlen;
4148 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
4149 			option_exists |= IPPF_RTHDR;
4150 			is_sticky |= IPPF_RTHDR;
4151 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
4152 		}
4153 	}
4154 
4155 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
4156 		/*
4157 		 * Need to have a router header to use these.
4158 		 */
4159 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
4160 			option_exists |= IPPF_RTDSTOPTS;
4161 			ip_hdr_len += ipp->ipp_rtdstoptslen;
4162 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
4163 			option_exists |= IPPF_RTDSTOPTS;
4164 			is_sticky |= IPPF_RTDSTOPTS;
4165 			ip_hdr_len +=
4166 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
4167 		}
4168 	}
4169 
4170 	if (!(ignore & IPPF_DSTOPTS)) {
4171 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
4172 			option_exists |= IPPF_DSTOPTS;
4173 			ip_hdr_len += ipp->ipp_dstoptslen;
4174 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
4175 			option_exists |= IPPF_DSTOPTS;
4176 			is_sticky |= IPPF_DSTOPTS;
4177 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
4178 		}
4179 	}
4180 
4181 	if (!(ignore & IPPF_IFINDEX)) {
4182 		if (ipp->ipp_fields & IPPF_IFINDEX) {
4183 			option_exists |= IPPF_IFINDEX;
4184 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
4185 			option_exists |= IPPF_IFINDEX;
4186 			is_sticky |= IPPF_IFINDEX;
4187 		}
4188 	}
4189 
4190 	if (!(ignore & IPPF_ADDR)) {
4191 		if (ipp->ipp_fields & IPPF_ADDR) {
4192 			option_exists |= IPPF_ADDR;
4193 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
4194 			option_exists |= IPPF_ADDR;
4195 			is_sticky |= IPPF_ADDR;
4196 		}
4197 	}
4198 
4199 	if (!(ignore & IPPF_DONTFRAG)) {
4200 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
4201 			option_exists |= IPPF_DONTFRAG;
4202 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
4203 			option_exists |= IPPF_DONTFRAG;
4204 			is_sticky |= IPPF_DONTFRAG;
4205 		}
4206 	}
4207 
4208 	if (!(ignore & IPPF_USE_MIN_MTU)) {
4209 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
4210 			option_exists |= IPPF_USE_MIN_MTU;
4211 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
4212 		    IPPF_USE_MIN_MTU) {
4213 			option_exists |= IPPF_USE_MIN_MTU;
4214 			is_sticky |= IPPF_USE_MIN_MTU;
4215 		}
4216 	}
4217 
4218 	if (!(ignore & IPPF_NEXTHOP)) {
4219 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
4220 			option_exists |= IPPF_NEXTHOP;
4221 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
4222 			option_exists |= IPPF_NEXTHOP;
4223 			is_sticky |= IPPF_NEXTHOP;
4224 		}
4225 	}
4226 
4227 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4228 		option_exists |= IPPF_HOPLIMIT;
4229 	/* IPV6_HOPLIMIT can never be sticky */
4230 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4231 
4232 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4233 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4234 		option_exists |= IPPF_UNICAST_HOPS;
4235 		is_sticky |= IPPF_UNICAST_HOPS;
4236 	}
4237 
4238 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4239 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4240 		option_exists |= IPPF_MULTICAST_HOPS;
4241 		is_sticky |= IPPF_MULTICAST_HOPS;
4242 	}
4243 
4244 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
4245 		/* This is a sticky socket option only */
4246 		option_exists |= IPPF_NO_CKSUM;
4247 		is_sticky |= IPPF_NO_CKSUM;
4248 	}
4249 
4250 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
4251 		/* This is a sticky socket option only */
4252 		option_exists |= IPPF_RAW_CKSUM;
4253 		is_sticky |= IPPF_RAW_CKSUM;
4254 	}
4255 
4256 	if (!(ignore & IPPF_TCLASS)) {
4257 		if (ipp->ipp_fields & IPPF_TCLASS) {
4258 			option_exists |= IPPF_TCLASS;
4259 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
4260 			option_exists |= IPPF_TCLASS;
4261 			is_sticky |= IPPF_TCLASS;
4262 		}
4263 	}
4264 
4265 no_options:
4266 
4267 	/*
4268 	 * If any options carried in the ip6i_t were specified, we
4269 	 * need to account for the ip6i_t in the data we'll be sending
4270 	 * down.
4271 	 */
4272 	if (option_exists & IPPF_HAS_IP6I)
4273 		ip_hdr_len += sizeof (ip6i_t);
4274 
4275 	/* check/fix buffer config, setup pointers into it */
4276 	mp1 = mp->b_cont;
4277 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
4278 	if ((mp1->b_datap->db_ref != 1) ||
4279 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
4280 	    !OK_32PTR(ip6h)) {
4281 		/* Try to get everything in a single mblk next time */
4282 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
4283 			icmp->icmp_max_hdr_len = ip_hdr_len;
4284 			(void) mi_set_sth_wroff(RD(q),
4285 			    icmp->icmp_max_hdr_len + icmp_wroff_extra);
4286 		}
4287 		mp1 = allocb(ip_hdr_len + icmp_wroff_extra, BPRI_LO);
4288 		if (!mp1) {
4289 			BUMP_MIB(&rawip_mib, rawipOutErrors);
4290 			icmp_ud_err(q, mp, ENOMEM);
4291 			return;
4292 		}
4293 		mp1->b_cont = mp->b_cont;
4294 		mp1->b_wptr = mp1->b_datap->db_lim;
4295 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
4296 	}
4297 	mp1->b_rptr = (unsigned char *)ip6h;
4298 	ip6i = (ip6i_t *)ip6h;
4299 
4300 #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
4301 	if (option_exists & IPPF_HAS_IP6I) {
4302 		ip6h = (ip6_t *)&ip6i[1];
4303 		ip6i->ip6i_flags = 0;
4304 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
4305 
4306 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
4307 		if (option_exists & IPPF_SCOPE_ID) {
4308 			ip6i->ip6i_flags |= IP6I_IFINDEX;
4309 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
4310 		} else if (option_exists & IPPF_IFINDEX) {
4311 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
4312 			ASSERT(tipp->ipp_ifindex != 0);
4313 			ip6i->ip6i_flags |= IP6I_IFINDEX;
4314 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
4315 		}
4316 
4317 		if (option_exists & IPPF_RAW_CKSUM) {
4318 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
4319 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
4320 		}
4321 
4322 		if (option_exists & IPPF_NO_CKSUM) {
4323 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
4324 		}
4325 
4326 		if (option_exists & IPPF_ADDR) {
4327 			/*
4328 			 * Enable per-packet source address verification if
4329 			 * IPV6_PKTINFO specified the source address.
4330 			 * ip6_src is set in the transport's _wput function.
4331 			 */
4332 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
4333 		}
4334 
4335 		if (option_exists & IPPF_DONTFRAG) {
4336 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
4337 		}
4338 
4339 		if (option_exists & IPPF_USE_MIN_MTU) {
4340 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
4341 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
4342 		}
4343 
4344 		if (option_exists & IPPF_NEXTHOP) {
4345 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
4346 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
4347 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
4348 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
4349 		}
4350 
4351 		/*
4352 		 * tell IP this is an ip6i_t private header
4353 		 */
4354 		ip6i->ip6i_nxt = IPPROTO_RAW;
4355 	}
4356 
4357 	/* Initialize IPv6 header */
4358 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
4359 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
4360 
4361 	/* Set the hoplimit of the outgoing packet. */
4362 	if (option_exists & IPPF_HOPLIMIT) {
4363 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4364 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4365 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4366 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4367 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4368 		if (option_exists & IPPF_MULTICAST_HOPS)
4369 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4370 	} else {
4371 		ip6h->ip6_hops = icmp->icmp_ttl;
4372 		if (option_exists & IPPF_UNICAST_HOPS)
4373 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4374 	}
4375 
4376 	if (option_exists & IPPF_ADDR) {
4377 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
4378 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
4379 		ip6h->ip6_src = tipp->ipp_addr;
4380 	} else {
4381 		/*
4382 		 * The source address was not set using IPV6_PKTINFO.
4383 		 * First look at the bound source.
4384 		 * If unspecified fallback to __sin6_src_id.
4385 		 */
4386 		ip6h->ip6_src = icmp->icmp_v6src;
4387 		if (sin6->__sin6_src_id != 0 &&
4388 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
4389 			ip_srcid_find_id(sin6->__sin6_src_id,
4390 			    &ip6h->ip6_src, icmp->icmp_zoneid);
4391 		}
4392 	}
4393 
4394 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
4395 	cp = (uint8_t *)&ip6h[1];
4396 
4397 	/*
4398 	 * Here's where we have to start stringing together
4399 	 * any extension headers in the right order:
4400 	 * Hop-by-hop, destination, routing, and final destination opts.
4401 	 */
4402 	if (option_exists & IPPF_HOPOPTS) {
4403 		/* Hop-by-hop options */
4404 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
4405 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
4406 
4407 		*nxthdr_ptr = IPPROTO_HOPOPTS;
4408 		nxthdr_ptr = &hbh->ip6h_nxt;
4409 
4410 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
4411 		cp += tipp->ipp_hopoptslen;
4412 	}
4413 	/*
4414 	 * En-route destination options
4415 	 * Only do them if there's a routing header as well
4416 	 */
4417 	if (option_exists & IPPF_RTDSTOPTS) {
4418 		ip6_dest_t *dst = (ip6_dest_t *)cp;
4419 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
4420 
4421 		*nxthdr_ptr = IPPROTO_DSTOPTS;
4422 		nxthdr_ptr = &dst->ip6d_nxt;
4423 
4424 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
4425 		cp += tipp->ipp_rtdstoptslen;
4426 	}
4427 	/*
4428 	 * Routing header next
4429 	 */
4430 	if (option_exists & IPPF_RTHDR) {
4431 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
4432 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
4433 
4434 		*nxthdr_ptr = IPPROTO_ROUTING;
4435 		nxthdr_ptr = &rt->ip6r_nxt;
4436 
4437 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
4438 		cp += tipp->ipp_rthdrlen;
4439 	}
4440 	/*
4441 	 * Do ultimate destination options
4442 	 */
4443 	if (option_exists & IPPF_DSTOPTS) {
4444 		ip6_dest_t *dest = (ip6_dest_t *)cp;
4445 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
4446 
4447 		*nxthdr_ptr = IPPROTO_DSTOPTS;
4448 		nxthdr_ptr = &dest->ip6d_nxt;
4449 
4450 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
4451 		cp += tipp->ipp_dstoptslen;
4452 	}
4453 
4454 	/*
4455 	 * Now set the last header pointer to the proto passed in
4456 	 */
4457 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
4458 	*nxthdr_ptr = icmp->icmp_proto;
4459 
4460 	/*
4461 	 * Copy in the destination address
4462 	 */
4463 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
4464 		ip6h->ip6_dst = ipv6_loopback;
4465 	else
4466 		ip6h->ip6_dst = sin6->sin6_addr;
4467 
4468 	ip6h->ip6_vcf =
4469 		(IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
4470 		(sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
4471 
4472 	if (option_exists & IPPF_TCLASS) {
4473 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
4474 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
4475 		    tipp->ipp_tclass);
4476 	}
4477 	if (option_exists & IPPF_RTHDR) {
4478 		ip6_rthdr_t	*rth;
4479 
4480 		/*
4481 		 * Perform any processing needed for source routing.
4482 		 * We know that all extension headers will be in the same mblk
4483 		 * as the IPv6 header.
4484 		 */
4485 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
4486 		if (rth != NULL && rth->ip6r_segleft != 0) {
4487 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
4488 				/*
4489 				 * Drop packet - only support Type 0 routing.
4490 				 * Notify the application as well.
4491 				 */
4492 				icmp_ud_err(q, mp, EPROTO);
4493 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4494 				return;
4495 			}
4496 			/*
4497 			 * rth->ip6r_len is twice the number of
4498 			 * addresses in the header
4499 			 */
4500 			if (rth->ip6r_len & 0x1) {
4501 				icmp_ud_err(q, mp, EPROTO);
4502 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4503 				return;
4504 			}
4505 			/*
4506 			 * Shuffle the routing header and ip6_dst
4507 			 * addresses, and get the checksum difference
4508 			 * between the first hop (in ip6_dst) and
4509 			 * the destination (in the last routing hdr entry).
4510 			 */
4511 			csum = ip_massage_options_v6(ip6h, rth);
4512 			/*
4513 			 * Verify that the first hop isn't a mapped address.
4514 			 * Routers along the path need to do this verification
4515 			 * for subsequent hops.
4516 			 */
4517 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
4518 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
4519 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4520 				return;
4521 			}
4522 		}
4523 	}
4524 
4525 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
4526 	if (mp1->b_cont != NULL)
4527 		ip_len += msgdsize(mp1->b_cont);
4528 
4529 	/*
4530 	 * Set the length into the IP header.
4531 	 * If the length is greater than the maximum allowed by IP,
4532 	 * then free the message and return. Do not try and send it
4533 	 * as this can cause problems in layers below.
4534 	 */
4535 	if (ip_len > IP_MAXPACKET) {
4536 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4537 		icmp_ud_err(q, mp1, EMSGSIZE);
4538 		return;
4539 	}
4540 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
4541 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
4542 		uint16_t *cksum_ptr;
4543 		uint_t	ext_hdrs_len;
4544 
4545 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
4546 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
4547 		    icmp->icmp_checksum_off == 2);
4548 
4549 		/*
4550 		 * We make it easy for IP to include our pseudo header
4551 		 * by putting our length in uh_checksum, modified (if
4552 		 * we have a routing header) by the checksum difference
4553 		 * between the ultimate destination and first hop addresses.
4554 		 * Note: ICMPv6 must always checksum the packet.
4555 		 */
4556 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
4557 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
4558 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
4559 				BUMP_MIB(&rawip_mib, rawipOutErrors);
4560 				freemsg(mp);
4561 				return;
4562 			}
4563 			ip6i = (ip6i_t *)mp1->b_rptr;
4564 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
4565 				ip6h = (ip6_t *)&ip6i[1];
4566 			else
4567 				ip6h = (ip6_t *)ip6i;
4568 		}
4569 		/* Add payload length to checksum */
4570 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
4571 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
4572 		csum += htons(ip_len - ext_hdrs_len);
4573 
4574 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
4575 		csum = (csum & 0xFFFF) + (csum >> 16);
4576 		*cksum_ptr = (uint16_t)csum;
4577 	}
4578 
4579 #ifdef _LITTLE_ENDIAN
4580 	ip_len = htons(ip_len);
4581 #endif
4582 	ip6h->ip6_plen = (uint16_t)ip_len;
4583 
4584 	freeb(mp);
4585 
4586 	/* We're done. Pass the packet to IP */
4587 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
4588 	putnext(q, mp1);
4589 }
4590 
4591 static void
4592 icmp_wput_other(queue_t *q, mblk_t *mp)
4593 {
4594 	uchar_t	*rptr = mp->b_rptr;
4595 	struct iocblk *iocp;
4596 #define	tudr ((struct T_unitdata_req *)rptr)
4597 	icmp_t	*icmp;
4598 	cred_t *cr;
4599 
4600 	icmp = (icmp_t *)q->q_ptr;
4601 
4602 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
4603 
4604 	switch (mp->b_datap->db_type) {
4605 	case M_PROTO:
4606 	case M_PCPROTO:
4607 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
4608 			/*
4609 			 * If the message does not contain a PRIM_type,
4610 			 * throw it away.
4611 			 */
4612 			freemsg(mp);
4613 			return;
4614 		}
4615 		switch (((union T_primitives *)rptr)->type) {
4616 		case T_ADDR_REQ:
4617 			icmp_addr_req(q, mp);
4618 			return;
4619 		case O_T_BIND_REQ:
4620 		case T_BIND_REQ:
4621 			qwriter(q, mp, icmp_bind, PERIM_OUTER);
4622 			return;
4623 		case T_CONN_REQ:
4624 			icmp_connect(q, mp);
4625 			return;
4626 		case T_CAPABILITY_REQ:
4627 			icmp_capability_req(q, mp);
4628 			return;
4629 		case T_INFO_REQ:
4630 			icmp_info_req(q, mp);
4631 			return;
4632 		case T_UNITDATA_REQ:
4633 			/*
4634 			 * If a T_UNITDATA_REQ gets here, the address must
4635 			 * be bad.  Valid T_UNITDATA_REQs are found above
4636 			 * and break to below this switch.
4637 			 */
4638 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
4639 			return;
4640 		case T_UNBIND_REQ:
4641 			icmp_unbind(q, mp);
4642 			return;
4643 
4644 		case T_SVR4_OPTMGMT_REQ:
4645 			if (!snmpcom_req(q, mp, icmp_snmp_set, icmp_snmp_get,
4646 			    cr))
4647 				/* Only IP can return anything meaningful */
4648 				(void) svr4_optcom_req(q, mp, cr,
4649 				    &icmp_opt_obj);
4650 			return;
4651 
4652 		case T_OPTMGMT_REQ:
4653 			/* Only IP can return anything meaningful */
4654 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
4655 			return;
4656 
4657 		case T_DISCON_REQ:
4658 			icmp_disconnect(q, mp);
4659 			return;
4660 
4661 		/* The following TPI message is not supported by icmp. */
4662 		case O_T_CONN_RES:
4663 		case T_CONN_RES:
4664 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
4665 			return;
4666 
4667 		/* The following 3 TPI requests are illegal for icmp. */
4668 		case T_DATA_REQ:
4669 		case T_EXDATA_REQ:
4670 		case T_ORDREL_REQ:
4671 			freemsg(mp);
4672 			(void) putctl1(RD(q), M_ERROR, EPROTO);
4673 			return;
4674 		default:
4675 			break;
4676 		}
4677 		break;
4678 	case M_IOCTL:
4679 		iocp = (struct iocblk *)mp->b_rptr;
4680 		switch (iocp->ioc_cmd) {
4681 		case TI_GETPEERNAME:
4682 			if (icmp->icmp_state != TS_DATA_XFER) {
4683 				/*
4684 				 * If a default destination address has not
4685 				 * been associated with the stream, then we
4686 				 * don't know the peer's name.
4687 				 */
4688 				iocp->ioc_error = ENOTCONN;
4689 			    err_ret:;
4690 				iocp->ioc_count = 0;
4691 				mp->b_datap->db_type = M_IOCACK;
4692 				qreply(q, mp);
4693 				return;
4694 			}
4695 			/* FALLTHRU */
4696 		case TI_GETMYNAME:
4697 			/*
4698 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
4699 			 * need to copyin the user's strbuf structure.
4700 			 * Processing will continue in the M_IOCDATA case
4701 			 * below.
4702 			 */
4703 			mi_copyin(q, mp, NULL,
4704 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
4705 			return;
4706 		case ND_SET:
4707 			/* nd_getset performs the necessary error checking */
4708 		case ND_GET:
4709 			if (nd_getset(q, icmp_g_nd, mp)) {
4710 				qreply(q, mp);
4711 				return;
4712 			}
4713 			break;
4714 		default:
4715 			break;
4716 		}
4717 		break;
4718 	case M_IOCDATA:
4719 		icmp_wput_iocdata(q, mp);
4720 		return;
4721 	default:
4722 		break;
4723 	}
4724 	putnext(q, mp);
4725 }
4726 
4727 /*
4728  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
4729  * messages.
4730  */
4731 static void
4732 icmp_wput_iocdata(queue_t *q, mblk_t *mp)
4733 {
4734 	mblk_t	*mp1;
4735 	STRUCT_HANDLE(strbuf, sb);
4736 	icmp_t	*icmp;
4737 	in6_addr_t	v6addr;
4738 	ipaddr_t	v4addr;
4739 	uint32_t	flowinfo = 0;
4740 	int		addrlen;
4741 
4742 	/* Make sure it is one of ours. */
4743 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4744 	case TI_GETMYNAME:
4745 	case TI_GETPEERNAME:
4746 		break;
4747 	default:
4748 		putnext(q, mp);
4749 		return;
4750 	}
4751 	switch (mi_copy_state(q, mp, &mp1)) {
4752 	case -1:
4753 		return;
4754 	case MI_COPY_CASE(MI_COPY_IN, 1):
4755 		break;
4756 	case MI_COPY_CASE(MI_COPY_OUT, 1):
4757 		/*
4758 		 * The address has been copied out, so now
4759 		 * copyout the strbuf.
4760 		 */
4761 		mi_copyout(q, mp);
4762 		return;
4763 	case MI_COPY_CASE(MI_COPY_OUT, 2):
4764 		/*
4765 		 * The address and strbuf have been copied out.
4766 		 * We're done, so just acknowledge the original
4767 		 * M_IOCTL.
4768 		 */
4769 		mi_copy_done(q, mp, 0);
4770 		return;
4771 	default:
4772 		/*
4773 		 * Something strange has happened, so acknowledge
4774 		 * the original M_IOCTL with an EPROTO error.
4775 		 */
4776 		mi_copy_done(q, mp, EPROTO);
4777 		return;
4778 	}
4779 	/*
4780 	 * Now we have the strbuf structure for TI_GETMYNAME
4781 	 * and TI_GETPEERNAME.  Next we copyout the requested
4782 	 * address and then we'll copyout the strbuf.
4783 	 */
4784 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
4785 	    (void *)mp1->b_rptr);
4786 	icmp = (icmp_t *)q->q_ptr;
4787 	if (icmp->icmp_family == AF_INET)
4788 		addrlen = sizeof (sin_t);
4789 	else
4790 		addrlen = sizeof (sin6_t);
4791 
4792 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
4793 		mi_copy_done(q, mp, EINVAL);
4794 		return;
4795 	}
4796 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4797 	case TI_GETMYNAME:
4798 		if (icmp->icmp_family == AF_INET) {
4799 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
4800 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
4801 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
4802 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
4803 			} else {
4804 				/*
4805 				 * INADDR_ANY
4806 				 * icmp_v6src is not set, we might be bound to
4807 				 * broadcast/multicast. Use icmp_bound_v6src as
4808 				 * local address instead (that could
4809 				 * also still be INADDR_ANY)
4810 				 */
4811 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
4812 			}
4813 		} else {
4814 			/* icmp->icmp_family == AF_INET6 */
4815 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
4816 				v6addr = icmp->icmp_v6src;
4817 			} else {
4818 				/*
4819 				 * UNSPECIFIED
4820 				 * icmp_v6src is not set, we might be bound to
4821 				 * broadcast/multicast. Use icmp_bound_v6src as
4822 				 * local address instead (that could
4823 				 * also still be UNSPECIFIED)
4824 				 */
4825 				v6addr = icmp->icmp_bound_v6src;
4826 			}
4827 		}
4828 		break;
4829 	case TI_GETPEERNAME:
4830 		if (icmp->icmp_family == AF_INET) {
4831 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
4832 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
4833 		} else {
4834 			/* icmp->icmp_family == AF_INET6) */
4835 			v6addr = icmp->icmp_v6dst;
4836 			flowinfo = icmp->icmp_flowinfo;
4837 		}
4838 		break;
4839 	default:
4840 		mi_copy_done(q, mp, EPROTO);
4841 		return;
4842 	}
4843 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
4844 	if (!mp1)
4845 		return;
4846 
4847 	if (icmp->icmp_family == AF_INET) {
4848 		sin_t *sin;
4849 
4850 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
4851 		sin = (sin_t *)mp1->b_rptr;
4852 		mp1->b_wptr = (uchar_t *)&sin[1];
4853 		*sin = sin_null;
4854 		sin->sin_family = AF_INET;
4855 		sin->sin_addr.s_addr = v4addr;
4856 	} else {
4857 		/* icmp->icmp_family == AF_INET6 */
4858 		sin6_t *sin6;
4859 
4860 		ASSERT(icmp->icmp_family == AF_INET6);
4861 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
4862 		sin6 = (sin6_t *)mp1->b_rptr;
4863 		mp1->b_wptr = (uchar_t *)&sin6[1];
4864 		*sin6 = sin6_null;
4865 		sin6->sin6_family = AF_INET6;
4866 		sin6->sin6_flowinfo = flowinfo;
4867 		sin6->sin6_addr = v6addr;
4868 	}
4869 	/* Copy out the address */
4870 	mi_copyout(q, mp);
4871 }
4872 
4873 /*
4874  * Only allow MIB requests and M_FLUSHes to pass.
4875  * All other messages are nacked or dropped.
4876  */
4877 static void
4878 icmp_wput_restricted(queue_t *q, mblk_t *mp)
4879 {
4880 	cred_t	*cr;
4881 	icmp_t	*icmp;
4882 
4883 	switch (DB_TYPE(mp)) {
4884 	case M_PROTO:
4885 	case M_PCPROTO:
4886 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
4887 			freemsg(mp);
4888 			return;
4889 		}
4890 		icmp = (icmp_t *)q->q_ptr;
4891 		cr = DB_CREDDEF(mp, icmp->icmp_credp);
4892 
4893 		switch (((union T_primitives *)mp->b_rptr)->type) {
4894 		case T_SVR4_OPTMGMT_REQ:
4895 			if (!snmpcom_req(q, mp,
4896 			    icmp_snmp_set, icmp_snmp_get, cr))
4897 				(void) svr4_optcom_req(q, mp, cr,
4898 				    &icmp_opt_obj);
4899 			return;
4900 		case T_OPTMGMT_REQ:
4901 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
4902 			return;
4903 		default:
4904 			icmp_err_ack(q, mp, TSYSERR, ENOTSUP);
4905 			return;
4906 		}
4907 		/* NOTREACHED */
4908 	case M_IOCTL:
4909 		miocnak(q, mp, 0, ENOTSUP);
4910 		break;
4911 	case M_FLUSH:
4912 		putnext(q, mp);
4913 		break;
4914 	default:
4915 		freemsg(mp);
4916 		break;
4917 	}
4918 }
4919 
4920 static int
4921 icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
4922     void *thisdg_attrs)
4923 {
4924 	icmp_t	*icmp;
4925 	struct T_unitdata_req *udreqp;
4926 	int is_absreq_failure;
4927 	cred_t *cr;
4928 
4929 	icmp = (icmp_t *)q->q_ptr;
4930 
4931 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
4932 	*errorp = 0;
4933 
4934 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
4935 
4936 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
4937 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
4938 	    thisdg_attrs, &is_absreq_failure);
4939 
4940 	if (*errorp != 0) {
4941 		/*
4942 		 * Note: No special action needed in this
4943 		 * module for "is_absreq_failure"
4944 		 */
4945 		return (-1);		/* failure */
4946 	}
4947 	ASSERT(is_absreq_failure == 0);
4948 	return (0);	/* success */
4949 }
4950 
4951 void
4952 icmp_ddi_init(void)
4953 {
4954 	ICMP6_MAJ = ddi_name_to_major(ICMP6);
4955 	icmp_max_optsize =
4956 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
4957 		icmp_opt_obj.odb_opt_arr_cnt);
4958 
4959 	(void) icmp_param_register(icmp_param_arr, A_CNT(icmp_param_arr));
4960 
4961 	rawip_kstat_init();
4962 }
4963 
4964 void
4965 icmp_ddi_destroy(void)
4966 {
4967 	nd_free(&icmp_g_nd);
4968 
4969 	rawip_kstat_fini();
4970 }
4971 
4972 static void
4973 rawip_kstat_init(void) {
4974 
4975 	rawip_named_kstat_t template = {
4976 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
4977 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
4978 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
4979 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
4980 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
4981 	};
4982 
4983 	rawip_mibkp = kstat_create("icmp", 0, "rawip", "mib2",
4984 					KSTAT_TYPE_NAMED,
4985 					NUM_OF_FIELDS(rawip_named_kstat_t),
4986 					0);
4987 	if (rawip_mibkp == NULL)
4988 		return;
4989 
4990 	bcopy(&template, rawip_mibkp->ks_data, sizeof (template));
4991 
4992 	rawip_mibkp->ks_update = rawip_kstat_update;
4993 
4994 	kstat_install(rawip_mibkp);
4995 }
4996 
4997 static void
4998 rawip_kstat_fini(void) {
4999 	if (rawip_mibkp) {
5000 		kstat_delete(rawip_mibkp);
5001 		rawip_mibkp = NULL;
5002 	}
5003 }
5004 
5005 static int
5006 rawip_kstat_update(kstat_t *kp, int rw) {
5007 	rawip_named_kstat_t *rawipkp;
5008 
5009 	if ((kp == NULL) || (kp->ks_data == NULL))
5010 		return (EIO);
5011 
5012 	if (rw == KSTAT_WRITE)
5013 		return (EACCES);
5014 
5015 	rawipkp = (rawip_named_kstat_t *)kp->ks_data;
5016 
5017 	rawipkp->inDatagrams.value.ui32 =	rawip_mib.rawipInDatagrams;
5018 	rawipkp->inCksumErrs.value.ui32 =	rawip_mib.rawipInCksumErrs;
5019 	rawipkp->inErrors.value.ui32 =		rawip_mib.rawipInErrors;
5020 	rawipkp->outDatagrams.value.ui32 =	rawip_mib.rawipOutDatagrams;
5021 	rawipkp->outErrors.value.ui32 =		rawip_mib.rawipOutErrors;
5022 
5023 	return (0);
5024 }
5025