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