xref: /onnv-gate/usr/src/uts/common/inet/ip/icmp.c (revision 5315:cb50e341f1b1)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51289Sja97890  * Common Development and Distribution License (the "License").
61289Sja97890  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223388Skcpoon  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/stropts.h>
330Sstevel@tonic-gate #include <sys/strlog.h>
340Sstevel@tonic-gate #include <sys/strsun.h>
350Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
360Sstevel@tonic-gate #include <sys/tihdr.h>
370Sstevel@tonic-gate #include <sys/timod.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
401676Sjpk #include <sys/strsubr.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/policy.h>
451676Sjpk #include <sys/priv.h>
460Sstevel@tonic-gate #include <sys/zone.h>
471673Sgt145670 #include <sys/time.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <sys/isa_defs.h>
510Sstevel@tonic-gate #include <sys/suntpi.h>
520Sstevel@tonic-gate #include <sys/xti_inet.h>
533448Sdh155122 #include <sys/netstack.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <net/route.h>
560Sstevel@tonic-gate #include <net/if.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <netinet/in.h>
590Sstevel@tonic-gate #include <netinet/ip6.h>
600Sstevel@tonic-gate #include <netinet/icmp6.h>
610Sstevel@tonic-gate #include <inet/common.h>
620Sstevel@tonic-gate #include <inet/ip.h>
630Sstevel@tonic-gate #include <inet/ip6.h>
640Sstevel@tonic-gate #include <inet/mi.h>
650Sstevel@tonic-gate #include <inet/nd.h>
660Sstevel@tonic-gate #include <inet/optcom.h>
670Sstevel@tonic-gate #include <inet/snmpcom.h>
680Sstevel@tonic-gate #include <inet/kstatcom.h>
690Sstevel@tonic-gate #include <inet/rawip_impl.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <netinet/ip_mroute.h>
720Sstevel@tonic-gate #include <inet/tcp.h>
730Sstevel@tonic-gate #include <net/pfkeyv2.h>
740Sstevel@tonic-gate #include <inet/ipsec_info.h>
750Sstevel@tonic-gate #include <inet/ipclassifier.h>
760Sstevel@tonic-gate 
771676Sjpk #include <sys/tsol/label.h>
781676Sjpk #include <sys/tsol/tnet.h>
791676Sjpk 
803318Srshoaib #include <inet/ip_ire.h>
813318Srshoaib #include <inet/ip_if.h>
823318Srshoaib 
833318Srshoaib #include <inet/ip_impl.h>
843318Srshoaib 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * Synchronization notes:
870Sstevel@tonic-gate  *
885240Snordmark  * RAWIP is MT and uses the usual kernel synchronization primitives. There is
895240Snordmark  * locks, which is icmp_rwlock. We also use conn_lock when updating things
905240Snordmark  * which affect the IP classifier lookup.
915240Snordmark  * The lock order is icmp_rwlock -> conn_lock.
925240Snordmark  *
935240Snordmark  * The icmp_rwlock:
945240Snordmark  * This protects most of the other fields in the icmp_t. The exact list of
955240Snordmark  * fields which are protected by each of the above locks is documented in
965240Snordmark  * the icmp_t structure definition.
975240Snordmark  *
985240Snordmark  * Plumbing notes:
995240Snordmark  * ICMP is always a device driver. For compatibility with mibopen() code
1005240Snordmark  * it is possible to I_PUSH "icmp", but that results in pushing a passthrough
1015240Snordmark  * dummy module.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static void	icmp_addr_req(queue_t *q, mblk_t *mp);
1050Sstevel@tonic-gate static void	icmp_bind(queue_t *q, mblk_t *mp);
1060Sstevel@tonic-gate static void	icmp_bind_proto(queue_t *q);
1075240Snordmark static void	icmp_bind_result(conn_t *, mblk_t *);
1085240Snordmark static void	icmp_bind_ack(conn_t *, mblk_t *mp);
1095240Snordmark static void	icmp_bind_error(conn_t *, mblk_t *mp);
1105240Snordmark static int	icmp_build_hdrs(icmp_t *icmp);
1110Sstevel@tonic-gate static void	icmp_capability_req(queue_t *q, mblk_t *mp);
1120Sstevel@tonic-gate static int	icmp_close(queue_t *q);
1130Sstevel@tonic-gate static void	icmp_connect(queue_t *q, mblk_t *mp);
1140Sstevel@tonic-gate static void	icmp_disconnect(queue_t *q, mblk_t *mp);
1150Sstevel@tonic-gate static void	icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
1160Sstevel@tonic-gate 		    int sys_error);
1170Sstevel@tonic-gate static void	icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
1180Sstevel@tonic-gate 		    t_scalar_t t_error, int sys_error);
1190Sstevel@tonic-gate static void	icmp_icmp_error(queue_t *q, mblk_t *mp);
1200Sstevel@tonic-gate static void	icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp);
1210Sstevel@tonic-gate static void	icmp_info_req(queue_t *q, mblk_t *mp);
1225240Snordmark static void	icmp_input(void *, mblk_t *, void *);
1230Sstevel@tonic-gate static mblk_t	*icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim,
1240Sstevel@tonic-gate 		    t_scalar_t addr_length, in_port_t);
1255240Snordmark static int	icmp_open(queue_t *q, dev_t *devp, int flag, int sflag,
1265240Snordmark 		    cred_t *credp, boolean_t isv6);
1275240Snordmark static int	icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag,
1285240Snordmark 		    cred_t *credp);
1295240Snordmark static int	icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag,
1305240Snordmark 		    cred_t *credp);
1315240Snordmark static void	icmp_output(queue_t *q, mblk_t *mp);
1320Sstevel@tonic-gate static int	icmp_unitdata_opt_process(queue_t *q, mblk_t *mp,
1330Sstevel@tonic-gate 		    int *errorp, void *thisdg_attrs);
1340Sstevel@tonic-gate static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
1350Sstevel@tonic-gate int		icmp_opt_set(queue_t *q, uint_t optset_context,
1360Sstevel@tonic-gate 		    int level, int name, uint_t inlen,
1370Sstevel@tonic-gate 		    uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
1380Sstevel@tonic-gate 		    void *thisdg_attrs, cred_t *cr, mblk_t *mblk);
1390Sstevel@tonic-gate int		icmp_opt_get(queue_t *q, int level, int name,
1400Sstevel@tonic-gate 		    uchar_t *ptr);
1410Sstevel@tonic-gate static int	icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
1423448Sdh155122 static boolean_t icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt);
1430Sstevel@tonic-gate static int	icmp_param_set(queue_t *q, mblk_t *mp, char *value,
1440Sstevel@tonic-gate 		    caddr_t cp, cred_t *cr);
1450Sstevel@tonic-gate static int	icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
1460Sstevel@tonic-gate 		    uchar_t *ptr, int len);
1470Sstevel@tonic-gate static int	icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp,
1480Sstevel@tonic-gate 		    cred_t *cr);
1490Sstevel@tonic-gate static void	icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err);
1500Sstevel@tonic-gate static void	icmp_unbind(queue_t *q, mblk_t *mp);
1510Sstevel@tonic-gate static void	icmp_wput(queue_t *q, mblk_t *mp);
1520Sstevel@tonic-gate static void	icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6,
1530Sstevel@tonic-gate 		    t_scalar_t tudr_optlen);
1540Sstevel@tonic-gate static void	icmp_wput_other(queue_t *q, mblk_t *mp);
1550Sstevel@tonic-gate static void	icmp_wput_iocdata(queue_t *q, mblk_t *mp);
1560Sstevel@tonic-gate static void	icmp_wput_restricted(queue_t *q, mblk_t *mp);
1570Sstevel@tonic-gate 
1583448Sdh155122 static void	*rawip_stack_init(netstackid_t stackid, netstack_t *ns);
1593448Sdh155122 static void	rawip_stack_fini(netstackid_t stackid, void *arg);
1603448Sdh155122 
1613448Sdh155122 static void	*rawip_kstat_init(netstackid_t stackid);
1623448Sdh155122 static void	rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp);
1630Sstevel@tonic-gate static int	rawip_kstat_update(kstat_t *kp, int rw);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 
1665240Snordmark static struct module_info icmp_mod_info =  {
1670Sstevel@tonic-gate 	5707, "icmp", 1, INFPSZ, 512, 128
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1705240Snordmark /*
1715240Snordmark  * Entry points for ICMP as a device.
1725240Snordmark  * We have separate open functions for the /dev/icmp and /dev/icmp6 devices.
1735240Snordmark  */
1745240Snordmark static struct qinit icmprinitv4 = {
1755240Snordmark 	NULL, NULL, icmp_openv4, icmp_close, NULL, &icmp_mod_info
1765240Snordmark };
1775240Snordmark 
1785240Snordmark static struct qinit icmprinitv6 = {
1795240Snordmark 	NULL, NULL, icmp_openv6, icmp_close, NULL, &icmp_mod_info
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1825240Snordmark static struct qinit icmpwinit = {
1835277Snordmark 	(pfi_t)icmp_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, &icmp_mod_info
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate 
1865240Snordmark /* For AF_INET aka /dev/icmp */
1875240Snordmark struct streamtab icmpinfov4 = {
1885240Snordmark 	&icmprinitv4, &icmpwinit
1895240Snordmark };
1905240Snordmark 
1915240Snordmark /* For AF_INET6 aka /dev/icmp6 */
1925240Snordmark struct streamtab icmpinfov6 = {
1935240Snordmark 	&icmprinitv6, &icmpwinit
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static sin_t	sin_null;	/* Zero address for quick clears */
1970Sstevel@tonic-gate static sin6_t	sin6_null;	/* Zero address for quick clears */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages */
2000Sstevel@tonic-gate static struct T_info_ack icmp_g_t_info_ack = {
2010Sstevel@tonic-gate 	T_INFO_ACK,
2020Sstevel@tonic-gate 	IP_MAXPACKET,	 /* TSDU_size.  icmp allows maximum size messages. */
2030Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size.  icmp does not support expedited data. */
2040Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. icmp does not support connect data. */
2050Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. icmp does not support disconnect data. */
2060Sstevel@tonic-gate 	0,		/* ADDR_size - filled in later. */
2070Sstevel@tonic-gate 	0,		/* OPT_size - not initialized here */
2080Sstevel@tonic-gate 	IP_MAXPACKET,	/* TIDU_size.  icmp allows maximum size messages. */
2090Sstevel@tonic-gate 	T_CLTS,		/* SERV_type.  icmp supports connection-less. */
2100Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state.  This is set from icmp_state. */
2110Sstevel@tonic-gate 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
2120Sstevel@tonic-gate };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2153448Sdh155122  * Table of ND variables supported by icmp.  These are loaded into is_nd
2163448Sdh155122  * when the stack instance is created.
2170Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate static icmpparam_t	icmp_param_arr[] = {
2200Sstevel@tonic-gate 	/* min	max	value	name */
2210Sstevel@tonic-gate 	{ 0,	128,	32,	"icmp_wroff_extra" },
2220Sstevel@tonic-gate 	{ 1,	255,	255,	"icmp_ipv4_ttl" },
2230Sstevel@tonic-gate 	{ 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS,	"icmp_ipv6_hoplimit"},
2240Sstevel@tonic-gate 	{ 0,	1,	1,	"icmp_bsd_compat" },
2250Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_xmit_hiwat"},
2260Sstevel@tonic-gate 	{ 0,	65536,	1024,	"icmp_xmit_lowat"},
2270Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_recv_hiwat"},
2280Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"icmp_max_buf"},
2290Sstevel@tonic-gate };
2303448Sdh155122 #define	is_wroff_extra			is_param_arr[0].icmp_param_value
2313448Sdh155122 #define	is_ipv4_ttl			is_param_arr[1].icmp_param_value
2323448Sdh155122 #define	is_ipv6_hoplimit		is_param_arr[2].icmp_param_value
2333448Sdh155122 #define	is_bsd_compat			is_param_arr[3].icmp_param_value
2343448Sdh155122 #define	is_xmit_hiwat			is_param_arr[4].icmp_param_value
2353448Sdh155122 #define	is_xmit_lowat			is_param_arr[5].icmp_param_value
2363448Sdh155122 #define	is_recv_hiwat			is_param_arr[6].icmp_param_value
2373448Sdh155122 #define	is_max_buf			is_param_arr[7].icmp_param_value
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
2410Sstevel@tonic-gate  * passed to icmp_wput.
2420Sstevel@tonic-gate  * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP
2430Sstevel@tonic-gate  * protocol type placed in the message following the address. A T_BIND_ACK
2445240Snordmark  * message is returned by ip_bind_v4/v6.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static void
2470Sstevel@tonic-gate icmp_bind(queue_t *q, mblk_t *mp)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	sin_t	*sin;
2500Sstevel@tonic-gate 	sin6_t	*sin6;
2510Sstevel@tonic-gate 	mblk_t	*mp1;
2520Sstevel@tonic-gate 	struct T_bind_req	*tbr;
2530Sstevel@tonic-gate 	icmp_t	*icmp;
2545240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
2555240Snordmark 
2565240Snordmark 	icmp = connp->conn_icmp;
2570Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
2580Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2590Sstevel@tonic-gate 		    "icmp_bind: bad req, len %u",
2600Sstevel@tonic-gate 		    (uint_t)(mp->b_wptr - mp->b_rptr));
2610Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
2620Sstevel@tonic-gate 		return;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
2650Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2660Sstevel@tonic-gate 		    "icmp_bind: bad state, %d", icmp->icmp_state);
2670Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
2680Sstevel@tonic-gate 		return;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * Reallocate the message to make sure we have enough room for an
2720Sstevel@tonic-gate 	 * address and the protocol type.
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1);
2750Sstevel@tonic-gate 	if (!mp1) {
2760Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
2770Sstevel@tonic-gate 		return;
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 	mp = mp1;
2800Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
2810Sstevel@tonic-gate 	switch (tbr->ADDR_length) {
2820Sstevel@tonic-gate 	case 0:			/* Generic request */
2830Sstevel@tonic-gate 		tbr->ADDR_offset = sizeof (struct T_bind_req);
2840Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
2850Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin_t);
2860Sstevel@tonic-gate 			sin = (sin_t *)&tbr[1];
2870Sstevel@tonic-gate 			*sin = sin_null;
2880Sstevel@tonic-gate 			sin->sin_family = AF_INET;
2890Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin[1];
2900Sstevel@tonic-gate 		} else {
2910Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
2920Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin6_t);
2930Sstevel@tonic-gate 			sin6 = (sin6_t *)&tbr[1];
2940Sstevel@tonic-gate 			*sin6 = sin6_null;
2950Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
2960Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin6[1];
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		break;
2990Sstevel@tonic-gate 	case sizeof (sin_t):	/* Complete IP address */
3000Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset,
3010Sstevel@tonic-gate 		    sizeof (sin_t));
3020Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
3030Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
3040Sstevel@tonic-gate 			return;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
3070Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
3080Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3090Sstevel@tonic-gate 			return;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 		break;
3120Sstevel@tonic-gate 	case sizeof (sin6_t):	/* Complete IP address */
3130Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset,
3140Sstevel@tonic-gate 		    sizeof (sin6_t));
3150Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
3160Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
3170Sstevel@tonic-gate 			return;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
3200Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
3210Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3220Sstevel@tonic-gate 			return;
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
3250Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3260Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
3270Sstevel@tonic-gate 			return;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 	default:
3310Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
3320Sstevel@tonic-gate 		    "icmp_bind: bad ADDR_length %d", tbr->ADDR_length);
3330Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
3340Sstevel@tonic-gate 		return;
3350Sstevel@tonic-gate 	}
3365240Snordmark 
3375240Snordmark 	/*
3385240Snordmark 	 * The state must be TS_UNBND. TPI mandates that users must send
3395240Snordmark 	 * TPI primitives only 1 at a time and wait for the response before
3405240Snordmark 	 * sending the next primitive.
3415240Snordmark 	 */
3425240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
3435240Snordmark 	if (icmp->icmp_state != TS_UNBND || icmp->icmp_pending_op != -1) {
3445240Snordmark 		rw_exit(&icmp->icmp_rwlock);
3455240Snordmark 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
3465240Snordmark 		    "icmp_bind: bad state, %d", icmp->icmp_state);
3475240Snordmark 		icmp_err_ack(q, mp, TOUTSTATE, 0);
3485240Snordmark 		return;
3495240Snordmark 	}
3505240Snordmark 
3515240Snordmark 	icmp->icmp_pending_op = tbr->PRIM_type;
3525240Snordmark 
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * Copy the source address into our icmp structure.  This address
3550Sstevel@tonic-gate 	 * may still be zero; if so, ip will fill in the correct address
3560Sstevel@tonic-gate 	 * each time an outbound packet is passed to it.
3575240Snordmark 	 * If we are binding to a broadcast or multicast address then
3585240Snordmark 	 * icmp_bind_ack will clear the source address when it receives
3595240Snordmark 	 * the T_BIND_ACK.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
3640Sstevel@tonic-gate 		ASSERT(sin != NULL);
3650Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3660Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr,
3670Sstevel@tonic-gate 		    &icmp->icmp_v6src);
3680Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
3690Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
3700Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3710Sstevel@tonic-gate 	} else {
3720Sstevel@tonic-gate 		int error;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		ASSERT(sin6 != NULL);
3750Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
3760Sstevel@tonic-gate 		icmp->icmp_v6src = sin6->sin6_addr;
3770Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
3780Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		/* Rebuild the header template */
3815240Snordmark 		error = icmp_build_hdrs(icmp);
3820Sstevel@tonic-gate 		if (error != 0) {
3835240Snordmark 			icmp->icmp_pending_op = -1;
3845240Snordmark 			rw_exit(&icmp->icmp_rwlock);
3850Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
3860Sstevel@tonic-gate 			return;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	/*
3900Sstevel@tonic-gate 	 * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following
3910Sstevel@tonic-gate 	 * the address.
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
3940Sstevel@tonic-gate 	if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) {
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * Append a request for an IRE if src not 0 (INADDR_ANY)
3970Sstevel@tonic-gate 		 */
3980Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
3990Sstevel@tonic-gate 		if (!mp->b_cont) {
4005240Snordmark 			icmp->icmp_pending_op = -1;
4015240Snordmark 			rw_exit(&icmp->icmp_rwlock);
4020Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOMEM);
4030Sstevel@tonic-gate 			return;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
4060Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
4070Sstevel@tonic-gate 	}
4085240Snordmark 	rw_exit(&icmp->icmp_rwlock);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */
4115240Snordmark 	if (icmp->icmp_family == AF_INET6)
4125240Snordmark 		mp = ip_bind_v6(q, mp, connp, NULL);
4135240Snordmark 	else
4145240Snordmark 		mp = ip_bind_v4(q, mp, connp);
4155240Snordmark 
4165240Snordmark 	/* The above return NULL if the bind needs to be deferred */
4175240Snordmark 	if (mp != NULL)
4185240Snordmark 		icmp_bind_result(connp, mp);
4195240Snordmark 	else
4205240Snordmark 		CONN_INC_REF(connp);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate  * Send message to IP to just bind to the protocol.
4250Sstevel@tonic-gate  */
4260Sstevel@tonic-gate static void
4270Sstevel@tonic-gate icmp_bind_proto(queue_t *q)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	mblk_t	*mp;
4300Sstevel@tonic-gate 	struct T_bind_req	*tbr;
4310Sstevel@tonic-gate 	icmp_t	*icmp;
4325240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
4335240Snordmark 
4345240Snordmark 	icmp = connp->conn_icmp;
4355240Snordmark 
4360Sstevel@tonic-gate 	mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1,
4370Sstevel@tonic-gate 	    BPRI_MED);
4380Sstevel@tonic-gate 	if (!mp) {
4390Sstevel@tonic-gate 		return;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
4420Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
4430Sstevel@tonic-gate 	tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */
4440Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (struct T_bind_req);
4455240Snordmark 
4465240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
4470Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
4480Sstevel@tonic-gate 		sin_t	*sin;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin_t);
4510Sstevel@tonic-gate 		sin = (sin_t *)&tbr[1];
4520Sstevel@tonic-gate 		*sin = sin_null;
4530Sstevel@tonic-gate 		sin->sin_family = AF_INET;
4540Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin[1];
4550Sstevel@tonic-gate 	} else {
4560Sstevel@tonic-gate 		sin6_t	*sin6;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
4590Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin6_t);
4600Sstevel@tonic-gate 		sin6 = (sin6_t *)&tbr[1];
4610Sstevel@tonic-gate 		*sin6 = sin6_null;
4620Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
4630Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin6[1];
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* Place protocol type in the O_T_BIND_REQ following the address. */
4670Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
4685240Snordmark 	rw_exit(&icmp->icmp_rwlock);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ to ip. */
4715240Snordmark 	if (icmp->icmp_family == AF_INET6)
4725240Snordmark 		mp = ip_bind_v6(q, mp, connp, NULL);
4735240Snordmark 	else
4745240Snordmark 		mp = ip_bind_v4(q, mp, connp);
4755240Snordmark 
4765240Snordmark 	/* The above return NULL if the bind needs to be deferred */
4775240Snordmark 	if (mp != NULL)
4785240Snordmark 		icmp_bind_result(connp, mp);
4795240Snordmark 	else
4805240Snordmark 		CONN_INC_REF(connp);
4815240Snordmark }
4825240Snordmark 
4835240Snordmark /*
4845240Snordmark  * This is called from ip_wput_nondata to handle the results of a
4855240Snordmark  * deferred RAWIP bind.  It is called once the bind has been completed.
4865240Snordmark  */
4875240Snordmark void
4885240Snordmark rawip_resume_bind(conn_t *connp, mblk_t *mp)
4895240Snordmark {
4905240Snordmark 	ASSERT(connp != NULL && IPCL_IS_RAWIP(connp));
4915240Snordmark 
4925240Snordmark 	icmp_bind_result(connp, mp);
4935240Snordmark 
4945240Snordmark 	CONN_OPER_PENDING_DONE(connp);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * This routine handles each T_CONN_REQ message passed to icmp.  It
4990Sstevel@tonic-gate  * associates a default destination address with the stream.
5000Sstevel@tonic-gate  *
5010Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
5020Sstevel@tonic-gate  *	T_BIND_REQ	- specifying local and remote address.
5030Sstevel@tonic-gate  *	IRE_DB_REQ_TYPE	- to get an IRE back containing ire_type and src
5040Sstevel@tonic-gate  *	T_OK_ACK	- for the T_CONN_REQ
5050Sstevel@tonic-gate  *	T_CONN_CON	- to keep the TPI user happy
5060Sstevel@tonic-gate  *
5075240Snordmark  * The connect completes in icmp_bind_result.
5080Sstevel@tonic-gate  * When a T_BIND_ACK is received information is extracted from the IRE
5090Sstevel@tonic-gate  * and the two appended messages are sent to the TPI user.
5105240Snordmark  * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will
5115240Snordmark  * convert it to an error ack for the appropriate primitive.
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate static void
5140Sstevel@tonic-gate icmp_connect(queue_t *q, mblk_t *mp)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	sin_t	*sin;
5170Sstevel@tonic-gate 	sin6_t	*sin6;
5180Sstevel@tonic-gate 	mblk_t	*mp1, *mp2;
5190Sstevel@tonic-gate 	struct T_conn_req	*tcr;
5200Sstevel@tonic-gate 	icmp_t	*icmp;
5210Sstevel@tonic-gate 	ipaddr_t	v4dst;
5220Sstevel@tonic-gate 	in6_addr_t	v6dst;
5230Sstevel@tonic-gate 	uint32_t	flowinfo;
5245240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
5255240Snordmark 
5265240Snordmark 	icmp = connp->conn_icmp;
5270Sstevel@tonic-gate 	tcr = (struct T_conn_req *)mp->b_rptr;
5280Sstevel@tonic-gate 	/* Sanity checks */
5295240Snordmark 	if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) {
5300Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
5310Sstevel@tonic-gate 		return;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (tcr->OPT_length != 0) {
5350Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADOPT, 0);
5360Sstevel@tonic-gate 		return;
5370Sstevel@tonic-gate 	}
5385240Snordmark 
5390Sstevel@tonic-gate 	switch (tcr->DEST_length) {
5400Sstevel@tonic-gate 	default:
5410Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
5420Sstevel@tonic-gate 		return;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	case sizeof (sin_t):
5450Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset,
5460Sstevel@tonic-gate 		    sizeof (sin_t));
5470Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
5480Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
5490Sstevel@tonic-gate 			return;
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
5520Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
5530Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
5540Sstevel@tonic-gate 			return;
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
5570Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
5580Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
5590Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
5600Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
5610Sstevel@tonic-gate 		break;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	case sizeof (sin6_t):
5640Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset,
5650Sstevel@tonic-gate 		    sizeof (sin6_t));
5660Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
5670Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
5680Sstevel@tonic-gate 			return;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
5710Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
5720Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
5730Sstevel@tonic-gate 			return;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
5760Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5770Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
5780Sstevel@tonic-gate 			return;
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 		v6dst = sin6->sin6_addr;
5810Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
5820Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
5830Sstevel@tonic-gate 		flowinfo = sin6->sin6_flowinfo;
5840Sstevel@tonic-gate 		break;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
5870Sstevel@tonic-gate 		/*
5880Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
5890Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
5900Sstevel@tonic-gate 		 * generate the T_CONN_CON.
5910Sstevel@tonic-gate 		 */
5920Sstevel@tonic-gate 		if (v4dst == INADDR_ANY) {
5930Sstevel@tonic-gate 			v4dst = htonl(INADDR_LOOPBACK);
5940Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
5950Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET) {
5960Sstevel@tonic-gate 				sin->sin_addr.s_addr = v4dst;
5970Sstevel@tonic-gate 			} else {
5980Sstevel@tonic-gate 				sin6->sin6_addr = v6dst;
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
6020Sstevel@tonic-gate 		icmp->icmp_flowinfo = 0;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		/*
6050Sstevel@tonic-gate 		 * If the destination address is multicast and
6060Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
6070Sstevel@tonic-gate 		 * use the address of that interface as our
6080Sstevel@tonic-gate 		 * source address if no source address has been set.
6090Sstevel@tonic-gate 		 */
6100Sstevel@tonic-gate 		if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY &&
6110Sstevel@tonic-gate 		    CLASSD(v4dst) &&
6120Sstevel@tonic-gate 		    icmp->icmp_multicast_if_addr != INADDR_ANY) {
6130Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr,
6140Sstevel@tonic-gate 			    &icmp->icmp_v6src);
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 	} else {
6170Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
6180Sstevel@tonic-gate 		/*
6190Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
6200Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
6210Sstevel@tonic-gate 		 * generate the T_CONN_CON.
6220Sstevel@tonic-gate 		 */
6230Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
6240Sstevel@tonic-gate 			v6dst = ipv6_loopback;
6250Sstevel@tonic-gate 			sin6->sin6_addr = v6dst;
6260Sstevel@tonic-gate 		}
6270Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
6280Sstevel@tonic-gate 		icmp->icmp_flowinfo = flowinfo;
6290Sstevel@tonic-gate 		/*
6300Sstevel@tonic-gate 		 * If the destination address is multicast and
6310Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
6320Sstevel@tonic-gate 		 * then the ip bind logic will pick the correct source
6330Sstevel@tonic-gate 		 * address (i.e. matching the outgoing multicast interface).
6340Sstevel@tonic-gate 		 */
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6375240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
6385240Snordmark 	if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) {
6395240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6405240Snordmark 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
6415240Snordmark 		    "icmp_connect: bad state, %d", icmp->icmp_state);
6425240Snordmark 		icmp_err_ack(q, mp, TOUTSTATE, 0);
6435240Snordmark 		return;
6445240Snordmark 	}
6455240Snordmark 	icmp->icmp_pending_op = T_CONN_REQ;
6465240Snordmark 
6475240Snordmark 	if (icmp->icmp_state == TS_DATA_XFER) {
6485240Snordmark 		/* Already connected - clear out state */
6495240Snordmark 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
6505240Snordmark 		icmp->icmp_state = TS_IDLE;
6515240Snordmark 	}
6525240Snordmark 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * Send down bind to IP to verify that there is a route
6550Sstevel@tonic-gate 	 * and to determine the source address.
6560Sstevel@tonic-gate 	 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
6590Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t),
6600Sstevel@tonic-gate 		    sin->sin_port);
6610Sstevel@tonic-gate 	} else {
6620Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
6630Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t),
6640Sstevel@tonic-gate 		    sin6->sin6_port);
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 	if (mp1 == NULL) {
6675240Snordmark 		icmp->icmp_pending_op = -1;
6685240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6690Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
6700Sstevel@tonic-gate 		return;
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * We also have to send a connection confirmation to
6755240Snordmark 	 * keep TLI happy. Prepare it for icmp_bind_result.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
6780Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL,
6790Sstevel@tonic-gate 		    0);
6800Sstevel@tonic-gate 	} else {
6810Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
6820Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL,
6830Sstevel@tonic-gate 		    0);
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 	if (mp2 == NULL) {
6860Sstevel@tonic-gate 		freemsg(mp1);
6875240Snordmark 		icmp->icmp_pending_op = -1;
6885240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6890Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
6900Sstevel@tonic-gate 		return;
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
6940Sstevel@tonic-gate 	if (mp == NULL) {
6950Sstevel@tonic-gate 		/* Unable to reuse the T_CONN_REQ for the ack. */
6960Sstevel@tonic-gate 		freemsg(mp2);
6975240Snordmark 		icmp->icmp_pending_op = -1;
6985240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6990Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
7000Sstevel@tonic-gate 		return;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	icmp->icmp_state = TS_DATA_XFER;
7045240Snordmark 	rw_exit(&icmp->icmp_rwlock);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/* Hang onto the T_OK_ACK and T_CONN_CON for later. */
7070Sstevel@tonic-gate 	linkb(mp1, mp);
7080Sstevel@tonic-gate 	linkb(mp1, mp2);
7090Sstevel@tonic-gate 
7105240Snordmark 	mblk_setcred(mp1, connp->conn_cred);
7115240Snordmark 	if (icmp->icmp_family == AF_INET)
7125240Snordmark 		mp1 = ip_bind_v4(q, mp1, connp);
7135240Snordmark 	else
7145240Snordmark 		mp1 = ip_bind_v6(q, mp1, connp, NULL);
7155240Snordmark 
7165240Snordmark 	/* The above return NULL if the bind needs to be deferred */
7175240Snordmark 	if (mp1 != NULL)
7185240Snordmark 		icmp_bind_result(connp, mp1);
7195240Snordmark 	else
7205240Snordmark 		CONN_INC_REF(connp);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7235240Snordmark static void
7245240Snordmark icmp_close_free(conn_t *connp)
7250Sstevel@tonic-gate {
7265240Snordmark 	icmp_t *icmp = connp->conn_icmp;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/* If there are any options associated with the stream, free them. */
729*5315Snordmark 	if (icmp->icmp_ip_snd_options != NULL) {
7300Sstevel@tonic-gate 		mi_free((char *)icmp->icmp_ip_snd_options);
731*5315Snordmark 		icmp->icmp_ip_snd_options = NULL;
732*5315Snordmark 	}
733*5315Snordmark 
734*5315Snordmark 	if (icmp->icmp_filter != NULL) {
7350Sstevel@tonic-gate 		kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
736*5315Snordmark 		icmp->icmp_filter = NULL;
737*5315Snordmark 	}
7380Sstevel@tonic-gate 	/* Free memory associated with sticky options */
7390Sstevel@tonic-gate 	if (icmp->icmp_sticky_hdrs_len != 0) {
7400Sstevel@tonic-gate 		kmem_free(icmp->icmp_sticky_hdrs,
7410Sstevel@tonic-gate 		    icmp->icmp_sticky_hdrs_len);
7420Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = NULL;
7430Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = 0;
7440Sstevel@tonic-gate 	}
7451676Sjpk 	ip6_pkt_free(&icmp->icmp_sticky_ipp);
7465240Snordmark }
7475240Snordmark 
7485240Snordmark static int
7495240Snordmark icmp_close(queue_t *q)
7505240Snordmark {
7515240Snordmark 	conn_t	*connp = (conn_t *)q->q_ptr;
7525240Snordmark 
7535240Snordmark 	ASSERT(connp != NULL && IPCL_IS_RAWIP(connp));
7545240Snordmark 
7555240Snordmark 	ip_quiesce_conn(connp);
7565240Snordmark 
7575240Snordmark 	qprocsoff(connp->conn_rq);
7585240Snordmark 
7595240Snordmark 	icmp_close_free(connp);
7605240Snordmark 
7615240Snordmark 	/*
7625240Snordmark 	 * Now we are truly single threaded on this stream, and can
7635240Snordmark 	 * delete the things hanging off the connp, and finally the connp.
7645240Snordmark 	 * We removed this connp from the fanout list, it cannot be
7655240Snordmark 	 * accessed thru the fanouts, and we already waited for the
7665240Snordmark 	 * conn_ref to drop to 0. We are already in close, so
7675240Snordmark 	 * there cannot be any other thread from the top. qprocsoff
7685240Snordmark 	 * has completed, and service has completed or won't run in
7695240Snordmark 	 * future.
7705240Snordmark 	 */
7715240Snordmark 	ASSERT(connp->conn_ref == 1);
7725240Snordmark 
7735240Snordmark 	inet_minor_free(ip_minor_arena, connp->conn_dev);
7745240Snordmark 
7755240Snordmark 	connp->conn_ref--;
7765240Snordmark 	ipcl_conn_destroy(connp);
7775240Snordmark 
7785240Snordmark 	q->q_ptr = WR(q)->q_ptr = NULL;
7795240Snordmark 	return (0);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate  * This routine handles each T_DISCON_REQ message passed to icmp
7840Sstevel@tonic-gate  * as an indicating that ICMP is no longer connected. This results
7850Sstevel@tonic-gate  * in sending a T_BIND_REQ to IP to restore the binding to just
7860Sstevel@tonic-gate  * the local address.
7870Sstevel@tonic-gate  *
7880Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
7890Sstevel@tonic-gate  *	T_BIND_REQ	- specifying just the local address.
7900Sstevel@tonic-gate  *	T_OK_ACK	- for the T_DISCON_REQ
7910Sstevel@tonic-gate  *
7925240Snordmark  * The disconnect completes in icmp_bind_result.
7930Sstevel@tonic-gate  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
7945240Snordmark  * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will
7955240Snordmark  * convert it to an error ack for the appropriate primitive.
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate static void
7980Sstevel@tonic-gate icmp_disconnect(queue_t *q, mblk_t *mp)
7990Sstevel@tonic-gate {
8000Sstevel@tonic-gate 	icmp_t	*icmp;
8010Sstevel@tonic-gate 	mblk_t	*mp1;
8025240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
8035240Snordmark 
8045240Snordmark 	icmp = connp->conn_icmp;
8055240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
8065240Snordmark 	if (icmp->icmp_state != TS_DATA_XFER || icmp->icmp_pending_op != -1) {
8075240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8080Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
8090Sstevel@tonic-gate 		    "icmp_disconnect: bad state, %d", icmp->icmp_state);
8100Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
8110Sstevel@tonic-gate 		return;
8120Sstevel@tonic-gate 	}
8135240Snordmark 	icmp->icmp_pending_op = T_DISCON_REQ;
8140Sstevel@tonic-gate 	icmp->icmp_v6src = icmp->icmp_bound_v6src;
8150Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * Send down bind to IP to remove the full binding and revert
8190Sstevel@tonic-gate 	 * to the local address binding.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
8220Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0);
8230Sstevel@tonic-gate 	} else {
8240Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
8250Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0);
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 	if (mp1 == NULL) {
8285240Snordmark 		icmp->icmp_pending_op = -1;
8295240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8300Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
8310Sstevel@tonic-gate 		return;
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
8340Sstevel@tonic-gate 	if (mp == NULL) {
8350Sstevel@tonic-gate 		/* Unable to reuse the T_DISCON_REQ for the ack. */
8365240Snordmark 		icmp->icmp_pending_op = -1;
8375240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8380Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
8390Sstevel@tonic-gate 		return;
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
8430Sstevel@tonic-gate 		int error;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		/* Rebuild the header template */
8465240Snordmark 		error = icmp_build_hdrs(icmp);
8470Sstevel@tonic-gate 		if (error != 0) {
8485240Snordmark 			icmp->icmp_pending_op = -1;
8495240Snordmark 			rw_exit(&icmp->icmp_rwlock);
8500Sstevel@tonic-gate 			icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
8510Sstevel@tonic-gate 			freemsg(mp1);
8520Sstevel@tonic-gate 			return;
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 	}
8555240Snordmark 
8565240Snordmark 	rw_exit(&icmp->icmp_rwlock);
8575240Snordmark 	/* Append the T_OK_ACK to the T_BIND_REQ for icmp_bind_result */
8580Sstevel@tonic-gate 	linkb(mp1, mp);
8595240Snordmark 
8605240Snordmark 	if (icmp->icmp_family == AF_INET6)
8615240Snordmark 		mp1 = ip_bind_v6(q, mp1, connp, NULL);
8625240Snordmark 	else
8635240Snordmark 		mp1 = ip_bind_v4(q, mp1, connp);
8645240Snordmark 
8655240Snordmark 	/* The above return NULL if the bind needs to be deferred */
8665240Snordmark 	if (mp1 != NULL)
8675240Snordmark 		icmp_bind_result(connp, mp1);
8685240Snordmark 	else
8695240Snordmark 		CONN_INC_REF(connp);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate /* This routine creates a T_ERROR_ACK message and passes it upstream. */
8730Sstevel@tonic-gate static void
8740Sstevel@tonic-gate icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
8770Sstevel@tonic-gate 		qreply(q, mp);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /* Shorthand to generate and send TPI error acks to our client */
8810Sstevel@tonic-gate static void
8820Sstevel@tonic-gate icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
8830Sstevel@tonic-gate     t_scalar_t t_error, int sys_error)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	struct T_error_ack	*teackp;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
8880Sstevel@tonic-gate 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
8890Sstevel@tonic-gate 		teackp = (struct T_error_ack *)mp->b_rptr;
8900Sstevel@tonic-gate 		teackp->ERROR_prim = primitive;
8910Sstevel@tonic-gate 		teackp->TLI_error = t_error;
8920Sstevel@tonic-gate 		teackp->UNIX_error = sys_error;
8930Sstevel@tonic-gate 		qreply(q, mp);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8985240Snordmark  * icmp_icmp_error is called by icmp_input to process ICMP
8990Sstevel@tonic-gate  * messages passed up by IP.
9000Sstevel@tonic-gate  * Generates the appropriate T_UDERROR_IND for permanent
9010Sstevel@tonic-gate  * (non-transient) errors.
9020Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
9030Sstevel@tonic-gate  * the ICMP header.
9040Sstevel@tonic-gate  */
9050Sstevel@tonic-gate static void
9060Sstevel@tonic-gate icmp_icmp_error(queue_t *q, mblk_t *mp)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	icmph_t *icmph;
9090Sstevel@tonic-gate 	ipha_t	*ipha;
9100Sstevel@tonic-gate 	int	iph_hdr_length;
9110Sstevel@tonic-gate 	sin_t	sin;
9120Sstevel@tonic-gate 	sin6_t	sin6;
9130Sstevel@tonic-gate 	mblk_t	*mp1;
9140Sstevel@tonic-gate 	int	error = 0;
9155240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
9180Sstevel@tonic-gate 
9195240Snordmark 	ASSERT(OK_32PTR(mp->b_rptr));
9205240Snordmark 
9210Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
9220Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
9230Sstevel@tonic-gate 		icmp_icmp_error_ipv6(q, mp);
9240Sstevel@tonic-gate 		return;
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 	ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
9270Sstevel@tonic-gate 
9285240Snordmark 	/* Skip past the outer IP and ICMP headers */
9290Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
9300Sstevel@tonic-gate 	icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]);
9310Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
9320Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	switch (icmph->icmph_type) {
9350Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
9360Sstevel@tonic-gate 		switch (icmph->icmph_code) {
9370Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
9380Sstevel@tonic-gate 			/*
9390Sstevel@tonic-gate 			 * IP has already adjusted the path MTU.
9400Sstevel@tonic-gate 			 */
9410Sstevel@tonic-gate 			break;
9420Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
9430Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
9440Sstevel@tonic-gate 			error = ECONNREFUSED;
9450Sstevel@tonic-gate 			break;
9460Sstevel@tonic-gate 		default:
9470Sstevel@tonic-gate 			/* Transient errors */
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 		}
9500Sstevel@tonic-gate 		break;
9510Sstevel@tonic-gate 	default:
9520Sstevel@tonic-gate 		/* Transient errors */
9530Sstevel@tonic-gate 		break;
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 	if (error == 0) {
9560Sstevel@tonic-gate 		freemsg(mp);
9570Sstevel@tonic-gate 		return;
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 
9605240Snordmark 	/*
9615240Snordmark 	 * Deliver T_UDERROR_IND when the application has asked for it.
9625240Snordmark 	 * The socket layer enables this automatically when connected.
9635240Snordmark 	 */
9645240Snordmark 	if (!icmp->icmp_dgram_errind) {
9655240Snordmark 		freemsg(mp);
9665240Snordmark 		return;
9675240Snordmark 	}
9685240Snordmark 
9690Sstevel@tonic-gate 	switch (icmp->icmp_family) {
9700Sstevel@tonic-gate 	case AF_INET:
9710Sstevel@tonic-gate 		sin = sin_null;
9720Sstevel@tonic-gate 		sin.sin_family = AF_INET;
9730Sstevel@tonic-gate 		sin.sin_addr.s_addr = ipha->ipha_dst;
9740Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
9750Sstevel@tonic-gate 		    error);
9760Sstevel@tonic-gate 		break;
9770Sstevel@tonic-gate 	case AF_INET6:
9780Sstevel@tonic-gate 		sin6 = sin6_null;
9790Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
9800Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
9830Sstevel@tonic-gate 		    NULL, 0, error);
9840Sstevel@tonic-gate 		break;
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 	if (mp1)
9870Sstevel@tonic-gate 		putnext(q, mp1);
9880Sstevel@tonic-gate 	freemsg(mp);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6
9930Sstevel@tonic-gate  * for IPv6 packets.
9940Sstevel@tonic-gate  * Send permanent (non-transient) errors upstream.
9950Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
9960Sstevel@tonic-gate  * as the ICMPv6 header.
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate static void
9990Sstevel@tonic-gate icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	icmp6_t		*icmp6;
10020Sstevel@tonic-gate 	ip6_t		*ip6h, *outer_ip6h;
10030Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
10040Sstevel@tonic-gate 	uint8_t		*nexthdrp;
10050Sstevel@tonic-gate 	sin6_t		sin6;
10060Sstevel@tonic-gate 	mblk_t		*mp1;
10070Sstevel@tonic-gate 	int		error = 0;
10085240Snordmark 	icmp_t		*icmp = Q_TO_ICMP(q);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	outer_ip6h = (ip6_t *)mp->b_rptr;
10110Sstevel@tonic-gate 	if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
10120Sstevel@tonic-gate 		iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
10130Sstevel@tonic-gate 	else
10140Sstevel@tonic-gate 		iph_hdr_length = IPV6_HDR_LEN;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
10170Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
10180Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
10190Sstevel@tonic-gate 		freemsg(mp);
10200Sstevel@tonic-gate 		return;
10210Sstevel@tonic-gate 	}
10225240Snordmark 
10230Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
10240Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
10250Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
10260Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
10270Sstevel@tonic-gate 			error = ECONNREFUSED;
10280Sstevel@tonic-gate 			break;
10290Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
10300Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
10310Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
10320Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
10330Sstevel@tonic-gate 			/* Transient errors */
10340Sstevel@tonic-gate 			break;
10350Sstevel@tonic-gate 		default:
10360Sstevel@tonic-gate 			break;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 		break;
10390Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG: {
10400Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi;
10410Sstevel@tonic-gate 		struct T_opthdr		*toh;
10420Sstevel@tonic-gate 		size_t			udi_size;
10430Sstevel@tonic-gate 		mblk_t			*newmp;
10440Sstevel@tonic-gate 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
10450Sstevel@tonic-gate 		    sizeof (struct ip6_mtuinfo);
10460Sstevel@tonic-gate 		sin6_t			*sin6;
10470Sstevel@tonic-gate 		struct ip6_mtuinfo	*mtuinfo;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		/*
10500Sstevel@tonic-gate 		 * If the application has requested to receive path mtu
10510Sstevel@tonic-gate 		 * information, send up an empty message containing an
10520Sstevel@tonic-gate 		 * IPV6_PATHMTU ancillary data item.
10530Sstevel@tonic-gate 		 */
10540Sstevel@tonic-gate 		if (!icmp->icmp_ipv6_recvpathmtu)
10550Sstevel@tonic-gate 			break;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
10580Sstevel@tonic-gate 		    opt_length;
10590Sstevel@tonic-gate 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
10605240Snordmark 			BUMP_MIB(&icmp->icmp_is->is_rawip_mib, rawipInErrors);
10610Sstevel@tonic-gate 			break;
10620Sstevel@tonic-gate 		}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 		/*
10650Sstevel@tonic-gate 		 * newmp->b_cont is left to NULL on purpose.  This is an
10660Sstevel@tonic-gate 		 * empty message containing only ancillary data.
10670Sstevel@tonic-gate 		 */
10680Sstevel@tonic-gate 		newmp->b_datap->db_type = M_PROTO;
10690Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
10700Sstevel@tonic-gate 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
10710Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
10720Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin6_t);
10730Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
10740Sstevel@tonic-gate 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
10750Sstevel@tonic-gate 		tudi->OPT_length = opt_length;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		sin6 = (sin6_t *)&tudi[1];
10780Sstevel@tonic-gate 		bzero(sin6, sizeof (sin6_t));
10790Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
10800Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_v6dst;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 		toh = (struct T_opthdr *)&sin6[1];
10830Sstevel@tonic-gate 		toh->level = IPPROTO_IPV6;
10840Sstevel@tonic-gate 		toh->name = IPV6_PATHMTU;
10850Sstevel@tonic-gate 		toh->len = opt_length;
10860Sstevel@tonic-gate 		toh->status = 0;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
10890Sstevel@tonic-gate 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
10900Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
10910Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
10920Sstevel@tonic-gate 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
10930Sstevel@tonic-gate 		/*
10940Sstevel@tonic-gate 		 * We've consumed everything we need from the original
10950Sstevel@tonic-gate 		 * message.  Free it, then send our empty message.
10960Sstevel@tonic-gate 		 */
10970Sstevel@tonic-gate 		freemsg(mp);
10980Sstevel@tonic-gate 		putnext(q, newmp);
10990Sstevel@tonic-gate 		return;
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
11020Sstevel@tonic-gate 		/* Transient errors */
11030Sstevel@tonic-gate 		break;
11040Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
11050Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
11060Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
11070Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
11080Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
11090Sstevel@tonic-gate 			error = ECONNREFUSED;
11100Sstevel@tonic-gate 			break;
11110Sstevel@tonic-gate 		}
11120Sstevel@tonic-gate 		break;
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 	if (error == 0) {
11150Sstevel@tonic-gate 		freemsg(mp);
11160Sstevel@tonic-gate 		return;
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11195240Snordmark 	/*
11205240Snordmark 	 * Deliver T_UDERROR_IND when the application has asked for it.
11215240Snordmark 	 * The socket layer enables this automatically when connected.
11225240Snordmark 	 */
11235240Snordmark 	if (!icmp->icmp_dgram_errind) {
11245240Snordmark 		freemsg(mp);
11255240Snordmark 		return;
11265240Snordmark 	}
11275240Snordmark 
11280Sstevel@tonic-gate 	sin6 = sin6_null;
11290Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
11300Sstevel@tonic-gate 	sin6.sin6_addr = ip6h->ip6_dst;
11310Sstevel@tonic-gate 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
11340Sstevel@tonic-gate 	    error);
11350Sstevel@tonic-gate 	if (mp1)
11360Sstevel@tonic-gate 		putnext(q, mp1);
11370Sstevel@tonic-gate 	freemsg(mp);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate  * This routine responds to T_ADDR_REQ messages.  It is called by icmp_wput.
11420Sstevel@tonic-gate  * The local address is filled in if endpoint is bound. The remote address
11430Sstevel@tonic-gate  * is filled in if remote address has been precified ("connected endpoint")
11440Sstevel@tonic-gate  * (The concept of connected CLTS sockets is alien to published TPI
11450Sstevel@tonic-gate  *  but we support it anyway).
11460Sstevel@tonic-gate  */
11470Sstevel@tonic-gate static void
11480Sstevel@tonic-gate icmp_addr_req(queue_t *q, mblk_t *mp)
11490Sstevel@tonic-gate {
11505240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
11510Sstevel@tonic-gate 	mblk_t	*ackmp;
11520Sstevel@tonic-gate 	struct T_addr_ack *taa;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/* Make it large enough for worst case */
11550Sstevel@tonic-gate 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
11560Sstevel@tonic-gate 	    2 * sizeof (sin6_t), 1);
11570Sstevel@tonic-gate 	if (ackmp == NULL) {
11580Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
11590Sstevel@tonic-gate 		return;
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 	taa = (struct T_addr_ack *)ackmp->b_rptr;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	bzero(taa, sizeof (struct T_addr_ack));
11640Sstevel@tonic-gate 	ackmp->b_wptr = (uchar_t *)&taa[1];
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	taa->PRIM_type = T_ADDR_ACK;
11670Sstevel@tonic-gate 	ackmp->b_datap->db_type = M_PCPROTO;
11685240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_READER);
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * Note: Following code assumes 32 bit alignment of basic
11710Sstevel@tonic-gate 	 * data structures like sin_t and struct T_addr_ack.
11720Sstevel@tonic-gate 	 */
11730Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
11740Sstevel@tonic-gate 		/*
11750Sstevel@tonic-gate 		 * Fill in local address
11760Sstevel@tonic-gate 		 */
11770Sstevel@tonic-gate 		taa->LOCADDR_offset = sizeof (*taa);
11780Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
11790Sstevel@tonic-gate 			sin_t	*sin;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin_t);
11820Sstevel@tonic-gate 			sin = (sin_t *)&taa[1];
11830Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
11840Sstevel@tonic-gate 			*sin = sin_null;
11850Sstevel@tonic-gate 			sin->sin_family = AF_INET;
11860Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
11870Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
11880Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src,
11890Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
11900Sstevel@tonic-gate 			} else {
11910Sstevel@tonic-gate 				/*
11920Sstevel@tonic-gate 				 * INADDR_ANY
11930Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
11940Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
11950Sstevel@tonic-gate 				 * local address instead (that could
11960Sstevel@tonic-gate 				 * also still be INADDR_ANY)
11970Sstevel@tonic-gate 				 */
11980Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src,
11990Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
12000Sstevel@tonic-gate 			}
12010Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin[1];
12020Sstevel@tonic-gate 		} else {
12030Sstevel@tonic-gate 			sin6_t	*sin6;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
12060Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin6_t);
12070Sstevel@tonic-gate 			sin6 = (sin6_t *)&taa[1];
12080Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
12090Sstevel@tonic-gate 			*sin6 = sin6_null;
12100Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
12110Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
12120Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_v6src;
12130Sstevel@tonic-gate 			} else {
12140Sstevel@tonic-gate 				/*
12150Sstevel@tonic-gate 				 * UNSPECIFIED
12160Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
12170Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
12180Sstevel@tonic-gate 				 * local address instead (that could
12190Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
12200Sstevel@tonic-gate 				 */
12210Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_bound_v6src;
12220Sstevel@tonic-gate 			}
12230Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin6[1];
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 	}
12265240Snordmark 	rw_exit(&icmp->icmp_rwlock);
12270Sstevel@tonic-gate 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
12280Sstevel@tonic-gate 	qreply(q, ackmp);
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate static void
12320Sstevel@tonic-gate icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
12330Sstevel@tonic-gate {
12340Sstevel@tonic-gate 	*tap = icmp_g_t_info_ack;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6)
12370Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin6_t);
12380Sstevel@tonic-gate 	else
12390Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin_t);
12400Sstevel@tonic-gate 	tap->CURRENT_state = icmp->icmp_state;
12410Sstevel@tonic-gate 	tap->OPT_size = icmp_max_optsize;
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate /*
12450Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
12460Sstevel@tonic-gate  * icmp_wput.  Much of the T_CAPABILITY_ACK information is copied from
12470Sstevel@tonic-gate  * icmp_g_t_info_ack.  The current state of the stream is copied from
12480Sstevel@tonic-gate  * icmp_state.
12490Sstevel@tonic-gate  */
12500Sstevel@tonic-gate static void
12510Sstevel@tonic-gate icmp_capability_req(queue_t *q, mblk_t *mp)
12520Sstevel@tonic-gate {
12535240Snordmark 	icmp_t			*icmp = Q_TO_ICMP(q);
12540Sstevel@tonic-gate 	t_uscalar_t		cap_bits1;
12550Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
12605240Snordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
12610Sstevel@tonic-gate 	if (!mp)
12620Sstevel@tonic-gate 		return;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
12650Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
12680Sstevel@tonic-gate 		icmp_copy_info(&tcap->INFO_ack, icmp);
12690Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	qreply(q, mp);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate /*
12760Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages.  It is called by icmp_wput.
12770Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
12780Sstevel@tonic-gate  * The current state of the stream is copied from icmp_state.
12790Sstevel@tonic-gate  */
12800Sstevel@tonic-gate static void
12810Sstevel@tonic-gate icmp_info_req(queue_t *q, mblk_t *mp)
12820Sstevel@tonic-gate {
12835240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
12860Sstevel@tonic-gate 	    T_INFO_ACK);
12870Sstevel@tonic-gate 	if (!mp)
12880Sstevel@tonic-gate 		return;
12890Sstevel@tonic-gate 	icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
12900Sstevel@tonic-gate 	qreply(q, mp);
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate /*
12940Sstevel@tonic-gate  * IP recognizes seven kinds of bind requests:
12950Sstevel@tonic-gate  *
12960Sstevel@tonic-gate  * - A zero-length address binds only to the protocol number.
12970Sstevel@tonic-gate  *
12980Sstevel@tonic-gate  * - A 4-byte address is treated as a request to
12990Sstevel@tonic-gate  * validate that the address is a valid local IPv4
13000Sstevel@tonic-gate  * address, appropriate for an application to bind to.
13010Sstevel@tonic-gate  * IP does the verification, but does not make any note
13020Sstevel@tonic-gate  * of the address at this time.
13030Sstevel@tonic-gate  *
13040Sstevel@tonic-gate  * - A 16-byte address contains is treated as a request
13050Sstevel@tonic-gate  * to validate a local IPv6 address, as the 4-byte
13060Sstevel@tonic-gate  * address case above.
13070Sstevel@tonic-gate  *
13080Sstevel@tonic-gate  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
13090Sstevel@tonic-gate  * use it for the inbound fanout of packets.
13100Sstevel@tonic-gate  *
13110Sstevel@tonic-gate  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
13120Sstevel@tonic-gate  * use it for the inbound fanout of packets.
13130Sstevel@tonic-gate  *
13140Sstevel@tonic-gate  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
13150Sstevel@tonic-gate  * information consisting of local and remote addresses
13160Sstevel@tonic-gate  * and ports (unused for raw sockets).  In this case, the addresses are both
13170Sstevel@tonic-gate  * validated as appropriate for this operation, and, if
13180Sstevel@tonic-gate  * so, the information is retained for use in the
13190Sstevel@tonic-gate  * inbound fanout.
13200Sstevel@tonic-gate  *
13210Sstevel@tonic-gate  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
13220Sstevel@tonic-gate  * fanout information, like the 12-byte case above.
13230Sstevel@tonic-gate  *
13240Sstevel@tonic-gate  * IP will also fill in the IRE request mblk with information
13250Sstevel@tonic-gate  * regarding our peer.  In all cases, we notify IP of our protocol
13260Sstevel@tonic-gate  * type by appending a single protocol byte to the bind request.
13270Sstevel@tonic-gate  */
13280Sstevel@tonic-gate static mblk_t *
13290Sstevel@tonic-gate icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length,
13300Sstevel@tonic-gate     in_port_t fport)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	char	*cp;
13330Sstevel@tonic-gate 	mblk_t	*mp;
13340Sstevel@tonic-gate 	struct T_bind_req *tbr;
13350Sstevel@tonic-gate 	ipa_conn_t	*ac;
13360Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
13370Sstevel@tonic-gate 	sin_t		*sin;
13380Sstevel@tonic-gate 	sin6_t		*sin6;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
13415240Snordmark 	ASSERT(RW_LOCK_HELD(&icmp->icmp_rwlock));
13420Sstevel@tonic-gate 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
13430Sstevel@tonic-gate 	if (mp == NULL)
13440Sstevel@tonic-gate 		return (NULL);
13450Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
13460Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
13470Sstevel@tonic-gate 	tbr->PRIM_type = bind_prim;
13480Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (*tbr);
13490Sstevel@tonic-gate 	tbr->CONIND_number = 0;
13500Sstevel@tonic-gate 	tbr->ADDR_length = addr_length;
13510Sstevel@tonic-gate 	cp = (char *)&tbr[1];
13520Sstevel@tonic-gate 	switch (addr_length) {
13530Sstevel@tonic-gate 	case sizeof (ipa_conn_t):
13540Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
13550Sstevel@tonic-gate 		/* Append a request for an IRE */
13560Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13570Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13580Sstevel@tonic-gate 			freemsg(mp);
13590Sstevel@tonic-gate 			return (NULL);
13600Sstevel@tonic-gate 		}
13610Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13620Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
13650Sstevel@tonic-gate 		ac = (ipa_conn_t *)cp;
13660Sstevel@tonic-gate 		ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src);
13670Sstevel@tonic-gate 		ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst);
13680Sstevel@tonic-gate 		ac->ac_fport = fport;
13690Sstevel@tonic-gate 		ac->ac_lport = 0;
13700Sstevel@tonic-gate 		break;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	case sizeof (ipa6_conn_t):
13730Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
13740Sstevel@tonic-gate 		/* Append a request for an IRE */
13750Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13760Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13770Sstevel@tonic-gate 			freemsg(mp);
13780Sstevel@tonic-gate 			return (NULL);
13790Sstevel@tonic-gate 		}
13800Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13810Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
13840Sstevel@tonic-gate 		ac6 = (ipa6_conn_t *)cp;
13850Sstevel@tonic-gate 		ac6->ac6_laddr = icmp->icmp_v6src;
13860Sstevel@tonic-gate 		ac6->ac6_faddr = icmp->icmp_v6dst;
13870Sstevel@tonic-gate 		ac6->ac6_fport = fport;
13880Sstevel@tonic-gate 		ac6->ac6_lport = 0;
13890Sstevel@tonic-gate 		break;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	case sizeof (sin_t):
13920Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
13930Sstevel@tonic-gate 		/* Append a request for an IRE */
13940Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13950Sstevel@tonic-gate 		if (!mp->b_cont) {
13960Sstevel@tonic-gate 			freemsg(mp);
13970Sstevel@tonic-gate 			return (NULL);
13980Sstevel@tonic-gate 		}
13990Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
14000Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		sin = (sin_t *)cp;
14030Sstevel@tonic-gate 		*sin = sin_null;
14040Sstevel@tonic-gate 		sin->sin_family = AF_INET;
14050Sstevel@tonic-gate 		sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
14060Sstevel@tonic-gate 		break;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	case sizeof (sin6_t):
14090Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
14100Sstevel@tonic-gate 		/* Append a request for an IRE */
14110Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
14120Sstevel@tonic-gate 		if (!mp->b_cont) {
14130Sstevel@tonic-gate 			freemsg(mp);
14140Sstevel@tonic-gate 			return (NULL);
14150Sstevel@tonic-gate 		}
14160Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
14170Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
14200Sstevel@tonic-gate 		*sin6 = sin6_null;
14210Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
14220Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_bound_v6src;
14230Sstevel@tonic-gate 		break;
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 	/* Add protocol number to end */
14260Sstevel@tonic-gate 	cp[addr_length] = icmp->icmp_proto;
14270Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
14280Sstevel@tonic-gate 	return (mp);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate 
14315240Snordmark /* For /dev/icmp aka AF_INET open */
14325240Snordmark static int
14335240Snordmark icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
14341676Sjpk {
14355240Snordmark 	return (icmp_open(q, devp, flag, sflag, credp, B_FALSE));
14365240Snordmark }
14375240Snordmark 
14385240Snordmark /* For /dev/icmp6 aka AF_INET6 open */
14395240Snordmark static int
14405240Snordmark icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
14415240Snordmark {
14425240Snordmark 	return (icmp_open(q, devp, flag, sflag, credp, B_TRUE));
14431676Sjpk }
14441676Sjpk 
14450Sstevel@tonic-gate /*
14460Sstevel@tonic-gate  * This is the open routine for icmp.  It allocates a icmp_t structure for
14470Sstevel@tonic-gate  * the stream and, on the first open of the module, creates an ND table.
14480Sstevel@tonic-gate  */
14495240Snordmark /*ARGSUSED2*/
14500Sstevel@tonic-gate static int
14515240Snordmark icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
14525240Snordmark     boolean_t isv6)
14530Sstevel@tonic-gate {
14540Sstevel@tonic-gate 	int	err;
14550Sstevel@tonic-gate 	icmp_t	*icmp;
14565240Snordmark 	conn_t *connp;
14575240Snordmark 	dev_t	conn_dev;
14585240Snordmark 	zoneid_t zoneid;
14593448Sdh155122 	netstack_t *ns;
14603448Sdh155122 	icmp_stack_t *is;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/* If the stream is already open, return immediately. */
14630Sstevel@tonic-gate 	if (q->q_ptr != NULL)
14640Sstevel@tonic-gate 		return (0);
14650Sstevel@tonic-gate 
14665240Snordmark 	if (sflag == MODOPEN)
14670Sstevel@tonic-gate 		return (EINVAL);
14680Sstevel@tonic-gate 
14693448Sdh155122 	ns = netstack_find_by_cred(credp);
14703448Sdh155122 	ASSERT(ns != NULL);
14713448Sdh155122 	is = ns->netstack_icmp;
14723448Sdh155122 	ASSERT(is != NULL);
14733448Sdh155122 
14743448Sdh155122 	/*
14753448Sdh155122 	 * For exclusive stacks we set the zoneid to zero
14763448Sdh155122 	 * to make ICMP operate as if in the global zone.
14773448Sdh155122 	 */
14785240Snordmark 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
14793448Sdh155122 		zoneid = GLOBAL_ZONEID;
14803448Sdh155122 	else
14813448Sdh155122 		zoneid = crgetzoneid(credp);
14823448Sdh155122 
14835240Snordmark 	if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) {
14845240Snordmark 		netstack_rele(ns);
14855240Snordmark 		return (EBUSY);
14863448Sdh155122 	}
14875240Snordmark 	*devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
14885240Snordmark 
14895240Snordmark 	connp = ipcl_conn_create(IPCL_RAWIPCONN, KM_SLEEP, ns);
14905240Snordmark 	connp->conn_dev = conn_dev;
14915240Snordmark 	icmp = connp->conn_icmp;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	/*
14945240Snordmark 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
14955240Snordmark 	 * done by netstack_find_by_cred()
14965240Snordmark 	 */
14975240Snordmark 	netstack_rele(ns);
14985240Snordmark 
14995240Snordmark 	/*
15005240Snordmark 	 * Initialize the icmp_t structure for this stream.
15010Sstevel@tonic-gate 	 */
15025240Snordmark 	q->q_ptr = connp;
15035240Snordmark 	WR(q)->q_ptr = connp;
15045240Snordmark 	connp->conn_rq = q;
15055240Snordmark 	connp->conn_wq = WR(q);
15065240Snordmark 
15075240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
15085240Snordmark 	ASSERT(connp->conn_ulp == IPPROTO_ICMP);
15095240Snordmark 	ASSERT(connp->conn_icmp == icmp);
15105240Snordmark 	ASSERT(icmp->icmp_connp == connp);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	/* Set the initial state of the stream and the privilege status. */
15130Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
15145240Snordmark 	if (isv6) {
15150Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV6_VERSION;
15160Sstevel@tonic-gate 		icmp->icmp_family = AF_INET6;
15175240Snordmark 		connp->conn_ulp = IPPROTO_ICMPV6;
15180Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
15190Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMPV6;
15200Sstevel@tonic-gate 		icmp->icmp_checksum_off = 2;	/* Offset for icmp6_cksum */
15210Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IPV6_HDR_LEN;
15223448Sdh155122 		icmp->icmp_ttl = (uint8_t)is->is_ipv6_hoplimit;
15235240Snordmark 		connp->conn_af_isv6 = B_TRUE;
15245240Snordmark 		connp->conn_flags |= IPCL_ISV6;
15250Sstevel@tonic-gate 	} else {
15260Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV4_VERSION;
15270Sstevel@tonic-gate 		icmp->icmp_family = AF_INET;
15280Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
15290Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMP;
15300Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH;
15313448Sdh155122 		icmp->icmp_ttl = (uint8_t)is->is_ipv4_ttl;
15325240Snordmark 		connp->conn_af_isv6 = B_FALSE;
15335240Snordmark 		connp->conn_flags &= ~IPCL_ISV6;
15340Sstevel@tonic-gate 	}
15355240Snordmark 	icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
15365240Snordmark 	icmp->icmp_pending_op = -1;
15375240Snordmark 	connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
15385240Snordmark 	connp->conn_zoneid = zoneid;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	/*
15415240Snordmark 	 * If the caller has the process-wide flag set, then default to MAC
15425240Snordmark 	 * exempt mode.  This allows read-down to unlabeled hosts.
15430Sstevel@tonic-gate 	 */
15445240Snordmark 	if (getpflags(NET_MAC_AWARE, credp) != 0)
15455240Snordmark 		icmp->icmp_mac_exempt = B_TRUE;
15465240Snordmark 
15475240Snordmark 	connp->conn_ulp_labeled = is_system_labeled();
15485240Snordmark 
15495240Snordmark 	icmp->icmp_is = is;
15505240Snordmark 
15515240Snordmark 	q->q_hiwat = is->is_recv_hiwat;
15523448Sdh155122 	WR(q)->q_hiwat = is->is_xmit_hiwat;
15533448Sdh155122 	WR(q)->q_lowat = is->is_xmit_lowat;
15545240Snordmark 
15555240Snordmark 	connp->conn_recv = icmp_input;
15565240Snordmark 	crhold(credp);
15575240Snordmark 	connp->conn_cred = credp;
15585240Snordmark 
15595240Snordmark 	mutex_enter(&connp->conn_lock);
15605240Snordmark 	connp->conn_state_flags &= ~CONN_INCIPIENT;
15615240Snordmark 	mutex_exit(&connp->conn_lock);
15625240Snordmark 
15635240Snordmark 	qprocson(q);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
15660Sstevel@tonic-gate 		/* Build initial header template for transmit */
15675240Snordmark 		if ((err = icmp_build_hdrs(icmp)) != 0) {
15685240Snordmark 			rw_exit(&icmp->icmp_rwlock);
15695240Snordmark 			qprocsoff(q);
15705240Snordmark 			ipcl_conn_destroy(connp);
15715240Snordmark 			return (err);
15725240Snordmark 		}
15730Sstevel@tonic-gate 	}
15745240Snordmark 	rw_exit(&icmp->icmp_rwlock);
15755240Snordmark 
15760Sstevel@tonic-gate 	/* Set the Stream head write offset. */
15773448Sdh155122 	(void) mi_set_sth_wroff(q,
15783448Sdh155122 	    icmp->icmp_max_hdr_len + is->is_wroff_extra);
15790Sstevel@tonic-gate 	(void) mi_set_sth_hiwat(q, q->q_hiwat);
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	return (0);
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate /*
15850Sstevel@tonic-gate  * Which ICMP options OK to set through T_UNITDATA_REQ...
15860Sstevel@tonic-gate  */
15870Sstevel@tonic-gate /* ARGSUSED */
15880Sstevel@tonic-gate static boolean_t
15890Sstevel@tonic-gate icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	return (B_TRUE);
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate /*
15950Sstevel@tonic-gate  * This routine gets default values of certain options whose default
15960Sstevel@tonic-gate  * values are maintained by protcol specific code
15970Sstevel@tonic-gate  */
15980Sstevel@tonic-gate /* ARGSUSED */
15990Sstevel@tonic-gate int
16000Sstevel@tonic-gate icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr)
16010Sstevel@tonic-gate {
16025240Snordmark 	icmp_t *icmp = Q_TO_ICMP(q);
16033448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
16040Sstevel@tonic-gate 	int *i1 = (int *)ptr;
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	switch (level) {
16070Sstevel@tonic-gate 	case IPPROTO_IP:
16080Sstevel@tonic-gate 		switch (name) {
16090Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
16100Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
16110Sstevel@tonic-gate 			return (sizeof (uchar_t));
16120Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
16130Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
16140Sstevel@tonic-gate 			return (sizeof (uchar_t));
16150Sstevel@tonic-gate 		}
16160Sstevel@tonic-gate 		break;
16170Sstevel@tonic-gate 	case IPPROTO_IPV6:
16180Sstevel@tonic-gate 		switch (name) {
16190Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
16200Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_TTL;
16210Sstevel@tonic-gate 			return (sizeof (int));
16220Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
16230Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
16240Sstevel@tonic-gate 			return (sizeof (int));
16250Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
16263448Sdh155122 			*i1 = is->is_ipv6_hoplimit;
16270Sstevel@tonic-gate 			return (sizeof (int));
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 		break;
16300Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
16310Sstevel@tonic-gate 		switch (name) {
16320Sstevel@tonic-gate 		case ICMP6_FILTER:
16330Sstevel@tonic-gate 			/* Make it look like "pass all" */
16340Sstevel@tonic-gate 			ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
16350Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
16360Sstevel@tonic-gate 		}
16370Sstevel@tonic-gate 		break;
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 	return (-1);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate /*
16430Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
16440Sstevel@tonic-gate  * It returns the size of the option retrieved.
16450Sstevel@tonic-gate  */
16460Sstevel@tonic-gate int
16475240Snordmark icmp_opt_get_locked(queue_t *q, int level, int name, uchar_t *ptr)
16480Sstevel@tonic-gate {
16495240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
16505240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
16515240Snordmark 	icmp_stack_t *is = icmp->icmp_is;
16520Sstevel@tonic-gate 	int	*i1 = (int *)ptr;
16530Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &icmp->icmp_sticky_ipp;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	switch (level) {
16560Sstevel@tonic-gate 	case SOL_SOCKET:
16570Sstevel@tonic-gate 		switch (name) {
16580Sstevel@tonic-gate 		case SO_DEBUG:
16590Sstevel@tonic-gate 			*i1 = icmp->icmp_debug;
16600Sstevel@tonic-gate 			break;
16610Sstevel@tonic-gate 		case SO_TYPE:
16620Sstevel@tonic-gate 			*i1 = SOCK_RAW;
16630Sstevel@tonic-gate 			break;
16640Sstevel@tonic-gate 		case SO_PROTOTYPE:
16650Sstevel@tonic-gate 			*i1 = icmp->icmp_proto;
16660Sstevel@tonic-gate 			break;
16670Sstevel@tonic-gate 		case SO_REUSEADDR:
16680Sstevel@tonic-gate 			*i1 = icmp->icmp_reuseaddr;
16690Sstevel@tonic-gate 			break;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 		/*
16720Sstevel@tonic-gate 		 * The following three items are available here,
16730Sstevel@tonic-gate 		 * but are only meaningful to IP.
16740Sstevel@tonic-gate 		 */
16750Sstevel@tonic-gate 		case SO_DONTROUTE:
16760Sstevel@tonic-gate 			*i1 = icmp->icmp_dontroute;
16770Sstevel@tonic-gate 			break;
16780Sstevel@tonic-gate 		case SO_USELOOPBACK:
16790Sstevel@tonic-gate 			*i1 = icmp->icmp_useloopback;
16800Sstevel@tonic-gate 			break;
16810Sstevel@tonic-gate 		case SO_BROADCAST:
16820Sstevel@tonic-gate 			*i1 = icmp->icmp_broadcast;
16830Sstevel@tonic-gate 			break;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 		case SO_SNDBUF:
16860Sstevel@tonic-gate 			ASSERT(q->q_hiwat <= INT_MAX);
16870Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
16880Sstevel@tonic-gate 			break;
16890Sstevel@tonic-gate 		case SO_RCVBUF:
16900Sstevel@tonic-gate 			ASSERT(RD(q)->q_hiwat <= INT_MAX);
16910Sstevel@tonic-gate 			*i1 = (int)RD(q)->q_hiwat;
16920Sstevel@tonic-gate 			break;
16930Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
16940Sstevel@tonic-gate 			*i1 = icmp->icmp_dgram_errind;
16950Sstevel@tonic-gate 			break;
16961673Sgt145670 		case SO_TIMESTAMP:
16971673Sgt145670 			*i1 = icmp->icmp_timestamp;
16981673Sgt145670 			break;
16991676Sjpk 		case SO_MAC_EXEMPT:
17001676Sjpk 			*i1 = icmp->icmp_mac_exempt;
17011676Sjpk 			break;
17023388Skcpoon 		case SO_DOMAIN:
17033388Skcpoon 			*i1 = icmp->icmp_family;
17043388Skcpoon 			break;
17053388Skcpoon 
17060Sstevel@tonic-gate 		/*
17072263Ssommerfe 		 * Following four not meaningful for icmp
17080Sstevel@tonic-gate 		 * Action is same as "default" to which we fallthrough
17090Sstevel@tonic-gate 		 * so we keep them in comments.
17100Sstevel@tonic-gate 		 * case SO_LINGER:
17110Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
17120Sstevel@tonic-gate 		 * case SO_OOBINLINE:
17132263Ssommerfe 		 * case SO_ALLZONES:
17140Sstevel@tonic-gate 		 */
17150Sstevel@tonic-gate 		default:
17160Sstevel@tonic-gate 			return (-1);
17170Sstevel@tonic-gate 		}
17180Sstevel@tonic-gate 		break;
17190Sstevel@tonic-gate 	case IPPROTO_IP:
17200Sstevel@tonic-gate 		/*
17210Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
17220Sstevel@tonic-gate 		 */
17230Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET)
17240Sstevel@tonic-gate 			return (-1);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 		switch (name) {
17270Sstevel@tonic-gate 		case IP_OPTIONS:
17280Sstevel@tonic-gate 		case T_IP_OPTIONS:
17290Sstevel@tonic-gate 			/* Options are passed up with each packet */
17300Sstevel@tonic-gate 			return (0);
17310Sstevel@tonic-gate 		case IP_HDRINCL:
17320Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_hdrincl;
17330Sstevel@tonic-gate 			break;
17340Sstevel@tonic-gate 		case IP_TOS:
17350Sstevel@tonic-gate 		case T_IP_TOS:
17360Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_type_of_service;
17370Sstevel@tonic-gate 			break;
17380Sstevel@tonic-gate 		case IP_TTL:
17390Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_ttl;
17400Sstevel@tonic-gate 			break;
17410Sstevel@tonic-gate 		case IP_MULTICAST_IF:
17420Sstevel@tonic-gate 			/* 0 address if not set */
17430Sstevel@tonic-gate 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
17440Sstevel@tonic-gate 			return (sizeof (ipaddr_t));
17450Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
17460Sstevel@tonic-gate 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
17470Sstevel@tonic-gate 			return (sizeof (uchar_t));
17480Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
17495240Snordmark 			*ptr = connp->conn_multicast_loop;
17500Sstevel@tonic-gate 			return (sizeof (uint8_t));
17510Sstevel@tonic-gate 		case IP_BOUND_IF:
17520Sstevel@tonic-gate 			/* Zero if not set */
17530Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
17540Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17550Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
17560Sstevel@tonic-gate 			*ptr = icmp->icmp_unspec_source;
17570Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17580Sstevel@tonic-gate 		case IP_XMIT_IF:
17590Sstevel@tonic-gate 			*i1 = icmp->icmp_xmit_if;
17600Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17610Sstevel@tonic-gate 		case IP_RECVIF:
17620Sstevel@tonic-gate 			*ptr = icmp->icmp_recvif;
17630Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17643318Srshoaib 		case IP_RECVPKTINFO:
17653318Srshoaib 			/*
17663318Srshoaib 			 * This also handles IP_PKTINFO.
17673318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
17683318Srshoaib 			 * Differentiation is based on the size of the argument
17693318Srshoaib 			 * passed in.
17703318Srshoaib 			 * This option is handled in IP which will return an
17713318Srshoaib 			 * error for IP_PKTINFO as it's not supported as a
17723318Srshoaib 			 * sticky option.
17733318Srshoaib 			 */
17743318Srshoaib 			return (-EINVAL);
17750Sstevel@tonic-gate 		/*
17760Sstevel@tonic-gate 		 * Cannot "get" the value of following options
17770Sstevel@tonic-gate 		 * at this level. Action is same as "default" to
17780Sstevel@tonic-gate 		 * which we fallthrough so we keep them in comments.
17790Sstevel@tonic-gate 		 *
17800Sstevel@tonic-gate 		 * case IP_ADD_MEMBERSHIP:
17810Sstevel@tonic-gate 		 * case IP_DROP_MEMBERSHIP:
17820Sstevel@tonic-gate 		 * case IP_BLOCK_SOURCE:
17830Sstevel@tonic-gate 		 * case IP_UNBLOCK_SOURCE:
17840Sstevel@tonic-gate 		 * case IP_ADD_SOURCE_MEMBERSHIP:
17850Sstevel@tonic-gate 		 * case IP_DROP_SOURCE_MEMBERSHIP:
17860Sstevel@tonic-gate 		 * case MCAST_JOIN_GROUP:
17870Sstevel@tonic-gate 		 * case MCAST_LEAVE_GROUP:
17880Sstevel@tonic-gate 		 * case MCAST_BLOCK_SOURCE:
17890Sstevel@tonic-gate 		 * case MCAST_UNBLOCK_SOURCE:
17900Sstevel@tonic-gate 		 * case MCAST_JOIN_SOURCE_GROUP:
17910Sstevel@tonic-gate 		 * case MCAST_LEAVE_SOURCE_GROUP:
17920Sstevel@tonic-gate 		 * case MRT_INIT:
17930Sstevel@tonic-gate 		 * case MRT_DONE:
17940Sstevel@tonic-gate 		 * case MRT_ADD_VIF:
17950Sstevel@tonic-gate 		 * case MRT_DEL_VIF:
17960Sstevel@tonic-gate 		 * case MRT_ADD_MFC:
17970Sstevel@tonic-gate 		 * case MRT_DEL_MFC:
17980Sstevel@tonic-gate 		 * case MRT_VERSION:
17990Sstevel@tonic-gate 		 * case MRT_ASSERT:
18000Sstevel@tonic-gate 		 * case IP_SEC_OPT:
18010Sstevel@tonic-gate 		 * case IP_DONTFAILOVER_IF:
18021663Spriyanka 		 * case IP_NEXTHOP:
18030Sstevel@tonic-gate 		 */
18040Sstevel@tonic-gate 		default:
18050Sstevel@tonic-gate 			return (-1);
18060Sstevel@tonic-gate 		}
18070Sstevel@tonic-gate 		break;
18080Sstevel@tonic-gate 	case IPPROTO_IPV6:
18090Sstevel@tonic-gate 		/*
18100Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
18110Sstevel@tonic-gate 		 */
18120Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
18130Sstevel@tonic-gate 			return (-1);
18140Sstevel@tonic-gate 		switch (name) {
18150Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
18160Sstevel@tonic-gate 			*i1 = (unsigned int)icmp->icmp_ttl;
18170Sstevel@tonic-gate 			break;
18180Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
18190Sstevel@tonic-gate 			/* 0 index if not set */
18200Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_if_index;
18210Sstevel@tonic-gate 			break;
18220Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
18230Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_ttl;
18240Sstevel@tonic-gate 			break;
18250Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
18265240Snordmark 			*i1 = connp->conn_multicast_loop;
18270Sstevel@tonic-gate 			break;
18280Sstevel@tonic-gate 		case IPV6_BOUND_IF:
18290Sstevel@tonic-gate 			/* Zero if not set */
18300Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
18310Sstevel@tonic-gate 			break;
18320Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
18330Sstevel@tonic-gate 			*i1 = icmp->icmp_unspec_source;
18340Sstevel@tonic-gate 			break;
18350Sstevel@tonic-gate 		case IPV6_CHECKSUM:
18360Sstevel@tonic-gate 			/*
18370Sstevel@tonic-gate 			 * Return offset or -1 if no checksum offset.
18380Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6
18390Sstevel@tonic-gate 			 */
18400Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
18410Sstevel@tonic-gate 				return (-1);
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 			if (icmp->icmp_raw_checksum) {
18440Sstevel@tonic-gate 				*i1 = icmp->icmp_checksum_off;
18450Sstevel@tonic-gate 			} else {
18460Sstevel@tonic-gate 				*i1 = -1;
18470Sstevel@tonic-gate 			}
18480Sstevel@tonic-gate 			break;
18490Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
18500Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
18510Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
18520Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
18530Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
18540Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
18550Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
18560Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
18570Sstevel@tonic-gate 			/* cannot "get" the value for these */
18580Sstevel@tonic-gate 			return (-1);
18590Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
18603318Srshoaib 			*i1 = icmp->icmp_ip_recvpktinfo;
18610Sstevel@tonic-gate 			break;
18620Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
18630Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvtclass;
18640Sstevel@tonic-gate 			break;
18650Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
18660Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpathmtu;
18670Sstevel@tonic-gate 			break;
18680Sstevel@tonic-gate 		case IPV6_V6ONLY:
18690Sstevel@tonic-gate 			*i1 = 1;
18700Sstevel@tonic-gate 			break;
18710Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
18720Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhoplimit;
18730Sstevel@tonic-gate 			break;
18740Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
18750Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhopopts;
18760Sstevel@tonic-gate 			break;
18770Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
18780Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvdstopts;
18790Sstevel@tonic-gate 			break;
18800Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
18810Sstevel@tonic-gate 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
18820Sstevel@tonic-gate 			break;
18830Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
18840Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
18850Sstevel@tonic-gate 			break;
18860Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
18870Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrthdr;
18880Sstevel@tonic-gate 			break;
18890Sstevel@tonic-gate 		case IPV6_PKTINFO: {
18900Sstevel@tonic-gate 			/* XXX assumes that caller has room for max size! */
18910Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
18940Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
18950Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
18960Sstevel@tonic-gate 			else
18970Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
18980Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
18990Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
19000Sstevel@tonic-gate 			else
19010Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
19020Sstevel@tonic-gate 			return (sizeof (struct in6_pktinfo));
19030Sstevel@tonic-gate 		}
19040Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
19050Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)ptr;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
19080Sstevel@tonic-gate 				return (0);
19090Sstevel@tonic-gate 			*sin6 = sin6_null;
19100Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
19110Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
19120Sstevel@tonic-gate 			return (sizeof (sin6_t));
19130Sstevel@tonic-gate 		}
19140Sstevel@tonic-gate 		case IPV6_HOPOPTS:
19150Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
19160Sstevel@tonic-gate 				return (0);
19171676Sjpk 			if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6)
19181676Sjpk 				return (0);
19191676Sjpk 			bcopy((char *)ipp->ipp_hopopts +
19201676Sjpk 			    icmp->icmp_label_len_v6, ptr,
19211676Sjpk 			    ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
19221676Sjpk 			if (icmp->icmp_label_len_v6 > 0) {
19231676Sjpk 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
19241676Sjpk 				ptr[1] = (ipp->ipp_hopoptslen -
19251676Sjpk 				    icmp->icmp_label_len_v6 + 7) / 8 - 1;
19261676Sjpk 			}
19271676Sjpk 			return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
19280Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
19290Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
19300Sstevel@tonic-gate 				return (0);
19310Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
19320Sstevel@tonic-gate 			return (ipp->ipp_rtdstoptslen);
19330Sstevel@tonic-gate 		case IPV6_RTHDR:
19340Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
19350Sstevel@tonic-gate 				return (0);
19360Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
19370Sstevel@tonic-gate 			return (ipp->ipp_rthdrlen);
19380Sstevel@tonic-gate 		case IPV6_DSTOPTS:
19390Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
19400Sstevel@tonic-gate 				return (0);
19410Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
19420Sstevel@tonic-gate 			return (ipp->ipp_dstoptslen);
19430Sstevel@tonic-gate 		case IPV6_PATHMTU:
19440Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
19450Sstevel@tonic-gate 				return (0);
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
19485240Snordmark 			    (struct ip6_mtuinfo *)ptr,
19495240Snordmark 			    is->is_netstack));
19500Sstevel@tonic-gate 		case IPV6_TCLASS:
19510Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_TCLASS)
19520Sstevel@tonic-gate 				*i1 = ipp->ipp_tclass;
19530Sstevel@tonic-gate 			else
19540Sstevel@tonic-gate 				*i1 = IPV6_FLOW_TCLASS(
19550Sstevel@tonic-gate 				    IPV6_DEFAULT_VERS_AND_FLOW);
19560Sstevel@tonic-gate 			break;
19570Sstevel@tonic-gate 		default:
19580Sstevel@tonic-gate 			return (-1);
19590Sstevel@tonic-gate 		}
19600Sstevel@tonic-gate 		break;
19610Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
19620Sstevel@tonic-gate 		/*
19630Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
19640Sstevel@tonic-gate 		 */
19650Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
19660Sstevel@tonic-gate 			return (-1);
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
19690Sstevel@tonic-gate 			return (-1);
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 		switch (name) {
19720Sstevel@tonic-gate 		case ICMP6_FILTER:
19730Sstevel@tonic-gate 			if (icmp->icmp_filter == NULL) {
19740Sstevel@tonic-gate 				/* Make it look like "pass all" */
19750Sstevel@tonic-gate 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
19760Sstevel@tonic-gate 			} else {
19770Sstevel@tonic-gate 				(void) bcopy(icmp->icmp_filter, ptr,
19780Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
19790Sstevel@tonic-gate 			}
19800Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
19810Sstevel@tonic-gate 		default:
19820Sstevel@tonic-gate 			return (-1);
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 	default:
19850Sstevel@tonic-gate 		return (-1);
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate 	return (sizeof (int));
19880Sstevel@tonic-gate }
19890Sstevel@tonic-gate 
19905240Snordmark /*
19915240Snordmark  * This routine retrieves the current status of socket options.
19925240Snordmark  * It returns the size of the option retrieved.
19935240Snordmark  */
19945240Snordmark int
19955240Snordmark icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
19965240Snordmark {
19975240Snordmark 	icmp_t  *icmp = Q_TO_ICMP(q);
19985240Snordmark 	int 	err;
19995240Snordmark 
20005240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_READER);
20015240Snordmark 	err = icmp_opt_get_locked(q, level, name, ptr);
20025240Snordmark 	rw_exit(&icmp->icmp_rwlock);
20035240Snordmark 	return (err);
20045240Snordmark }
20055240Snordmark 
20065240Snordmark 
20070Sstevel@tonic-gate /* This routine sets socket options. */
20080Sstevel@tonic-gate /* ARGSUSED */
20090Sstevel@tonic-gate int
20105240Snordmark icmp_opt_set_locked(queue_t *q, uint_t optset_context, int level, int name,
20110Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
20120Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
20130Sstevel@tonic-gate {
20145240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
20155240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
20163448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
20170Sstevel@tonic-gate 	int	*i1 = (int *)invalp;
20180Sstevel@tonic-gate 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
20190Sstevel@tonic-gate 	boolean_t checkonly;
20200Sstevel@tonic-gate 	int	error;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	switch (optset_context) {
20230Sstevel@tonic-gate 	case SETFN_OPTCOM_CHECKONLY:
20240Sstevel@tonic-gate 		checkonly = B_TRUE;
20250Sstevel@tonic-gate 		/*
20260Sstevel@tonic-gate 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
20270Sstevel@tonic-gate 		 * inlen != 0 implies value supplied and
20280Sstevel@tonic-gate 		 * 	we have to "pretend" to set it.
20290Sstevel@tonic-gate 		 * inlen == 0 implies that there is no
20300Sstevel@tonic-gate 		 * 	value part in T_CHECK request and just validation
20310Sstevel@tonic-gate 		 * done elsewhere should be enough, we just return here.
20320Sstevel@tonic-gate 		 */
20330Sstevel@tonic-gate 		if (inlen == 0) {
20340Sstevel@tonic-gate 			*outlenp = 0;
20350Sstevel@tonic-gate 			return (0);
20360Sstevel@tonic-gate 		}
20370Sstevel@tonic-gate 		break;
20380Sstevel@tonic-gate 	case SETFN_OPTCOM_NEGOTIATE:
20390Sstevel@tonic-gate 		checkonly = B_FALSE;
20400Sstevel@tonic-gate 		break;
20410Sstevel@tonic-gate 	case SETFN_UD_NEGOTIATE:
20420Sstevel@tonic-gate 	case SETFN_CONN_NEGOTIATE:
20430Sstevel@tonic-gate 		checkonly = B_FALSE;
20440Sstevel@tonic-gate 		/*
20450Sstevel@tonic-gate 		 * Negotiating local and "association-related" options
20460Sstevel@tonic-gate 		 * through T_UNITDATA_REQ.
20470Sstevel@tonic-gate 		 *
20480Sstevel@tonic-gate 		 * Following routine can filter out ones we do not
20490Sstevel@tonic-gate 		 * want to be "set" this way.
20500Sstevel@tonic-gate 		 */
20510Sstevel@tonic-gate 		if (!icmp_opt_allow_udr_set(level, name)) {
20520Sstevel@tonic-gate 			*outlenp = 0;
20530Sstevel@tonic-gate 			return (EINVAL);
20540Sstevel@tonic-gate 		}
20550Sstevel@tonic-gate 		break;
20560Sstevel@tonic-gate 	default:
20570Sstevel@tonic-gate 		/*
20580Sstevel@tonic-gate 		 * We should never get here
20590Sstevel@tonic-gate 		 */
20600Sstevel@tonic-gate 		*outlenp = 0;
20610Sstevel@tonic-gate 		return (EINVAL);
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
20650Sstevel@tonic-gate 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	/*
20680Sstevel@tonic-gate 	 * For fixed length options, no sanity check
20690Sstevel@tonic-gate 	 * of passed in length is done. It is assumed *_optcom_req()
20700Sstevel@tonic-gate 	 * routines do the right thing.
20710Sstevel@tonic-gate 	 */
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	switch (level) {
20740Sstevel@tonic-gate 	case SOL_SOCKET:
20750Sstevel@tonic-gate 		switch (name) {
20760Sstevel@tonic-gate 		case SO_DEBUG:
20770Sstevel@tonic-gate 			if (!checkonly)
20780Sstevel@tonic-gate 				icmp->icmp_debug = onoff;
20790Sstevel@tonic-gate 			break;
20800Sstevel@tonic-gate 		case SO_PROTOTYPE:
20810Sstevel@tonic-gate 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
20820Sstevel@tonic-gate 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
20830Sstevel@tonic-gate 			    secpolicy_net_rawaccess(cr) != 0) {
20840Sstevel@tonic-gate 				*outlenp = 0;
20850Sstevel@tonic-gate 				return (EACCES);
20860Sstevel@tonic-gate 			}
20870Sstevel@tonic-gate 			/* Can't use IPPROTO_RAW with IPv6 */
20880Sstevel@tonic-gate 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
20890Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET6) {
20900Sstevel@tonic-gate 				*outlenp = 0;
20910Sstevel@tonic-gate 				return (EPROTONOSUPPORT);
20920Sstevel@tonic-gate 			}
20930Sstevel@tonic-gate 			if (checkonly) {
20940Sstevel@tonic-gate 				/* T_CHECK case */
20950Sstevel@tonic-gate 				*(int *)outvalp = (*i1 & 0xFF);
20960Sstevel@tonic-gate 				break;
20970Sstevel@tonic-gate 			}
20980Sstevel@tonic-gate 			icmp->icmp_proto = *i1 & 0xFF;
20990Sstevel@tonic-gate 			if ((icmp->icmp_proto == IPPROTO_RAW ||
21000Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_IGMP) &&
21010Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET)
21020Sstevel@tonic-gate 				icmp->icmp_hdrincl = 1;
21030Sstevel@tonic-gate 			else
21040Sstevel@tonic-gate 				icmp->icmp_hdrincl = 0;
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6 &&
21070Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
21080Sstevel@tonic-gate 				/* Set offset for icmp6_cksum */
21090Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
21100Sstevel@tonic-gate 				icmp->icmp_checksum_off = 2;
21110Sstevel@tonic-gate 			}
21120Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_UDP ||
21130Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_TCP ||
21140Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_SCTP) {
21150Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 1;
21160Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields |=
21170Sstevel@tonic-gate 				    IPPF_NO_CKSUM;
21180Sstevel@tonic-gate 			} else {
21190Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 0;
21200Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields &=
21210Sstevel@tonic-gate 				    ~IPPF_NO_CKSUM;
21220Sstevel@tonic-gate 			}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 			if (icmp->icmp_filter != NULL &&
21250Sstevel@tonic-gate 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
21260Sstevel@tonic-gate 				kmem_free(icmp->icmp_filter,
21270Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
21280Sstevel@tonic-gate 				icmp->icmp_filter = NULL;
21290Sstevel@tonic-gate 			}
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 			/* Rebuild the header template */
21325240Snordmark 			error = icmp_build_hdrs(icmp);
21330Sstevel@tonic-gate 			if (error != 0) {
21340Sstevel@tonic-gate 				*outlenp = 0;
21350Sstevel@tonic-gate 				return (error);
21360Sstevel@tonic-gate 			}
21370Sstevel@tonic-gate 
2138409Skcpoon 			/*
2139409Skcpoon 			 * For SCTP, we don't use icmp_bind_proto() for
2140409Skcpoon 			 * raw socket binding.  Note that we do not need
2141409Skcpoon 			 * to set *outlenp.
21425240Snordmark 			 * FIXME: how does SCTP work?
2143409Skcpoon 			 */
2144409Skcpoon 			if (icmp->icmp_proto == IPPROTO_SCTP)
2145409Skcpoon 				return (0);
2146409Skcpoon 
21470Sstevel@tonic-gate 			*outlenp = sizeof (int);
21480Sstevel@tonic-gate 			*(int *)outvalp = *i1 & 0xFF;
21495240Snordmark 
21505240Snordmark 			/* Drop lock across the bind operation */
21515240Snordmark 			rw_exit(&icmp->icmp_rwlock);
21525240Snordmark 			icmp_bind_proto(q);
21535240Snordmark 			rw_enter(&icmp->icmp_rwlock, RW_WRITER);
21540Sstevel@tonic-gate 			return (0);
21550Sstevel@tonic-gate 		case SO_REUSEADDR:
21560Sstevel@tonic-gate 			if (!checkonly)
21570Sstevel@tonic-gate 				icmp->icmp_reuseaddr = onoff;
21580Sstevel@tonic-gate 			break;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 		/*
21610Sstevel@tonic-gate 		 * The following three items are available here,
21620Sstevel@tonic-gate 		 * but are only meaningful to IP.
21630Sstevel@tonic-gate 		 */
21640Sstevel@tonic-gate 		case SO_DONTROUTE:
21650Sstevel@tonic-gate 			if (!checkonly)
21660Sstevel@tonic-gate 				icmp->icmp_dontroute = onoff;
21670Sstevel@tonic-gate 			break;
21680Sstevel@tonic-gate 		case SO_USELOOPBACK:
21690Sstevel@tonic-gate 			if (!checkonly)
21700Sstevel@tonic-gate 				icmp->icmp_useloopback = onoff;
21710Sstevel@tonic-gate 			break;
21720Sstevel@tonic-gate 		case SO_BROADCAST:
21730Sstevel@tonic-gate 			if (!checkonly)
21740Sstevel@tonic-gate 				icmp->icmp_broadcast = onoff;
21750Sstevel@tonic-gate 			break;
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 		case SO_SNDBUF:
21783448Sdh155122 			if (*i1 > is->is_max_buf) {
21790Sstevel@tonic-gate 				*outlenp = 0;
21800Sstevel@tonic-gate 				return (ENOBUFS);
21810Sstevel@tonic-gate 			}
21820Sstevel@tonic-gate 			if (!checkonly) {
21830Sstevel@tonic-gate 				q->q_hiwat = *i1;
21840Sstevel@tonic-gate 			}
21850Sstevel@tonic-gate 			break;
21860Sstevel@tonic-gate 		case SO_RCVBUF:
21873448Sdh155122 			if (*i1 > is->is_max_buf) {
21880Sstevel@tonic-gate 				*outlenp = 0;
21890Sstevel@tonic-gate 				return (ENOBUFS);
21900Sstevel@tonic-gate 			}
21910Sstevel@tonic-gate 			if (!checkonly) {
21920Sstevel@tonic-gate 				RD(q)->q_hiwat = *i1;
21935240Snordmark 				rw_exit(&icmp->icmp_rwlock);
21940Sstevel@tonic-gate 				(void) mi_set_sth_hiwat(RD(q), *i1);
21955240Snordmark 				rw_enter(&icmp->icmp_rwlock, RW_WRITER);
21960Sstevel@tonic-gate 			}
21970Sstevel@tonic-gate 			break;
21980Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
21990Sstevel@tonic-gate 			if (!checkonly)
22000Sstevel@tonic-gate 				icmp->icmp_dgram_errind = onoff;
22010Sstevel@tonic-gate 			break;
22022263Ssommerfe 		case SO_ALLZONES:
22032263Ssommerfe 			/*
22042263Ssommerfe 			 * "soft" error (negative)
22052263Ssommerfe 			 * option not handled at this level
22062263Ssommerfe 			 * Note: Do not modify *outlenp
22072263Ssommerfe 			 */
22082263Ssommerfe 			return (-EINVAL);
22091673Sgt145670 		case SO_TIMESTAMP:
22101673Sgt145670 			if (!checkonly) {
22111673Sgt145670 				icmp->icmp_timestamp = onoff;
22121673Sgt145670 			}
22131673Sgt145670 			break;
22141676Sjpk 		case SO_MAC_EXEMPT:
22151676Sjpk 			if (secpolicy_net_mac_aware(cr) != 0 ||
22161676Sjpk 			    icmp->icmp_state != TS_UNBND)
22171676Sjpk 				return (EACCES);
22181676Sjpk 			if (!checkonly)
22191676Sjpk 				icmp->icmp_mac_exempt = onoff;
22201676Sjpk 			break;
22210Sstevel@tonic-gate 		/*
22220Sstevel@tonic-gate 		 * Following three not meaningful for icmp
22230Sstevel@tonic-gate 		 * Action is same as "default" so we keep them
22240Sstevel@tonic-gate 		 * in comments.
22250Sstevel@tonic-gate 		 * case SO_LINGER:
22260Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
22270Sstevel@tonic-gate 		 * case SO_OOBINLINE:
22280Sstevel@tonic-gate 		 */
22290Sstevel@tonic-gate 		default:
22300Sstevel@tonic-gate 			*outlenp = 0;
22310Sstevel@tonic-gate 			return (EINVAL);
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 		break;
22340Sstevel@tonic-gate 	case IPPROTO_IP:
22350Sstevel@tonic-gate 		/*
22360Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
22370Sstevel@tonic-gate 		 */
22380Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET) {
22390Sstevel@tonic-gate 			*outlenp = 0;
22400Sstevel@tonic-gate 			return (ENOPROTOOPT);
22410Sstevel@tonic-gate 		}
22420Sstevel@tonic-gate 		switch (name) {
22430Sstevel@tonic-gate 		case IP_OPTIONS:
22440Sstevel@tonic-gate 		case T_IP_OPTIONS:
22450Sstevel@tonic-gate 			/* Save options for use by IP. */
22461676Sjpk 			if ((inlen & 0x3) ||
22471676Sjpk 			    inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) {
22480Sstevel@tonic-gate 				*outlenp = 0;
22490Sstevel@tonic-gate 				return (EINVAL);
22500Sstevel@tonic-gate 			}
22510Sstevel@tonic-gate 			if (checkonly)
22520Sstevel@tonic-gate 				break;
22530Sstevel@tonic-gate 
22541676Sjpk 			if (!tsol_option_set(&icmp->icmp_ip_snd_options,
22551676Sjpk 			    &icmp->icmp_ip_snd_options_len,
22561676Sjpk 			    icmp->icmp_label_len, invalp, inlen)) {
22571676Sjpk 				*outlenp = 0;
22581676Sjpk 				return (ENOMEM);
22590Sstevel@tonic-gate 			}
22601676Sjpk 
22610Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
22620Sstevel@tonic-gate 			    icmp->icmp_ip_snd_options_len;
22635240Snordmark 			rw_exit(&icmp->icmp_rwlock);
22640Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
22655240Snordmark 			    is->is_wroff_extra);
22665240Snordmark 			rw_enter(&icmp->icmp_rwlock, RW_WRITER);
22670Sstevel@tonic-gate 			break;
22680Sstevel@tonic-gate 		case IP_HDRINCL:
22690Sstevel@tonic-gate 			if (!checkonly)
22700Sstevel@tonic-gate 				icmp->icmp_hdrincl = onoff;
22710Sstevel@tonic-gate 			break;
22720Sstevel@tonic-gate 		case IP_TOS:
22730Sstevel@tonic-gate 		case T_IP_TOS:
22740Sstevel@tonic-gate 			if (!checkonly) {
22750Sstevel@tonic-gate 				icmp->icmp_type_of_service = (uint8_t)*i1;
22760Sstevel@tonic-gate 			}
22770Sstevel@tonic-gate 			break;
22780Sstevel@tonic-gate 		case IP_TTL:
22790Sstevel@tonic-gate 			if (!checkonly) {
22800Sstevel@tonic-gate 				icmp->icmp_ttl = (uint8_t)*i1;
22810Sstevel@tonic-gate 			}
22820Sstevel@tonic-gate 			break;
22830Sstevel@tonic-gate 		case IP_MULTICAST_IF:
22840Sstevel@tonic-gate 			/*
22850Sstevel@tonic-gate 			 * TODO should check OPTMGMT reply and undo this if
22860Sstevel@tonic-gate 			 * there is an error.
22870Sstevel@tonic-gate 			 */
22880Sstevel@tonic-gate 			if (!checkonly)
22890Sstevel@tonic-gate 				icmp->icmp_multicast_if_addr = *i1;
22900Sstevel@tonic-gate 			break;
22910Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
22920Sstevel@tonic-gate 			if (!checkonly)
22930Sstevel@tonic-gate 				icmp->icmp_multicast_ttl = *invalp;
22940Sstevel@tonic-gate 			break;
22950Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
22960Sstevel@tonic-gate 			if (!checkonly) {
22975240Snordmark 				connp->conn_multicast_loop =
22980Sstevel@tonic-gate 				    (*invalp == 0) ? 0 : 1;
22990Sstevel@tonic-gate 			}
23000Sstevel@tonic-gate 			break;
23010Sstevel@tonic-gate 		case IP_BOUND_IF:
23020Sstevel@tonic-gate 			if (!checkonly)
23030Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
23040Sstevel@tonic-gate 			break;
23050Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
23060Sstevel@tonic-gate 			if (!checkonly)
23070Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
23080Sstevel@tonic-gate 			break;
23090Sstevel@tonic-gate 		case IP_XMIT_IF:
23100Sstevel@tonic-gate 			if (!checkonly)
23110Sstevel@tonic-gate 				icmp->icmp_xmit_if = *i1;
23120Sstevel@tonic-gate 			break;
23130Sstevel@tonic-gate 		case IP_RECVIF:
23140Sstevel@tonic-gate 			if (!checkonly)
23150Sstevel@tonic-gate 				icmp->icmp_recvif = onoff;
23165240Snordmark 			/*
23175240Snordmark 			 * pass to ip
23185240Snordmark 			 */
23195240Snordmark 			return (-EINVAL);
23203318Srshoaib 		case IP_PKTINFO: {
23213318Srshoaib 			/*
23223318Srshoaib 			 * This also handles IP_RECVPKTINFO.
23233318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
23243318Srshoaib 			 * Differentiation is based on the size of the argument
23253318Srshoaib 			 * passed in.
23263318Srshoaib 			 */
23273318Srshoaib 			struct in_pktinfo *pktinfop;
23283318Srshoaib 			ip4_pkt_t *attr_pktinfop;
23293318Srshoaib 
23303318Srshoaib 			if (checkonly)
23313318Srshoaib 				break;
23323318Srshoaib 
23333318Srshoaib 			if (inlen == sizeof (int)) {
23343318Srshoaib 				/*
23353318Srshoaib 				 * This is IP_RECVPKTINFO option.
23363318Srshoaib 				 * Keep a local copy of wether this option is
23373318Srshoaib 				 * set or not and pass it down to IP for
23383318Srshoaib 				 * processing.
23393318Srshoaib 				 */
23403318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
23413318Srshoaib 				return (-EINVAL);
23423318Srshoaib 			}
23433318Srshoaib 
23443318Srshoaib 
23453318Srshoaib 			if (inlen != sizeof (struct in_pktinfo))
23463318Srshoaib 				return (EINVAL);
23473318Srshoaib 
23483318Srshoaib 			if ((attr_pktinfop = (ip4_pkt_t *)thisdg_attrs)
23493318Srshoaib 			    == NULL) {
23503318Srshoaib 				/*
23513318Srshoaib 				 * sticky option is not supported
23523318Srshoaib 				 */
23533318Srshoaib 				return (EINVAL);
23543318Srshoaib 			}
23553318Srshoaib 
23563318Srshoaib 			pktinfop = (struct in_pktinfo *)invalp;
23573318Srshoaib 
23583318Srshoaib 			/*
23593318Srshoaib 			 * Atleast one of the values should be specified
23603318Srshoaib 			 */
23613318Srshoaib 			if (pktinfop->ipi_ifindex == 0 &&
23623318Srshoaib 			    pktinfop->ipi_spec_dst.s_addr == INADDR_ANY) {
23633318Srshoaib 				return (EINVAL);
23643318Srshoaib 			}
23653318Srshoaib 
23663318Srshoaib 			attr_pktinfop->ip4_addr = pktinfop->ipi_spec_dst.s_addr;
23673318Srshoaib 			attr_pktinfop->ip4_ill_index = pktinfop->ipi_ifindex;
23683318Srshoaib 		}
23693318Srshoaib 			break;
23700Sstevel@tonic-gate 		case IP_ADD_MEMBERSHIP:
23710Sstevel@tonic-gate 		case IP_DROP_MEMBERSHIP:
23720Sstevel@tonic-gate 		case IP_BLOCK_SOURCE:
23730Sstevel@tonic-gate 		case IP_UNBLOCK_SOURCE:
23740Sstevel@tonic-gate 		case IP_ADD_SOURCE_MEMBERSHIP:
23750Sstevel@tonic-gate 		case IP_DROP_SOURCE_MEMBERSHIP:
23760Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
23770Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
23780Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
23790Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
23800Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
23810Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
23820Sstevel@tonic-gate 		case MRT_INIT:
23830Sstevel@tonic-gate 		case MRT_DONE:
23840Sstevel@tonic-gate 		case MRT_ADD_VIF:
23850Sstevel@tonic-gate 		case MRT_DEL_VIF:
23860Sstevel@tonic-gate 		case MRT_ADD_MFC:
23870Sstevel@tonic-gate 		case MRT_DEL_MFC:
23880Sstevel@tonic-gate 		case MRT_VERSION:
23890Sstevel@tonic-gate 		case MRT_ASSERT:
23900Sstevel@tonic-gate 		case IP_SEC_OPT:
23910Sstevel@tonic-gate 		case IP_DONTFAILOVER_IF:
23921663Spriyanka 		case IP_NEXTHOP:
23930Sstevel@tonic-gate 			/*
23940Sstevel@tonic-gate 			 * "soft" error (negative)
23950Sstevel@tonic-gate 			 * option not handled at this level
23960Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
23970Sstevel@tonic-gate 			 */
23980Sstevel@tonic-gate 			return (-EINVAL);
23990Sstevel@tonic-gate 		default:
24000Sstevel@tonic-gate 			*outlenp = 0;
24010Sstevel@tonic-gate 			return (EINVAL);
24020Sstevel@tonic-gate 		}
24030Sstevel@tonic-gate 		break;
24040Sstevel@tonic-gate 	case IPPROTO_IPV6: {
24050Sstevel@tonic-gate 		ip6_pkt_t		*ipp;
24060Sstevel@tonic-gate 		boolean_t		sticky;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
24090Sstevel@tonic-gate 			*outlenp = 0;
24100Sstevel@tonic-gate 			return (ENOPROTOOPT);
24110Sstevel@tonic-gate 		}
24120Sstevel@tonic-gate 		/*
24130Sstevel@tonic-gate 		 * Deal with both sticky options and ancillary data
24140Sstevel@tonic-gate 		 */
24150Sstevel@tonic-gate 		if (thisdg_attrs == NULL) {
24160Sstevel@tonic-gate 			/* sticky options, or none */
24170Sstevel@tonic-gate 			ipp = &icmp->icmp_sticky_ipp;
24180Sstevel@tonic-gate 			sticky = B_TRUE;
24190Sstevel@tonic-gate 		} else {
24200Sstevel@tonic-gate 			/* ancillary data */
24210Sstevel@tonic-gate 			ipp = (ip6_pkt_t *)thisdg_attrs;
24220Sstevel@tonic-gate 			sticky = B_FALSE;
24230Sstevel@tonic-gate 		}
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 		switch (name) {
24260Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
24270Sstevel@tonic-gate 			if (!checkonly)
24280Sstevel@tonic-gate 				icmp->icmp_multicast_if_index = *i1;
24290Sstevel@tonic-gate 			break;
24300Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
24310Sstevel@tonic-gate 			/* -1 means use default */
24320Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
24330Sstevel@tonic-gate 				*outlenp = 0;
24340Sstevel@tonic-gate 				return (EINVAL);
24350Sstevel@tonic-gate 			}
24360Sstevel@tonic-gate 			if (!checkonly) {
24370Sstevel@tonic-gate 				if (*i1 == -1) {
2438679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
24393448Sdh155122 					    is->is_ipv6_hoplimit;
2440679Sseb 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
24410Sstevel@tonic-gate 					/* Pass modified value to IP. */
24420Sstevel@tonic-gate 					*i1 = ipp->ipp_hoplimit;
24430Sstevel@tonic-gate 				} else {
2444679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
24450Sstevel@tonic-gate 					    (uint8_t)*i1;
2446679Sseb 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
24470Sstevel@tonic-gate 				}
24480Sstevel@tonic-gate 				/* Rebuild the header template */
24495240Snordmark 				error = icmp_build_hdrs(icmp);
24500Sstevel@tonic-gate 				if (error != 0) {
24510Sstevel@tonic-gate 					*outlenp = 0;
24520Sstevel@tonic-gate 					return (error);
24530Sstevel@tonic-gate 				}
24540Sstevel@tonic-gate 			}
24550Sstevel@tonic-gate 			break;
24560Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
24570Sstevel@tonic-gate 			/* -1 means use default */
24580Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
24590Sstevel@tonic-gate 				*outlenp = 0;
24600Sstevel@tonic-gate 				return (EINVAL);
24610Sstevel@tonic-gate 			}
24620Sstevel@tonic-gate 			if (!checkonly) {
24630Sstevel@tonic-gate 				if (*i1 == -1) {
24640Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2465679Sseb 					    ipp->ipp_multicast_hops =
24660Sstevel@tonic-gate 					    IP_DEFAULT_MULTICAST_TTL;
2467679Sseb 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
24680Sstevel@tonic-gate 					/* Pass modified value to IP. */
2469679Sseb 					*i1 = icmp->icmp_multicast_ttl;
24700Sstevel@tonic-gate 				} else {
24710Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2472679Sseb 					    ipp->ipp_multicast_hops =
24730Sstevel@tonic-gate 					    (uint8_t)*i1;
2474679Sseb 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
24750Sstevel@tonic-gate 				}
24760Sstevel@tonic-gate 			}
24770Sstevel@tonic-gate 			break;
24780Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
24790Sstevel@tonic-gate 			if (*i1 != 0 && *i1 != 1) {
24800Sstevel@tonic-gate 				*outlenp = 0;
24810Sstevel@tonic-gate 				return (EINVAL);
24820Sstevel@tonic-gate 			}
24830Sstevel@tonic-gate 			if (!checkonly)
24845240Snordmark 				connp->conn_multicast_loop = *i1;
24850Sstevel@tonic-gate 			break;
24860Sstevel@tonic-gate 		case IPV6_CHECKSUM:
24870Sstevel@tonic-gate 			/*
24880Sstevel@tonic-gate 			 * Integer offset into the user data of where the
24890Sstevel@tonic-gate 			 * checksum is located.
24900Sstevel@tonic-gate 			 * Offset of -1 disables option.
24910Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6.
24920Sstevel@tonic-gate 			 */
24930Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
24940Sstevel@tonic-gate 				*outlenp = 0;
24950Sstevel@tonic-gate 				return (EINVAL);
24960Sstevel@tonic-gate 			}
24970Sstevel@tonic-gate 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
24980Sstevel@tonic-gate 				/* Negative or not 16 bit aligned offset */
24990Sstevel@tonic-gate 				*outlenp = 0;
25000Sstevel@tonic-gate 				return (EINVAL);
25010Sstevel@tonic-gate 			}
25020Sstevel@tonic-gate 			if (checkonly)
25030Sstevel@tonic-gate 				break;
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 			if (*i1 == -1) {
25060Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
25070Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
25080Sstevel@tonic-gate 			} else {
25090Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 1;
25100Sstevel@tonic-gate 				icmp->icmp_checksum_off = *i1;
25110Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
25120Sstevel@tonic-gate 			}
25130Sstevel@tonic-gate 			/* Rebuild the header template */
25145240Snordmark 			error = icmp_build_hdrs(icmp);
25150Sstevel@tonic-gate 			if (error != 0) {
25160Sstevel@tonic-gate 				*outlenp = 0;
25170Sstevel@tonic-gate 				return (error);
25180Sstevel@tonic-gate 			}
25190Sstevel@tonic-gate 			break;
25200Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
25210Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
25220Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
25230Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
25240Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
25250Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
25260Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
25270Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
25280Sstevel@tonic-gate 			/*
25290Sstevel@tonic-gate 			 * "soft" error (negative)
25300Sstevel@tonic-gate 			 * option not handled at this level
25310Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
25320Sstevel@tonic-gate 			 */
25330Sstevel@tonic-gate 			return (-EINVAL);
25340Sstevel@tonic-gate 		case IPV6_BOUND_IF:
25350Sstevel@tonic-gate 			if (!checkonly)
25360Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
25370Sstevel@tonic-gate 			break;
25380Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
25390Sstevel@tonic-gate 			if (!checkonly)
25400Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
25410Sstevel@tonic-gate 			break;
25420Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
25430Sstevel@tonic-gate 			if (!checkonly)
25440Sstevel@tonic-gate 				icmp->icmp_ipv6_recvtclass = onoff;
25450Sstevel@tonic-gate 			break;
25460Sstevel@tonic-gate 		/*
25470Sstevel@tonic-gate 		 * Set boolean switches for ancillary data delivery
25480Sstevel@tonic-gate 		 */
25490Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
25500Sstevel@tonic-gate 			if (!checkonly)
25513318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
25520Sstevel@tonic-gate 			break;
25530Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
25540Sstevel@tonic-gate 			if (!checkonly)
25550Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpathmtu = onoff;
25560Sstevel@tonic-gate 			break;
25570Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
25580Sstevel@tonic-gate 			if (!checkonly)
25590Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhoplimit = onoff;
25600Sstevel@tonic-gate 			break;
25610Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
25620Sstevel@tonic-gate 			if (!checkonly)
25630Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhopopts = onoff;
25640Sstevel@tonic-gate 			break;
25650Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
25660Sstevel@tonic-gate 			if (!checkonly)
25670Sstevel@tonic-gate 				icmp->icmp_ipv6_recvdstopts = onoff;
25680Sstevel@tonic-gate 			break;
25690Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
25700Sstevel@tonic-gate 			if (!checkonly)
25710Sstevel@tonic-gate 				icmp->icmp_old_ipv6_recvdstopts = onoff;
25720Sstevel@tonic-gate 			break;
25730Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
25740Sstevel@tonic-gate 			if (!checkonly)
25750Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrtdstopts = onoff;
25760Sstevel@tonic-gate 			break;
25770Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
25780Sstevel@tonic-gate 			if (!checkonly)
25790Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrthdr = onoff;
25800Sstevel@tonic-gate 			break;
25810Sstevel@tonic-gate 		/*
25820Sstevel@tonic-gate 		 * Set sticky options or ancillary data.
25830Sstevel@tonic-gate 		 * If sticky options, (re)build any extension headers
25840Sstevel@tonic-gate 		 * that might be needed as a result.
25850Sstevel@tonic-gate 		 */
25860Sstevel@tonic-gate 		case IPV6_PKTINFO:
25870Sstevel@tonic-gate 			/*
25880Sstevel@tonic-gate 			 * The source address and ifindex are verified
25890Sstevel@tonic-gate 			 * in ip_opt_set(). For ancillary data the
25900Sstevel@tonic-gate 			 * source address is checked in ip_wput_v6.
25910Sstevel@tonic-gate 			 */
25920Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
25930Sstevel@tonic-gate 				return (EINVAL);
25940Sstevel@tonic-gate 			if (checkonly)
25950Sstevel@tonic-gate 				break;
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 			if (inlen == 0) {
25980Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
25990Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |=
26000Sstevel@tonic-gate 				    (IPPF_IFINDEX|IPPF_ADDR);
26010Sstevel@tonic-gate 			} else {
26020Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
26050Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
26060Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
26070Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
26080Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
26090Sstevel@tonic-gate 				else
26100Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
26110Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
26120Sstevel@tonic-gate 				    &ipp->ipp_addr))
26130Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
26140Sstevel@tonic-gate 				else
26150Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
26160Sstevel@tonic-gate 			}
26170Sstevel@tonic-gate 			if (sticky) {
26185240Snordmark 				error = icmp_build_hdrs(icmp);
26190Sstevel@tonic-gate 				if (error != 0)
26200Sstevel@tonic-gate 					return (error);
26210Sstevel@tonic-gate 			}
26220Sstevel@tonic-gate 			break;
26230Sstevel@tonic-gate 		case IPV6_HOPLIMIT:
2624679Sseb 			/* This option can only be used as ancillary data. */
2625679Sseb 			if (sticky)
2626679Sseb 				return (EINVAL);
26270Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
26280Sstevel@tonic-gate 				return (EINVAL);
26290Sstevel@tonic-gate 			if (checkonly)
26300Sstevel@tonic-gate 				break;
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 			if (inlen == 0) {
26330Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
26340Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
26350Sstevel@tonic-gate 			} else {
26360Sstevel@tonic-gate 				if (*i1 > 255 || *i1 < -1)
26370Sstevel@tonic-gate 					return (EINVAL);
26380Sstevel@tonic-gate 				if (*i1 == -1)
26393448Sdh155122 					ipp->ipp_hoplimit =
26403448Sdh155122 					    is->is_ipv6_hoplimit;
26410Sstevel@tonic-gate 				else
26420Sstevel@tonic-gate 					ipp->ipp_hoplimit = *i1;
26430Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPLIMIT;
26440Sstevel@tonic-gate 			}
26450Sstevel@tonic-gate 			break;
26460Sstevel@tonic-gate 		case IPV6_TCLASS:
26470Sstevel@tonic-gate 			/*
26480Sstevel@tonic-gate 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
26490Sstevel@tonic-gate 			 * and [0, 255] as the actualy traffic class.
26500Sstevel@tonic-gate 			 */
26510Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
26520Sstevel@tonic-gate 				return (EINVAL);
26530Sstevel@tonic-gate 			if (checkonly)
26540Sstevel@tonic-gate 				break;
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 			if (inlen == 0) {
26570Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_TCLASS;
26580Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
26590Sstevel@tonic-gate 			} else {
26600Sstevel@tonic-gate 				if (*i1 >= 256 || *i1 < -1)
26610Sstevel@tonic-gate 					return (EINVAL);
26620Sstevel@tonic-gate 				if (*i1 == -1) {
26630Sstevel@tonic-gate 					ipp->ipp_tclass =
26640Sstevel@tonic-gate 					    IPV6_FLOW_TCLASS(
26650Sstevel@tonic-gate 					    IPV6_DEFAULT_VERS_AND_FLOW);
26660Sstevel@tonic-gate 				} else {
26670Sstevel@tonic-gate 					ipp->ipp_tclass = *i1;
26680Sstevel@tonic-gate 				}
26690Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_TCLASS;
26700Sstevel@tonic-gate 			}
26710Sstevel@tonic-gate 			if (sticky) {
26725240Snordmark 				error = icmp_build_hdrs(icmp);
26730Sstevel@tonic-gate 				if (error != 0)
26740Sstevel@tonic-gate 					return (error);
26750Sstevel@tonic-gate 			}
26760Sstevel@tonic-gate 			break;
26770Sstevel@tonic-gate 		case IPV6_NEXTHOP:
26780Sstevel@tonic-gate 			/*
26790Sstevel@tonic-gate 			 * IP will verify that the nexthop is reachable
26800Sstevel@tonic-gate 			 * and fail for sticky options.
26810Sstevel@tonic-gate 			 */
26820Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t))
26830Sstevel@tonic-gate 				return (EINVAL);
26840Sstevel@tonic-gate 			if (checkonly)
26850Sstevel@tonic-gate 				break;
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 			if (inlen == 0) {
26880Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
26890Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
26900Sstevel@tonic-gate 			} else {
26910Sstevel@tonic-gate 				sin6_t *sin6 = (sin6_t *)invalp;
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6)
26940Sstevel@tonic-gate 					return (EAFNOSUPPORT);
26950Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
26960Sstevel@tonic-gate 					return (EADDRNOTAVAIL);
26970Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
26980Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
26990Sstevel@tonic-gate 				    &ipp->ipp_nexthop))
27000Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
27010Sstevel@tonic-gate 				else
27020Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
27030Sstevel@tonic-gate 			}
27040Sstevel@tonic-gate 			if (sticky) {
27055240Snordmark 				error = icmp_build_hdrs(icmp);
27060Sstevel@tonic-gate 				if (error != 0)
27070Sstevel@tonic-gate 					return (error);
27080Sstevel@tonic-gate 			}
27090Sstevel@tonic-gate 			break;
27100Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
27110Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
27120Sstevel@tonic-gate 			/*
27130Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27140Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27150Sstevel@tonic-gate 			 */
27160Sstevel@tonic-gate 			if (inlen != 0 &&
27170Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1)))
27180Sstevel@tonic-gate 				return (EINVAL);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 			if (checkonly)
27210Sstevel@tonic-gate 				break;
27221676Sjpk 			error = optcom_pkt_set(invalp, inlen, sticky,
27231676Sjpk 			    (uchar_t **)&ipp->ipp_hopopts,
27241676Sjpk 			    &ipp->ipp_hopoptslen,
27251676Sjpk 			    sticky ? icmp->icmp_label_len_v6 : 0);
27261676Sjpk 			if (error != 0)
27271676Sjpk 				return (error);
27281676Sjpk 			if (ipp->ipp_hopoptslen == 0) {
27290Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
27300Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
27310Sstevel@tonic-gate 			} else {
27320Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
27330Sstevel@tonic-gate 			}
27340Sstevel@tonic-gate 			if (sticky) {
27355240Snordmark 				error = icmp_build_hdrs(icmp);
27360Sstevel@tonic-gate 				if (error != 0)
27370Sstevel@tonic-gate 					return (error);
27380Sstevel@tonic-gate 			}
27390Sstevel@tonic-gate 			break;
27400Sstevel@tonic-gate 		}
27410Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
27420Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 			/*
27450Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27460Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27470Sstevel@tonic-gate 			 */
27480Sstevel@tonic-gate 			if (inlen != 0 &&
27490Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
27500Sstevel@tonic-gate 				return (EINVAL);
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 			if (checkonly)
27530Sstevel@tonic-gate 				break;
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate 			if (inlen == 0) {
27560Sstevel@tonic-gate 				if (sticky &&
27570Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
27580Sstevel@tonic-gate 					kmem_free(ipp->ipp_rtdstopts,
27590Sstevel@tonic-gate 					    ipp->ipp_rtdstoptslen);
27600Sstevel@tonic-gate 					ipp->ipp_rtdstopts = NULL;
27610Sstevel@tonic-gate 					ipp->ipp_rtdstoptslen = 0;
27620Sstevel@tonic-gate 				}
27630Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
27640Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
27650Sstevel@tonic-gate 			} else {
27661676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
27670Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rtdstopts,
27681676Sjpk 				    &ipp->ipp_rtdstoptslen, 0);
27690Sstevel@tonic-gate 				if (error != 0)
27700Sstevel@tonic-gate 					return (error);
27710Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
27720Sstevel@tonic-gate 			}
27730Sstevel@tonic-gate 			if (sticky) {
27745240Snordmark 				error = icmp_build_hdrs(icmp);
27750Sstevel@tonic-gate 				if (error != 0)
27760Sstevel@tonic-gate 					return (error);
27770Sstevel@tonic-gate 			}
27780Sstevel@tonic-gate 			break;
27790Sstevel@tonic-gate 		}
27800Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
27810Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 			/*
27840Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27850Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27860Sstevel@tonic-gate 			 */
27870Sstevel@tonic-gate 			if (inlen != 0 &&
27880Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
27890Sstevel@tonic-gate 				return (EINVAL);
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 			if (checkonly)
27920Sstevel@tonic-gate 				break;
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 			if (inlen == 0) {
27950Sstevel@tonic-gate 				if (sticky &&
27960Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
27970Sstevel@tonic-gate 					kmem_free(ipp->ipp_dstopts,
27980Sstevel@tonic-gate 					    ipp->ipp_dstoptslen);
27990Sstevel@tonic-gate 					ipp->ipp_dstopts = NULL;
28000Sstevel@tonic-gate 					ipp->ipp_dstoptslen = 0;
28010Sstevel@tonic-gate 				}
28020Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
28030Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
28040Sstevel@tonic-gate 			} else {
28051676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
28060Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_dstopts,
28071676Sjpk 				    &ipp->ipp_dstoptslen, 0);
28080Sstevel@tonic-gate 				if (error != 0)
28090Sstevel@tonic-gate 					return (error);
28100Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
28110Sstevel@tonic-gate 			}
28120Sstevel@tonic-gate 			if (sticky) {
28135240Snordmark 				error = icmp_build_hdrs(icmp);
28140Sstevel@tonic-gate 				if (error != 0)
28150Sstevel@tonic-gate 					return (error);
28160Sstevel@tonic-gate 			}
28170Sstevel@tonic-gate 			break;
28180Sstevel@tonic-gate 		}
28190Sstevel@tonic-gate 		case IPV6_RTHDR: {
28200Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 			/*
28230Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
28240Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
28250Sstevel@tonic-gate 			 */
28260Sstevel@tonic-gate 			if (inlen != 0 &&
28270Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1)))
28280Sstevel@tonic-gate 				return (EINVAL);
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 			if (checkonly)
28310Sstevel@tonic-gate 				break;
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 			if (inlen == 0) {
28340Sstevel@tonic-gate 				if (sticky &&
28350Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
28360Sstevel@tonic-gate 					kmem_free(ipp->ipp_rthdr,
28370Sstevel@tonic-gate 					    ipp->ipp_rthdrlen);
28380Sstevel@tonic-gate 					ipp->ipp_rthdr = NULL;
28390Sstevel@tonic-gate 					ipp->ipp_rthdrlen = 0;
28400Sstevel@tonic-gate 				}
28410Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
28420Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
28430Sstevel@tonic-gate 			} else {
28441676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
28450Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rthdr,
28461676Sjpk 				    &ipp->ipp_rthdrlen, 0);
28470Sstevel@tonic-gate 				if (error != 0)
28480Sstevel@tonic-gate 					return (error);
28490Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
28500Sstevel@tonic-gate 			}
28510Sstevel@tonic-gate 			if (sticky) {
28525240Snordmark 				error = icmp_build_hdrs(icmp);
28530Sstevel@tonic-gate 				if (error != 0)
28540Sstevel@tonic-gate 					return (error);
28550Sstevel@tonic-gate 			}
28560Sstevel@tonic-gate 			break;
28570Sstevel@tonic-gate 		}
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate 		case IPV6_DONTFRAG:
28600Sstevel@tonic-gate 			if (checkonly)
28610Sstevel@tonic-gate 				break;
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 			if (onoff) {
28640Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DONTFRAG;
28650Sstevel@tonic-gate 			} else {
28660Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
28670Sstevel@tonic-gate 			}
28680Sstevel@tonic-gate 			break;
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 		case IPV6_USE_MIN_MTU:
28710Sstevel@tonic-gate 			if (inlen != sizeof (int))
28720Sstevel@tonic-gate 				return (EINVAL);
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > 1)
28750Sstevel@tonic-gate 				return (EINVAL);
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 			if (checkonly)
28780Sstevel@tonic-gate 				break;
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
28810Sstevel@tonic-gate 			ipp->ipp_use_min_mtu = *i1;
28820Sstevel@tonic-gate 			break;
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 		/*
28850Sstevel@tonic-gate 		 * This option can't be set.  Its only returned via
28860Sstevel@tonic-gate 		 * getsockopt() or ancillary data.
28870Sstevel@tonic-gate 		 */
28880Sstevel@tonic-gate 		case IPV6_PATHMTU:
28890Sstevel@tonic-gate 			return (EINVAL);
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate 		case IPV6_BOUND_PIF:
28920Sstevel@tonic-gate 		case IPV6_SEC_OPT:
28930Sstevel@tonic-gate 		case IPV6_DONTFAILOVER_IF:
28940Sstevel@tonic-gate 		case IPV6_SRC_PREFERENCES:
28950Sstevel@tonic-gate 		case IPV6_V6ONLY:
28960Sstevel@tonic-gate 			/* Handled at IP level */
28970Sstevel@tonic-gate 			return (-EINVAL);
28980Sstevel@tonic-gate 		default:
28990Sstevel@tonic-gate 			*outlenp = 0;
29000Sstevel@tonic-gate 			return (EINVAL);
29010Sstevel@tonic-gate 		}
29020Sstevel@tonic-gate 		break;
29030Sstevel@tonic-gate 	}		/* end IPPROTO_IPV6 */
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
29060Sstevel@tonic-gate 		/*
29070Sstevel@tonic-gate 		 * Only allow IPv6 option processing on IPv6 sockets.
29080Sstevel@tonic-gate 		 */
29090Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
29100Sstevel@tonic-gate 			*outlenp = 0;
29110Sstevel@tonic-gate 			return (ENOPROTOOPT);
29120Sstevel@tonic-gate 		}
29130Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
29140Sstevel@tonic-gate 			*outlenp = 0;
29150Sstevel@tonic-gate 			return (ENOPROTOOPT);
29160Sstevel@tonic-gate 		}
29170Sstevel@tonic-gate 		switch (name) {
29180Sstevel@tonic-gate 		case ICMP6_FILTER:
29190Sstevel@tonic-gate 			if (!checkonly) {
29200Sstevel@tonic-gate 				if ((inlen != 0) &&
29210Sstevel@tonic-gate 				    (inlen != sizeof (icmp6_filter_t)))
29220Sstevel@tonic-gate 					return (EINVAL);
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 				if (inlen == 0) {
29250Sstevel@tonic-gate 					if (icmp->icmp_filter != NULL) {
29260Sstevel@tonic-gate 						kmem_free(icmp->icmp_filter,
29270Sstevel@tonic-gate 						    sizeof (icmp6_filter_t));
29280Sstevel@tonic-gate 						icmp->icmp_filter = NULL;
29290Sstevel@tonic-gate 					}
29300Sstevel@tonic-gate 				} else {
29310Sstevel@tonic-gate 					if (icmp->icmp_filter == NULL) {
29320Sstevel@tonic-gate 						icmp->icmp_filter = kmem_alloc(
29330Sstevel@tonic-gate 						    sizeof (icmp6_filter_t),
29340Sstevel@tonic-gate 						    KM_NOSLEEP);
29350Sstevel@tonic-gate 						if (icmp->icmp_filter == NULL) {
29360Sstevel@tonic-gate 							*outlenp = 0;
29370Sstevel@tonic-gate 							return (ENOBUFS);
29380Sstevel@tonic-gate 						}
29390Sstevel@tonic-gate 					}
29400Sstevel@tonic-gate 					(void) bcopy(invalp, icmp->icmp_filter,
29410Sstevel@tonic-gate 					    inlen);
29420Sstevel@tonic-gate 				}
29430Sstevel@tonic-gate 			}
29440Sstevel@tonic-gate 			break;
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 		default:
29470Sstevel@tonic-gate 			*outlenp = 0;
29480Sstevel@tonic-gate 			return (EINVAL);
29490Sstevel@tonic-gate 		}
29500Sstevel@tonic-gate 		break;
29510Sstevel@tonic-gate 	default:
29520Sstevel@tonic-gate 		*outlenp = 0;
29530Sstevel@tonic-gate 		return (EINVAL);
29540Sstevel@tonic-gate 	}
29550Sstevel@tonic-gate 	/*
29560Sstevel@tonic-gate 	 * Common case of OK return with outval same as inval.
29570Sstevel@tonic-gate 	 */
29580Sstevel@tonic-gate 	if (invalp != outvalp) {
29590Sstevel@tonic-gate 		/* don't trust bcopy for identical src/dst */
29600Sstevel@tonic-gate 		(void) bcopy(invalp, outvalp, inlen);
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 	*outlenp = inlen;
29630Sstevel@tonic-gate 	return (0);
29640Sstevel@tonic-gate }
29655240Snordmark /* This routine sets socket options. */
29665240Snordmark /* ARGSUSED */
29675240Snordmark int
29685240Snordmark icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
29695240Snordmark     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
29705240Snordmark     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
29715240Snordmark {
29725240Snordmark 	icmp_t	*icmp;
29735240Snordmark 	int	err;
29745240Snordmark 
29755240Snordmark 	icmp = Q_TO_ICMP(q);
29765240Snordmark 
29775240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
29785240Snordmark 	err = icmp_opt_set_locked(q, optset_context, level, name, inlen, invalp,
29795240Snordmark 	    outlenp, outvalp, thisdg_attrs, cr, mblk);
29805240Snordmark 	rw_exit(&icmp->icmp_rwlock);
29815240Snordmark 	return (err);
29825240Snordmark }
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate /*
29850Sstevel@tonic-gate  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
29860Sstevel@tonic-gate  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
29870Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
29880Sstevel@tonic-gate  * headers.
29890Sstevel@tonic-gate  * Returns failure if can't allocate memory.
29900Sstevel@tonic-gate  */
29910Sstevel@tonic-gate static int
29925240Snordmark icmp_build_hdrs(icmp_t *icmp)
29930Sstevel@tonic-gate {
29943448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
29950Sstevel@tonic-gate 	uchar_t	*hdrs;
29960Sstevel@tonic-gate 	uint_t	hdrs_len;
29970Sstevel@tonic-gate 	ip6_t	*ip6h;
29980Sstevel@tonic-gate 	ip6i_t	*ip6i;
29990Sstevel@tonic-gate 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
30000Sstevel@tonic-gate 
30015240Snordmark 	ASSERT(RW_WRITE_HELD(&icmp->icmp_rwlock));
30020Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp);
30030Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
30040Sstevel@tonic-gate 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
30050Sstevel@tonic-gate 		/* Need to reallocate */
30060Sstevel@tonic-gate 		if (hdrs_len != 0) {
30070Sstevel@tonic-gate 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
30080Sstevel@tonic-gate 			if (hdrs == NULL)
30090Sstevel@tonic-gate 				return (ENOMEM);
30100Sstevel@tonic-gate 		} else {
30110Sstevel@tonic-gate 			hdrs = NULL;
30120Sstevel@tonic-gate 		}
30130Sstevel@tonic-gate 		if (icmp->icmp_sticky_hdrs_len != 0) {
30140Sstevel@tonic-gate 			kmem_free(icmp->icmp_sticky_hdrs,
30150Sstevel@tonic-gate 			    icmp->icmp_sticky_hdrs_len);
30160Sstevel@tonic-gate 		}
30170Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = hdrs;
30180Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = hdrs_len;
30190Sstevel@tonic-gate 	}
30200Sstevel@tonic-gate 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
30210Sstevel@tonic-gate 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 	/* Set header fields not in ipp */
30240Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
30250Sstevel@tonic-gate 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
30260Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
30290Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
30300Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
30310Sstevel@tonic-gate 		}
30320Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
30330Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
30340Sstevel@tonic-gate 		}
30350Sstevel@tonic-gate 	} else {
30360Sstevel@tonic-gate 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
30370Sstevel@tonic-gate 	}
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_ADDR))
30400Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	/* Try to get everything in a single mblk */
30430Sstevel@tonic-gate 	if (hdrs_len > icmp->icmp_max_hdr_len) {
30440Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = hdrs_len;
30455240Snordmark 		rw_exit(&icmp->icmp_rwlock);
30465240Snordmark 		(void) mi_set_sth_wroff(icmp->icmp_connp->conn_rq,
30475240Snordmark 		    icmp->icmp_max_hdr_len + is->is_wroff_extra);
30485240Snordmark 		rw_enter(&icmp->icmp_rwlock, RW_WRITER);
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate 	return (0);
30510Sstevel@tonic-gate }
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate /*
30540Sstevel@tonic-gate  * This routine retrieves the value of an ND variable in a icmpparam_t
30550Sstevel@tonic-gate  * structure.  It is called through nd_getset when a user reads the
30560Sstevel@tonic-gate  * variable.
30570Sstevel@tonic-gate  */
30580Sstevel@tonic-gate /* ARGSUSED */
30590Sstevel@tonic-gate static int
30600Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
30610Sstevel@tonic-gate {
30620Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
30650Sstevel@tonic-gate 	return (0);
30660Sstevel@tonic-gate }
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate /*
30690Sstevel@tonic-gate  * Walk through the param array specified registering each element with the
30700Sstevel@tonic-gate  * named dispatch (ND) handler.
30710Sstevel@tonic-gate  */
30720Sstevel@tonic-gate static boolean_t
30733448Sdh155122 icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt)
30740Sstevel@tonic-gate {
30750Sstevel@tonic-gate 	for (; cnt-- > 0; icmppa++) {
30760Sstevel@tonic-gate 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
30773448Sdh155122 			if (!nd_load(ndp, icmppa->icmp_param_name,
30780Sstevel@tonic-gate 			    icmp_param_get, icmp_param_set,
30790Sstevel@tonic-gate 			    (caddr_t)icmppa)) {
30803448Sdh155122 				nd_free(ndp);
30810Sstevel@tonic-gate 				return (B_FALSE);
30820Sstevel@tonic-gate 			}
30830Sstevel@tonic-gate 		}
30840Sstevel@tonic-gate 	}
30853448Sdh155122 	if (!nd_load(ndp, "icmp_status", icmp_status_report, NULL,
30860Sstevel@tonic-gate 	    NULL)) {
30873448Sdh155122 		nd_free(ndp);
30880Sstevel@tonic-gate 		return (B_FALSE);
30890Sstevel@tonic-gate 	}
30900Sstevel@tonic-gate 	return (B_TRUE);
30910Sstevel@tonic-gate }
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */
30940Sstevel@tonic-gate /* ARGSUSED */
30950Sstevel@tonic-gate static int
30960Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
30970Sstevel@tonic-gate {
30980Sstevel@tonic-gate 	long		new_value;
30990Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 	/*
31020Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
31030Sstevel@tonic-gate 	 * required bounds.
31040Sstevel@tonic-gate 	 */
31050Sstevel@tonic-gate 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
31060Sstevel@tonic-gate 	    new_value < icmppa->icmp_param_min ||
31070Sstevel@tonic-gate 	    new_value > icmppa->icmp_param_max) {
31080Sstevel@tonic-gate 		return (EINVAL);
31090Sstevel@tonic-gate 	}
31100Sstevel@tonic-gate 	/* Set the new value */
31110Sstevel@tonic-gate 	icmppa->icmp_param_value = new_value;
31120Sstevel@tonic-gate 	return (0);
31130Sstevel@tonic-gate }
31145240Snordmark /*ARGSUSED2*/
31150Sstevel@tonic-gate static void
31165240Snordmark icmp_input(void *arg1, mblk_t *mp, void *arg2)
31170Sstevel@tonic-gate {
31185240Snordmark 	conn_t *connp = (conn_t *)arg1;
31190Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
31200Sstevel@tonic-gate 	uchar_t			*rptr;
31215240Snordmark 	icmp_t			*icmp;
31225240Snordmark 	icmp_stack_t		*is;
31230Sstevel@tonic-gate 	sin_t			*sin;
31240Sstevel@tonic-gate 	sin6_t			*sin6;
31250Sstevel@tonic-gate 	ip6_t			*ip6h;
31260Sstevel@tonic-gate 	ip6i_t			*ip6i;
31270Sstevel@tonic-gate 	mblk_t			*mp1;
31280Sstevel@tonic-gate 	int			hdr_len;
31290Sstevel@tonic-gate 	ipha_t			*ipha;
31300Sstevel@tonic-gate 	int			udi_size;	/* Size of T_unitdata_ind */
31310Sstevel@tonic-gate 	uint_t			ipvers;
31320Sstevel@tonic-gate 	ip6_pkt_t		ipp;
31330Sstevel@tonic-gate 	uint8_t			nexthdr;
31343318Srshoaib 	ip_pktinfo_t		*pinfo = NULL;
31350Sstevel@tonic-gate 	mblk_t			*options_mp = NULL;
31360Sstevel@tonic-gate 	uint_t			icmp_opt = 0;
31370Sstevel@tonic-gate 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
31381676Sjpk 	uint_t			hopstrip;
31390Sstevel@tonic-gate 
31405240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
31415240Snordmark 
31425240Snordmark 	icmp = connp->conn_icmp;
31435240Snordmark 	is = icmp->icmp_is;
31445240Snordmark 	rptr = mp->b_rptr;
31455240Snordmark 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_CTL);
31465240Snordmark 	ASSERT(OK_32PTR(rptr));
31475240Snordmark 
31485240Snordmark 	/*
31495240Snordmark 	 * IP should have prepended the options data in an M_CTL
31505240Snordmark 	 * Check M_CTL "type" to make sure are not here bcos of
31515240Snordmark 	 * a valid ICMP message
31525240Snordmark 	 */
31535240Snordmark 	if (DB_TYPE(mp) == M_CTL) {
31540Sstevel@tonic-gate 		/*
31555240Snordmark 		 * FIXME: does IP still do this?
31560Sstevel@tonic-gate 		 * IP sends up the IPSEC_IN message for handling IPSEC
31570Sstevel@tonic-gate 		 * policy at the TCP level. We don't need it here.
31580Sstevel@tonic-gate 		 */
31590Sstevel@tonic-gate 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
31600Sstevel@tonic-gate 			mp1 = mp->b_cont;
31610Sstevel@tonic-gate 			freeb(mp);
31620Sstevel@tonic-gate 			mp = mp1;
31635240Snordmark 			rptr = mp->b_rptr;
31645240Snordmark 		} else if (MBLKL(mp) == sizeof (ip_pktinfo_t) &&
31655240Snordmark 		    ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type ==
31665240Snordmark 		    IN_PKTINFO) {
31675240Snordmark 			/*
31685240Snordmark 			 * IP_RECVIF or IP_RECVSLLA or IPF_RECVADDR information
31695240Snordmark 			 * has been prepended to the packet by IP. We need to
31705240Snordmark 			 * extract the mblk and adjust the rptr
31715240Snordmark 			 */
31723318Srshoaib 			pinfo = (ip_pktinfo_t *)mp->b_rptr;
31735240Snordmark 			options_mp = mp;
31745240Snordmark 			mp = mp->b_cont;
31755240Snordmark 			rptr = mp->b_rptr;
31765240Snordmark 		} else {
31775240Snordmark 			/*
31785240Snordmark 			 * ICMP messages.
31795240Snordmark 			 */
31805240Snordmark 			icmp_icmp_error(connp->conn_rq, mp);
31810Sstevel@tonic-gate 			return;
31820Sstevel@tonic-gate 		}
31830Sstevel@tonic-gate 	}
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	/*
31860Sstevel@tonic-gate 	 * Discard message if it is misaligned or smaller than the IP header.
31870Sstevel@tonic-gate 	 */
31880Sstevel@tonic-gate 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
31890Sstevel@tonic-gate 		freemsg(mp);
31900Sstevel@tonic-gate 		if (options_mp != NULL)
31910Sstevel@tonic-gate 			freeb(options_mp);
31925240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
31930Sstevel@tonic-gate 		return;
31940Sstevel@tonic-gate 	}
31950Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 	/* Handle M_DATA messages containing IP packets messages */
31980Sstevel@tonic-gate 	if (ipvers == IPV4_VERSION) {
31990Sstevel@tonic-gate 		/*
32000Sstevel@tonic-gate 		 * Special case where IP attaches
32010Sstevel@tonic-gate 		 * the IRE needs to be handled so that we don't send up
32020Sstevel@tonic-gate 		 * IRE to the user land.
32030Sstevel@tonic-gate 		 */
32040Sstevel@tonic-gate 		ipha = (ipha_t *)rptr;
32050Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 		if (ipha->ipha_protocol == IPPROTO_TCP) {
32080Sstevel@tonic-gate 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
32110Sstevel@tonic-gate 			    TH_SYN) && mp->b_cont != NULL) {
32120Sstevel@tonic-gate 				mp1 = mp->b_cont;
32130Sstevel@tonic-gate 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
32140Sstevel@tonic-gate 					freeb(mp1);
32150Sstevel@tonic-gate 					mp->b_cont = NULL;
32160Sstevel@tonic-gate 				}
32170Sstevel@tonic-gate 			}
32180Sstevel@tonic-gate 		}
32193448Sdh155122 		if (is->is_bsd_compat) {
32200Sstevel@tonic-gate 			ushort_t len;
32210Sstevel@tonic-gate 			len = ntohs(ipha->ipha_length);
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 			if (mp->b_datap->db_ref > 1) {
32240Sstevel@tonic-gate 				/*
32250Sstevel@tonic-gate 				 * Allocate a new IP header so that we can
32260Sstevel@tonic-gate 				 * modify ipha_length.
32270Sstevel@tonic-gate 				 */
32280Sstevel@tonic-gate 				mblk_t	*mp1;
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 				mp1 = allocb(hdr_len, BPRI_MED);
32310Sstevel@tonic-gate 				if (!mp1) {
32320Sstevel@tonic-gate 					freemsg(mp);
32330Sstevel@tonic-gate 					if (options_mp != NULL)
32340Sstevel@tonic-gate 						freeb(options_mp);
32355240Snordmark 					BUMP_MIB(&is->is_rawip_mib,
32363448Sdh155122 					    rawipInErrors);
32370Sstevel@tonic-gate 					return;
32380Sstevel@tonic-gate 				}
32390Sstevel@tonic-gate 				bcopy(rptr, mp1->b_rptr, hdr_len);
32400Sstevel@tonic-gate 				mp->b_rptr = rptr + hdr_len;
32410Sstevel@tonic-gate 				rptr = mp1->b_rptr;
32420Sstevel@tonic-gate 				ipha = (ipha_t *)rptr;
32430Sstevel@tonic-gate 				mp1->b_cont = mp;
32440Sstevel@tonic-gate 				mp1->b_wptr = rptr + hdr_len;
32450Sstevel@tonic-gate 				mp = mp1;
32460Sstevel@tonic-gate 			}
32470Sstevel@tonic-gate 			len -= hdr_len;
32480Sstevel@tonic-gate 			ipha->ipha_length = htons(len);
32490Sstevel@tonic-gate 		}
32500Sstevel@tonic-gate 	}
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 	/*
32530Sstevel@tonic-gate 	 * This is the inbound data path.  Packets are passed upstream as
32540Sstevel@tonic-gate 	 * T_UNITDATA_IND messages with full IP headers still attached.
32550Sstevel@tonic-gate 	 */
32560Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
32570Sstevel@tonic-gate 		ASSERT(ipvers == IPV4_VERSION);
32580Sstevel@tonic-gate 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
32595267Snordmark 		if (icmp->icmp_recvif && (pinfo != NULL) &&
32603318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
32610Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
32620Sstevel@tonic-gate 			    sizeof (uint_t);
32630Sstevel@tonic-gate 		}
32643318Srshoaib 
32655267Snordmark 		if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) &&
32663318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
32673318Srshoaib 			udi_size += sizeof (struct T_opthdr) +
32683318Srshoaib 			    sizeof (struct in_pktinfo);
32693318Srshoaib 		}
32703318Srshoaib 
32711673Sgt145670 		/*
32721673Sgt145670 		 * If SO_TIMESTAMP is set allocate the appropriate sized
32731673Sgt145670 		 * buffer. Since gethrestime() expects a pointer aligned
32741673Sgt145670 		 * argument, we allocate space necessary for extra
32751673Sgt145670 		 * alignment (even though it might not be used).
32761673Sgt145670 		 */
32771673Sgt145670 		if (icmp->icmp_timestamp) {
32781673Sgt145670 			udi_size += sizeof (struct T_opthdr) +
32791673Sgt145670 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
32801673Sgt145670 		}
32810Sstevel@tonic-gate 		mp1 = allocb(udi_size, BPRI_MED);
32820Sstevel@tonic-gate 		if (mp1 == NULL) {
32830Sstevel@tonic-gate 			freemsg(mp);
32840Sstevel@tonic-gate 			if (options_mp != NULL)
32850Sstevel@tonic-gate 				freeb(options_mp);
32865240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
32870Sstevel@tonic-gate 			return;
32880Sstevel@tonic-gate 		}
32890Sstevel@tonic-gate 		mp1->b_cont = mp;
32900Sstevel@tonic-gate 		mp = mp1;
32910Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
32920Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
32930Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)tudi + udi_size;
32940Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
32950Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin_t);
32960Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
32970Sstevel@tonic-gate 		sin = (sin_t *)&tudi[1];
32980Sstevel@tonic-gate 		*sin = sin_null;
32990Sstevel@tonic-gate 		sin->sin_family = AF_INET;
33000Sstevel@tonic-gate 		sin->sin_addr.s_addr = ipha->ipha_src;
33010Sstevel@tonic-gate 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
33020Sstevel@tonic-gate 		    sizeof (sin_t);
33030Sstevel@tonic-gate 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
33040Sstevel@tonic-gate 		tudi->OPT_length = udi_size;
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 		/*
33070Sstevel@tonic-gate 		 * Add options if IP_RECVIF is set
33080Sstevel@tonic-gate 		 */
33090Sstevel@tonic-gate 		if (udi_size != 0) {
33100Sstevel@tonic-gate 			char *dstopt;
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 			dstopt = (char *)&sin[1];
33135267Snordmark 			if (icmp->icmp_recvif && (pinfo != NULL) &&
33143318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 				struct T_opthdr *toh;
33170Sstevel@tonic-gate 				uint_t		*dstptr;
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 				toh = (struct T_opthdr *)dstopt;
33200Sstevel@tonic-gate 				toh->level = IPPROTO_IP;
33210Sstevel@tonic-gate 				toh->name = IP_RECVIF;
33220Sstevel@tonic-gate 				toh->len = sizeof (struct T_opthdr) +
33235240Snordmark 				    sizeof (uint_t);
33240Sstevel@tonic-gate 				toh->status = 0;
33250Sstevel@tonic-gate 				dstopt += sizeof (struct T_opthdr);
33260Sstevel@tonic-gate 				dstptr = (uint_t *)dstopt;
33273318Srshoaib 				*dstptr = pinfo->ip_pkt_ifindex;
33280Sstevel@tonic-gate 				dstopt += sizeof (uint_t);
33290Sstevel@tonic-gate 				udi_size -= toh->len;
33300Sstevel@tonic-gate 			}
33311673Sgt145670 			if (icmp->icmp_timestamp) {
33321673Sgt145670 				struct	T_opthdr *toh;
33331673Sgt145670 
33341673Sgt145670 				toh = (struct T_opthdr *)dstopt;
33351673Sgt145670 				toh->level = SOL_SOCKET;
33361673Sgt145670 				toh->name = SCM_TIMESTAMP;
33371673Sgt145670 				toh->len = sizeof (struct T_opthdr) +
33381673Sgt145670 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
33391673Sgt145670 				toh->status = 0;
33401673Sgt145670 				dstopt += sizeof (struct T_opthdr);
33411673Sgt145670 				/* Align for gethrestime() */
33421673Sgt145670 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
33431673Sgt145670 				    sizeof (intptr_t));
33441673Sgt145670 				gethrestime((timestruc_t *)dstopt);
33453318Srshoaib 				dstopt = (char *)toh + toh->len;
33463318Srshoaib 				udi_size -= toh->len;
33473318Srshoaib 			}
33485267Snordmark 			if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) &&
33493318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
33503318Srshoaib 				struct	T_opthdr *toh;
33513318Srshoaib 				struct	in_pktinfo *pktinfop;
33523318Srshoaib 
33533318Srshoaib 				toh = (struct T_opthdr *)dstopt;
33543318Srshoaib 				toh->level = IPPROTO_IP;
33553318Srshoaib 				toh->name = IP_PKTINFO;
33563318Srshoaib 				toh->len = sizeof (struct T_opthdr) +
33573318Srshoaib 				    sizeof (in_pktinfo_t);
33583318Srshoaib 				toh->status = 0;
33593318Srshoaib 				dstopt += sizeof (struct T_opthdr);
33603318Srshoaib 				pktinfop = (struct in_pktinfo *)dstopt;
33613318Srshoaib 				pktinfop->ipi_ifindex = pinfo->ip_pkt_ifindex;
33623318Srshoaib 				pktinfop->ipi_spec_dst =
33633318Srshoaib 				    pinfo->ip_pkt_match_addr;
33643318Srshoaib 
33653318Srshoaib 				pktinfop->ipi_addr.s_addr = ipha->ipha_dst;
33663318Srshoaib 
33673318Srshoaib 				dstopt += sizeof (struct in_pktinfo);
33681673Sgt145670 				udi_size -= toh->len;
33691673Sgt145670 			}
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 			/* Consumed all of allocated space */
33720Sstevel@tonic-gate 			ASSERT(udi_size == 0);
33730Sstevel@tonic-gate 		}
33740Sstevel@tonic-gate 
33755267Snordmark 		if (options_mp != NULL)
33765267Snordmark 			freeb(options_mp);
33775267Snordmark 
33785240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
33795240Snordmark 		putnext(connp->conn_rq, mp);
33800Sstevel@tonic-gate 		return;
33810Sstevel@tonic-gate 	}
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	/*
33840Sstevel@tonic-gate 	 * We don't need options_mp in the IPv6 path.
33850Sstevel@tonic-gate 	 */
33860Sstevel@tonic-gate 	if (options_mp != NULL) {
33870Sstevel@tonic-gate 		freeb(options_mp);
33880Sstevel@tonic-gate 		options_mp = NULL;
33890Sstevel@tonic-gate 	}
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 	/*
33920Sstevel@tonic-gate 	 * Discard message if it is smaller than the IPv6 header
33930Sstevel@tonic-gate 	 * or if the header is malformed.
33940Sstevel@tonic-gate 	 */
33950Sstevel@tonic-gate 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
33960Sstevel@tonic-gate 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
33970Sstevel@tonic-gate 	    icmp->icmp_family != AF_INET6) {
33980Sstevel@tonic-gate 		freemsg(mp);
33995240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
34000Sstevel@tonic-gate 		return;
34010Sstevel@tonic-gate 	}
34020Sstevel@tonic-gate 
34030Sstevel@tonic-gate 	/* Initialize */
34040Sstevel@tonic-gate 	ipp.ipp_fields = 0;
34051676Sjpk 	hopstrip = 0;
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 	ip6h = (ip6_t *)rptr;
34080Sstevel@tonic-gate 	/*
34090Sstevel@tonic-gate 	 * Call on ip_find_hdr_v6 which gets the total hdr len
34100Sstevel@tonic-gate 	 * as well as individual lenghts of ext hdrs (and ptrs to
34110Sstevel@tonic-gate 	 * them).
34120Sstevel@tonic-gate 	 */
34130Sstevel@tonic-gate 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
34140Sstevel@tonic-gate 		/* Look for ifindex information */
34150Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
34160Sstevel@tonic-gate 			ip6i = (ip6i_t *)ip6h;
34170Sstevel@tonic-gate 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
34180Sstevel@tonic-gate 				ASSERT(ip6i->ip6i_ifindex != 0);
34190Sstevel@tonic-gate 				ipp.ipp_fields |= IPPF_IFINDEX;
34200Sstevel@tonic-gate 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
34210Sstevel@tonic-gate 			}
34220Sstevel@tonic-gate 			rptr = (uchar_t *)&ip6i[1];
34230Sstevel@tonic-gate 			mp->b_rptr = rptr;
34240Sstevel@tonic-gate 			if (rptr == mp->b_wptr) {
34250Sstevel@tonic-gate 				mp1 = mp->b_cont;
34260Sstevel@tonic-gate 				freeb(mp);
34270Sstevel@tonic-gate 				mp = mp1;
34280Sstevel@tonic-gate 				rptr = mp->b_rptr;
34290Sstevel@tonic-gate 			}
34300Sstevel@tonic-gate 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
34310Sstevel@tonic-gate 			ip6h = (ip6_t *)rptr;
34320Sstevel@tonic-gate 		}
34330Sstevel@tonic-gate 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
34341676Sjpk 
34351676Sjpk 		/*
34361676Sjpk 		 * We need to lie a bit to the user because users inside
34371676Sjpk 		 * labeled compartments should not see their own labels.  We
34381676Sjpk 		 * assume that in all other respects IP has checked the label,
34391676Sjpk 		 * and that the label is always first among the options.  (If
34401676Sjpk 		 * it's not first, then this code won't see it, and the option
34411676Sjpk 		 * will be passed along to the user.)
34421676Sjpk 		 *
34431676Sjpk 		 * If we had multilevel ICMP sockets, then the following code
34441676Sjpk 		 * should be skipped for them to allow the user to see the
34451676Sjpk 		 * label.
34461676Sjpk 		 *
34471676Sjpk 		 * Alignment restrictions in the definition of IP options
34481676Sjpk 		 * (namely, the requirement that the 4-octet DOI goes on a
34491676Sjpk 		 * 4-octet boundary) mean that we know exactly where the option
34501676Sjpk 		 * should start, but we're lenient for other hosts.
34511676Sjpk 		 *
34521676Sjpk 		 * Note that there are no multilevel ICMP or raw IP sockets
34531676Sjpk 		 * yet, thus nobody ever sees the IP6OPT_LS option.
34541676Sjpk 		 */
34551676Sjpk 		if ((ipp.ipp_fields & IPPF_HOPOPTS) &&
34561676Sjpk 		    ipp.ipp_hopoptslen > 5 && is_system_labeled()) {
34571676Sjpk 			const uchar_t *ucp =
34581676Sjpk 			    (const uchar_t *)ipp.ipp_hopopts + 2;
34591676Sjpk 			int remlen = ipp.ipp_hopoptslen - 2;
34601676Sjpk 
34611676Sjpk 			while (remlen > 0) {
34621676Sjpk 				if (*ucp == IP6OPT_PAD1) {
34631676Sjpk 					remlen--;
34641676Sjpk 					ucp++;
34651676Sjpk 				} else if (*ucp == IP6OPT_PADN) {
34661676Sjpk 					remlen -= ucp[1] + 2;
34671676Sjpk 					ucp += ucp[1] + 2;
34681676Sjpk 				} else if (*ucp == ip6opt_ls) {
34691676Sjpk 					hopstrip = (ucp -
34701676Sjpk 					    (const uchar_t *)ipp.ipp_hopopts) +
34711676Sjpk 					    ucp[1] + 2;
34721676Sjpk 					hopstrip = (hopstrip + 7) & ~7;
34731676Sjpk 					break;
34741676Sjpk 				} else {
34751676Sjpk 					/* label option must be first */
34761676Sjpk 					break;
34771676Sjpk 				}
34781676Sjpk 			}
34791676Sjpk 		}
34800Sstevel@tonic-gate 	} else {
34810Sstevel@tonic-gate 		hdr_len = IPV6_HDR_LEN;
34820Sstevel@tonic-gate 		ip6i = NULL;
34830Sstevel@tonic-gate 		nexthdr = ip6h->ip6_nxt;
34840Sstevel@tonic-gate 	}
34850Sstevel@tonic-gate 	/*
34860Sstevel@tonic-gate 	 * One special case where IP attaches the IRE needs to
34870Sstevel@tonic-gate 	 * be handled so that we don't send up IRE to the user land.
34880Sstevel@tonic-gate 	 */
34890Sstevel@tonic-gate 	if (nexthdr == IPPROTO_TCP) {
34900Sstevel@tonic-gate 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
34930Sstevel@tonic-gate 		    mp->b_cont != NULL) {
34940Sstevel@tonic-gate 			mp1 = mp->b_cont;
34950Sstevel@tonic-gate 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
34960Sstevel@tonic-gate 				freeb(mp1);
34970Sstevel@tonic-gate 				mp->b_cont = NULL;
34980Sstevel@tonic-gate 			}
34990Sstevel@tonic-gate 		}
35000Sstevel@tonic-gate 	}
35010Sstevel@tonic-gate 	/*
35020Sstevel@tonic-gate 	 * Check a filter for ICMPv6 types if needed.
35030Sstevel@tonic-gate 	 * Verify raw checksums if needed.
35040Sstevel@tonic-gate 	 */
35050Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
35060Sstevel@tonic-gate 		if (icmp->icmp_filter != NULL) {
35070Sstevel@tonic-gate 			int type;
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 			/* Assumes that IP has done the pullupmsg */
35100Sstevel@tonic-gate 			type = mp->b_rptr[hdr_len];
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
35130Sstevel@tonic-gate 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
35140Sstevel@tonic-gate 				freemsg(mp);
35150Sstevel@tonic-gate 				return;
35160Sstevel@tonic-gate 			}
35170Sstevel@tonic-gate 		} else {
35180Sstevel@tonic-gate 			/* Checksum */
35190Sstevel@tonic-gate 			uint16_t	*up;
35200Sstevel@tonic-gate 			uint32_t	sum;
35210Sstevel@tonic-gate 			int		remlen;
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 			up = (uint16_t *)&ip6h->ip6_src;
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 			remlen = msgdsize(mp) - hdr_len;
35260Sstevel@tonic-gate 			sum = htons(icmp->icmp_proto + remlen)
35270Sstevel@tonic-gate 			    + up[0] + up[1] + up[2] + up[3]
35280Sstevel@tonic-gate 			    + up[4] + up[5] + up[6] + up[7]
35290Sstevel@tonic-gate 			    + up[8] + up[9] + up[10] + up[11]
35300Sstevel@tonic-gate 			    + up[12] + up[13] + up[14] + up[15];
35310Sstevel@tonic-gate 			sum = (sum & 0xffff) + (sum >> 16);
35320Sstevel@tonic-gate 			sum = IP_CSUM(mp, hdr_len, sum);
35330Sstevel@tonic-gate 			if (sum != 0) {
35340Sstevel@tonic-gate 				/* IPv6 RAW checksum failed */
35350Sstevel@tonic-gate 				ip0dbg(("icmp_rput: RAW checksum "
35360Sstevel@tonic-gate 				    "failed %x\n", sum));
35370Sstevel@tonic-gate 				freemsg(mp);
35385240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
35393448Sdh155122 				    rawipInCksumErrs);
35400Sstevel@tonic-gate 				return;
35410Sstevel@tonic-gate 			}
35420Sstevel@tonic-gate 		}
35430Sstevel@tonic-gate 	}
35440Sstevel@tonic-gate 	/* Skip all the IPv6 headers per API */
35450Sstevel@tonic-gate 	mp->b_rptr += hdr_len;
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate 	/*
35500Sstevel@tonic-gate 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
35510Sstevel@tonic-gate 	 * maintain state information, instead of relying on icmp_t
35520Sstevel@tonic-gate 	 * structure, since there arent any locks protecting these members
35530Sstevel@tonic-gate 	 * and there is a window where there might be a race between a
35540Sstevel@tonic-gate 	 * thread setting options on the write side and a thread reading
35550Sstevel@tonic-gate 	 * these options on the read size.
35560Sstevel@tonic-gate 	 */
35570Sstevel@tonic-gate 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
35580Sstevel@tonic-gate 	    IPPF_RTHDR|IPPF_IFINDEX)) {
35590Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvhopopts &&
35601676Sjpk 		    (ipp.ipp_fields & IPPF_HOPOPTS) &&
35611676Sjpk 		    ipp.ipp_hopoptslen > hopstrip) {
35620Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35631676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
35640Sstevel@tonic-gate 			icmp_opt |= IPPF_HOPOPTS;
35650Sstevel@tonic-gate 		}
35660Sstevel@tonic-gate 		if ((icmp->icmp_ipv6_recvdstopts ||
35675240Snordmark 		    icmp->icmp_old_ipv6_recvdstopts) &&
35680Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
35690Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35700Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
35710Sstevel@tonic-gate 			icmp_opt |= IPPF_DSTOPTS;
35720Sstevel@tonic-gate 		}
35730Sstevel@tonic-gate 		if (((icmp->icmp_ipv6_recvdstopts &&
35740Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrthdr &&
35750Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
35760Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrtdstopts) &&
35770Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
35780Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35790Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
35800Sstevel@tonic-gate 			icmp_opt |= IPPF_RTDSTOPTS;
35810Sstevel@tonic-gate 		}
35820Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvrthdr &&
35830Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) {
35840Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35850Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
35860Sstevel@tonic-gate 			icmp_opt |= IPPF_RTHDR;
35870Sstevel@tonic-gate 		}
35883318Srshoaib 		if (icmp->icmp_ip_recvpktinfo &&
35890Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
35900Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35910Sstevel@tonic-gate 			    sizeof (struct in6_pktinfo);
35920Sstevel@tonic-gate 			icmp_opt |= IPPF_IFINDEX;
35930Sstevel@tonic-gate 		}
35940Sstevel@tonic-gate 	}
35950Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvhoplimit) {
35960Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
35970Sstevel@tonic-gate 		icmp_ipv6_recvhoplimit = B_TRUE;
35980Sstevel@tonic-gate 	}
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvtclass)
36010Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 	mp1 = allocb(udi_size, BPRI_MED);
36040Sstevel@tonic-gate 	if (mp1 == NULL) {
36050Sstevel@tonic-gate 		freemsg(mp);
36065240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
36070Sstevel@tonic-gate 		return;
36080Sstevel@tonic-gate 	}
36090Sstevel@tonic-gate 	mp1->b_cont = mp;
36100Sstevel@tonic-gate 	mp = mp1;
36110Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
36120Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
36130Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)tudi + udi_size;
36140Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
36150Sstevel@tonic-gate 	tudi->SRC_length = sizeof (sin6_t);
36160Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
36170Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
36180Sstevel@tonic-gate 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
36190Sstevel@tonic-gate 	tudi->OPT_length = udi_size;
36200Sstevel@tonic-gate 	sin6 = (sin6_t *)&tudi[1];
36210Sstevel@tonic-gate 	sin6->sin6_port = 0;
36220Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	sin6->sin6_addr = ip6h->ip6_src;
36250Sstevel@tonic-gate 	/* No sin6_flowinfo per API */
36260Sstevel@tonic-gate 	sin6->sin6_flowinfo = 0;
36270Sstevel@tonic-gate 	/* For link-scope source pass up scope id */
36280Sstevel@tonic-gate 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
36290Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
36300Sstevel@tonic-gate 		sin6->sin6_scope_id = ipp.ipp_ifindex;
36310Sstevel@tonic-gate 	else
36320Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
36330Sstevel@tonic-gate 
36340Sstevel@tonic-gate 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
36353448Sdh155122 	    icmp->icmp_zoneid, is->is_netstack);
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 	if (udi_size != 0) {
36380Sstevel@tonic-gate 		uchar_t *dstopt;
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate 		dstopt = (uchar_t *)&sin6[1];
36410Sstevel@tonic-gate 		if (icmp_opt & IPPF_IFINDEX) {
36420Sstevel@tonic-gate 			struct T_opthdr *toh;
36430Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
36440Sstevel@tonic-gate 
36450Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36460Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36470Sstevel@tonic-gate 			toh->name = IPV6_PKTINFO;
36480Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36490Sstevel@tonic-gate 			    sizeof (*pkti);
36500Sstevel@tonic-gate 			toh->status = 0;
36510Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36520Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)dstopt;
36530Sstevel@tonic-gate 			pkti->ipi6_addr = ip6h->ip6_dst;
36540Sstevel@tonic-gate 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
36550Sstevel@tonic-gate 			dstopt += sizeof (*pkti);
36560Sstevel@tonic-gate 			udi_size -= toh->len;
36570Sstevel@tonic-gate 		}
36580Sstevel@tonic-gate 		if (icmp_ipv6_recvhoplimit) {
36590Sstevel@tonic-gate 			struct T_opthdr *toh;
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36620Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36630Sstevel@tonic-gate 			toh->name = IPV6_HOPLIMIT;
36640Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36650Sstevel@tonic-gate 			    sizeof (uint_t);
36660Sstevel@tonic-gate 			toh->status = 0;
36670Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36680Sstevel@tonic-gate 			*(uint_t *)dstopt = ip6h->ip6_hops;
36690Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
36700Sstevel@tonic-gate 			udi_size -= toh->len;
36710Sstevel@tonic-gate 		}
36720Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvtclass) {
36730Sstevel@tonic-gate 			struct T_opthdr *toh;
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36760Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36770Sstevel@tonic-gate 			toh->name = IPV6_TCLASS;
36780Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36790Sstevel@tonic-gate 			    sizeof (uint_t);
36800Sstevel@tonic-gate 			toh->status = 0;
36810Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36820Sstevel@tonic-gate 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
36830Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
36840Sstevel@tonic-gate 			udi_size -= toh->len;
36850Sstevel@tonic-gate 		}
36860Sstevel@tonic-gate 		if (icmp_opt & IPPF_HOPOPTS) {
36870Sstevel@tonic-gate 			struct T_opthdr *toh;
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36900Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36910Sstevel@tonic-gate 			toh->name = IPV6_HOPOPTS;
36920Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36931676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
36940Sstevel@tonic-gate 			toh->status = 0;
36950Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36961676Sjpk 			bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt,
36971676Sjpk 			    ipp.ipp_hopoptslen - hopstrip);
36981676Sjpk 			if (hopstrip > 0) {
36991676Sjpk 				/* copy next header value and fake length */
37001676Sjpk 				dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0];
37011676Sjpk 				dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] -
37021676Sjpk 				    hopstrip / 8;
37031676Sjpk 			}
37041676Sjpk 			dstopt += ipp.ipp_hopoptslen - hopstrip;
37050Sstevel@tonic-gate 			udi_size -= toh->len;
37060Sstevel@tonic-gate 		}
37070Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTDSTOPTS) {
37080Sstevel@tonic-gate 			struct T_opthdr *toh;
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37110Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37120Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
37130Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37140Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
37150Sstevel@tonic-gate 			toh->status = 0;
37160Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37170Sstevel@tonic-gate 			bcopy(ipp.ipp_rtdstopts, dstopt,
37180Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen);
37190Sstevel@tonic-gate 			dstopt += ipp.ipp_rtdstoptslen;
37200Sstevel@tonic-gate 			udi_size -= toh->len;
37210Sstevel@tonic-gate 		}
37220Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTHDR) {
37230Sstevel@tonic-gate 			struct T_opthdr *toh;
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37260Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37270Sstevel@tonic-gate 			toh->name = IPV6_RTHDR;
37280Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37290Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
37300Sstevel@tonic-gate 			toh->status = 0;
37310Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37320Sstevel@tonic-gate 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
37330Sstevel@tonic-gate 			dstopt += ipp.ipp_rthdrlen;
37340Sstevel@tonic-gate 			udi_size -= toh->len;
37350Sstevel@tonic-gate 		}
37360Sstevel@tonic-gate 		if (icmp_opt & IPPF_DSTOPTS) {
37370Sstevel@tonic-gate 			struct T_opthdr *toh;
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37400Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37410Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
37420Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37430Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
37440Sstevel@tonic-gate 			toh->status = 0;
37450Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37460Sstevel@tonic-gate 			bcopy(ipp.ipp_dstopts, dstopt,
37470Sstevel@tonic-gate 			    ipp.ipp_dstoptslen);
37480Sstevel@tonic-gate 			dstopt += ipp.ipp_dstoptslen;
37490Sstevel@tonic-gate 			udi_size -= toh->len;
37500Sstevel@tonic-gate 		}
37510Sstevel@tonic-gate 		/* Consumed all of allocated space */
37520Sstevel@tonic-gate 		ASSERT(udi_size == 0);
37530Sstevel@tonic-gate 	}
37545240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
37555240Snordmark 	putnext(connp->conn_rq, mp);
37565240Snordmark }
37575240Snordmark 
37585240Snordmark /*
37595240Snordmark  * Handle the results of a T_BIND_REQ whether deferred by IP or handled
37605240Snordmark  * immediately.
37615240Snordmark  */
37625240Snordmark static void
37635240Snordmark icmp_bind_result(conn_t *connp, mblk_t *mp)
37645240Snordmark {
37655240Snordmark 	struct T_error_ack	*tea;
37665240Snordmark 
37675240Snordmark 	switch (mp->b_datap->db_type) {
37685240Snordmark 	case M_PROTO:
37695240Snordmark 	case M_PCPROTO:
37705240Snordmark 		/* M_PROTO messages contain some type of TPI message. */
37715240Snordmark 		if ((mp->b_wptr - mp->b_rptr) < sizeof (t_scalar_t)) {
37725240Snordmark 			freemsg(mp);
37735240Snordmark 			return;
37745240Snordmark 		}
37755240Snordmark 		tea = (struct T_error_ack *)mp->b_rptr;
37765240Snordmark 
37775240Snordmark 		switch (tea->PRIM_type) {
37785240Snordmark 		case T_ERROR_ACK:
37795240Snordmark 			switch (tea->ERROR_prim) {
37805240Snordmark 			case O_T_BIND_REQ:
37815240Snordmark 			case T_BIND_REQ:
37825240Snordmark 				icmp_bind_error(connp, mp);
37835240Snordmark 				return;
37845240Snordmark 			default:
37855240Snordmark 				break;
37865240Snordmark 			}
37875240Snordmark 			ASSERT(0);
37885240Snordmark 			freemsg(mp);
37895240Snordmark 			return;
37905240Snordmark 
37915240Snordmark 		case T_BIND_ACK:
37925240Snordmark 			icmp_bind_ack(connp, mp);
37935240Snordmark 			return;
37945240Snordmark 
37955240Snordmark 		default:
37965240Snordmark 			break;
37975240Snordmark 		}
37985240Snordmark 		freemsg(mp);
37995240Snordmark 		return;
38005240Snordmark 	default:
38015240Snordmark 		/* FIXME: other cases? */
38025240Snordmark 		ASSERT(0);
38035240Snordmark 		freemsg(mp);
38045240Snordmark 		return;
38055240Snordmark 	}
38060Sstevel@tonic-gate }
38070Sstevel@tonic-gate 
38080Sstevel@tonic-gate /*
38090Sstevel@tonic-gate  * Process a T_BIND_ACK
38100Sstevel@tonic-gate  */
38110Sstevel@tonic-gate static void
38125240Snordmark icmp_bind_ack(conn_t *connp, mblk_t *mp)
38130Sstevel@tonic-gate {
38145240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
38150Sstevel@tonic-gate 	mblk_t	*mp1;
38160Sstevel@tonic-gate 	ire_t	*ire;
38170Sstevel@tonic-gate 	struct T_bind_ack *tba;
38180Sstevel@tonic-gate 	uchar_t *addrp;
38190Sstevel@tonic-gate 	ipa_conn_t	*ac;
38200Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
38210Sstevel@tonic-gate 
38225240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
38230Sstevel@tonic-gate 	/*
38240Sstevel@tonic-gate 	 * We know if headers are included or not so we can
38250Sstevel@tonic-gate 	 * safely do this.
38260Sstevel@tonic-gate 	 */
38270Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
38280Sstevel@tonic-gate 		/*
38290Sstevel@tonic-gate 		 * TPI has not yet bound - bind sent by
38300Sstevel@tonic-gate 		 * icmp_bind_proto.
38310Sstevel@tonic-gate 		 */
38320Sstevel@tonic-gate 		freemsg(mp);
38335240Snordmark 		rw_exit(&icmp->icmp_rwlock);
38340Sstevel@tonic-gate 		return;
38350Sstevel@tonic-gate 	}
38365240Snordmark 	ASSERT(icmp->icmp_pending_op != -1);
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 	/*
38390Sstevel@tonic-gate 	 * If a broadcast/multicast address was bound set
38400Sstevel@tonic-gate 	 * the source address to 0.
38410Sstevel@tonic-gate 	 * This ensures no datagrams with broadcast address
38420Sstevel@tonic-gate 	 * as source address are emitted (which would violate
38430Sstevel@tonic-gate 	 * RFC1122 - Hosts requirements)
38440Sstevel@tonic-gate 	 *
38450Sstevel@tonic-gate 	 * Note that when connecting the returned IRE is
38460Sstevel@tonic-gate 	 * for the destination address and we only perform
38470Sstevel@tonic-gate 	 * the broadcast check for the source address (it
38480Sstevel@tonic-gate 	 * is OK to connect to a broadcast/multicast address.)
38490Sstevel@tonic-gate 	 */
38500Sstevel@tonic-gate 	mp1 = mp->b_cont;
38510Sstevel@tonic-gate 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
38520Sstevel@tonic-gate 		ire = (ire_t *)mp1->b_rptr;
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate 		/*
38550Sstevel@tonic-gate 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
38560Sstevel@tonic-gate 		 * local address.
38570Sstevel@tonic-gate 		 */
38580Sstevel@tonic-gate 		if (ire->ire_type == IRE_BROADCAST &&
38590Sstevel@tonic-gate 		    icmp->icmp_state != TS_DATA_XFER) {
38605240Snordmark 			ASSERT(icmp->icmp_pending_op == T_BIND_REQ ||
38615240Snordmark 			    icmp->icmp_pending_op == O_T_BIND_REQ);
38620Sstevel@tonic-gate 			/* This was just a local bind to a MC/broadcast addr */
38630Sstevel@tonic-gate 			V6_SET_ZERO(icmp->icmp_v6src);
38640Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6)
38655240Snordmark 				(void) icmp_build_hdrs(icmp);
38660Sstevel@tonic-gate 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
38670Sstevel@tonic-gate 			/*
38680Sstevel@tonic-gate 			 * Local address not yet set - pick it from the
38690Sstevel@tonic-gate 			 * T_bind_ack
38700Sstevel@tonic-gate 			 */
38710Sstevel@tonic-gate 			tba = (struct T_bind_ack *)mp->b_rptr;
38720Sstevel@tonic-gate 			addrp = &mp->b_rptr[tba->ADDR_offset];
38730Sstevel@tonic-gate 			switch (icmp->icmp_family) {
38740Sstevel@tonic-gate 			case AF_INET:
38750Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
38760Sstevel@tonic-gate 					ac = (ipa_conn_t *)addrp;
38770Sstevel@tonic-gate 				} else {
38780Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
38790Sstevel@tonic-gate 					    sizeof (ipa_conn_x_t));
38800Sstevel@tonic-gate 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
38810Sstevel@tonic-gate 				}
38820Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
38830Sstevel@tonic-gate 				    &icmp->icmp_v6src);
38840Sstevel@tonic-gate 				break;
38850Sstevel@tonic-gate 			case AF_INET6:
38860Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
38870Sstevel@tonic-gate 					ac6 = (ipa6_conn_t *)addrp;
38880Sstevel@tonic-gate 				} else {
38890Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
38900Sstevel@tonic-gate 					    sizeof (ipa6_conn_x_t));
38910Sstevel@tonic-gate 					ac6 = &((ipa6_conn_x_t *)
38920Sstevel@tonic-gate 					    addrp)->ac6x_conn;
38930Sstevel@tonic-gate 				}
38940Sstevel@tonic-gate 				icmp->icmp_v6src = ac6->ac6_laddr;
38955240Snordmark 				(void) icmp_build_hdrs(icmp);
38960Sstevel@tonic-gate 			}
38970Sstevel@tonic-gate 		}
38980Sstevel@tonic-gate 		mp1 = mp1->b_cont;
38990Sstevel@tonic-gate 	}
39005240Snordmark 	icmp->icmp_pending_op = -1;
39015240Snordmark 	rw_exit(&icmp->icmp_rwlock);
39020Sstevel@tonic-gate 	/*
39030Sstevel@tonic-gate 	 * Look for one or more appended ACK message added by
39040Sstevel@tonic-gate 	 * icmp_connect or icmp_disconnect.
39050Sstevel@tonic-gate 	 * If none found just send up the T_BIND_ACK.
39060Sstevel@tonic-gate 	 * icmp_connect has appended a T_OK_ACK and a
39070Sstevel@tonic-gate 	 * T_CONN_CON.
39080Sstevel@tonic-gate 	 * icmp_disconnect has appended a T_OK_ACK.
39090Sstevel@tonic-gate 	 */
39100Sstevel@tonic-gate 	if (mp1 != NULL) {
39110Sstevel@tonic-gate 		if (mp->b_cont == mp1)
39120Sstevel@tonic-gate 			mp->b_cont = NULL;
39130Sstevel@tonic-gate 		else {
39140Sstevel@tonic-gate 			ASSERT(mp->b_cont->b_cont == mp1);
39150Sstevel@tonic-gate 			mp->b_cont->b_cont = NULL;
39160Sstevel@tonic-gate 		}
39170Sstevel@tonic-gate 		freemsg(mp);
39180Sstevel@tonic-gate 		mp = mp1;
39190Sstevel@tonic-gate 		while (mp != NULL) {
39200Sstevel@tonic-gate 			mp1 = mp->b_cont;
39210Sstevel@tonic-gate 			mp->b_cont = NULL;
39225240Snordmark 			putnext(connp->conn_rq, mp);
39230Sstevel@tonic-gate 			mp = mp1;
39240Sstevel@tonic-gate 		}
39250Sstevel@tonic-gate 		return;
39260Sstevel@tonic-gate 	}
39270Sstevel@tonic-gate 	freemsg(mp->b_cont);
39280Sstevel@tonic-gate 	mp->b_cont = NULL;
39295240Snordmark 	putnext(connp->conn_rq, mp);
39305240Snordmark }
39315240Snordmark 
39325240Snordmark static void
39335240Snordmark icmp_bind_error(conn_t *connp, mblk_t *mp)
39345240Snordmark {
39355240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
39365240Snordmark 	struct T_error_ack *tea;
39375240Snordmark 
39385240Snordmark 	tea = (struct T_error_ack *)mp->b_rptr;
39395240Snordmark 	/*
39405240Snordmark 	 * If our O_T_BIND_REQ/T_BIND_REQ fails,
39415240Snordmark 	 * clear out the source address before
39425240Snordmark 	 * passing the message upstream.
39435240Snordmark 	 * If this was caused by a T_CONN_REQ
39445240Snordmark 	 * revert back to bound state.
39455240Snordmark 	 */
39465240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
39475240Snordmark 	if (icmp->icmp_state == TS_UNBND) {
39485240Snordmark 		/*
39495240Snordmark 		 * TPI has not yet bound - bind sent by icmp_bind_proto.
39505240Snordmark 		 */
39515240Snordmark 		freemsg(mp);
39525240Snordmark 		rw_exit(&icmp->icmp_rwlock);
39535240Snordmark 		return;
39545240Snordmark 	}
39555240Snordmark 	ASSERT(icmp->icmp_pending_op != -1);
39565240Snordmark 	tea->ERROR_prim = icmp->icmp_pending_op;
39575240Snordmark 	icmp->icmp_pending_op = -1;
39585240Snordmark 
39595240Snordmark 	switch (tea->ERROR_prim) {
39605240Snordmark 	case T_CONN_REQ:
39615240Snordmark 		ASSERT(icmp->icmp_state == TS_DATA_XFER);
39625240Snordmark 		/* Connect failed */
39635240Snordmark 		/* Revert back to the bound source */
39645240Snordmark 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
39655240Snordmark 		icmp->icmp_state = TS_IDLE;
39665240Snordmark 		if (icmp->icmp_family == AF_INET6)
39675240Snordmark 			(void) icmp_build_hdrs(icmp);
39685240Snordmark 		break;
39695240Snordmark 
39705240Snordmark 	case T_DISCON_REQ:
39715240Snordmark 	case T_BIND_REQ:
39725240Snordmark 	case O_T_BIND_REQ:
39735240Snordmark 		V6_SET_ZERO(icmp->icmp_v6src);
39745240Snordmark 		V6_SET_ZERO(icmp->icmp_bound_v6src);
39755240Snordmark 		icmp->icmp_state = TS_UNBND;
39765240Snordmark 		if (icmp->icmp_family == AF_INET6)
39775240Snordmark 			(void) icmp_build_hdrs(icmp);
39785240Snordmark 		break;
39795240Snordmark 	default:
39805240Snordmark 		break;
39815240Snordmark 	}
39825240Snordmark 	rw_exit(&icmp->icmp_rwlock);
39835240Snordmark 	putnext(connp->conn_rq, mp);
39840Sstevel@tonic-gate }
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate /*
39870Sstevel@tonic-gate  * return SNMP stuff in buffer in mpdata
39880Sstevel@tonic-gate  */
39895240Snordmark mblk_t *
39900Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl)
39910Sstevel@tonic-gate {
39920Sstevel@tonic-gate 	mblk_t			*mpdata;
39930Sstevel@tonic-gate 	struct opthdr		*optp;
39945240Snordmark 	conn_t			*connp = Q_TO_CONN(q);
39955240Snordmark 	icmp_stack_t		*is = connp->conn_netstack->netstack_icmp;
39965240Snordmark 	mblk_t			*mp2ctl;
39975240Snordmark 
39985240Snordmark 	/*
39995240Snordmark 	 * make a copy of the original message
40005240Snordmark 	 */
40015240Snordmark 	mp2ctl = copymsg(mpctl);
40020Sstevel@tonic-gate 
40030Sstevel@tonic-gate 	if (mpctl == NULL ||
40040Sstevel@tonic-gate 	    (mpdata = mpctl->b_cont) == NULL) {
40055240Snordmark 		freemsg(mpctl);
40065240Snordmark 		freemsg(mp2ctl);
40070Sstevel@tonic-gate 		return (0);
40080Sstevel@tonic-gate 	}
40090Sstevel@tonic-gate 
40100Sstevel@tonic-gate 	/* fixed length structure for IPv4 and IPv6 counters */
40110Sstevel@tonic-gate 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
40120Sstevel@tonic-gate 	optp->level = EXPER_RAWIP;
40130Sstevel@tonic-gate 	optp->name = 0;
40145240Snordmark 	(void) snmp_append_data(mpdata, (char *)&is->is_rawip_mib,
40155240Snordmark 	    sizeof (is->is_rawip_mib));
40160Sstevel@tonic-gate 	optp->len = msgdsize(mpdata);
40170Sstevel@tonic-gate 	qreply(q, mpctl);
40180Sstevel@tonic-gate 
40195240Snordmark 	return (mp2ctl);
40200Sstevel@tonic-gate }
40210Sstevel@tonic-gate 
40220Sstevel@tonic-gate /*
40230Sstevel@tonic-gate  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
40240Sstevel@tonic-gate  * TODO:  If this ever actually tries to set anything, it needs to be
40250Sstevel@tonic-gate  * to do the appropriate locking.
40260Sstevel@tonic-gate  */
40270Sstevel@tonic-gate /* ARGSUSED */
40285240Snordmark int
40290Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
40300Sstevel@tonic-gate     uchar_t *ptr, int len)
40310Sstevel@tonic-gate {
40320Sstevel@tonic-gate 	switch (level) {
40330Sstevel@tonic-gate 	case EXPER_RAWIP:
40340Sstevel@tonic-gate 		return (0);
40350Sstevel@tonic-gate 	default:
40360Sstevel@tonic-gate 		return (1);
40370Sstevel@tonic-gate 	}
40380Sstevel@tonic-gate }
40390Sstevel@tonic-gate 
40400Sstevel@tonic-gate /* Report for ndd "icmp_status" */
40410Sstevel@tonic-gate /* ARGSUSED */
40420Sstevel@tonic-gate static int
40430Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
40440Sstevel@tonic-gate {
40455240Snordmark 	conn_t  *connp;
40465240Snordmark 	ip_stack_t *ipst;
40470Sstevel@tonic-gate 	char	laddrbuf[INET6_ADDRSTRLEN];
40480Sstevel@tonic-gate 	char	faddrbuf[INET6_ADDRSTRLEN];
40495240Snordmark 	int	i;
40500Sstevel@tonic-gate 
40510Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
40520Sstevel@tonic-gate 	    "RAWIP    " MI_COL_HDRPAD_STR
40530Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
40540Sstevel@tonic-gate 	    "  src addr        dest addr       state");
40550Sstevel@tonic-gate 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
40560Sstevel@tonic-gate 
40575240Snordmark 	connp = Q_TO_CONN(q);
40585240Snordmark 	ipst = connp->conn_netstack->netstack_ip;
40595240Snordmark 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
40605240Snordmark 		connf_t *connfp;
40615240Snordmark 		char	*state;
40625240Snordmark 
40635240Snordmark 		connfp = &ipst->ips_ipcl_globalhash_fanout[i];
40645240Snordmark 		connp = NULL;
40655240Snordmark 
40665240Snordmark 		while ((connp = ipcl_get_next_conn(connfp, connp,
40675240Snordmark 		    IPCL_RAWIPCONN)) != NULL) {
40685240Snordmark 			icmp_t  *icmp;
40695240Snordmark 
40705240Snordmark 			mutex_enter(&(connp)->conn_lock);
40715240Snordmark 			icmp = connp->conn_icmp;
40725240Snordmark 
40735240Snordmark 			if (icmp->icmp_state == TS_UNBND)
40745240Snordmark 				state = "UNBOUND";
40755240Snordmark 			else if (icmp->icmp_state == TS_IDLE)
40765240Snordmark 				state = "IDLE";
40775240Snordmark 			else if (icmp->icmp_state == TS_DATA_XFER)
40785240Snordmark 				state = "CONNECTED";
40795240Snordmark 			else
40805240Snordmark 				state = "UnkState";
40815240Snordmark 
40825240Snordmark 			(void) mi_mpprintf(mp, MI_COL_PTRFMT_STR "%s %s %s",
40835240Snordmark 			    (void *)icmp,
40845240Snordmark 			    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
40855240Snordmark 			    sizeof (faddrbuf)),
40865240Snordmark 			    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
40875240Snordmark 			    sizeof (laddrbuf)),
40885240Snordmark 			    state);
40895240Snordmark 			mutex_exit(&(connp)->conn_lock);
40905240Snordmark 		}
40910Sstevel@tonic-gate 	}
40920Sstevel@tonic-gate 	return (0);
40930Sstevel@tonic-gate }
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate /*
40960Sstevel@tonic-gate  * This routine creates a T_UDERROR_IND message and passes it upstream.
40970Sstevel@tonic-gate  * The address and options are copied from the T_UNITDATA_REQ message
40980Sstevel@tonic-gate  * passed in mp.  This message is freed.
40990Sstevel@tonic-gate  */
41000Sstevel@tonic-gate static void
41010Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
41020Sstevel@tonic-gate {
41030Sstevel@tonic-gate 	mblk_t	*mp1;
41040Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
41050Sstevel@tonic-gate 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
41060Sstevel@tonic-gate 
41070Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
41080Sstevel@tonic-gate 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
41090Sstevel@tonic-gate 	    tudr->OPT_length, err);
41100Sstevel@tonic-gate 	if (mp1)
41110Sstevel@tonic-gate 		qreply(q, mp1);
41120Sstevel@tonic-gate 	freemsg(mp);
41130Sstevel@tonic-gate }
41140Sstevel@tonic-gate 
41150Sstevel@tonic-gate /*
41160Sstevel@tonic-gate  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
41170Sstevel@tonic-gate  * After some error checking, the message is passed downstream to ip.
41180Sstevel@tonic-gate  */
41190Sstevel@tonic-gate static void
41200Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp)
41210Sstevel@tonic-gate {
41225240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
41235240Snordmark 
41245240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
41250Sstevel@tonic-gate 	/* If a bind has not been done, we can't unbind. */
41265240Snordmark 	if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) {
41275240Snordmark 		rw_exit(&icmp->icmp_rwlock);
41280Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
41290Sstevel@tonic-gate 		return;
41300Sstevel@tonic-gate 	}
41315240Snordmark 	icmp->icmp_pending_op = T_UNBIND_REQ;
41325240Snordmark 	rw_exit(&icmp->icmp_rwlock);
41335240Snordmark 
41345240Snordmark 	/*
41355240Snordmark 	 * Pass the unbind to IP; T_UNBIND_REQ is larger than T_OK_ACK
41365240Snordmark 	 * and therefore ip_unbind must never return NULL.
41375240Snordmark 	 */
41385240Snordmark 	mp = ip_unbind(q, mp);
41395240Snordmark 	ASSERT(mp != NULL);
41405240Snordmark 	ASSERT(((struct T_ok_ack *)mp->b_rptr)->PRIM_type == T_OK_ACK);
41415240Snordmark 
41425240Snordmark 	/*
41435240Snordmark 	 * Once we're unbound from IP, the pending operation may be cleared
41445240Snordmark 	 * here.
41455240Snordmark 	 */
41465240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
41470Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_v6src);
41480Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_bound_v6src);
41495240Snordmark 	icmp->icmp_pending_op = -1;
41500Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
41515240Snordmark 	if (icmp->icmp_family == AF_INET6)
41525240Snordmark 		(void) icmp_build_hdrs(icmp);
41535240Snordmark 	rw_exit(&icmp->icmp_rwlock);
41545240Snordmark 
41555240Snordmark 	qreply(q, mp);
41560Sstevel@tonic-gate }
41570Sstevel@tonic-gate 
41580Sstevel@tonic-gate /*
41590Sstevel@tonic-gate  * Process IPv4 packets that already include an IP header.
41600Sstevel@tonic-gate  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
41610Sstevel@tonic-gate  * IPPROTO_IGMP).
41620Sstevel@tonic-gate  */
41630Sstevel@tonic-gate static void
41645240Snordmark icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp, ip4_pkt_t *pktinfop)
41650Sstevel@tonic-gate {
41663448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
41670Sstevel@tonic-gate 	ipha_t	*ipha;
41680Sstevel@tonic-gate 	int	ip_hdr_length;
41690Sstevel@tonic-gate 	int	tp_hdr_len;
41700Sstevel@tonic-gate 	mblk_t	*mp1;
41710Sstevel@tonic-gate 	uint_t	pkt_len;
41723318Srshoaib 	ip_opt_info_t optinfo;
41735240Snordmark 	conn_t	*connp = icmp->icmp_connp;
41743318Srshoaib 
41753318Srshoaib 	optinfo.ip_opt_flags = 0;
41763318Srshoaib 	optinfo.ip_opt_ill_index = 0;
41770Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
41780Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
41790Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
41800Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
41813448Sdh155122 			ASSERT(icmp != NULL);
41825240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
41830Sstevel@tonic-gate 			freemsg(mp);
41840Sstevel@tonic-gate 			return;
41850Sstevel@tonic-gate 		}
41860Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
41870Sstevel@tonic-gate 	}
41880Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length =
41890Sstevel@tonic-gate 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	/*
41920Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
41930Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
41940Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
41950Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
41960Sstevel@tonic-gate 	 */
41970Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
41980Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
41990Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
42000Sstevel@tonic-gate 	/* Insert options if any */
42010Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
42020Sstevel@tonic-gate 		/*
42030Sstevel@tonic-gate 		 * Put the IP header plus any transport header that is
42040Sstevel@tonic-gate 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
42050Sstevel@tonic-gate 		 * that at least the checksum field is in the first mblk.)
42060Sstevel@tonic-gate 		 */
42070Sstevel@tonic-gate 		switch (ipha->ipha_protocol) {
42080Sstevel@tonic-gate 		case IPPROTO_UDP:
42090Sstevel@tonic-gate 			tp_hdr_len = 8;
42100Sstevel@tonic-gate 			break;
42110Sstevel@tonic-gate 		case IPPROTO_TCP:
42120Sstevel@tonic-gate 			tp_hdr_len = 20;
42130Sstevel@tonic-gate 			break;
42140Sstevel@tonic-gate 		default:
42150Sstevel@tonic-gate 			tp_hdr_len = 0;
42160Sstevel@tonic-gate 			break;
42170Sstevel@tonic-gate 		}
42180Sstevel@tonic-gate 		/*
42190Sstevel@tonic-gate 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
42200Sstevel@tonic-gate 		 * tp_hdr_len bytes will be in a single mblk.
42210Sstevel@tonic-gate 		 */
42220Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
42230Sstevel@tonic-gate 		    tp_hdr_len)) {
42240Sstevel@tonic-gate 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
42250Sstevel@tonic-gate 			    tp_hdr_len)) {
42265240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
42273448Sdh155122 				    rawipOutErrors);
42280Sstevel@tonic-gate 				freemsg(mp);
42290Sstevel@tonic-gate 				return;
42300Sstevel@tonic-gate 			}
42310Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
42320Sstevel@tonic-gate 		}
42330Sstevel@tonic-gate 
42340Sstevel@tonic-gate 		/*
42350Sstevel@tonic-gate 		 * if the length is larger then the max allowed IP packet,
42360Sstevel@tonic-gate 		 * then send an error and abort the processing.
42370Sstevel@tonic-gate 		 */
42380Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length)
42390Sstevel@tonic-gate 		    + icmp->icmp_ip_snd_options_len;
42400Sstevel@tonic-gate 		if (pkt_len > IP_MAXPACKET) {
42410Sstevel@tonic-gate 			icmp_ud_err(q, mp, EMSGSIZE);
42420Sstevel@tonic-gate 			return;
42430Sstevel@tonic-gate 		}
42443448Sdh155122 		if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra +
42450Sstevel@tonic-gate 		    tp_hdr_len, BPRI_LO))) {
42460Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
42470Sstevel@tonic-gate 			return;
42480Sstevel@tonic-gate 		}
42493448Sdh155122 		mp1->b_rptr += is->is_wroff_extra;
42500Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
42510Sstevel@tonic-gate 
42520Sstevel@tonic-gate 		ipha->ipha_length = htons((uint16_t)pkt_len);
42530Sstevel@tonic-gate 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
42540Sstevel@tonic-gate 
42550Sstevel@tonic-gate 		/* Copy transport header if any */
42560Sstevel@tonic-gate 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
42570Sstevel@tonic-gate 		mp1->b_wptr += tp_hdr_len;
42580Sstevel@tonic-gate 
42590Sstevel@tonic-gate 		/* Add options */
42600Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_rptr;
42610Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
42620Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len);
42630Sstevel@tonic-gate 
42640Sstevel@tonic-gate 		/* Drop IP header and transport header from original */
42650Sstevel@tonic-gate 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
42660Sstevel@tonic-gate 
42670Sstevel@tonic-gate 		mp1->b_cont = mp;
42680Sstevel@tonic-gate 		mp = mp1;
42690Sstevel@tonic-gate 		/*
42700Sstevel@tonic-gate 		 * Massage source route putting first source
42710Sstevel@tonic-gate 		 * route in ipha_dst.
42720Sstevel@tonic-gate 		 */
42735240Snordmark 		(void) ip_massage_options(ipha, is->is_netstack);
42740Sstevel@tonic-gate 	}
42753318Srshoaib 
42763318Srshoaib 	if (pktinfop != NULL) {
42773318Srshoaib 		/*
42783318Srshoaib 		 * Over write the source address provided in the header
42793318Srshoaib 		 */
42803318Srshoaib 		if (pktinfop->ip4_addr != INADDR_ANY) {
42813318Srshoaib 			ipha->ipha_src = pktinfop->ip4_addr;
42823318Srshoaib 			optinfo.ip_opt_flags = IP_VERIFY_SRC;
42833318Srshoaib 		}
42843318Srshoaib 
42853318Srshoaib 		if (pktinfop->ip4_ill_index != 0) {
42863318Srshoaib 			optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
42873318Srshoaib 		}
42883318Srshoaib 	}
42893318Srshoaib 
42905240Snordmark 	mblk_setcred(mp, connp->conn_cred);
42915240Snordmark 	ip_output_options(connp, mp, q, IP_WPUT,
42925240Snordmark 	    &optinfo);
42930Sstevel@tonic-gate }
42940Sstevel@tonic-gate 
42951676Sjpk static boolean_t
42961676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst)
42971676Sjpk {
42981676Sjpk 	int err;
42991676Sjpk 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
43005240Snordmark 	icmp_stack_t		*is = icmp->icmp_is;
43015240Snordmark 	conn_t	*connp = icmp->icmp_connp;
43025240Snordmark 
43035240Snordmark 	err = tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst,
43043448Sdh155122 	    opt_storage, icmp->icmp_mac_exempt,
43055240Snordmark 	    is->is_netstack->netstack_ip);
43061676Sjpk 	if (err == 0) {
43071676Sjpk 		err = tsol_update_options(&icmp->icmp_ip_snd_options,
43081676Sjpk 		    &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len,
43091676Sjpk 		    opt_storage);
43101676Sjpk 	}
43111676Sjpk 	if (err != 0) {
43125240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
43131676Sjpk 		DTRACE_PROBE4(
43141676Sjpk 		    tx__ip__log__drop__updatelabel__icmp,
43151676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
43161676Sjpk 		    queue_t *, q, char *, opt_storage, mblk_t *, mp);
43171676Sjpk 		icmp_ud_err(q, mp, err);
43181676Sjpk 		return (B_FALSE);
43191676Sjpk 	}
43201676Sjpk 	IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst);
43211676Sjpk 	return (B_TRUE);
43221676Sjpk }
43231676Sjpk 
43240Sstevel@tonic-gate /*
43250Sstevel@tonic-gate  * This routine handles all messages passed downstream.  It either
43260Sstevel@tonic-gate  * consumes the message or passes it downstream; it never queues a
43270Sstevel@tonic-gate  * a message.
43280Sstevel@tonic-gate  */
43290Sstevel@tonic-gate static void
43300Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp)
43310Sstevel@tonic-gate {
43320Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
43330Sstevel@tonic-gate 	ipha_t	*ipha;
43340Sstevel@tonic-gate 	mblk_t	*mp1;
43350Sstevel@tonic-gate 	int	ip_hdr_length;
43360Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
43370Sstevel@tonic-gate 	size_t	ip_len;
43385240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
43395240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
43403448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
43410Sstevel@tonic-gate 	sin6_t	*sin6;
43420Sstevel@tonic-gate 	sin_t	*sin;
43430Sstevel@tonic-gate 	ipaddr_t	v4dst;
43443318Srshoaib 	ip4_pkt_t	pktinfo;
43453318Srshoaib 	ip4_pkt_t	*pktinfop = &pktinfo;
43463318Srshoaib 	ip_opt_info_t	optinfo;
43470Sstevel@tonic-gate 
43480Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
43490Sstevel@tonic-gate 	case M_DATA:
43500Sstevel@tonic-gate 		if (icmp->icmp_hdrincl) {
43510Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
43521676Sjpk 			ipha = (ipha_t *)mp->b_rptr;
43531676Sjpk 			if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
43541676Sjpk 				if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
43555240Snordmark 					BUMP_MIB(&is->is_rawip_mib,
43563448Sdh155122 					    rawipOutErrors);
43571676Sjpk 					freemsg(mp);
43581676Sjpk 					return;
43591676Sjpk 				}
43601676Sjpk 				ipha = (ipha_t *)mp->b_rptr;
43611676Sjpk 			}
43621676Sjpk 			/*
43631676Sjpk 			 * If this connection was used for v6 (inconceivable!)
43641676Sjpk 			 * or if we have a new destination, then it's time to
43651676Sjpk 			 * figure a new label.
43661676Sjpk 			 */
43671676Sjpk 			if (is_system_labeled() &&
43681676Sjpk 			    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
43691676Sjpk 			    V4_PART_OF_V6(icmp->icmp_v6lastdst) !=
43701676Sjpk 			    ipha->ipha_dst) &&
43711676Sjpk 			    !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) {
43721676Sjpk 				return;
43731676Sjpk 			}
43745240Snordmark 			icmp_wput_hdrincl(q, mp, icmp, NULL);
43750Sstevel@tonic-gate 			return;
43760Sstevel@tonic-gate 		}
43770Sstevel@tonic-gate 		freemsg(mp);
43780Sstevel@tonic-gate 		return;
43790Sstevel@tonic-gate 	case M_PROTO:
43800Sstevel@tonic-gate 	case M_PCPROTO:
43810Sstevel@tonic-gate 		ip_len = mp->b_wptr - rptr;
43820Sstevel@tonic-gate 		if (ip_len >= sizeof (struct T_unitdata_req)) {
43830Sstevel@tonic-gate 			/* Expedite valid T_UNITDATA_REQ to below the switch */
43840Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type
43850Sstevel@tonic-gate 			    == T_UNITDATA_REQ)
43860Sstevel@tonic-gate 				break;
43870Sstevel@tonic-gate 		}
43880Sstevel@tonic-gate 		/* FALLTHRU */
43890Sstevel@tonic-gate 	default:
43900Sstevel@tonic-gate 		icmp_wput_other(q, mp);
43910Sstevel@tonic-gate 		return;
43920Sstevel@tonic-gate 	}
43930Sstevel@tonic-gate 
43940Sstevel@tonic-gate 	/* Handle T_UNITDATA_REQ messages here. */
43950Sstevel@tonic-gate 
43963318Srshoaib 
43973318Srshoaib 
43980Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
43990Sstevel@tonic-gate 		/* If a port has not been bound to the stream, fail. */
44005240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44010Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
44020Sstevel@tonic-gate 		return;
44030Sstevel@tonic-gate 	}
44040Sstevel@tonic-gate 	mp1 = mp->b_cont;
44050Sstevel@tonic-gate 	if (mp1 == NULL) {
44065240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44070Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
44080Sstevel@tonic-gate 		return;
44090Sstevel@tonic-gate 	}
44100Sstevel@tonic-gate 
44110Sstevel@tonic-gate 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
44125240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44130Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
44140Sstevel@tonic-gate 		return;
44150Sstevel@tonic-gate 	}
44160Sstevel@tonic-gate 
44170Sstevel@tonic-gate 	switch (icmp->icmp_family) {
44180Sstevel@tonic-gate 	case AF_INET6:
44190Sstevel@tonic-gate 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
44200Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin6) ||
44210Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin6_t) ||
44220Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
44235240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44240Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44250Sstevel@tonic-gate 			return;
44260Sstevel@tonic-gate 		}
44270Sstevel@tonic-gate 
44280Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
44290Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
44305240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44310Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44320Sstevel@tonic-gate 			return;
44330Sstevel@tonic-gate 		}
44340Sstevel@tonic-gate 
44350Sstevel@tonic-gate 		/*
44360Sstevel@tonic-gate 		 * Destination is a native IPv6 address.
44370Sstevel@tonic-gate 		 * Send out an IPv6 format packet.
44380Sstevel@tonic-gate 		 */
44390Sstevel@tonic-gate 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
44400Sstevel@tonic-gate 		return;
44410Sstevel@tonic-gate 
44420Sstevel@tonic-gate 	case AF_INET:
44430Sstevel@tonic-gate 		sin = (sin_t *)&rptr[tudr->DEST_offset];
44440Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin) ||
44450Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin_t) ||
44460Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
44475240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44480Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44490Sstevel@tonic-gate 			return;
44500Sstevel@tonic-gate 		}
44510Sstevel@tonic-gate 		/* Extract and ipaddr */
44520Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
44530Sstevel@tonic-gate 		break;
44541676Sjpk 
44551676Sjpk 	default:
44561676Sjpk 		ASSERT(0);
44570Sstevel@tonic-gate 	}
44580Sstevel@tonic-gate 
44593318Srshoaib 	pktinfop->ip4_ill_index = 0;
44603318Srshoaib 	pktinfop->ip4_addr = INADDR_ANY;
44613318Srshoaib 	optinfo.ip_opt_flags = 0;
44623318Srshoaib 	optinfo.ip_opt_ill_index = 0;
44633318Srshoaib 
44643318Srshoaib 
44650Sstevel@tonic-gate 	/*
44660Sstevel@tonic-gate 	 * If options passed in, feed it for verification and handling
44670Sstevel@tonic-gate 	 */
44680Sstevel@tonic-gate 	if (tudr->OPT_length != 0) {
44690Sstevel@tonic-gate 		int error;
44700Sstevel@tonic-gate 
44713318Srshoaib 		error = 0;
44720Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
44733318Srshoaib 		    (void *)pktinfop) < 0) {
44740Sstevel@tonic-gate 			/* failure */
44755240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44760Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
44770Sstevel@tonic-gate 			return;
44780Sstevel@tonic-gate 		}
44793318Srshoaib 		ASSERT(error == 0);
44800Sstevel@tonic-gate 		/*
44810Sstevel@tonic-gate 		 * Note: Success in processing options.
44820Sstevel@tonic-gate 		 * mp option buffer represented by
44830Sstevel@tonic-gate 		 * OPT_length/offset now potentially modified
44840Sstevel@tonic-gate 		 * and contain option setting results
44850Sstevel@tonic-gate 		 */
44863318Srshoaib 
44870Sstevel@tonic-gate 	}
44880Sstevel@tonic-gate 
44891676Sjpk 	if (v4dst == INADDR_ANY)
44901676Sjpk 		v4dst = htonl(INADDR_LOOPBACK);
44911676Sjpk 
44921676Sjpk 	/* Check if our saved options are valid; update if not */
44931676Sjpk 	if (is_system_labeled() &&
44941676Sjpk 	    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
44951676Sjpk 	    V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) &&
44961676Sjpk 	    !icmp_update_label(q, icmp, mp, v4dst)) {
44971676Sjpk 		return;
44981676Sjpk 	}
44991676Sjpk 
45000Sstevel@tonic-gate 	/* Protocol 255 contains full IP headers */
45010Sstevel@tonic-gate 	if (icmp->icmp_hdrincl) {
45020Sstevel@tonic-gate 		freeb(mp);
45035240Snordmark 		icmp_wput_hdrincl(q, mp1, icmp, pktinfop);
45040Sstevel@tonic-gate 		return;
45050Sstevel@tonic-gate 	}
45061676Sjpk 
45073318Srshoaib 
45080Sstevel@tonic-gate 	/* Add an IP header */
45090Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
45100Sstevel@tonic-gate 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
45110Sstevel@tonic-gate 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
45120Sstevel@tonic-gate 	    mp1->b_datap->db_ref != 1 ||
45130Sstevel@tonic-gate 	    !OK_32PTR(ipha)) {
45143448Sdh155122 		if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra,
45150Sstevel@tonic-gate 		    BPRI_LO))) {
45165240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
45171289Sja97890 			icmp_ud_err(q, mp, ENOMEM);
45180Sstevel@tonic-gate 			return;
45190Sstevel@tonic-gate 		}
45200Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
45210Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_datap->db_lim;
45220Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)ipha;
45230Sstevel@tonic-gate 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
45240Sstevel@tonic-gate 	}
45250Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
45260Sstevel@tonic-gate 	/* Set version, header length, and tos */
45270Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
45280Sstevel@tonic-gate 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
45295240Snordmark 	    icmp->icmp_type_of_service);
45300Sstevel@tonic-gate 	/* Set ttl and protocol */
45310Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
45320Sstevel@tonic-gate #else
45330Sstevel@tonic-gate 	/* Set version, header length, and tos */
45340Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
45350Sstevel@tonic-gate 	    ((icmp->icmp_type_of_service << 8) |
45365240Snordmark 	    ((IP_VERSION << 4) | (ip_hdr_length>>2)));
45370Sstevel@tonic-gate 	/* Set ttl and protocol */
45380Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
45390Sstevel@tonic-gate #endif
45403318Srshoaib 	if (pktinfop->ip4_addr != INADDR_ANY) {
45413318Srshoaib 		ipha->ipha_src = pktinfop->ip4_addr;
45423318Srshoaib 		optinfo.ip_opt_flags = IP_VERIFY_SRC;
45433318Srshoaib 	} else {
45443318Srshoaib 
45453318Srshoaib 		/*
45463318Srshoaib 		 * Copy our address into the packet.  If this is zero,
45473318Srshoaib 		 * ip will fill in the real source address.
45483318Srshoaib 		 */
45493318Srshoaib 		IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
45503318Srshoaib 	}
45513318Srshoaib 
45520Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
45530Sstevel@tonic-gate 
45543318Srshoaib 	if (pktinfop->ip4_ill_index != 0) {
45553318Srshoaib 		optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
45563318Srshoaib 	}
45573318Srshoaib 
45583318Srshoaib 
45590Sstevel@tonic-gate 	/*
45600Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
45610Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
45620Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
45630Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
45640Sstevel@tonic-gate 	 */
45650Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
45660Sstevel@tonic-gate 
45670Sstevel@tonic-gate 	/* Finish common formatting of the packet. */
45680Sstevel@tonic-gate 	mp1->b_rptr = (uchar_t *)ipha;
45690Sstevel@tonic-gate 
45700Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
45710Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
45720Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
45730Sstevel@tonic-gate 
45740Sstevel@tonic-gate 	/*
45750Sstevel@tonic-gate 	 * Set the length into the IP header.
45760Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
45770Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
45780Sstevel@tonic-gate 	 * as this can cause problems in layers below.
45790Sstevel@tonic-gate 	 */
45800Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
45815240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
45820Sstevel@tonic-gate 		icmp_ud_err(q, mp, EMSGSIZE);
45830Sstevel@tonic-gate 		return;
45840Sstevel@tonic-gate 	}
45850Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)ip_len);
45860Sstevel@tonic-gate 	/*
45870Sstevel@tonic-gate 	 * Copy in the destination address from the T_UNITDATA
45880Sstevel@tonic-gate 	 * request
45890Sstevel@tonic-gate 	 */
45901676Sjpk 	ipha->ipha_dst = v4dst;
45910Sstevel@tonic-gate 
45920Sstevel@tonic-gate 	/*
45930Sstevel@tonic-gate 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
45940Sstevel@tonic-gate 	 */
45950Sstevel@tonic-gate 	if (CLASSD(v4dst))
45960Sstevel@tonic-gate 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
45970Sstevel@tonic-gate 
45980Sstevel@tonic-gate 	/* Copy in options if any */
45990Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
46000Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options,
46010Sstevel@tonic-gate 		    &ipha[1], icmp->icmp_ip_snd_options_len);
46020Sstevel@tonic-gate 		/*
46030Sstevel@tonic-gate 		 * Massage source route putting first source route in ipha_dst.
46040Sstevel@tonic-gate 		 * Ignore the destination in the T_unitdata_req.
46050Sstevel@tonic-gate 		 */
46065240Snordmark 		(void) ip_massage_options(ipha, is->is_netstack);
46070Sstevel@tonic-gate 	}
46083318Srshoaib 
46090Sstevel@tonic-gate 	freeb(mp);
46105240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
46115240Snordmark 	mblk_setcred(mp1, connp->conn_cred);
46125240Snordmark 	ip_output_options(Q_TO_CONN(q), mp1, q, IP_WPUT, &optinfo);
46130Sstevel@tonic-gate #undef	ipha
46140Sstevel@tonic-gate #undef tudr
46150Sstevel@tonic-gate }
46160Sstevel@tonic-gate 
46171676Sjpk static boolean_t
46181676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst)
46191676Sjpk {
46201676Sjpk 	int err;
46211676Sjpk 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
46225240Snordmark 	icmp_stack_t		*is = icmp->icmp_is;
46235240Snordmark 	conn_t	*connp = icmp->icmp_connp;
46245240Snordmark 
46255240Snordmark 	err = tsol_compute_label_v6(DB_CREDDEF(mp, connp->conn_cred), dst,
46263448Sdh155122 	    opt_storage, icmp->icmp_mac_exempt,
46275240Snordmark 	    is->is_netstack->netstack_ip);
46281676Sjpk 	if (err == 0) {
46291676Sjpk 		err = tsol_update_sticky(&icmp->icmp_sticky_ipp,
46301676Sjpk 		    &icmp->icmp_label_len_v6, opt_storage);
46311676Sjpk 	}
46321676Sjpk 	if (err != 0) {
46335240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46341676Sjpk 		DTRACE_PROBE4(
46351676Sjpk 		    tx__ip__log__drop__updatelabel__icmp6,
46361676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
46371676Sjpk 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
46381676Sjpk 		icmp_ud_err(wq, mp, err);
46391676Sjpk 		return (B_FALSE);
46401676Sjpk 	}
46411676Sjpk 
46421676Sjpk 	icmp->icmp_v6lastdst = *dst;
46431676Sjpk 	return (B_TRUE);
46441676Sjpk }
46451676Sjpk 
46460Sstevel@tonic-gate /*
46470Sstevel@tonic-gate  * icmp_wput_ipv6():
46480Sstevel@tonic-gate  * Assumes that icmp_wput did some sanity checking on the destination
46491676Sjpk  * address, but that the label may not yet be correct.
46500Sstevel@tonic-gate  */
46510Sstevel@tonic-gate void
46520Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
46530Sstevel@tonic-gate {
46540Sstevel@tonic-gate 	ip6_t			*ip6h;
46550Sstevel@tonic-gate 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
46560Sstevel@tonic-gate 	mblk_t			*mp1;
46570Sstevel@tonic-gate 	int			ip_hdr_len = IPV6_HDR_LEN;
46580Sstevel@tonic-gate 	size_t			ip_len;
46595240Snordmark 	icmp_t			*icmp = Q_TO_ICMP(q);
46603448Sdh155122 	icmp_stack_t		*is = icmp->icmp_is;
46610Sstevel@tonic-gate 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
46620Sstevel@tonic-gate 	ip6_pkt_t		*ipp = &ipp_s;
46630Sstevel@tonic-gate 	ip6_pkt_t		*tipp;
46640Sstevel@tonic-gate 	uint32_t		csum = 0;
46650Sstevel@tonic-gate 	uint_t			ignore = 0;
46660Sstevel@tonic-gate 	uint_t			option_exists = 0, is_sticky = 0;
46670Sstevel@tonic-gate 	uint8_t			*cp;
46680Sstevel@tonic-gate 	uint8_t			*nxthdr_ptr;
46691676Sjpk 	in6_addr_t		ip6_dst;
46700Sstevel@tonic-gate 
46710Sstevel@tonic-gate 	/*
46720Sstevel@tonic-gate 	 * If the local address is a mapped address return
46730Sstevel@tonic-gate 	 * an error.
46740Sstevel@tonic-gate 	 * It would be possible to send an IPv6 packet but the
46750Sstevel@tonic-gate 	 * response would never make it back to the application
46760Sstevel@tonic-gate 	 * since it is bound to a mapped address.
46770Sstevel@tonic-gate 	 */
46780Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
46795240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46800Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
46810Sstevel@tonic-gate 		return;
46820Sstevel@tonic-gate 	}
46830Sstevel@tonic-gate 
46840Sstevel@tonic-gate 	ipp->ipp_fields = 0;
46850Sstevel@tonic-gate 	ipp->ipp_sticky_ignored = 0;
46860Sstevel@tonic-gate 
46870Sstevel@tonic-gate 	/*
46880Sstevel@tonic-gate 	 * If TPI options passed in, feed it for verification and handling
46890Sstevel@tonic-gate 	 */
46900Sstevel@tonic-gate 	if (tudr_optlen != 0) {
46910Sstevel@tonic-gate 		int error;
46920Sstevel@tonic-gate 
46930Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
46940Sstevel@tonic-gate 		    (void *)ipp) < 0) {
46950Sstevel@tonic-gate 			/* failure */
46965240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46970Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
46980Sstevel@tonic-gate 			return;
46990Sstevel@tonic-gate 		}
47000Sstevel@tonic-gate 		ignore = ipp->ipp_sticky_ignored;
47010Sstevel@tonic-gate 		ASSERT(error == 0);
47020Sstevel@tonic-gate 	}
47030Sstevel@tonic-gate 
47040Sstevel@tonic-gate 	if (sin6->sin6_scope_id != 0 &&
47050Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
47060Sstevel@tonic-gate 		/*
47070Sstevel@tonic-gate 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
47080Sstevel@tonic-gate 		 * option nor ancillary data.  It needs to be
47090Sstevel@tonic-gate 		 * explicitly set in options_exists.
47100Sstevel@tonic-gate 		 */
47110Sstevel@tonic-gate 		option_exists |= IPPF_SCOPE_ID;
47120Sstevel@tonic-gate 	}
47130Sstevel@tonic-gate 
47141676Sjpk 	/*
47151676Sjpk 	 * Compute the destination address
47161676Sjpk 	 */
47171676Sjpk 	ip6_dst = sin6->sin6_addr;
47181676Sjpk 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
47191676Sjpk 		ip6_dst = ipv6_loopback;
47201676Sjpk 
47211676Sjpk 	/*
47221676Sjpk 	 * If we're not going to the same destination as last time, then
47231676Sjpk 	 * recompute the label required.  This is done in a separate routine to
47241676Sjpk 	 * avoid blowing up our stack here.
47251676Sjpk 	 */
47261676Sjpk 	if (is_system_labeled() &&
47271676Sjpk 	    !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) &&
47281676Sjpk 	    !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) {
47291676Sjpk 		return;
47301676Sjpk 	}
47311676Sjpk 
47321676Sjpk 	/*
47331676Sjpk 	 * If there's a security label here, then we ignore any options the
47341676Sjpk 	 * user may try to set.  We keep the peer's label as a hidden sticky
47351676Sjpk 	 * option.
47361676Sjpk 	 */
47371676Sjpk 	if (icmp->icmp_label_len_v6 > 0) {
47381676Sjpk 		ignore &= ~IPPF_HOPOPTS;
47391676Sjpk 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
47401676Sjpk 	}
47411676Sjpk 
47420Sstevel@tonic-gate 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
47430Sstevel@tonic-gate 	    (ipp->ipp_fields == 0)) {
47440Sstevel@tonic-gate 		/* No sticky options nor ancillary data. */
47450Sstevel@tonic-gate 		goto no_options;
47460Sstevel@tonic-gate 	}
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 	/*
47490Sstevel@tonic-gate 	 * Go through the options figuring out where each is going to
47500Sstevel@tonic-gate 	 * come from and build two masks.  The first mask indicates if
47510Sstevel@tonic-gate 	 * the option exists at all.  The second mask indicates if the
47520Sstevel@tonic-gate 	 * option is sticky or ancillary.
47530Sstevel@tonic-gate 	 */
47540Sstevel@tonic-gate 	if (!(ignore & IPPF_HOPOPTS)) {
47550Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
47560Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
47570Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_hopoptslen;
47580Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
47590Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
47600Sstevel@tonic-gate 			is_sticky |= IPPF_HOPOPTS;
47610Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
47620Sstevel@tonic-gate 		}
47630Sstevel@tonic-gate 	}
47640Sstevel@tonic-gate 
47650Sstevel@tonic-gate 	if (!(ignore & IPPF_RTHDR)) {
47660Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTHDR) {
47670Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
47680Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rthdrlen;
47690Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
47700Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
47710Sstevel@tonic-gate 			is_sticky |= IPPF_RTHDR;
47720Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
47730Sstevel@tonic-gate 		}
47740Sstevel@tonic-gate 	}
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
47770Sstevel@tonic-gate 		/*
47780Sstevel@tonic-gate 		 * Need to have a router header to use these.
47790Sstevel@tonic-gate 		 */
47800Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
47810Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
47820Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rtdstoptslen;
47830Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
47840Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
47850Sstevel@tonic-gate 			is_sticky |= IPPF_RTDSTOPTS;
47860Sstevel@tonic-gate 			ip_hdr_len +=
47870Sstevel@tonic-gate 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
47880Sstevel@tonic-gate 		}
47890Sstevel@tonic-gate 	}
47900Sstevel@tonic-gate 
47910Sstevel@tonic-gate 	if (!(ignore & IPPF_DSTOPTS)) {
47920Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
47930Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
47940Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_dstoptslen;
47950Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
47960Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
47970Sstevel@tonic-gate 			is_sticky |= IPPF_DSTOPTS;
47980Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
47990Sstevel@tonic-gate 		}
48000Sstevel@tonic-gate 	}
48010Sstevel@tonic-gate 
48020Sstevel@tonic-gate 	if (!(ignore & IPPF_IFINDEX)) {
48030Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_IFINDEX) {
48040Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
48050Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
48060Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
48070Sstevel@tonic-gate 			is_sticky |= IPPF_IFINDEX;
48080Sstevel@tonic-gate 		}
48090Sstevel@tonic-gate 	}
48100Sstevel@tonic-gate 
48110Sstevel@tonic-gate 	if (!(ignore & IPPF_ADDR)) {
48120Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_ADDR) {
48130Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
48140Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
48150Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
48160Sstevel@tonic-gate 			is_sticky |= IPPF_ADDR;
48170Sstevel@tonic-gate 		}
48180Sstevel@tonic-gate 	}
48190Sstevel@tonic-gate 
48200Sstevel@tonic-gate 	if (!(ignore & IPPF_DONTFRAG)) {
48210Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
48220Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
48230Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
48240Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
48250Sstevel@tonic-gate 			is_sticky |= IPPF_DONTFRAG;
48260Sstevel@tonic-gate 		}
48270Sstevel@tonic-gate 	}
48280Sstevel@tonic-gate 
48290Sstevel@tonic-gate 	if (!(ignore & IPPF_USE_MIN_MTU)) {
48300Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
48310Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
48320Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
48330Sstevel@tonic-gate 		    IPPF_USE_MIN_MTU) {
48340Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
48350Sstevel@tonic-gate 			is_sticky |= IPPF_USE_MIN_MTU;
48360Sstevel@tonic-gate 		}
48370Sstevel@tonic-gate 	}
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	if (!(ignore & IPPF_NEXTHOP)) {
48400Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
48410Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
48420Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
48430Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
48440Sstevel@tonic-gate 			is_sticky |= IPPF_NEXTHOP;
48450Sstevel@tonic-gate 		}
48460Sstevel@tonic-gate 	}
48470Sstevel@tonic-gate 
4848679Sseb 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4849679Sseb 		option_exists |= IPPF_HOPLIMIT;
4850679Sseb 	/* IPV6_HOPLIMIT can never be sticky */
4851679Sseb 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4852679Sseb 
4853679Sseb 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4854679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4855679Sseb 		option_exists |= IPPF_UNICAST_HOPS;
4856679Sseb 		is_sticky |= IPPF_UNICAST_HOPS;
4857679Sseb 	}
4858679Sseb 
4859679Sseb 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4860679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4861679Sseb 		option_exists |= IPPF_MULTICAST_HOPS;
4862679Sseb 		is_sticky |= IPPF_MULTICAST_HOPS;
48630Sstevel@tonic-gate 	}
48640Sstevel@tonic-gate 
48650Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
48660Sstevel@tonic-gate 		/* This is a sticky socket option only */
48670Sstevel@tonic-gate 		option_exists |= IPPF_NO_CKSUM;
48680Sstevel@tonic-gate 		is_sticky |= IPPF_NO_CKSUM;
48690Sstevel@tonic-gate 	}
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
48720Sstevel@tonic-gate 		/* This is a sticky socket option only */
48730Sstevel@tonic-gate 		option_exists |= IPPF_RAW_CKSUM;
48740Sstevel@tonic-gate 		is_sticky |= IPPF_RAW_CKSUM;
48750Sstevel@tonic-gate 	}
48760Sstevel@tonic-gate 
48770Sstevel@tonic-gate 	if (!(ignore & IPPF_TCLASS)) {
48780Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_TCLASS) {
48790Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
48800Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
48810Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
48820Sstevel@tonic-gate 			is_sticky |= IPPF_TCLASS;
48830Sstevel@tonic-gate 		}
48840Sstevel@tonic-gate 	}
48850Sstevel@tonic-gate 
48860Sstevel@tonic-gate no_options:
48870Sstevel@tonic-gate 
48880Sstevel@tonic-gate 	/*
48890Sstevel@tonic-gate 	 * If any options carried in the ip6i_t were specified, we
48900Sstevel@tonic-gate 	 * need to account for the ip6i_t in the data we'll be sending
48910Sstevel@tonic-gate 	 * down.
48920Sstevel@tonic-gate 	 */
48930Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I)
48940Sstevel@tonic-gate 		ip_hdr_len += sizeof (ip6i_t);
48950Sstevel@tonic-gate 
48960Sstevel@tonic-gate 	/* check/fix buffer config, setup pointers into it */
48970Sstevel@tonic-gate 	mp1 = mp->b_cont;
48980Sstevel@tonic-gate 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
48990Sstevel@tonic-gate 	if ((mp1->b_datap->db_ref != 1) ||
49000Sstevel@tonic-gate 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
49010Sstevel@tonic-gate 	    !OK_32PTR(ip6h)) {
49020Sstevel@tonic-gate 		/* Try to get everything in a single mblk next time */
49030Sstevel@tonic-gate 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
49040Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = ip_hdr_len;
49050Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q),
49063448Sdh155122 			    icmp->icmp_max_hdr_len + is->is_wroff_extra);
49070Sstevel@tonic-gate 		}
49083448Sdh155122 		mp1 = allocb(ip_hdr_len + is->is_wroff_extra, BPRI_LO);
49090Sstevel@tonic-gate 		if (!mp1) {
49105240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
49110Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
49120Sstevel@tonic-gate 			return;
49130Sstevel@tonic-gate 		}
49140Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
49150Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_datap->db_lim;
49160Sstevel@tonic-gate 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
49170Sstevel@tonic-gate 	}
49180Sstevel@tonic-gate 	mp1->b_rptr = (unsigned char *)ip6h;
49190Sstevel@tonic-gate 	ip6i = (ip6i_t *)ip6h;
49200Sstevel@tonic-gate 
49210Sstevel@tonic-gate #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
49220Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I) {
49230Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
49240Sstevel@tonic-gate 		ip6i->ip6i_flags = 0;
49250Sstevel@tonic-gate 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
49260Sstevel@tonic-gate 
49270Sstevel@tonic-gate 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
49280Sstevel@tonic-gate 		if (option_exists & IPPF_SCOPE_ID) {
49290Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
49300Sstevel@tonic-gate 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
49310Sstevel@tonic-gate 		} else if (option_exists & IPPF_IFINDEX) {
49320Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
49330Sstevel@tonic-gate 			ASSERT(tipp->ipp_ifindex != 0);
49340Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
49350Sstevel@tonic-gate 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
49360Sstevel@tonic-gate 		}
49370Sstevel@tonic-gate 
49380Sstevel@tonic-gate 		if (option_exists & IPPF_RAW_CKSUM) {
49390Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
49400Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
49410Sstevel@tonic-gate 		}
49420Sstevel@tonic-gate 
49430Sstevel@tonic-gate 		if (option_exists & IPPF_NO_CKSUM) {
49440Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
49450Sstevel@tonic-gate 		}
49460Sstevel@tonic-gate 
49470Sstevel@tonic-gate 		if (option_exists & IPPF_ADDR) {
49480Sstevel@tonic-gate 			/*
49490Sstevel@tonic-gate 			 * Enable per-packet source address verification if
49500Sstevel@tonic-gate 			 * IPV6_PKTINFO specified the source address.
49510Sstevel@tonic-gate 			 * ip6_src is set in the transport's _wput function.
49520Sstevel@tonic-gate 			 */
49530Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
49540Sstevel@tonic-gate 		}
49550Sstevel@tonic-gate 
49560Sstevel@tonic-gate 		if (option_exists & IPPF_DONTFRAG) {
49570Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
49580Sstevel@tonic-gate 		}
49590Sstevel@tonic-gate 
49600Sstevel@tonic-gate 		if (option_exists & IPPF_USE_MIN_MTU) {
49610Sstevel@tonic-gate 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
49620Sstevel@tonic-gate 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
49630Sstevel@tonic-gate 		}
49640Sstevel@tonic-gate 
49650Sstevel@tonic-gate 		if (option_exists & IPPF_NEXTHOP) {
49660Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
49670Sstevel@tonic-gate 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
49680Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
49690Sstevel@tonic-gate 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
49700Sstevel@tonic-gate 		}
49710Sstevel@tonic-gate 
49720Sstevel@tonic-gate 		/*
49730Sstevel@tonic-gate 		 * tell IP this is an ip6i_t private header
49740Sstevel@tonic-gate 		 */
49750Sstevel@tonic-gate 		ip6i->ip6i_nxt = IPPROTO_RAW;
49760Sstevel@tonic-gate 	}
49770Sstevel@tonic-gate 
49780Sstevel@tonic-gate 	/* Initialize IPv6 header */
49790Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
49800Sstevel@tonic-gate 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
49810Sstevel@tonic-gate 
4982679Sseb 	/* Set the hoplimit of the outgoing packet. */
49830Sstevel@tonic-gate 	if (option_exists & IPPF_HOPLIMIT) {
4984679Sseb 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4985679Sseb 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4986679Sseb 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4987679Sseb 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
49880Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4989679Sseb 		if (option_exists & IPPF_MULTICAST_HOPS)
4990679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
49910Sstevel@tonic-gate 	} else {
49920Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_ttl;
4993679Sseb 		if (option_exists & IPPF_UNICAST_HOPS)
4994679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
49950Sstevel@tonic-gate 	}
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	if (option_exists & IPPF_ADDR) {
49980Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
49990Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
50000Sstevel@tonic-gate 		ip6h->ip6_src = tipp->ipp_addr;
50010Sstevel@tonic-gate 	} else {
50020Sstevel@tonic-gate 		/*
50030Sstevel@tonic-gate 		 * The source address was not set using IPV6_PKTINFO.
50040Sstevel@tonic-gate 		 * First look at the bound source.
50050Sstevel@tonic-gate 		 * If unspecified fallback to __sin6_src_id.
50060Sstevel@tonic-gate 		 */
50070Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
50080Sstevel@tonic-gate 		if (sin6->__sin6_src_id != 0 &&
50090Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
50100Sstevel@tonic-gate 			ip_srcid_find_id(sin6->__sin6_src_id,
50113448Sdh155122 			    &ip6h->ip6_src, icmp->icmp_zoneid,
50123448Sdh155122 			    is->is_netstack);
50130Sstevel@tonic-gate 		}
50140Sstevel@tonic-gate 	}
50150Sstevel@tonic-gate 
50160Sstevel@tonic-gate 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
50170Sstevel@tonic-gate 	cp = (uint8_t *)&ip6h[1];
50180Sstevel@tonic-gate 
50190Sstevel@tonic-gate 	/*
50200Sstevel@tonic-gate 	 * Here's where we have to start stringing together
50210Sstevel@tonic-gate 	 * any extension headers in the right order:
50220Sstevel@tonic-gate 	 * Hop-by-hop, destination, routing, and final destination opts.
50230Sstevel@tonic-gate 	 */
50240Sstevel@tonic-gate 	if (option_exists & IPPF_HOPOPTS) {
50250Sstevel@tonic-gate 		/* Hop-by-hop options */
50260Sstevel@tonic-gate 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
50270Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
50280Sstevel@tonic-gate 
50290Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_HOPOPTS;
50300Sstevel@tonic-gate 		nxthdr_ptr = &hbh->ip6h_nxt;
50310Sstevel@tonic-gate 
50320Sstevel@tonic-gate 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
50330Sstevel@tonic-gate 		cp += tipp->ipp_hopoptslen;
50340Sstevel@tonic-gate 	}
50350Sstevel@tonic-gate 	/*
50360Sstevel@tonic-gate 	 * En-route destination options
50370Sstevel@tonic-gate 	 * Only do them if there's a routing header as well
50380Sstevel@tonic-gate 	 */
50390Sstevel@tonic-gate 	if (option_exists & IPPF_RTDSTOPTS) {
50400Sstevel@tonic-gate 		ip6_dest_t *dst = (ip6_dest_t *)cp;
50410Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
50420Sstevel@tonic-gate 
50430Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
50440Sstevel@tonic-gate 		nxthdr_ptr = &dst->ip6d_nxt;
50450Sstevel@tonic-gate 
50460Sstevel@tonic-gate 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
50470Sstevel@tonic-gate 		cp += tipp->ipp_rtdstoptslen;
50480Sstevel@tonic-gate 	}
50490Sstevel@tonic-gate 	/*
50500Sstevel@tonic-gate 	 * Routing header next
50510Sstevel@tonic-gate 	 */
50520Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
50530Sstevel@tonic-gate 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
50540Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
50550Sstevel@tonic-gate 
50560Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_ROUTING;
50570Sstevel@tonic-gate 		nxthdr_ptr = &rt->ip6r_nxt;
50580Sstevel@tonic-gate 
50590Sstevel@tonic-gate 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
50600Sstevel@tonic-gate 		cp += tipp->ipp_rthdrlen;
50610Sstevel@tonic-gate 	}
50620Sstevel@tonic-gate 	/*
50630Sstevel@tonic-gate 	 * Do ultimate destination options
50640Sstevel@tonic-gate 	 */
50650Sstevel@tonic-gate 	if (option_exists & IPPF_DSTOPTS) {
50660Sstevel@tonic-gate 		ip6_dest_t *dest = (ip6_dest_t *)cp;
50670Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
50680Sstevel@tonic-gate 
50690Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
50700Sstevel@tonic-gate 		nxthdr_ptr = &dest->ip6d_nxt;
50710Sstevel@tonic-gate 
50720Sstevel@tonic-gate 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
50730Sstevel@tonic-gate 		cp += tipp->ipp_dstoptslen;
50740Sstevel@tonic-gate 	}
50750Sstevel@tonic-gate 
50760Sstevel@tonic-gate 	/*
50770Sstevel@tonic-gate 	 * Now set the last header pointer to the proto passed in
50780Sstevel@tonic-gate 	 */
50790Sstevel@tonic-gate 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
50800Sstevel@tonic-gate 	*nxthdr_ptr = icmp->icmp_proto;
50810Sstevel@tonic-gate 
50820Sstevel@tonic-gate 	/*
50830Sstevel@tonic-gate 	 * Copy in the destination address
50840Sstevel@tonic-gate 	 */
50851676Sjpk 	ip6h->ip6_dst = ip6_dst;
50860Sstevel@tonic-gate 
50870Sstevel@tonic-gate 	ip6h->ip6_vcf =
50885240Snordmark 	    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
50895240Snordmark 	    (sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
50900Sstevel@tonic-gate 
50910Sstevel@tonic-gate 	if (option_exists & IPPF_TCLASS) {
50920Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
50930Sstevel@tonic-gate 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
50940Sstevel@tonic-gate 		    tipp->ipp_tclass);
50950Sstevel@tonic-gate 	}
50960Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
50970Sstevel@tonic-gate 		ip6_rthdr_t	*rth;
50980Sstevel@tonic-gate 
50990Sstevel@tonic-gate 		/*
51000Sstevel@tonic-gate 		 * Perform any processing needed for source routing.
51010Sstevel@tonic-gate 		 * We know that all extension headers will be in the same mblk
51020Sstevel@tonic-gate 		 * as the IPv6 header.
51030Sstevel@tonic-gate 		 */
51040Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
51050Sstevel@tonic-gate 		if (rth != NULL && rth->ip6r_segleft != 0) {
51060Sstevel@tonic-gate 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
51070Sstevel@tonic-gate 				/*
51080Sstevel@tonic-gate 				 * Drop packet - only support Type 0 routing.
51090Sstevel@tonic-gate 				 * Notify the application as well.
51100Sstevel@tonic-gate 				 */
51110Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
51125240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51133448Sdh155122 				    rawipOutErrors);
51140Sstevel@tonic-gate 				return;
51150Sstevel@tonic-gate 			}
51160Sstevel@tonic-gate 			/*
51170Sstevel@tonic-gate 			 * rth->ip6r_len is twice the number of
51180Sstevel@tonic-gate 			 * addresses in the header
51190Sstevel@tonic-gate 			 */
51200Sstevel@tonic-gate 			if (rth->ip6r_len & 0x1) {
51210Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
51225240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51233448Sdh155122 				    rawipOutErrors);
51240Sstevel@tonic-gate 				return;
51250Sstevel@tonic-gate 			}
51260Sstevel@tonic-gate 			/*
51270Sstevel@tonic-gate 			 * Shuffle the routing header and ip6_dst
51280Sstevel@tonic-gate 			 * addresses, and get the checksum difference
51290Sstevel@tonic-gate 			 * between the first hop (in ip6_dst) and
51300Sstevel@tonic-gate 			 * the destination (in the last routing hdr entry).
51310Sstevel@tonic-gate 			 */
51323448Sdh155122 			csum = ip_massage_options_v6(ip6h, rth,
51335240Snordmark 			    is->is_netstack);
51340Sstevel@tonic-gate 			/*
51350Sstevel@tonic-gate 			 * Verify that the first hop isn't a mapped address.
51360Sstevel@tonic-gate 			 * Routers along the path need to do this verification
51370Sstevel@tonic-gate 			 * for subsequent hops.
51380Sstevel@tonic-gate 			 */
51390Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
51400Sstevel@tonic-gate 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
51415240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51423448Sdh155122 				    rawipOutErrors);
51430Sstevel@tonic-gate 				return;
51440Sstevel@tonic-gate 			}
51450Sstevel@tonic-gate 		}
51460Sstevel@tonic-gate 	}
51470Sstevel@tonic-gate 
51480Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
51490Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
51500Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
51510Sstevel@tonic-gate 
51520Sstevel@tonic-gate 	/*
51530Sstevel@tonic-gate 	 * Set the length into the IP header.
51540Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
51550Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
51560Sstevel@tonic-gate 	 * as this can cause problems in layers below.
51570Sstevel@tonic-gate 	 */
51580Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
51595240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
51601289Sja97890 		icmp_ud_err(q, mp, EMSGSIZE);
51610Sstevel@tonic-gate 		return;
51620Sstevel@tonic-gate 	}
51630Sstevel@tonic-gate 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
51640Sstevel@tonic-gate 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
51650Sstevel@tonic-gate 		uint16_t *cksum_ptr;
51660Sstevel@tonic-gate 		uint_t	ext_hdrs_len;
51670Sstevel@tonic-gate 
51680Sstevel@tonic-gate 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
51690Sstevel@tonic-gate 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
51700Sstevel@tonic-gate 		    icmp->icmp_checksum_off == 2);
51710Sstevel@tonic-gate 
51720Sstevel@tonic-gate 		/*
51730Sstevel@tonic-gate 		 * We make it easy for IP to include our pseudo header
51740Sstevel@tonic-gate 		 * by putting our length in uh_checksum, modified (if
51750Sstevel@tonic-gate 		 * we have a routing header) by the checksum difference
51760Sstevel@tonic-gate 		 * between the ultimate destination and first hop addresses.
51770Sstevel@tonic-gate 		 * Note: ICMPv6 must always checksum the packet.
51780Sstevel@tonic-gate 		 */
51790Sstevel@tonic-gate 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
51800Sstevel@tonic-gate 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
51810Sstevel@tonic-gate 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
51825240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51833448Sdh155122 				    rawipOutErrors);
51840Sstevel@tonic-gate 				freemsg(mp);
51850Sstevel@tonic-gate 				return;
51860Sstevel@tonic-gate 			}
51870Sstevel@tonic-gate 			ip6i = (ip6i_t *)mp1->b_rptr;
51880Sstevel@tonic-gate 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
51890Sstevel@tonic-gate 				ip6h = (ip6_t *)&ip6i[1];
51900Sstevel@tonic-gate 			else
51910Sstevel@tonic-gate 				ip6h = (ip6_t *)ip6i;
51920Sstevel@tonic-gate 		}
51930Sstevel@tonic-gate 		/* Add payload length to checksum */
51940Sstevel@tonic-gate 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
51950Sstevel@tonic-gate 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
51960Sstevel@tonic-gate 		csum += htons(ip_len - ext_hdrs_len);
51970Sstevel@tonic-gate 
51980Sstevel@tonic-gate 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
51990Sstevel@tonic-gate 		csum = (csum & 0xFFFF) + (csum >> 16);
52000Sstevel@tonic-gate 		*cksum_ptr = (uint16_t)csum;
52010Sstevel@tonic-gate 	}
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
52040Sstevel@tonic-gate 	ip_len = htons(ip_len);
52050Sstevel@tonic-gate #endif
52060Sstevel@tonic-gate 	ip6h->ip6_plen = (uint16_t)ip_len;
52070Sstevel@tonic-gate 
52080Sstevel@tonic-gate 	freeb(mp);
52090Sstevel@tonic-gate 
52100Sstevel@tonic-gate 	/* We're done. Pass the packet to IP */
52115240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
52125240Snordmark 	ip_output_v6(icmp->icmp_connp, mp1, q, IP_WPUT);
52130Sstevel@tonic-gate }
52140Sstevel@tonic-gate 
52150Sstevel@tonic-gate static void
52160Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp)
52170Sstevel@tonic-gate {
52180Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
52190Sstevel@tonic-gate 	struct iocblk *iocp;
52200Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
52215240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
52225240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
52235240Snordmark 	icmp_stack_t *is = icmp->icmp_is;
52240Sstevel@tonic-gate 	cred_t *cr;
52250Sstevel@tonic-gate 
52265240Snordmark 	cr = DB_CREDDEF(mp, connp->conn_cred);
52270Sstevel@tonic-gate 
52280Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
52290Sstevel@tonic-gate 	case M_PROTO:
52300Sstevel@tonic-gate 	case M_PCPROTO:
52310Sstevel@tonic-gate 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
52320Sstevel@tonic-gate 			/*
52330Sstevel@tonic-gate 			 * If the message does not contain a PRIM_type,
52340Sstevel@tonic-gate 			 * throw it away.
52350Sstevel@tonic-gate 			 */
52360Sstevel@tonic-gate 			freemsg(mp);
52370Sstevel@tonic-gate 			return;
52380Sstevel@tonic-gate 		}
52390Sstevel@tonic-gate 		switch (((union T_primitives *)rptr)->type) {
52400Sstevel@tonic-gate 		case T_ADDR_REQ:
52410Sstevel@tonic-gate 			icmp_addr_req(q, mp);
52420Sstevel@tonic-gate 			return;
52430Sstevel@tonic-gate 		case O_T_BIND_REQ:
52440Sstevel@tonic-gate 		case T_BIND_REQ:
52455240Snordmark 			icmp_bind(q, mp);
52460Sstevel@tonic-gate 			return;
52470Sstevel@tonic-gate 		case T_CONN_REQ:
52480Sstevel@tonic-gate 			icmp_connect(q, mp);
52490Sstevel@tonic-gate 			return;
52500Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
52510Sstevel@tonic-gate 			icmp_capability_req(q, mp);
52520Sstevel@tonic-gate 			return;
52530Sstevel@tonic-gate 		case T_INFO_REQ:
52540Sstevel@tonic-gate 			icmp_info_req(q, mp);
52550Sstevel@tonic-gate 			return;
52560Sstevel@tonic-gate 		case T_UNITDATA_REQ:
52570Sstevel@tonic-gate 			/*
52580Sstevel@tonic-gate 			 * If a T_UNITDATA_REQ gets here, the address must
52590Sstevel@tonic-gate 			 * be bad.  Valid T_UNITDATA_REQs are found above
52600Sstevel@tonic-gate 			 * and break to below this switch.
52610Sstevel@tonic-gate 			 */
52620Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
52630Sstevel@tonic-gate 			return;
52640Sstevel@tonic-gate 		case T_UNBIND_REQ:
52650Sstevel@tonic-gate 			icmp_unbind(q, mp);
52660Sstevel@tonic-gate 			return;
52670Sstevel@tonic-gate 
52680Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
52695240Snordmark 			if (!snmpcom_req(q, mp, icmp_snmp_set, ip_snmp_get,
52705240Snordmark 			    cr)) {
52710Sstevel@tonic-gate 				/* Only IP can return anything meaningful */
52720Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
52735240Snordmark 				    &icmp_opt_obj, B_TRUE);
52745240Snordmark 			}
52750Sstevel@tonic-gate 			return;
52760Sstevel@tonic-gate 
52770Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
52780Sstevel@tonic-gate 			/* Only IP can return anything meaningful */
52795240Snordmark 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj, B_TRUE);
52800Sstevel@tonic-gate 			return;
52810Sstevel@tonic-gate 
52820Sstevel@tonic-gate 		case T_DISCON_REQ:
52830Sstevel@tonic-gate 			icmp_disconnect(q, mp);
52840Sstevel@tonic-gate 			return;
52850Sstevel@tonic-gate 
52860Sstevel@tonic-gate 		/* The following TPI message is not supported by icmp. */
52870Sstevel@tonic-gate 		case O_T_CONN_RES:
52880Sstevel@tonic-gate 		case T_CONN_RES:
52890Sstevel@tonic-gate 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
52900Sstevel@tonic-gate 			return;
52910Sstevel@tonic-gate 
52920Sstevel@tonic-gate 		/* The following 3 TPI requests are illegal for icmp. */
52930Sstevel@tonic-gate 		case T_DATA_REQ:
52940Sstevel@tonic-gate 		case T_EXDATA_REQ:
52950Sstevel@tonic-gate 		case T_ORDREL_REQ:
52960Sstevel@tonic-gate 			freemsg(mp);
52970Sstevel@tonic-gate 			(void) putctl1(RD(q), M_ERROR, EPROTO);
52980Sstevel@tonic-gate 			return;
52990Sstevel@tonic-gate 		default:
53000Sstevel@tonic-gate 			break;
53010Sstevel@tonic-gate 		}
53020Sstevel@tonic-gate 		break;
53030Sstevel@tonic-gate 	case M_IOCTL:
53040Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
53050Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
53060Sstevel@tonic-gate 		case TI_GETPEERNAME:
53070Sstevel@tonic-gate 			if (icmp->icmp_state != TS_DATA_XFER) {
53080Sstevel@tonic-gate 				/*
53090Sstevel@tonic-gate 				 * If a default destination address has not
53100Sstevel@tonic-gate 				 * been associated with the stream, then we
53110Sstevel@tonic-gate 				 * don't know the peer's name.
53120Sstevel@tonic-gate 				 */
53130Sstevel@tonic-gate 				iocp->ioc_error = ENOTCONN;
53145240Snordmark 		err_ret:;
53150Sstevel@tonic-gate 				iocp->ioc_count = 0;
53160Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
53170Sstevel@tonic-gate 				qreply(q, mp);
53180Sstevel@tonic-gate 				return;
53190Sstevel@tonic-gate 			}
53200Sstevel@tonic-gate 			/* FALLTHRU */
53210Sstevel@tonic-gate 		case TI_GETMYNAME:
53220Sstevel@tonic-gate 			/*
53230Sstevel@tonic-gate 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
53240Sstevel@tonic-gate 			 * need to copyin the user's strbuf structure.
53250Sstevel@tonic-gate 			 * Processing will continue in the M_IOCDATA case
53260Sstevel@tonic-gate 			 * below.
53270Sstevel@tonic-gate 			 */
53280Sstevel@tonic-gate 			mi_copyin(q, mp, NULL,
53290Sstevel@tonic-gate 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
53300Sstevel@tonic-gate 			return;
53310Sstevel@tonic-gate 		case ND_SET:
53320Sstevel@tonic-gate 			/* nd_getset performs the necessary error checking */
53330Sstevel@tonic-gate 		case ND_GET:
53345240Snordmark 			if (nd_getset(q, is->is_nd, mp)) {
53350Sstevel@tonic-gate 				qreply(q, mp);
53360Sstevel@tonic-gate 				return;
53370Sstevel@tonic-gate 			}
53380Sstevel@tonic-gate 			break;
53390Sstevel@tonic-gate 		default:
53400Sstevel@tonic-gate 			break;
53410Sstevel@tonic-gate 		}
53420Sstevel@tonic-gate 		break;
53430Sstevel@tonic-gate 	case M_IOCDATA:
53440Sstevel@tonic-gate 		icmp_wput_iocdata(q, mp);
53450Sstevel@tonic-gate 		return;
53460Sstevel@tonic-gate 	default:
53470Sstevel@tonic-gate 		break;
53480Sstevel@tonic-gate 	}
53495240Snordmark 	ip_wput(q, mp);
53500Sstevel@tonic-gate }
53510Sstevel@tonic-gate 
53520Sstevel@tonic-gate /*
53530Sstevel@tonic-gate  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
53540Sstevel@tonic-gate  * messages.
53550Sstevel@tonic-gate  */
53560Sstevel@tonic-gate static void
53570Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp)
53580Sstevel@tonic-gate {
53590Sstevel@tonic-gate 	mblk_t	*mp1;
53600Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
53610Sstevel@tonic-gate 	icmp_t	*icmp;
53620Sstevel@tonic-gate 	in6_addr_t	v6addr;
53630Sstevel@tonic-gate 	ipaddr_t	v4addr;
53640Sstevel@tonic-gate 	uint32_t	flowinfo = 0;
53650Sstevel@tonic-gate 	int		addrlen;
53660Sstevel@tonic-gate 
53670Sstevel@tonic-gate 	/* Make sure it is one of ours. */
53680Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
53690Sstevel@tonic-gate 	case TI_GETMYNAME:
53700Sstevel@tonic-gate 	case TI_GETPEERNAME:
53710Sstevel@tonic-gate 		break;
53720Sstevel@tonic-gate 	default:
53735240Snordmark 		icmp = Q_TO_ICMP(q);
53745240Snordmark 		ip_output(icmp->icmp_connp, mp, q, IP_WPUT);
53750Sstevel@tonic-gate 		return;
53760Sstevel@tonic-gate 	}
53770Sstevel@tonic-gate 	switch (mi_copy_state(q, mp, &mp1)) {
53780Sstevel@tonic-gate 	case -1:
53790Sstevel@tonic-gate 		return;
53800Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_IN, 1):
53810Sstevel@tonic-gate 		break;
53820Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 1):
53830Sstevel@tonic-gate 		/*
53840Sstevel@tonic-gate 		 * The address has been copied out, so now
53850Sstevel@tonic-gate 		 * copyout the strbuf.
53860Sstevel@tonic-gate 		 */
53870Sstevel@tonic-gate 		mi_copyout(q, mp);
53880Sstevel@tonic-gate 		return;
53890Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 2):
53900Sstevel@tonic-gate 		/*
53910Sstevel@tonic-gate 		 * The address and strbuf have been copied out.
53920Sstevel@tonic-gate 		 * We're done, so just acknowledge the original
53930Sstevel@tonic-gate 		 * M_IOCTL.
53940Sstevel@tonic-gate 		 */
53950Sstevel@tonic-gate 		mi_copy_done(q, mp, 0);
53960Sstevel@tonic-gate 		return;
53970Sstevel@tonic-gate 	default:
53980Sstevel@tonic-gate 		/*
53990Sstevel@tonic-gate 		 * Something strange has happened, so acknowledge
54000Sstevel@tonic-gate 		 * the original M_IOCTL with an EPROTO error.
54010Sstevel@tonic-gate 		 */
54020Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
54030Sstevel@tonic-gate 		return;
54040Sstevel@tonic-gate 	}
54050Sstevel@tonic-gate 	/*
54060Sstevel@tonic-gate 	 * Now we have the strbuf structure for TI_GETMYNAME
54070Sstevel@tonic-gate 	 * and TI_GETPEERNAME.  Next we copyout the requested
54080Sstevel@tonic-gate 	 * address and then we'll copyout the strbuf.
54090Sstevel@tonic-gate 	 */
54100Sstevel@tonic-gate 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
54110Sstevel@tonic-gate 	    (void *)mp1->b_rptr);
54125240Snordmark 	icmp = Q_TO_ICMP(q);
54130Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET)
54140Sstevel@tonic-gate 		addrlen = sizeof (sin_t);
54150Sstevel@tonic-gate 	else
54160Sstevel@tonic-gate 		addrlen = sizeof (sin6_t);
54170Sstevel@tonic-gate 
54180Sstevel@tonic-gate 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
54190Sstevel@tonic-gate 		mi_copy_done(q, mp, EINVAL);
54200Sstevel@tonic-gate 		return;
54210Sstevel@tonic-gate 	}
54220Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
54230Sstevel@tonic-gate 	case TI_GETMYNAME:
54240Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
54250Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
54260Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
54270Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
54280Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
54290Sstevel@tonic-gate 			} else {
54300Sstevel@tonic-gate 				/*
54310Sstevel@tonic-gate 				 * INADDR_ANY
54320Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
54330Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
54340Sstevel@tonic-gate 				 * local address instead (that could
54350Sstevel@tonic-gate 				 * also still be INADDR_ANY)
54360Sstevel@tonic-gate 				 */
54370Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
54380Sstevel@tonic-gate 			}
54390Sstevel@tonic-gate 		} else {
54400Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6 */
54410Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
54420Sstevel@tonic-gate 				v6addr = icmp->icmp_v6src;
54430Sstevel@tonic-gate 			} else {
54440Sstevel@tonic-gate 				/*
54450Sstevel@tonic-gate 				 * UNSPECIFIED
54460Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
54470Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
54480Sstevel@tonic-gate 				 * local address instead (that could
54490Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
54500Sstevel@tonic-gate 				 */
54510Sstevel@tonic-gate 				v6addr = icmp->icmp_bound_v6src;
54520Sstevel@tonic-gate 			}
54530Sstevel@tonic-gate 		}
54540Sstevel@tonic-gate 		break;
54550Sstevel@tonic-gate 	case TI_GETPEERNAME:
54560Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
54570Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
54580Sstevel@tonic-gate 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
54590Sstevel@tonic-gate 		} else {
54600Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6) */
54610Sstevel@tonic-gate 			v6addr = icmp->icmp_v6dst;
54620Sstevel@tonic-gate 			flowinfo = icmp->icmp_flowinfo;
54630Sstevel@tonic-gate 		}
54640Sstevel@tonic-gate 		break;
54650Sstevel@tonic-gate 	default:
54660Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
54670Sstevel@tonic-gate 		return;
54680Sstevel@tonic-gate 	}
54690Sstevel@tonic-gate 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
54700Sstevel@tonic-gate 	if (!mp1)
54710Sstevel@tonic-gate 		return;
54720Sstevel@tonic-gate 
54730Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
54740Sstevel@tonic-gate 		sin_t *sin;
54750Sstevel@tonic-gate 
54760Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
54770Sstevel@tonic-gate 		sin = (sin_t *)mp1->b_rptr;
54780Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin[1];
54790Sstevel@tonic-gate 		*sin = sin_null;
54800Sstevel@tonic-gate 		sin->sin_family = AF_INET;
54810Sstevel@tonic-gate 		sin->sin_addr.s_addr = v4addr;
54820Sstevel@tonic-gate 	} else {
54830Sstevel@tonic-gate 		/* icmp->icmp_family == AF_INET6 */
54840Sstevel@tonic-gate 		sin6_t *sin6;
54850Sstevel@tonic-gate 
54860Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
54870Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
54880Sstevel@tonic-gate 		sin6 = (sin6_t *)mp1->b_rptr;
54890Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin6[1];
54900Sstevel@tonic-gate 		*sin6 = sin6_null;
54910Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
54920Sstevel@tonic-gate 		sin6->sin6_flowinfo = flowinfo;
54930Sstevel@tonic-gate 		sin6->sin6_addr = v6addr;
54940Sstevel@tonic-gate 	}
54950Sstevel@tonic-gate 	/* Copy out the address */
54960Sstevel@tonic-gate 	mi_copyout(q, mp);
54970Sstevel@tonic-gate }
54980Sstevel@tonic-gate 
54990Sstevel@tonic-gate static int
55000Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
55010Sstevel@tonic-gate     void *thisdg_attrs)
55020Sstevel@tonic-gate {
55035240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
55040Sstevel@tonic-gate 	struct T_unitdata_req *udreqp;
55050Sstevel@tonic-gate 	int is_absreq_failure;
55060Sstevel@tonic-gate 	cred_t *cr;
55070Sstevel@tonic-gate 
55080Sstevel@tonic-gate 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
55090Sstevel@tonic-gate 	*errorp = 0;
55100Sstevel@tonic-gate 
55115240Snordmark 	cr = DB_CREDDEF(mp, connp->conn_cred);
55120Sstevel@tonic-gate 
55130Sstevel@tonic-gate 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
55140Sstevel@tonic-gate 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
55150Sstevel@tonic-gate 	    thisdg_attrs, &is_absreq_failure);
55160Sstevel@tonic-gate 
55170Sstevel@tonic-gate 	if (*errorp != 0) {
55180Sstevel@tonic-gate 		/*
55190Sstevel@tonic-gate 		 * Note: No special action needed in this
55200Sstevel@tonic-gate 		 * module for "is_absreq_failure"
55210Sstevel@tonic-gate 		 */
55220Sstevel@tonic-gate 		return (-1);		/* failure */
55230Sstevel@tonic-gate 	}
55240Sstevel@tonic-gate 	ASSERT(is_absreq_failure == 0);
55250Sstevel@tonic-gate 	return (0);	/* success */
55260Sstevel@tonic-gate }
55270Sstevel@tonic-gate 
55280Sstevel@tonic-gate void
55290Sstevel@tonic-gate icmp_ddi_init(void)
55300Sstevel@tonic-gate {
55310Sstevel@tonic-gate 	icmp_max_optsize =
55320Sstevel@tonic-gate 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
55335240Snordmark 	    icmp_opt_obj.odb_opt_arr_cnt);
55340Sstevel@tonic-gate 
55353448Sdh155122 	/*
55363448Sdh155122 	 * We want to be informed each time a stack is created or
55373448Sdh155122 	 * destroyed in the kernel, so we can maintain the
55383448Sdh155122 	 * set of icmp_stack_t's.
55393448Sdh155122 	 */
55403448Sdh155122 	netstack_register(NS_ICMP, rawip_stack_init, NULL, rawip_stack_fini);
55410Sstevel@tonic-gate }
55420Sstevel@tonic-gate 
55430Sstevel@tonic-gate void
55440Sstevel@tonic-gate icmp_ddi_destroy(void)
55450Sstevel@tonic-gate {
55463448Sdh155122 	netstack_unregister(NS_ICMP);
55470Sstevel@tonic-gate }
55480Sstevel@tonic-gate 
55493448Sdh155122 /*
55503448Sdh155122  * Initialize the ICMP stack instance.
55513448Sdh155122  */
55523448Sdh155122 static void *
55533448Sdh155122 rawip_stack_init(netstackid_t stackid, netstack_t *ns)
55543448Sdh155122 {
55553448Sdh155122 	icmp_stack_t	*is;
55563448Sdh155122 	icmpparam_t	*pa;
55573448Sdh155122 
55583448Sdh155122 	is = (icmp_stack_t *)kmem_zalloc(sizeof (*is), KM_SLEEP);
55593448Sdh155122 	is->is_netstack = ns;
55603448Sdh155122 
55613448Sdh155122 	pa = (icmpparam_t *)kmem_alloc(sizeof (icmp_param_arr), KM_SLEEP);
55623448Sdh155122 	is->is_param_arr = pa;
55633448Sdh155122 	bcopy(icmp_param_arr, is->is_param_arr, sizeof (icmp_param_arr));
55643448Sdh155122 
55653448Sdh155122 	(void) icmp_param_register(&is->is_nd,
55663448Sdh155122 	    is->is_param_arr, A_CNT(icmp_param_arr));
55673448Sdh155122 	is->is_ksp = rawip_kstat_init(stackid);
55683448Sdh155122 	return (is);
55693448Sdh155122 }
55703448Sdh155122 
55713448Sdh155122 /*
55723448Sdh155122  * Free the ICMP stack instance.
55733448Sdh155122  */
55740Sstevel@tonic-gate static void
55753448Sdh155122 rawip_stack_fini(netstackid_t stackid, void *arg)
55763448Sdh155122 {
55773448Sdh155122 	icmp_stack_t *is = (icmp_stack_t *)arg;
55783448Sdh155122 
55793448Sdh155122 	nd_free(&is->is_nd);
55803448Sdh155122 	kmem_free(is->is_param_arr, sizeof (icmp_param_arr));
55813448Sdh155122 	is->is_param_arr = NULL;
55823448Sdh155122 
55833448Sdh155122 	rawip_kstat_fini(stackid, is->is_ksp);
55843448Sdh155122 	is->is_ksp = NULL;
55853448Sdh155122 	kmem_free(is, sizeof (*is));
55863448Sdh155122 }
55873448Sdh155122 
55883448Sdh155122 static void *
55893448Sdh155122 rawip_kstat_init(netstackid_t stackid) {
55903448Sdh155122 	kstat_t	*ksp;
55910Sstevel@tonic-gate 
55920Sstevel@tonic-gate 	rawip_named_kstat_t template = {
55930Sstevel@tonic-gate 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
55940Sstevel@tonic-gate 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
55950Sstevel@tonic-gate 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
55960Sstevel@tonic-gate 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
55970Sstevel@tonic-gate 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
55980Sstevel@tonic-gate 	};
55990Sstevel@tonic-gate 
56003448Sdh155122 	ksp = kstat_create_netstack("icmp", 0, "rawip", "mib2",
56010Sstevel@tonic-gate 					KSTAT_TYPE_NAMED,
56020Sstevel@tonic-gate 					NUM_OF_FIELDS(rawip_named_kstat_t),
56033448Sdh155122 					0, stackid);
56043448Sdh155122 	if (ksp == NULL || ksp->ks_data == NULL)
56053448Sdh155122 		return (NULL);
56063448Sdh155122 
56073448Sdh155122 	bcopy(&template, ksp->ks_data, sizeof (template));
56083448Sdh155122 	ksp->ks_update = rawip_kstat_update;
56093448Sdh155122 	ksp->ks_private = (void *)(uintptr_t)stackid;
56103448Sdh155122 
56113448Sdh155122 	kstat_install(ksp);
56123448Sdh155122 	return (ksp);
56130Sstevel@tonic-gate }
56140Sstevel@tonic-gate 
56150Sstevel@tonic-gate static void
56163448Sdh155122 rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
56173448Sdh155122 {
56183448Sdh155122 	if (ksp != NULL) {
56193448Sdh155122 		ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
56203448Sdh155122 		kstat_delete_netstack(ksp, stackid);
56210Sstevel@tonic-gate 	}
56220Sstevel@tonic-gate }
56230Sstevel@tonic-gate 
56240Sstevel@tonic-gate static int
56253448Sdh155122 rawip_kstat_update(kstat_t *ksp, int rw)
56263448Sdh155122 {
56270Sstevel@tonic-gate 	rawip_named_kstat_t *rawipkp;
56283448Sdh155122 	netstackid_t	stackid = (netstackid_t)(uintptr_t)ksp->ks_private;
56293448Sdh155122 	netstack_t	*ns;
56303448Sdh155122 	icmp_stack_t	*is;
56313448Sdh155122 
56323448Sdh155122 	if ((ksp == NULL) || (ksp->ks_data == NULL))
56330Sstevel@tonic-gate 		return (EIO);
56340Sstevel@tonic-gate 
56350Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
56360Sstevel@tonic-gate 		return (EACCES);
56370Sstevel@tonic-gate 
56383448Sdh155122 	rawipkp = (rawip_named_kstat_t *)ksp->ks_data;
56393448Sdh155122 
56403448Sdh155122 	ns = netstack_find_by_stackid(stackid);
56413448Sdh155122 	if (ns == NULL)
56423448Sdh155122 		return (-1);
56433448Sdh155122 	is = ns->netstack_icmp;
56443448Sdh155122 	if (is == NULL) {
56453448Sdh155122 		netstack_rele(ns);
56463448Sdh155122 		return (-1);
56473448Sdh155122 	}
56483448Sdh155122 	rawipkp->inDatagrams.value.ui32 =  is->is_rawip_mib.rawipInDatagrams;
56493448Sdh155122 	rawipkp->inCksumErrs.value.ui32 =  is->is_rawip_mib.rawipInCksumErrs;
56503448Sdh155122 	rawipkp->inErrors.value.ui32 =	   is->is_rawip_mib.rawipInErrors;
56513448Sdh155122 	rawipkp->outDatagrams.value.ui32 = is->is_rawip_mib.rawipOutDatagrams;
56523448Sdh155122 	rawipkp->outErrors.value.ui32 =	   is->is_rawip_mib.rawipOutErrors;
56533448Sdh155122 	netstack_rele(ns);
56540Sstevel@tonic-gate 	return (0);
56550Sstevel@tonic-gate }
5656