xref: /onnv-gate/usr/src/uts/common/inet/ip/icmp.c (revision 5277:4f992c690322)
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 = {
183*5277Snordmark 	(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. */
7290Sstevel@tonic-gate 	if (icmp->icmp_ip_snd_options)
7300Sstevel@tonic-gate 		mi_free((char *)icmp->icmp_ip_snd_options);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL)
7330Sstevel@tonic-gate 		kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/* Free memory associated with sticky options */
7360Sstevel@tonic-gate 	if (icmp->icmp_sticky_hdrs_len != 0) {
7370Sstevel@tonic-gate 		kmem_free(icmp->icmp_sticky_hdrs,
7380Sstevel@tonic-gate 		    icmp->icmp_sticky_hdrs_len);
7390Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = NULL;
7400Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = 0;
7410Sstevel@tonic-gate 	}
7421676Sjpk 	ip6_pkt_free(&icmp->icmp_sticky_ipp);
7435240Snordmark }
7445240Snordmark 
7455240Snordmark static int
7465240Snordmark icmp_close(queue_t *q)
7475240Snordmark {
7485240Snordmark 	conn_t	*connp = (conn_t *)q->q_ptr;
7495240Snordmark 
7505240Snordmark 	ASSERT(connp != NULL && IPCL_IS_RAWIP(connp));
7515240Snordmark 
7525240Snordmark 	ip_quiesce_conn(connp);
7535240Snordmark 
7545240Snordmark 	qprocsoff(connp->conn_rq);
7555240Snordmark 
7565240Snordmark 	icmp_close_free(connp);
7575240Snordmark 
7585240Snordmark 	/*
7595240Snordmark 	 * Now we are truly single threaded on this stream, and can
7605240Snordmark 	 * delete the things hanging off the connp, and finally the connp.
7615240Snordmark 	 * We removed this connp from the fanout list, it cannot be
7625240Snordmark 	 * accessed thru the fanouts, and we already waited for the
7635240Snordmark 	 * conn_ref to drop to 0. We are already in close, so
7645240Snordmark 	 * there cannot be any other thread from the top. qprocsoff
7655240Snordmark 	 * has completed, and service has completed or won't run in
7665240Snordmark 	 * future.
7675240Snordmark 	 */
7685240Snordmark 	ASSERT(connp->conn_ref == 1);
7695240Snordmark 
7705240Snordmark 	inet_minor_free(ip_minor_arena, connp->conn_dev);
7715240Snordmark 
7725240Snordmark 	connp->conn_ref--;
7735240Snordmark 	ipcl_conn_destroy(connp);
7745240Snordmark 
7755240Snordmark 	q->q_ptr = WR(q)->q_ptr = NULL;
7765240Snordmark 	return (0);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * This routine handles each T_DISCON_REQ message passed to icmp
7810Sstevel@tonic-gate  * as an indicating that ICMP is no longer connected. This results
7820Sstevel@tonic-gate  * in sending a T_BIND_REQ to IP to restore the binding to just
7830Sstevel@tonic-gate  * the local address.
7840Sstevel@tonic-gate  *
7850Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
7860Sstevel@tonic-gate  *	T_BIND_REQ	- specifying just the local address.
7870Sstevel@tonic-gate  *	T_OK_ACK	- for the T_DISCON_REQ
7880Sstevel@tonic-gate  *
7895240Snordmark  * The disconnect completes in icmp_bind_result.
7900Sstevel@tonic-gate  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
7915240Snordmark  * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will
7925240Snordmark  * convert it to an error ack for the appropriate primitive.
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate static void
7950Sstevel@tonic-gate icmp_disconnect(queue_t *q, mblk_t *mp)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	icmp_t	*icmp;
7980Sstevel@tonic-gate 	mblk_t	*mp1;
7995240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
8005240Snordmark 
8015240Snordmark 	icmp = connp->conn_icmp;
8025240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
8035240Snordmark 	if (icmp->icmp_state != TS_DATA_XFER || icmp->icmp_pending_op != -1) {
8045240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8050Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
8060Sstevel@tonic-gate 		    "icmp_disconnect: bad state, %d", icmp->icmp_state);
8070Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
8080Sstevel@tonic-gate 		return;
8090Sstevel@tonic-gate 	}
8105240Snordmark 	icmp->icmp_pending_op = T_DISCON_REQ;
8110Sstevel@tonic-gate 	icmp->icmp_v6src = icmp->icmp_bound_v6src;
8120Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/*
8150Sstevel@tonic-gate 	 * Send down bind to IP to remove the full binding and revert
8160Sstevel@tonic-gate 	 * to the local address binding.
8170Sstevel@tonic-gate 	 */
8180Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
8190Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0);
8200Sstevel@tonic-gate 	} else {
8210Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
8220Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 	if (mp1 == NULL) {
8255240Snordmark 		icmp->icmp_pending_op = -1;
8265240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8270Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
8280Sstevel@tonic-gate 		return;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
8310Sstevel@tonic-gate 	if (mp == NULL) {
8320Sstevel@tonic-gate 		/* Unable to reuse the T_DISCON_REQ for the ack. */
8335240Snordmark 		icmp->icmp_pending_op = -1;
8345240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8350Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
8360Sstevel@tonic-gate 		return;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
8400Sstevel@tonic-gate 		int error;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 		/* Rebuild the header template */
8435240Snordmark 		error = icmp_build_hdrs(icmp);
8440Sstevel@tonic-gate 		if (error != 0) {
8455240Snordmark 			icmp->icmp_pending_op = -1;
8465240Snordmark 			rw_exit(&icmp->icmp_rwlock);
8470Sstevel@tonic-gate 			icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
8480Sstevel@tonic-gate 			freemsg(mp1);
8490Sstevel@tonic-gate 			return;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 	}
8525240Snordmark 
8535240Snordmark 	rw_exit(&icmp->icmp_rwlock);
8545240Snordmark 	/* Append the T_OK_ACK to the T_BIND_REQ for icmp_bind_result */
8550Sstevel@tonic-gate 	linkb(mp1, mp);
8565240Snordmark 
8575240Snordmark 	if (icmp->icmp_family == AF_INET6)
8585240Snordmark 		mp1 = ip_bind_v6(q, mp1, connp, NULL);
8595240Snordmark 	else
8605240Snordmark 		mp1 = ip_bind_v4(q, mp1, connp);
8615240Snordmark 
8625240Snordmark 	/* The above return NULL if the bind needs to be deferred */
8635240Snordmark 	if (mp1 != NULL)
8645240Snordmark 		icmp_bind_result(connp, mp1);
8655240Snordmark 	else
8665240Snordmark 		CONN_INC_REF(connp);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate /* This routine creates a T_ERROR_ACK message and passes it upstream. */
8700Sstevel@tonic-gate static void
8710Sstevel@tonic-gate icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
8740Sstevel@tonic-gate 		qreply(q, mp);
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate /* Shorthand to generate and send TPI error acks to our client */
8780Sstevel@tonic-gate static void
8790Sstevel@tonic-gate icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
8800Sstevel@tonic-gate     t_scalar_t t_error, int sys_error)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate 	struct T_error_ack	*teackp;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
8850Sstevel@tonic-gate 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
8860Sstevel@tonic-gate 		teackp = (struct T_error_ack *)mp->b_rptr;
8870Sstevel@tonic-gate 		teackp->ERROR_prim = primitive;
8880Sstevel@tonic-gate 		teackp->TLI_error = t_error;
8890Sstevel@tonic-gate 		teackp->UNIX_error = sys_error;
8900Sstevel@tonic-gate 		qreply(q, mp);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*
8955240Snordmark  * icmp_icmp_error is called by icmp_input to process ICMP
8960Sstevel@tonic-gate  * messages passed up by IP.
8970Sstevel@tonic-gate  * Generates the appropriate T_UDERROR_IND for permanent
8980Sstevel@tonic-gate  * (non-transient) errors.
8990Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
9000Sstevel@tonic-gate  * the ICMP header.
9010Sstevel@tonic-gate  */
9020Sstevel@tonic-gate static void
9030Sstevel@tonic-gate icmp_icmp_error(queue_t *q, mblk_t *mp)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate 	icmph_t *icmph;
9060Sstevel@tonic-gate 	ipha_t	*ipha;
9070Sstevel@tonic-gate 	int	iph_hdr_length;
9080Sstevel@tonic-gate 	sin_t	sin;
9090Sstevel@tonic-gate 	sin6_t	sin6;
9100Sstevel@tonic-gate 	mblk_t	*mp1;
9110Sstevel@tonic-gate 	int	error = 0;
9125240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
9150Sstevel@tonic-gate 
9165240Snordmark 	ASSERT(OK_32PTR(mp->b_rptr));
9175240Snordmark 
9180Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
9190Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
9200Sstevel@tonic-gate 		icmp_icmp_error_ipv6(q, mp);
9210Sstevel@tonic-gate 		return;
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 	ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
9240Sstevel@tonic-gate 
9255240Snordmark 	/* Skip past the outer IP and ICMP headers */
9260Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
9270Sstevel@tonic-gate 	icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]);
9280Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
9290Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	switch (icmph->icmph_type) {
9320Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
9330Sstevel@tonic-gate 		switch (icmph->icmph_code) {
9340Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
9350Sstevel@tonic-gate 			/*
9360Sstevel@tonic-gate 			 * IP has already adjusted the path MTU.
9370Sstevel@tonic-gate 			 */
9380Sstevel@tonic-gate 			break;
9390Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
9400Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
9410Sstevel@tonic-gate 			error = ECONNREFUSED;
9420Sstevel@tonic-gate 			break;
9430Sstevel@tonic-gate 		default:
9440Sstevel@tonic-gate 			/* Transient errors */
9450Sstevel@tonic-gate 			break;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 		break;
9480Sstevel@tonic-gate 	default:
9490Sstevel@tonic-gate 		/* Transient errors */
9500Sstevel@tonic-gate 		break;
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	if (error == 0) {
9530Sstevel@tonic-gate 		freemsg(mp);
9540Sstevel@tonic-gate 		return;
9550Sstevel@tonic-gate 	}
9560Sstevel@tonic-gate 
9575240Snordmark 	/*
9585240Snordmark 	 * Deliver T_UDERROR_IND when the application has asked for it.
9595240Snordmark 	 * The socket layer enables this automatically when connected.
9605240Snordmark 	 */
9615240Snordmark 	if (!icmp->icmp_dgram_errind) {
9625240Snordmark 		freemsg(mp);
9635240Snordmark 		return;
9645240Snordmark 	}
9655240Snordmark 
9660Sstevel@tonic-gate 	switch (icmp->icmp_family) {
9670Sstevel@tonic-gate 	case AF_INET:
9680Sstevel@tonic-gate 		sin = sin_null;
9690Sstevel@tonic-gate 		sin.sin_family = AF_INET;
9700Sstevel@tonic-gate 		sin.sin_addr.s_addr = ipha->ipha_dst;
9710Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
9720Sstevel@tonic-gate 		    error);
9730Sstevel@tonic-gate 		break;
9740Sstevel@tonic-gate 	case AF_INET6:
9750Sstevel@tonic-gate 		sin6 = sin6_null;
9760Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
9770Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
9800Sstevel@tonic-gate 		    NULL, 0, error);
9810Sstevel@tonic-gate 		break;
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 	if (mp1)
9840Sstevel@tonic-gate 		putnext(q, mp1);
9850Sstevel@tonic-gate 	freemsg(mp);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate  * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6
9900Sstevel@tonic-gate  * for IPv6 packets.
9910Sstevel@tonic-gate  * Send permanent (non-transient) errors upstream.
9920Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
9930Sstevel@tonic-gate  * as the ICMPv6 header.
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate static void
9960Sstevel@tonic-gate icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	icmp6_t		*icmp6;
9990Sstevel@tonic-gate 	ip6_t		*ip6h, *outer_ip6h;
10000Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
10010Sstevel@tonic-gate 	uint8_t		*nexthdrp;
10020Sstevel@tonic-gate 	sin6_t		sin6;
10030Sstevel@tonic-gate 	mblk_t		*mp1;
10040Sstevel@tonic-gate 	int		error = 0;
10055240Snordmark 	icmp_t		*icmp = Q_TO_ICMP(q);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	outer_ip6h = (ip6_t *)mp->b_rptr;
10080Sstevel@tonic-gate 	if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
10090Sstevel@tonic-gate 		iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
10100Sstevel@tonic-gate 	else
10110Sstevel@tonic-gate 		iph_hdr_length = IPV6_HDR_LEN;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
10140Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
10150Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
10160Sstevel@tonic-gate 		freemsg(mp);
10170Sstevel@tonic-gate 		return;
10180Sstevel@tonic-gate 	}
10195240Snordmark 
10200Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
10210Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
10220Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
10230Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
10240Sstevel@tonic-gate 			error = ECONNREFUSED;
10250Sstevel@tonic-gate 			break;
10260Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
10270Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
10280Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
10290Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
10300Sstevel@tonic-gate 			/* Transient errors */
10310Sstevel@tonic-gate 			break;
10320Sstevel@tonic-gate 		default:
10330Sstevel@tonic-gate 			break;
10340Sstevel@tonic-gate 		}
10350Sstevel@tonic-gate 		break;
10360Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG: {
10370Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi;
10380Sstevel@tonic-gate 		struct T_opthdr		*toh;
10390Sstevel@tonic-gate 		size_t			udi_size;
10400Sstevel@tonic-gate 		mblk_t			*newmp;
10410Sstevel@tonic-gate 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
10420Sstevel@tonic-gate 		    sizeof (struct ip6_mtuinfo);
10430Sstevel@tonic-gate 		sin6_t			*sin6;
10440Sstevel@tonic-gate 		struct ip6_mtuinfo	*mtuinfo;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		/*
10470Sstevel@tonic-gate 		 * If the application has requested to receive path mtu
10480Sstevel@tonic-gate 		 * information, send up an empty message containing an
10490Sstevel@tonic-gate 		 * IPV6_PATHMTU ancillary data item.
10500Sstevel@tonic-gate 		 */
10510Sstevel@tonic-gate 		if (!icmp->icmp_ipv6_recvpathmtu)
10520Sstevel@tonic-gate 			break;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
10550Sstevel@tonic-gate 		    opt_length;
10560Sstevel@tonic-gate 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
10575240Snordmark 			BUMP_MIB(&icmp->icmp_is->is_rawip_mib, rawipInErrors);
10580Sstevel@tonic-gate 			break;
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		/*
10620Sstevel@tonic-gate 		 * newmp->b_cont is left to NULL on purpose.  This is an
10630Sstevel@tonic-gate 		 * empty message containing only ancillary data.
10640Sstevel@tonic-gate 		 */
10650Sstevel@tonic-gate 		newmp->b_datap->db_type = M_PROTO;
10660Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
10670Sstevel@tonic-gate 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
10680Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
10690Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin6_t);
10700Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
10710Sstevel@tonic-gate 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
10720Sstevel@tonic-gate 		tudi->OPT_length = opt_length;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		sin6 = (sin6_t *)&tudi[1];
10750Sstevel@tonic-gate 		bzero(sin6, sizeof (sin6_t));
10760Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
10770Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_v6dst;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		toh = (struct T_opthdr *)&sin6[1];
10800Sstevel@tonic-gate 		toh->level = IPPROTO_IPV6;
10810Sstevel@tonic-gate 		toh->name = IPV6_PATHMTU;
10820Sstevel@tonic-gate 		toh->len = opt_length;
10830Sstevel@tonic-gate 		toh->status = 0;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
10860Sstevel@tonic-gate 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
10870Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
10880Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
10890Sstevel@tonic-gate 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
10900Sstevel@tonic-gate 		/*
10910Sstevel@tonic-gate 		 * We've consumed everything we need from the original
10920Sstevel@tonic-gate 		 * message.  Free it, then send our empty message.
10930Sstevel@tonic-gate 		 */
10940Sstevel@tonic-gate 		freemsg(mp);
10950Sstevel@tonic-gate 		putnext(q, newmp);
10960Sstevel@tonic-gate 		return;
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
10990Sstevel@tonic-gate 		/* Transient errors */
11000Sstevel@tonic-gate 		break;
11010Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
11020Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
11030Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
11040Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
11050Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
11060Sstevel@tonic-gate 			error = ECONNREFUSED;
11070Sstevel@tonic-gate 			break;
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		break;
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 	if (error == 0) {
11120Sstevel@tonic-gate 		freemsg(mp);
11130Sstevel@tonic-gate 		return;
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11165240Snordmark 	/*
11175240Snordmark 	 * Deliver T_UDERROR_IND when the application has asked for it.
11185240Snordmark 	 * The socket layer enables this automatically when connected.
11195240Snordmark 	 */
11205240Snordmark 	if (!icmp->icmp_dgram_errind) {
11215240Snordmark 		freemsg(mp);
11225240Snordmark 		return;
11235240Snordmark 	}
11245240Snordmark 
11250Sstevel@tonic-gate 	sin6 = sin6_null;
11260Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
11270Sstevel@tonic-gate 	sin6.sin6_addr = ip6h->ip6_dst;
11280Sstevel@tonic-gate 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
11310Sstevel@tonic-gate 	    error);
11320Sstevel@tonic-gate 	if (mp1)
11330Sstevel@tonic-gate 		putnext(q, mp1);
11340Sstevel@tonic-gate 	freemsg(mp);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate /*
11380Sstevel@tonic-gate  * This routine responds to T_ADDR_REQ messages.  It is called by icmp_wput.
11390Sstevel@tonic-gate  * The local address is filled in if endpoint is bound. The remote address
11400Sstevel@tonic-gate  * is filled in if remote address has been precified ("connected endpoint")
11410Sstevel@tonic-gate  * (The concept of connected CLTS sockets is alien to published TPI
11420Sstevel@tonic-gate  *  but we support it anyway).
11430Sstevel@tonic-gate  */
11440Sstevel@tonic-gate static void
11450Sstevel@tonic-gate icmp_addr_req(queue_t *q, mblk_t *mp)
11460Sstevel@tonic-gate {
11475240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
11480Sstevel@tonic-gate 	mblk_t	*ackmp;
11490Sstevel@tonic-gate 	struct T_addr_ack *taa;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/* Make it large enough for worst case */
11520Sstevel@tonic-gate 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
11530Sstevel@tonic-gate 	    2 * sizeof (sin6_t), 1);
11540Sstevel@tonic-gate 	if (ackmp == NULL) {
11550Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
11560Sstevel@tonic-gate 		return;
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 	taa = (struct T_addr_ack *)ackmp->b_rptr;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	bzero(taa, sizeof (struct T_addr_ack));
11610Sstevel@tonic-gate 	ackmp->b_wptr = (uchar_t *)&taa[1];
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	taa->PRIM_type = T_ADDR_ACK;
11640Sstevel@tonic-gate 	ackmp->b_datap->db_type = M_PCPROTO;
11655240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_READER);
11660Sstevel@tonic-gate 	/*
11670Sstevel@tonic-gate 	 * Note: Following code assumes 32 bit alignment of basic
11680Sstevel@tonic-gate 	 * data structures like sin_t and struct T_addr_ack.
11690Sstevel@tonic-gate 	 */
11700Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
11710Sstevel@tonic-gate 		/*
11720Sstevel@tonic-gate 		 * Fill in local address
11730Sstevel@tonic-gate 		 */
11740Sstevel@tonic-gate 		taa->LOCADDR_offset = sizeof (*taa);
11750Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
11760Sstevel@tonic-gate 			sin_t	*sin;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin_t);
11790Sstevel@tonic-gate 			sin = (sin_t *)&taa[1];
11800Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
11810Sstevel@tonic-gate 			*sin = sin_null;
11820Sstevel@tonic-gate 			sin->sin_family = AF_INET;
11830Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
11840Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
11850Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src,
11860Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
11870Sstevel@tonic-gate 			} else {
11880Sstevel@tonic-gate 				/*
11890Sstevel@tonic-gate 				 * INADDR_ANY
11900Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
11910Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
11920Sstevel@tonic-gate 				 * local address instead (that could
11930Sstevel@tonic-gate 				 * also still be INADDR_ANY)
11940Sstevel@tonic-gate 				 */
11950Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src,
11960Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
11970Sstevel@tonic-gate 			}
11980Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin[1];
11990Sstevel@tonic-gate 		} else {
12000Sstevel@tonic-gate 			sin6_t	*sin6;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
12030Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin6_t);
12040Sstevel@tonic-gate 			sin6 = (sin6_t *)&taa[1];
12050Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
12060Sstevel@tonic-gate 			*sin6 = sin6_null;
12070Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
12080Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
12090Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_v6src;
12100Sstevel@tonic-gate 			} else {
12110Sstevel@tonic-gate 				/*
12120Sstevel@tonic-gate 				 * UNSPECIFIED
12130Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
12140Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
12150Sstevel@tonic-gate 				 * local address instead (that could
12160Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
12170Sstevel@tonic-gate 				 */
12180Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_bound_v6src;
12190Sstevel@tonic-gate 			}
12200Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin6[1];
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 	}
12235240Snordmark 	rw_exit(&icmp->icmp_rwlock);
12240Sstevel@tonic-gate 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
12250Sstevel@tonic-gate 	qreply(q, ackmp);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate static void
12290Sstevel@tonic-gate icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	*tap = icmp_g_t_info_ack;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6)
12340Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin6_t);
12350Sstevel@tonic-gate 	else
12360Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin_t);
12370Sstevel@tonic-gate 	tap->CURRENT_state = icmp->icmp_state;
12380Sstevel@tonic-gate 	tap->OPT_size = icmp_max_optsize;
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
12430Sstevel@tonic-gate  * icmp_wput.  Much of the T_CAPABILITY_ACK information is copied from
12440Sstevel@tonic-gate  * icmp_g_t_info_ack.  The current state of the stream is copied from
12450Sstevel@tonic-gate  * icmp_state.
12460Sstevel@tonic-gate  */
12470Sstevel@tonic-gate static void
12480Sstevel@tonic-gate icmp_capability_req(queue_t *q, mblk_t *mp)
12490Sstevel@tonic-gate {
12505240Snordmark 	icmp_t			*icmp = Q_TO_ICMP(q);
12510Sstevel@tonic-gate 	t_uscalar_t		cap_bits1;
12520Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
12575240Snordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
12580Sstevel@tonic-gate 	if (!mp)
12590Sstevel@tonic-gate 		return;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
12620Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
12650Sstevel@tonic-gate 		icmp_copy_info(&tcap->INFO_ack, icmp);
12660Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
12670Sstevel@tonic-gate 	}
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	qreply(q, mp);
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate /*
12730Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages.  It is called by icmp_wput.
12740Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
12750Sstevel@tonic-gate  * The current state of the stream is copied from icmp_state.
12760Sstevel@tonic-gate  */
12770Sstevel@tonic-gate static void
12780Sstevel@tonic-gate icmp_info_req(queue_t *q, mblk_t *mp)
12790Sstevel@tonic-gate {
12805240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
12830Sstevel@tonic-gate 	    T_INFO_ACK);
12840Sstevel@tonic-gate 	if (!mp)
12850Sstevel@tonic-gate 		return;
12860Sstevel@tonic-gate 	icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
12870Sstevel@tonic-gate 	qreply(q, mp);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate  * IP recognizes seven kinds of bind requests:
12920Sstevel@tonic-gate  *
12930Sstevel@tonic-gate  * - A zero-length address binds only to the protocol number.
12940Sstevel@tonic-gate  *
12950Sstevel@tonic-gate  * - A 4-byte address is treated as a request to
12960Sstevel@tonic-gate  * validate that the address is a valid local IPv4
12970Sstevel@tonic-gate  * address, appropriate for an application to bind to.
12980Sstevel@tonic-gate  * IP does the verification, but does not make any note
12990Sstevel@tonic-gate  * of the address at this time.
13000Sstevel@tonic-gate  *
13010Sstevel@tonic-gate  * - A 16-byte address contains is treated as a request
13020Sstevel@tonic-gate  * to validate a local IPv6 address, as the 4-byte
13030Sstevel@tonic-gate  * address case above.
13040Sstevel@tonic-gate  *
13050Sstevel@tonic-gate  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
13060Sstevel@tonic-gate  * use it for the inbound fanout of packets.
13070Sstevel@tonic-gate  *
13080Sstevel@tonic-gate  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
13090Sstevel@tonic-gate  * use it for the inbound fanout of packets.
13100Sstevel@tonic-gate  *
13110Sstevel@tonic-gate  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
13120Sstevel@tonic-gate  * information consisting of local and remote addresses
13130Sstevel@tonic-gate  * and ports (unused for raw sockets).  In this case, the addresses are both
13140Sstevel@tonic-gate  * validated as appropriate for this operation, and, if
13150Sstevel@tonic-gate  * so, the information is retained for use in the
13160Sstevel@tonic-gate  * inbound fanout.
13170Sstevel@tonic-gate  *
13180Sstevel@tonic-gate  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
13190Sstevel@tonic-gate  * fanout information, like the 12-byte case above.
13200Sstevel@tonic-gate  *
13210Sstevel@tonic-gate  * IP will also fill in the IRE request mblk with information
13220Sstevel@tonic-gate  * regarding our peer.  In all cases, we notify IP of our protocol
13230Sstevel@tonic-gate  * type by appending a single protocol byte to the bind request.
13240Sstevel@tonic-gate  */
13250Sstevel@tonic-gate static mblk_t *
13260Sstevel@tonic-gate icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length,
13270Sstevel@tonic-gate     in_port_t fport)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	char	*cp;
13300Sstevel@tonic-gate 	mblk_t	*mp;
13310Sstevel@tonic-gate 	struct T_bind_req *tbr;
13320Sstevel@tonic-gate 	ipa_conn_t	*ac;
13330Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
13340Sstevel@tonic-gate 	sin_t		*sin;
13350Sstevel@tonic-gate 	sin6_t		*sin6;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
13385240Snordmark 	ASSERT(RW_LOCK_HELD(&icmp->icmp_rwlock));
13390Sstevel@tonic-gate 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
13400Sstevel@tonic-gate 	if (mp == NULL)
13410Sstevel@tonic-gate 		return (NULL);
13420Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
13430Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
13440Sstevel@tonic-gate 	tbr->PRIM_type = bind_prim;
13450Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (*tbr);
13460Sstevel@tonic-gate 	tbr->CONIND_number = 0;
13470Sstevel@tonic-gate 	tbr->ADDR_length = addr_length;
13480Sstevel@tonic-gate 	cp = (char *)&tbr[1];
13490Sstevel@tonic-gate 	switch (addr_length) {
13500Sstevel@tonic-gate 	case sizeof (ipa_conn_t):
13510Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
13520Sstevel@tonic-gate 		/* Append a request for an IRE */
13530Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13540Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13550Sstevel@tonic-gate 			freemsg(mp);
13560Sstevel@tonic-gate 			return (NULL);
13570Sstevel@tonic-gate 		}
13580Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13590Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
13620Sstevel@tonic-gate 		ac = (ipa_conn_t *)cp;
13630Sstevel@tonic-gate 		ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src);
13640Sstevel@tonic-gate 		ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst);
13650Sstevel@tonic-gate 		ac->ac_fport = fport;
13660Sstevel@tonic-gate 		ac->ac_lport = 0;
13670Sstevel@tonic-gate 		break;
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	case sizeof (ipa6_conn_t):
13700Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
13710Sstevel@tonic-gate 		/* Append a request for an IRE */
13720Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13730Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13740Sstevel@tonic-gate 			freemsg(mp);
13750Sstevel@tonic-gate 			return (NULL);
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13780Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
13810Sstevel@tonic-gate 		ac6 = (ipa6_conn_t *)cp;
13820Sstevel@tonic-gate 		ac6->ac6_laddr = icmp->icmp_v6src;
13830Sstevel@tonic-gate 		ac6->ac6_faddr = icmp->icmp_v6dst;
13840Sstevel@tonic-gate 		ac6->ac6_fport = fport;
13850Sstevel@tonic-gate 		ac6->ac6_lport = 0;
13860Sstevel@tonic-gate 		break;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	case sizeof (sin_t):
13890Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
13900Sstevel@tonic-gate 		/* Append a request for an IRE */
13910Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13920Sstevel@tonic-gate 		if (!mp->b_cont) {
13930Sstevel@tonic-gate 			freemsg(mp);
13940Sstevel@tonic-gate 			return (NULL);
13950Sstevel@tonic-gate 		}
13960Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13970Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 		sin = (sin_t *)cp;
14000Sstevel@tonic-gate 		*sin = sin_null;
14010Sstevel@tonic-gate 		sin->sin_family = AF_INET;
14020Sstevel@tonic-gate 		sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
14030Sstevel@tonic-gate 		break;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	case sizeof (sin6_t):
14060Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
14070Sstevel@tonic-gate 		/* Append a request for an IRE */
14080Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
14090Sstevel@tonic-gate 		if (!mp->b_cont) {
14100Sstevel@tonic-gate 			freemsg(mp);
14110Sstevel@tonic-gate 			return (NULL);
14120Sstevel@tonic-gate 		}
14130Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
14140Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
14170Sstevel@tonic-gate 		*sin6 = sin6_null;
14180Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
14190Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_bound_v6src;
14200Sstevel@tonic-gate 		break;
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 	/* Add protocol number to end */
14230Sstevel@tonic-gate 	cp[addr_length] = icmp->icmp_proto;
14240Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
14250Sstevel@tonic-gate 	return (mp);
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14285240Snordmark /* For /dev/icmp aka AF_INET open */
14295240Snordmark static int
14305240Snordmark icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
14311676Sjpk {
14325240Snordmark 	return (icmp_open(q, devp, flag, sflag, credp, B_FALSE));
14335240Snordmark }
14345240Snordmark 
14355240Snordmark /* For /dev/icmp6 aka AF_INET6 open */
14365240Snordmark static int
14375240Snordmark icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
14385240Snordmark {
14395240Snordmark 	return (icmp_open(q, devp, flag, sflag, credp, B_TRUE));
14401676Sjpk }
14411676Sjpk 
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate  * This is the open routine for icmp.  It allocates a icmp_t structure for
14440Sstevel@tonic-gate  * the stream and, on the first open of the module, creates an ND table.
14450Sstevel@tonic-gate  */
14465240Snordmark /*ARGSUSED2*/
14470Sstevel@tonic-gate static int
14485240Snordmark icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
14495240Snordmark     boolean_t isv6)
14500Sstevel@tonic-gate {
14510Sstevel@tonic-gate 	int	err;
14520Sstevel@tonic-gate 	icmp_t	*icmp;
14535240Snordmark 	conn_t *connp;
14545240Snordmark 	dev_t	conn_dev;
14555240Snordmark 	zoneid_t zoneid;
14563448Sdh155122 	netstack_t *ns;
14573448Sdh155122 	icmp_stack_t *is;
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	/* If the stream is already open, return immediately. */
14600Sstevel@tonic-gate 	if (q->q_ptr != NULL)
14610Sstevel@tonic-gate 		return (0);
14620Sstevel@tonic-gate 
14635240Snordmark 	if (sflag == MODOPEN)
14640Sstevel@tonic-gate 		return (EINVAL);
14650Sstevel@tonic-gate 
14663448Sdh155122 	ns = netstack_find_by_cred(credp);
14673448Sdh155122 	ASSERT(ns != NULL);
14683448Sdh155122 	is = ns->netstack_icmp;
14693448Sdh155122 	ASSERT(is != NULL);
14703448Sdh155122 
14713448Sdh155122 	/*
14723448Sdh155122 	 * For exclusive stacks we set the zoneid to zero
14733448Sdh155122 	 * to make ICMP operate as if in the global zone.
14743448Sdh155122 	 */
14755240Snordmark 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
14763448Sdh155122 		zoneid = GLOBAL_ZONEID;
14773448Sdh155122 	else
14783448Sdh155122 		zoneid = crgetzoneid(credp);
14793448Sdh155122 
14805240Snordmark 	if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) {
14815240Snordmark 		netstack_rele(ns);
14825240Snordmark 		return (EBUSY);
14833448Sdh155122 	}
14845240Snordmark 	*devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
14855240Snordmark 
14865240Snordmark 	connp = ipcl_conn_create(IPCL_RAWIPCONN, KM_SLEEP, ns);
14875240Snordmark 	connp->conn_dev = conn_dev;
14885240Snordmark 	icmp = connp->conn_icmp;
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14915240Snordmark 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
14925240Snordmark 	 * done by netstack_find_by_cred()
14935240Snordmark 	 */
14945240Snordmark 	netstack_rele(ns);
14955240Snordmark 
14965240Snordmark 	/*
14975240Snordmark 	 * Initialize the icmp_t structure for this stream.
14980Sstevel@tonic-gate 	 */
14995240Snordmark 	q->q_ptr = connp;
15005240Snordmark 	WR(q)->q_ptr = connp;
15015240Snordmark 	connp->conn_rq = q;
15025240Snordmark 	connp->conn_wq = WR(q);
15035240Snordmark 
15045240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
15055240Snordmark 	ASSERT(connp->conn_ulp == IPPROTO_ICMP);
15065240Snordmark 	ASSERT(connp->conn_icmp == icmp);
15075240Snordmark 	ASSERT(icmp->icmp_connp == connp);
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	/* Set the initial state of the stream and the privilege status. */
15100Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
15115240Snordmark 	if (isv6) {
15120Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV6_VERSION;
15130Sstevel@tonic-gate 		icmp->icmp_family = AF_INET6;
15145240Snordmark 		connp->conn_ulp = IPPROTO_ICMPV6;
15150Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
15160Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMPV6;
15170Sstevel@tonic-gate 		icmp->icmp_checksum_off = 2;	/* Offset for icmp6_cksum */
15180Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IPV6_HDR_LEN;
15193448Sdh155122 		icmp->icmp_ttl = (uint8_t)is->is_ipv6_hoplimit;
15205240Snordmark 		connp->conn_af_isv6 = B_TRUE;
15215240Snordmark 		connp->conn_flags |= IPCL_ISV6;
15220Sstevel@tonic-gate 	} else {
15230Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV4_VERSION;
15240Sstevel@tonic-gate 		icmp->icmp_family = AF_INET;
15250Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
15260Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMP;
15270Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH;
15283448Sdh155122 		icmp->icmp_ttl = (uint8_t)is->is_ipv4_ttl;
15295240Snordmark 		connp->conn_af_isv6 = B_FALSE;
15305240Snordmark 		connp->conn_flags &= ~IPCL_ISV6;
15310Sstevel@tonic-gate 	}
15325240Snordmark 	icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
15335240Snordmark 	icmp->icmp_pending_op = -1;
15345240Snordmark 	connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
15355240Snordmark 	connp->conn_zoneid = zoneid;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/*
15385240Snordmark 	 * If the caller has the process-wide flag set, then default to MAC
15395240Snordmark 	 * exempt mode.  This allows read-down to unlabeled hosts.
15400Sstevel@tonic-gate 	 */
15415240Snordmark 	if (getpflags(NET_MAC_AWARE, credp) != 0)
15425240Snordmark 		icmp->icmp_mac_exempt = B_TRUE;
15435240Snordmark 
15445240Snordmark 	connp->conn_ulp_labeled = is_system_labeled();
15455240Snordmark 
15465240Snordmark 	icmp->icmp_is = is;
15475240Snordmark 
15485240Snordmark 	q->q_hiwat = is->is_recv_hiwat;
15493448Sdh155122 	WR(q)->q_hiwat = is->is_xmit_hiwat;
15503448Sdh155122 	WR(q)->q_lowat = is->is_xmit_lowat;
15515240Snordmark 
15525240Snordmark 	connp->conn_recv = icmp_input;
15535240Snordmark 	crhold(credp);
15545240Snordmark 	connp->conn_cred = credp;
15555240Snordmark 
15565240Snordmark 	mutex_enter(&connp->conn_lock);
15575240Snordmark 	connp->conn_state_flags &= ~CONN_INCIPIENT;
15585240Snordmark 	mutex_exit(&connp->conn_lock);
15595240Snordmark 
15605240Snordmark 	qprocson(q);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
15630Sstevel@tonic-gate 		/* Build initial header template for transmit */
15645240Snordmark 		if ((err = icmp_build_hdrs(icmp)) != 0) {
15655240Snordmark 			rw_exit(&icmp->icmp_rwlock);
15665240Snordmark 			qprocsoff(q);
15675240Snordmark 			ipcl_conn_destroy(connp);
15685240Snordmark 			return (err);
15695240Snordmark 		}
15700Sstevel@tonic-gate 	}
15715240Snordmark 	rw_exit(&icmp->icmp_rwlock);
15725240Snordmark 
15730Sstevel@tonic-gate 	/* Set the Stream head write offset. */
15743448Sdh155122 	(void) mi_set_sth_wroff(q,
15753448Sdh155122 	    icmp->icmp_max_hdr_len + is->is_wroff_extra);
15760Sstevel@tonic-gate 	(void) mi_set_sth_hiwat(q, q->q_hiwat);
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	return (0);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate /*
15820Sstevel@tonic-gate  * Which ICMP options OK to set through T_UNITDATA_REQ...
15830Sstevel@tonic-gate  */
15840Sstevel@tonic-gate /* ARGSUSED */
15850Sstevel@tonic-gate static boolean_t
15860Sstevel@tonic-gate icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
15870Sstevel@tonic-gate {
15880Sstevel@tonic-gate 	return (B_TRUE);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate /*
15920Sstevel@tonic-gate  * This routine gets default values of certain options whose default
15930Sstevel@tonic-gate  * values are maintained by protcol specific code
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate /* ARGSUSED */
15960Sstevel@tonic-gate int
15970Sstevel@tonic-gate icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr)
15980Sstevel@tonic-gate {
15995240Snordmark 	icmp_t *icmp = Q_TO_ICMP(q);
16003448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
16010Sstevel@tonic-gate 	int *i1 = (int *)ptr;
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	switch (level) {
16040Sstevel@tonic-gate 	case IPPROTO_IP:
16050Sstevel@tonic-gate 		switch (name) {
16060Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
16070Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
16080Sstevel@tonic-gate 			return (sizeof (uchar_t));
16090Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
16100Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
16110Sstevel@tonic-gate 			return (sizeof (uchar_t));
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 		break;
16140Sstevel@tonic-gate 	case IPPROTO_IPV6:
16150Sstevel@tonic-gate 		switch (name) {
16160Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
16170Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_TTL;
16180Sstevel@tonic-gate 			return (sizeof (int));
16190Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
16200Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
16210Sstevel@tonic-gate 			return (sizeof (int));
16220Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
16233448Sdh155122 			*i1 = is->is_ipv6_hoplimit;
16240Sstevel@tonic-gate 			return (sizeof (int));
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 		break;
16270Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
16280Sstevel@tonic-gate 		switch (name) {
16290Sstevel@tonic-gate 		case ICMP6_FILTER:
16300Sstevel@tonic-gate 			/* Make it look like "pass all" */
16310Sstevel@tonic-gate 			ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
16320Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
16330Sstevel@tonic-gate 		}
16340Sstevel@tonic-gate 		break;
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 	return (-1);
16370Sstevel@tonic-gate }
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate /*
16400Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
16410Sstevel@tonic-gate  * It returns the size of the option retrieved.
16420Sstevel@tonic-gate  */
16430Sstevel@tonic-gate int
16445240Snordmark icmp_opt_get_locked(queue_t *q, int level, int name, uchar_t *ptr)
16450Sstevel@tonic-gate {
16465240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
16475240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
16485240Snordmark 	icmp_stack_t *is = icmp->icmp_is;
16490Sstevel@tonic-gate 	int	*i1 = (int *)ptr;
16500Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &icmp->icmp_sticky_ipp;
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	switch (level) {
16530Sstevel@tonic-gate 	case SOL_SOCKET:
16540Sstevel@tonic-gate 		switch (name) {
16550Sstevel@tonic-gate 		case SO_DEBUG:
16560Sstevel@tonic-gate 			*i1 = icmp->icmp_debug;
16570Sstevel@tonic-gate 			break;
16580Sstevel@tonic-gate 		case SO_TYPE:
16590Sstevel@tonic-gate 			*i1 = SOCK_RAW;
16600Sstevel@tonic-gate 			break;
16610Sstevel@tonic-gate 		case SO_PROTOTYPE:
16620Sstevel@tonic-gate 			*i1 = icmp->icmp_proto;
16630Sstevel@tonic-gate 			break;
16640Sstevel@tonic-gate 		case SO_REUSEADDR:
16650Sstevel@tonic-gate 			*i1 = icmp->icmp_reuseaddr;
16660Sstevel@tonic-gate 			break;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 		/*
16690Sstevel@tonic-gate 		 * The following three items are available here,
16700Sstevel@tonic-gate 		 * but are only meaningful to IP.
16710Sstevel@tonic-gate 		 */
16720Sstevel@tonic-gate 		case SO_DONTROUTE:
16730Sstevel@tonic-gate 			*i1 = icmp->icmp_dontroute;
16740Sstevel@tonic-gate 			break;
16750Sstevel@tonic-gate 		case SO_USELOOPBACK:
16760Sstevel@tonic-gate 			*i1 = icmp->icmp_useloopback;
16770Sstevel@tonic-gate 			break;
16780Sstevel@tonic-gate 		case SO_BROADCAST:
16790Sstevel@tonic-gate 			*i1 = icmp->icmp_broadcast;
16800Sstevel@tonic-gate 			break;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 		case SO_SNDBUF:
16830Sstevel@tonic-gate 			ASSERT(q->q_hiwat <= INT_MAX);
16840Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
16850Sstevel@tonic-gate 			break;
16860Sstevel@tonic-gate 		case SO_RCVBUF:
16870Sstevel@tonic-gate 			ASSERT(RD(q)->q_hiwat <= INT_MAX);
16880Sstevel@tonic-gate 			*i1 = (int)RD(q)->q_hiwat;
16890Sstevel@tonic-gate 			break;
16900Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
16910Sstevel@tonic-gate 			*i1 = icmp->icmp_dgram_errind;
16920Sstevel@tonic-gate 			break;
16931673Sgt145670 		case SO_TIMESTAMP:
16941673Sgt145670 			*i1 = icmp->icmp_timestamp;
16951673Sgt145670 			break;
16961676Sjpk 		case SO_MAC_EXEMPT:
16971676Sjpk 			*i1 = icmp->icmp_mac_exempt;
16981676Sjpk 			break;
16993388Skcpoon 		case SO_DOMAIN:
17003388Skcpoon 			*i1 = icmp->icmp_family;
17013388Skcpoon 			break;
17023388Skcpoon 
17030Sstevel@tonic-gate 		/*
17042263Ssommerfe 		 * Following four not meaningful for icmp
17050Sstevel@tonic-gate 		 * Action is same as "default" to which we fallthrough
17060Sstevel@tonic-gate 		 * so we keep them in comments.
17070Sstevel@tonic-gate 		 * case SO_LINGER:
17080Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
17090Sstevel@tonic-gate 		 * case SO_OOBINLINE:
17102263Ssommerfe 		 * case SO_ALLZONES:
17110Sstevel@tonic-gate 		 */
17120Sstevel@tonic-gate 		default:
17130Sstevel@tonic-gate 			return (-1);
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 		break;
17160Sstevel@tonic-gate 	case IPPROTO_IP:
17170Sstevel@tonic-gate 		/*
17180Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
17190Sstevel@tonic-gate 		 */
17200Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET)
17210Sstevel@tonic-gate 			return (-1);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 		switch (name) {
17240Sstevel@tonic-gate 		case IP_OPTIONS:
17250Sstevel@tonic-gate 		case T_IP_OPTIONS:
17260Sstevel@tonic-gate 			/* Options are passed up with each packet */
17270Sstevel@tonic-gate 			return (0);
17280Sstevel@tonic-gate 		case IP_HDRINCL:
17290Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_hdrincl;
17300Sstevel@tonic-gate 			break;
17310Sstevel@tonic-gate 		case IP_TOS:
17320Sstevel@tonic-gate 		case T_IP_TOS:
17330Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_type_of_service;
17340Sstevel@tonic-gate 			break;
17350Sstevel@tonic-gate 		case IP_TTL:
17360Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_ttl;
17370Sstevel@tonic-gate 			break;
17380Sstevel@tonic-gate 		case IP_MULTICAST_IF:
17390Sstevel@tonic-gate 			/* 0 address if not set */
17400Sstevel@tonic-gate 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
17410Sstevel@tonic-gate 			return (sizeof (ipaddr_t));
17420Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
17430Sstevel@tonic-gate 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
17440Sstevel@tonic-gate 			return (sizeof (uchar_t));
17450Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
17465240Snordmark 			*ptr = connp->conn_multicast_loop;
17470Sstevel@tonic-gate 			return (sizeof (uint8_t));
17480Sstevel@tonic-gate 		case IP_BOUND_IF:
17490Sstevel@tonic-gate 			/* Zero if not set */
17500Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
17510Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17520Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
17530Sstevel@tonic-gate 			*ptr = icmp->icmp_unspec_source;
17540Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17550Sstevel@tonic-gate 		case IP_XMIT_IF:
17560Sstevel@tonic-gate 			*i1 = icmp->icmp_xmit_if;
17570Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17580Sstevel@tonic-gate 		case IP_RECVIF:
17590Sstevel@tonic-gate 			*ptr = icmp->icmp_recvif;
17600Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17613318Srshoaib 		case IP_RECVPKTINFO:
17623318Srshoaib 			/*
17633318Srshoaib 			 * This also handles IP_PKTINFO.
17643318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
17653318Srshoaib 			 * Differentiation is based on the size of the argument
17663318Srshoaib 			 * passed in.
17673318Srshoaib 			 * This option is handled in IP which will return an
17683318Srshoaib 			 * error for IP_PKTINFO as it's not supported as a
17693318Srshoaib 			 * sticky option.
17703318Srshoaib 			 */
17713318Srshoaib 			return (-EINVAL);
17720Sstevel@tonic-gate 		/*
17730Sstevel@tonic-gate 		 * Cannot "get" the value of following options
17740Sstevel@tonic-gate 		 * at this level. Action is same as "default" to
17750Sstevel@tonic-gate 		 * which we fallthrough so we keep them in comments.
17760Sstevel@tonic-gate 		 *
17770Sstevel@tonic-gate 		 * case IP_ADD_MEMBERSHIP:
17780Sstevel@tonic-gate 		 * case IP_DROP_MEMBERSHIP:
17790Sstevel@tonic-gate 		 * case IP_BLOCK_SOURCE:
17800Sstevel@tonic-gate 		 * case IP_UNBLOCK_SOURCE:
17810Sstevel@tonic-gate 		 * case IP_ADD_SOURCE_MEMBERSHIP:
17820Sstevel@tonic-gate 		 * case IP_DROP_SOURCE_MEMBERSHIP:
17830Sstevel@tonic-gate 		 * case MCAST_JOIN_GROUP:
17840Sstevel@tonic-gate 		 * case MCAST_LEAVE_GROUP:
17850Sstevel@tonic-gate 		 * case MCAST_BLOCK_SOURCE:
17860Sstevel@tonic-gate 		 * case MCAST_UNBLOCK_SOURCE:
17870Sstevel@tonic-gate 		 * case MCAST_JOIN_SOURCE_GROUP:
17880Sstevel@tonic-gate 		 * case MCAST_LEAVE_SOURCE_GROUP:
17890Sstevel@tonic-gate 		 * case MRT_INIT:
17900Sstevel@tonic-gate 		 * case MRT_DONE:
17910Sstevel@tonic-gate 		 * case MRT_ADD_VIF:
17920Sstevel@tonic-gate 		 * case MRT_DEL_VIF:
17930Sstevel@tonic-gate 		 * case MRT_ADD_MFC:
17940Sstevel@tonic-gate 		 * case MRT_DEL_MFC:
17950Sstevel@tonic-gate 		 * case MRT_VERSION:
17960Sstevel@tonic-gate 		 * case MRT_ASSERT:
17970Sstevel@tonic-gate 		 * case IP_SEC_OPT:
17980Sstevel@tonic-gate 		 * case IP_DONTFAILOVER_IF:
17991663Spriyanka 		 * case IP_NEXTHOP:
18000Sstevel@tonic-gate 		 */
18010Sstevel@tonic-gate 		default:
18020Sstevel@tonic-gate 			return (-1);
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 		break;
18050Sstevel@tonic-gate 	case IPPROTO_IPV6:
18060Sstevel@tonic-gate 		/*
18070Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
18080Sstevel@tonic-gate 		 */
18090Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
18100Sstevel@tonic-gate 			return (-1);
18110Sstevel@tonic-gate 		switch (name) {
18120Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
18130Sstevel@tonic-gate 			*i1 = (unsigned int)icmp->icmp_ttl;
18140Sstevel@tonic-gate 			break;
18150Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
18160Sstevel@tonic-gate 			/* 0 index if not set */
18170Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_if_index;
18180Sstevel@tonic-gate 			break;
18190Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
18200Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_ttl;
18210Sstevel@tonic-gate 			break;
18220Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
18235240Snordmark 			*i1 = connp->conn_multicast_loop;
18240Sstevel@tonic-gate 			break;
18250Sstevel@tonic-gate 		case IPV6_BOUND_IF:
18260Sstevel@tonic-gate 			/* Zero if not set */
18270Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
18280Sstevel@tonic-gate 			break;
18290Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
18300Sstevel@tonic-gate 			*i1 = icmp->icmp_unspec_source;
18310Sstevel@tonic-gate 			break;
18320Sstevel@tonic-gate 		case IPV6_CHECKSUM:
18330Sstevel@tonic-gate 			/*
18340Sstevel@tonic-gate 			 * Return offset or -1 if no checksum offset.
18350Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6
18360Sstevel@tonic-gate 			 */
18370Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
18380Sstevel@tonic-gate 				return (-1);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 			if (icmp->icmp_raw_checksum) {
18410Sstevel@tonic-gate 				*i1 = icmp->icmp_checksum_off;
18420Sstevel@tonic-gate 			} else {
18430Sstevel@tonic-gate 				*i1 = -1;
18440Sstevel@tonic-gate 			}
18450Sstevel@tonic-gate 			break;
18460Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
18470Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
18480Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
18490Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
18500Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
18510Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
18520Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
18530Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
18540Sstevel@tonic-gate 			/* cannot "get" the value for these */
18550Sstevel@tonic-gate 			return (-1);
18560Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
18573318Srshoaib 			*i1 = icmp->icmp_ip_recvpktinfo;
18580Sstevel@tonic-gate 			break;
18590Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
18600Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvtclass;
18610Sstevel@tonic-gate 			break;
18620Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
18630Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpathmtu;
18640Sstevel@tonic-gate 			break;
18650Sstevel@tonic-gate 		case IPV6_V6ONLY:
18660Sstevel@tonic-gate 			*i1 = 1;
18670Sstevel@tonic-gate 			break;
18680Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
18690Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhoplimit;
18700Sstevel@tonic-gate 			break;
18710Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
18720Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhopopts;
18730Sstevel@tonic-gate 			break;
18740Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
18750Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvdstopts;
18760Sstevel@tonic-gate 			break;
18770Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
18780Sstevel@tonic-gate 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
18790Sstevel@tonic-gate 			break;
18800Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
18810Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
18820Sstevel@tonic-gate 			break;
18830Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
18840Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrthdr;
18850Sstevel@tonic-gate 			break;
18860Sstevel@tonic-gate 		case IPV6_PKTINFO: {
18870Sstevel@tonic-gate 			/* XXX assumes that caller has room for max size! */
18880Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
18910Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
18920Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
18930Sstevel@tonic-gate 			else
18940Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
18950Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
18960Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
18970Sstevel@tonic-gate 			else
18980Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
18990Sstevel@tonic-gate 			return (sizeof (struct in6_pktinfo));
19000Sstevel@tonic-gate 		}
19010Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
19020Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)ptr;
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
19050Sstevel@tonic-gate 				return (0);
19060Sstevel@tonic-gate 			*sin6 = sin6_null;
19070Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
19080Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
19090Sstevel@tonic-gate 			return (sizeof (sin6_t));
19100Sstevel@tonic-gate 		}
19110Sstevel@tonic-gate 		case IPV6_HOPOPTS:
19120Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
19130Sstevel@tonic-gate 				return (0);
19141676Sjpk 			if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6)
19151676Sjpk 				return (0);
19161676Sjpk 			bcopy((char *)ipp->ipp_hopopts +
19171676Sjpk 			    icmp->icmp_label_len_v6, ptr,
19181676Sjpk 			    ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
19191676Sjpk 			if (icmp->icmp_label_len_v6 > 0) {
19201676Sjpk 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
19211676Sjpk 				ptr[1] = (ipp->ipp_hopoptslen -
19221676Sjpk 				    icmp->icmp_label_len_v6 + 7) / 8 - 1;
19231676Sjpk 			}
19241676Sjpk 			return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
19250Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
19260Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
19270Sstevel@tonic-gate 				return (0);
19280Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
19290Sstevel@tonic-gate 			return (ipp->ipp_rtdstoptslen);
19300Sstevel@tonic-gate 		case IPV6_RTHDR:
19310Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
19320Sstevel@tonic-gate 				return (0);
19330Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
19340Sstevel@tonic-gate 			return (ipp->ipp_rthdrlen);
19350Sstevel@tonic-gate 		case IPV6_DSTOPTS:
19360Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
19370Sstevel@tonic-gate 				return (0);
19380Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
19390Sstevel@tonic-gate 			return (ipp->ipp_dstoptslen);
19400Sstevel@tonic-gate 		case IPV6_PATHMTU:
19410Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
19420Sstevel@tonic-gate 				return (0);
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
19455240Snordmark 			    (struct ip6_mtuinfo *)ptr,
19465240Snordmark 			    is->is_netstack));
19470Sstevel@tonic-gate 		case IPV6_TCLASS:
19480Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_TCLASS)
19490Sstevel@tonic-gate 				*i1 = ipp->ipp_tclass;
19500Sstevel@tonic-gate 			else
19510Sstevel@tonic-gate 				*i1 = IPV6_FLOW_TCLASS(
19520Sstevel@tonic-gate 				    IPV6_DEFAULT_VERS_AND_FLOW);
19530Sstevel@tonic-gate 			break;
19540Sstevel@tonic-gate 		default:
19550Sstevel@tonic-gate 			return (-1);
19560Sstevel@tonic-gate 		}
19570Sstevel@tonic-gate 		break;
19580Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
19590Sstevel@tonic-gate 		/*
19600Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
19610Sstevel@tonic-gate 		 */
19620Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
19630Sstevel@tonic-gate 			return (-1);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
19660Sstevel@tonic-gate 			return (-1);
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		switch (name) {
19690Sstevel@tonic-gate 		case ICMP6_FILTER:
19700Sstevel@tonic-gate 			if (icmp->icmp_filter == NULL) {
19710Sstevel@tonic-gate 				/* Make it look like "pass all" */
19720Sstevel@tonic-gate 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
19730Sstevel@tonic-gate 			} else {
19740Sstevel@tonic-gate 				(void) bcopy(icmp->icmp_filter, ptr,
19750Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
19760Sstevel@tonic-gate 			}
19770Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
19780Sstevel@tonic-gate 		default:
19790Sstevel@tonic-gate 			return (-1);
19800Sstevel@tonic-gate 		}
19810Sstevel@tonic-gate 	default:
19820Sstevel@tonic-gate 		return (-1);
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 	return (sizeof (int));
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate 
19875240Snordmark /*
19885240Snordmark  * This routine retrieves the current status of socket options.
19895240Snordmark  * It returns the size of the option retrieved.
19905240Snordmark  */
19915240Snordmark int
19925240Snordmark icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
19935240Snordmark {
19945240Snordmark 	icmp_t  *icmp = Q_TO_ICMP(q);
19955240Snordmark 	int 	err;
19965240Snordmark 
19975240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_READER);
19985240Snordmark 	err = icmp_opt_get_locked(q, level, name, ptr);
19995240Snordmark 	rw_exit(&icmp->icmp_rwlock);
20005240Snordmark 	return (err);
20015240Snordmark }
20025240Snordmark 
20035240Snordmark 
20040Sstevel@tonic-gate /* This routine sets socket options. */
20050Sstevel@tonic-gate /* ARGSUSED */
20060Sstevel@tonic-gate int
20075240Snordmark icmp_opt_set_locked(queue_t *q, uint_t optset_context, int level, int name,
20080Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
20090Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
20100Sstevel@tonic-gate {
20115240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
20125240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
20133448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
20140Sstevel@tonic-gate 	int	*i1 = (int *)invalp;
20150Sstevel@tonic-gate 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
20160Sstevel@tonic-gate 	boolean_t checkonly;
20170Sstevel@tonic-gate 	int	error;
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	switch (optset_context) {
20200Sstevel@tonic-gate 	case SETFN_OPTCOM_CHECKONLY:
20210Sstevel@tonic-gate 		checkonly = B_TRUE;
20220Sstevel@tonic-gate 		/*
20230Sstevel@tonic-gate 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
20240Sstevel@tonic-gate 		 * inlen != 0 implies value supplied and
20250Sstevel@tonic-gate 		 * 	we have to "pretend" to set it.
20260Sstevel@tonic-gate 		 * inlen == 0 implies that there is no
20270Sstevel@tonic-gate 		 * 	value part in T_CHECK request and just validation
20280Sstevel@tonic-gate 		 * done elsewhere should be enough, we just return here.
20290Sstevel@tonic-gate 		 */
20300Sstevel@tonic-gate 		if (inlen == 0) {
20310Sstevel@tonic-gate 			*outlenp = 0;
20320Sstevel@tonic-gate 			return (0);
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 		break;
20350Sstevel@tonic-gate 	case SETFN_OPTCOM_NEGOTIATE:
20360Sstevel@tonic-gate 		checkonly = B_FALSE;
20370Sstevel@tonic-gate 		break;
20380Sstevel@tonic-gate 	case SETFN_UD_NEGOTIATE:
20390Sstevel@tonic-gate 	case SETFN_CONN_NEGOTIATE:
20400Sstevel@tonic-gate 		checkonly = B_FALSE;
20410Sstevel@tonic-gate 		/*
20420Sstevel@tonic-gate 		 * Negotiating local and "association-related" options
20430Sstevel@tonic-gate 		 * through T_UNITDATA_REQ.
20440Sstevel@tonic-gate 		 *
20450Sstevel@tonic-gate 		 * Following routine can filter out ones we do not
20460Sstevel@tonic-gate 		 * want to be "set" this way.
20470Sstevel@tonic-gate 		 */
20480Sstevel@tonic-gate 		if (!icmp_opt_allow_udr_set(level, name)) {
20490Sstevel@tonic-gate 			*outlenp = 0;
20500Sstevel@tonic-gate 			return (EINVAL);
20510Sstevel@tonic-gate 		}
20520Sstevel@tonic-gate 		break;
20530Sstevel@tonic-gate 	default:
20540Sstevel@tonic-gate 		/*
20550Sstevel@tonic-gate 		 * We should never get here
20560Sstevel@tonic-gate 		 */
20570Sstevel@tonic-gate 		*outlenp = 0;
20580Sstevel@tonic-gate 		return (EINVAL);
20590Sstevel@tonic-gate 	}
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
20620Sstevel@tonic-gate 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	/*
20650Sstevel@tonic-gate 	 * For fixed length options, no sanity check
20660Sstevel@tonic-gate 	 * of passed in length is done. It is assumed *_optcom_req()
20670Sstevel@tonic-gate 	 * routines do the right thing.
20680Sstevel@tonic-gate 	 */
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	switch (level) {
20710Sstevel@tonic-gate 	case SOL_SOCKET:
20720Sstevel@tonic-gate 		switch (name) {
20730Sstevel@tonic-gate 		case SO_DEBUG:
20740Sstevel@tonic-gate 			if (!checkonly)
20750Sstevel@tonic-gate 				icmp->icmp_debug = onoff;
20760Sstevel@tonic-gate 			break;
20770Sstevel@tonic-gate 		case SO_PROTOTYPE:
20780Sstevel@tonic-gate 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
20790Sstevel@tonic-gate 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
20800Sstevel@tonic-gate 			    secpolicy_net_rawaccess(cr) != 0) {
20810Sstevel@tonic-gate 				*outlenp = 0;
20820Sstevel@tonic-gate 				return (EACCES);
20830Sstevel@tonic-gate 			}
20840Sstevel@tonic-gate 			/* Can't use IPPROTO_RAW with IPv6 */
20850Sstevel@tonic-gate 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
20860Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET6) {
20870Sstevel@tonic-gate 				*outlenp = 0;
20880Sstevel@tonic-gate 				return (EPROTONOSUPPORT);
20890Sstevel@tonic-gate 			}
20900Sstevel@tonic-gate 			if (checkonly) {
20910Sstevel@tonic-gate 				/* T_CHECK case */
20920Sstevel@tonic-gate 				*(int *)outvalp = (*i1 & 0xFF);
20930Sstevel@tonic-gate 				break;
20940Sstevel@tonic-gate 			}
20950Sstevel@tonic-gate 			icmp->icmp_proto = *i1 & 0xFF;
20960Sstevel@tonic-gate 			if ((icmp->icmp_proto == IPPROTO_RAW ||
20970Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_IGMP) &&
20980Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET)
20990Sstevel@tonic-gate 				icmp->icmp_hdrincl = 1;
21000Sstevel@tonic-gate 			else
21010Sstevel@tonic-gate 				icmp->icmp_hdrincl = 0;
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6 &&
21040Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
21050Sstevel@tonic-gate 				/* Set offset for icmp6_cksum */
21060Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
21070Sstevel@tonic-gate 				icmp->icmp_checksum_off = 2;
21080Sstevel@tonic-gate 			}
21090Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_UDP ||
21100Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_TCP ||
21110Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_SCTP) {
21120Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 1;
21130Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields |=
21140Sstevel@tonic-gate 				    IPPF_NO_CKSUM;
21150Sstevel@tonic-gate 			} else {
21160Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 0;
21170Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields &=
21180Sstevel@tonic-gate 				    ~IPPF_NO_CKSUM;
21190Sstevel@tonic-gate 			}
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 			if (icmp->icmp_filter != NULL &&
21220Sstevel@tonic-gate 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
21230Sstevel@tonic-gate 				kmem_free(icmp->icmp_filter,
21240Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
21250Sstevel@tonic-gate 				icmp->icmp_filter = NULL;
21260Sstevel@tonic-gate 			}
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 			/* Rebuild the header template */
21295240Snordmark 			error = icmp_build_hdrs(icmp);
21300Sstevel@tonic-gate 			if (error != 0) {
21310Sstevel@tonic-gate 				*outlenp = 0;
21320Sstevel@tonic-gate 				return (error);
21330Sstevel@tonic-gate 			}
21340Sstevel@tonic-gate 
2135409Skcpoon 			/*
2136409Skcpoon 			 * For SCTP, we don't use icmp_bind_proto() for
2137409Skcpoon 			 * raw socket binding.  Note that we do not need
2138409Skcpoon 			 * to set *outlenp.
21395240Snordmark 			 * FIXME: how does SCTP work?
2140409Skcpoon 			 */
2141409Skcpoon 			if (icmp->icmp_proto == IPPROTO_SCTP)
2142409Skcpoon 				return (0);
2143409Skcpoon 
21440Sstevel@tonic-gate 			*outlenp = sizeof (int);
21450Sstevel@tonic-gate 			*(int *)outvalp = *i1 & 0xFF;
21465240Snordmark 
21475240Snordmark 			/* Drop lock across the bind operation */
21485240Snordmark 			rw_exit(&icmp->icmp_rwlock);
21495240Snordmark 			icmp_bind_proto(q);
21505240Snordmark 			rw_enter(&icmp->icmp_rwlock, RW_WRITER);
21510Sstevel@tonic-gate 			return (0);
21520Sstevel@tonic-gate 		case SO_REUSEADDR:
21530Sstevel@tonic-gate 			if (!checkonly)
21540Sstevel@tonic-gate 				icmp->icmp_reuseaddr = onoff;
21550Sstevel@tonic-gate 			break;
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 		/*
21580Sstevel@tonic-gate 		 * The following three items are available here,
21590Sstevel@tonic-gate 		 * but are only meaningful to IP.
21600Sstevel@tonic-gate 		 */
21610Sstevel@tonic-gate 		case SO_DONTROUTE:
21620Sstevel@tonic-gate 			if (!checkonly)
21630Sstevel@tonic-gate 				icmp->icmp_dontroute = onoff;
21640Sstevel@tonic-gate 			break;
21650Sstevel@tonic-gate 		case SO_USELOOPBACK:
21660Sstevel@tonic-gate 			if (!checkonly)
21670Sstevel@tonic-gate 				icmp->icmp_useloopback = onoff;
21680Sstevel@tonic-gate 			break;
21690Sstevel@tonic-gate 		case SO_BROADCAST:
21700Sstevel@tonic-gate 			if (!checkonly)
21710Sstevel@tonic-gate 				icmp->icmp_broadcast = onoff;
21720Sstevel@tonic-gate 			break;
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 		case SO_SNDBUF:
21753448Sdh155122 			if (*i1 > is->is_max_buf) {
21760Sstevel@tonic-gate 				*outlenp = 0;
21770Sstevel@tonic-gate 				return (ENOBUFS);
21780Sstevel@tonic-gate 			}
21790Sstevel@tonic-gate 			if (!checkonly) {
21800Sstevel@tonic-gate 				q->q_hiwat = *i1;
21810Sstevel@tonic-gate 			}
21820Sstevel@tonic-gate 			break;
21830Sstevel@tonic-gate 		case SO_RCVBUF:
21843448Sdh155122 			if (*i1 > is->is_max_buf) {
21850Sstevel@tonic-gate 				*outlenp = 0;
21860Sstevel@tonic-gate 				return (ENOBUFS);
21870Sstevel@tonic-gate 			}
21880Sstevel@tonic-gate 			if (!checkonly) {
21890Sstevel@tonic-gate 				RD(q)->q_hiwat = *i1;
21905240Snordmark 				rw_exit(&icmp->icmp_rwlock);
21910Sstevel@tonic-gate 				(void) mi_set_sth_hiwat(RD(q), *i1);
21925240Snordmark 				rw_enter(&icmp->icmp_rwlock, RW_WRITER);
21930Sstevel@tonic-gate 			}
21940Sstevel@tonic-gate 			break;
21950Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
21960Sstevel@tonic-gate 			if (!checkonly)
21970Sstevel@tonic-gate 				icmp->icmp_dgram_errind = onoff;
21980Sstevel@tonic-gate 			break;
21992263Ssommerfe 		case SO_ALLZONES:
22002263Ssommerfe 			/*
22012263Ssommerfe 			 * "soft" error (negative)
22022263Ssommerfe 			 * option not handled at this level
22032263Ssommerfe 			 * Note: Do not modify *outlenp
22042263Ssommerfe 			 */
22052263Ssommerfe 			return (-EINVAL);
22061673Sgt145670 		case SO_TIMESTAMP:
22071673Sgt145670 			if (!checkonly) {
22081673Sgt145670 				icmp->icmp_timestamp = onoff;
22091673Sgt145670 			}
22101673Sgt145670 			break;
22111676Sjpk 		case SO_MAC_EXEMPT:
22121676Sjpk 			if (secpolicy_net_mac_aware(cr) != 0 ||
22131676Sjpk 			    icmp->icmp_state != TS_UNBND)
22141676Sjpk 				return (EACCES);
22151676Sjpk 			if (!checkonly)
22161676Sjpk 				icmp->icmp_mac_exempt = onoff;
22171676Sjpk 			break;
22180Sstevel@tonic-gate 		/*
22190Sstevel@tonic-gate 		 * Following three not meaningful for icmp
22200Sstevel@tonic-gate 		 * Action is same as "default" so we keep them
22210Sstevel@tonic-gate 		 * in comments.
22220Sstevel@tonic-gate 		 * case SO_LINGER:
22230Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
22240Sstevel@tonic-gate 		 * case SO_OOBINLINE:
22250Sstevel@tonic-gate 		 */
22260Sstevel@tonic-gate 		default:
22270Sstevel@tonic-gate 			*outlenp = 0;
22280Sstevel@tonic-gate 			return (EINVAL);
22290Sstevel@tonic-gate 		}
22300Sstevel@tonic-gate 		break;
22310Sstevel@tonic-gate 	case IPPROTO_IP:
22320Sstevel@tonic-gate 		/*
22330Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
22340Sstevel@tonic-gate 		 */
22350Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET) {
22360Sstevel@tonic-gate 			*outlenp = 0;
22370Sstevel@tonic-gate 			return (ENOPROTOOPT);
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 		switch (name) {
22400Sstevel@tonic-gate 		case IP_OPTIONS:
22410Sstevel@tonic-gate 		case T_IP_OPTIONS:
22420Sstevel@tonic-gate 			/* Save options for use by IP. */
22431676Sjpk 			if ((inlen & 0x3) ||
22441676Sjpk 			    inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) {
22450Sstevel@tonic-gate 				*outlenp = 0;
22460Sstevel@tonic-gate 				return (EINVAL);
22470Sstevel@tonic-gate 			}
22480Sstevel@tonic-gate 			if (checkonly)
22490Sstevel@tonic-gate 				break;
22500Sstevel@tonic-gate 
22511676Sjpk 			if (!tsol_option_set(&icmp->icmp_ip_snd_options,
22521676Sjpk 			    &icmp->icmp_ip_snd_options_len,
22531676Sjpk 			    icmp->icmp_label_len, invalp, inlen)) {
22541676Sjpk 				*outlenp = 0;
22551676Sjpk 				return (ENOMEM);
22560Sstevel@tonic-gate 			}
22571676Sjpk 
22580Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
22590Sstevel@tonic-gate 			    icmp->icmp_ip_snd_options_len;
22605240Snordmark 			rw_exit(&icmp->icmp_rwlock);
22610Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
22625240Snordmark 			    is->is_wroff_extra);
22635240Snordmark 			rw_enter(&icmp->icmp_rwlock, RW_WRITER);
22640Sstevel@tonic-gate 			break;
22650Sstevel@tonic-gate 		case IP_HDRINCL:
22660Sstevel@tonic-gate 			if (!checkonly)
22670Sstevel@tonic-gate 				icmp->icmp_hdrincl = onoff;
22680Sstevel@tonic-gate 			break;
22690Sstevel@tonic-gate 		case IP_TOS:
22700Sstevel@tonic-gate 		case T_IP_TOS:
22710Sstevel@tonic-gate 			if (!checkonly) {
22720Sstevel@tonic-gate 				icmp->icmp_type_of_service = (uint8_t)*i1;
22730Sstevel@tonic-gate 			}
22740Sstevel@tonic-gate 			break;
22750Sstevel@tonic-gate 		case IP_TTL:
22760Sstevel@tonic-gate 			if (!checkonly) {
22770Sstevel@tonic-gate 				icmp->icmp_ttl = (uint8_t)*i1;
22780Sstevel@tonic-gate 			}
22790Sstevel@tonic-gate 			break;
22800Sstevel@tonic-gate 		case IP_MULTICAST_IF:
22810Sstevel@tonic-gate 			/*
22820Sstevel@tonic-gate 			 * TODO should check OPTMGMT reply and undo this if
22830Sstevel@tonic-gate 			 * there is an error.
22840Sstevel@tonic-gate 			 */
22850Sstevel@tonic-gate 			if (!checkonly)
22860Sstevel@tonic-gate 				icmp->icmp_multicast_if_addr = *i1;
22870Sstevel@tonic-gate 			break;
22880Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
22890Sstevel@tonic-gate 			if (!checkonly)
22900Sstevel@tonic-gate 				icmp->icmp_multicast_ttl = *invalp;
22910Sstevel@tonic-gate 			break;
22920Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
22930Sstevel@tonic-gate 			if (!checkonly) {
22945240Snordmark 				connp->conn_multicast_loop =
22950Sstevel@tonic-gate 				    (*invalp == 0) ? 0 : 1;
22960Sstevel@tonic-gate 			}
22970Sstevel@tonic-gate 			break;
22980Sstevel@tonic-gate 		case IP_BOUND_IF:
22990Sstevel@tonic-gate 			if (!checkonly)
23000Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
23010Sstevel@tonic-gate 			break;
23020Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
23030Sstevel@tonic-gate 			if (!checkonly)
23040Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
23050Sstevel@tonic-gate 			break;
23060Sstevel@tonic-gate 		case IP_XMIT_IF:
23070Sstevel@tonic-gate 			if (!checkonly)
23080Sstevel@tonic-gate 				icmp->icmp_xmit_if = *i1;
23090Sstevel@tonic-gate 			break;
23100Sstevel@tonic-gate 		case IP_RECVIF:
23110Sstevel@tonic-gate 			if (!checkonly)
23120Sstevel@tonic-gate 				icmp->icmp_recvif = onoff;
23135240Snordmark 			/*
23145240Snordmark 			 * pass to ip
23155240Snordmark 			 */
23165240Snordmark 			return (-EINVAL);
23173318Srshoaib 		case IP_PKTINFO: {
23183318Srshoaib 			/*
23193318Srshoaib 			 * This also handles IP_RECVPKTINFO.
23203318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
23213318Srshoaib 			 * Differentiation is based on the size of the argument
23223318Srshoaib 			 * passed in.
23233318Srshoaib 			 */
23243318Srshoaib 			struct in_pktinfo *pktinfop;
23253318Srshoaib 			ip4_pkt_t *attr_pktinfop;
23263318Srshoaib 
23273318Srshoaib 			if (checkonly)
23283318Srshoaib 				break;
23293318Srshoaib 
23303318Srshoaib 			if (inlen == sizeof (int)) {
23313318Srshoaib 				/*
23323318Srshoaib 				 * This is IP_RECVPKTINFO option.
23333318Srshoaib 				 * Keep a local copy of wether this option is
23343318Srshoaib 				 * set or not and pass it down to IP for
23353318Srshoaib 				 * processing.
23363318Srshoaib 				 */
23373318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
23383318Srshoaib 				return (-EINVAL);
23393318Srshoaib 			}
23403318Srshoaib 
23413318Srshoaib 
23423318Srshoaib 			if (inlen != sizeof (struct in_pktinfo))
23433318Srshoaib 				return (EINVAL);
23443318Srshoaib 
23453318Srshoaib 			if ((attr_pktinfop = (ip4_pkt_t *)thisdg_attrs)
23463318Srshoaib 			    == NULL) {
23473318Srshoaib 				/*
23483318Srshoaib 				 * sticky option is not supported
23493318Srshoaib 				 */
23503318Srshoaib 				return (EINVAL);
23513318Srshoaib 			}
23523318Srshoaib 
23533318Srshoaib 			pktinfop = (struct in_pktinfo *)invalp;
23543318Srshoaib 
23553318Srshoaib 			/*
23563318Srshoaib 			 * Atleast one of the values should be specified
23573318Srshoaib 			 */
23583318Srshoaib 			if (pktinfop->ipi_ifindex == 0 &&
23593318Srshoaib 			    pktinfop->ipi_spec_dst.s_addr == INADDR_ANY) {
23603318Srshoaib 				return (EINVAL);
23613318Srshoaib 			}
23623318Srshoaib 
23633318Srshoaib 			attr_pktinfop->ip4_addr = pktinfop->ipi_spec_dst.s_addr;
23643318Srshoaib 			attr_pktinfop->ip4_ill_index = pktinfop->ipi_ifindex;
23653318Srshoaib 		}
23663318Srshoaib 			break;
23670Sstevel@tonic-gate 		case IP_ADD_MEMBERSHIP:
23680Sstevel@tonic-gate 		case IP_DROP_MEMBERSHIP:
23690Sstevel@tonic-gate 		case IP_BLOCK_SOURCE:
23700Sstevel@tonic-gate 		case IP_UNBLOCK_SOURCE:
23710Sstevel@tonic-gate 		case IP_ADD_SOURCE_MEMBERSHIP:
23720Sstevel@tonic-gate 		case IP_DROP_SOURCE_MEMBERSHIP:
23730Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
23740Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
23750Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
23760Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
23770Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
23780Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
23790Sstevel@tonic-gate 		case MRT_INIT:
23800Sstevel@tonic-gate 		case MRT_DONE:
23810Sstevel@tonic-gate 		case MRT_ADD_VIF:
23820Sstevel@tonic-gate 		case MRT_DEL_VIF:
23830Sstevel@tonic-gate 		case MRT_ADD_MFC:
23840Sstevel@tonic-gate 		case MRT_DEL_MFC:
23850Sstevel@tonic-gate 		case MRT_VERSION:
23860Sstevel@tonic-gate 		case MRT_ASSERT:
23870Sstevel@tonic-gate 		case IP_SEC_OPT:
23880Sstevel@tonic-gate 		case IP_DONTFAILOVER_IF:
23891663Spriyanka 		case IP_NEXTHOP:
23900Sstevel@tonic-gate 			/*
23910Sstevel@tonic-gate 			 * "soft" error (negative)
23920Sstevel@tonic-gate 			 * option not handled at this level
23930Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
23940Sstevel@tonic-gate 			 */
23950Sstevel@tonic-gate 			return (-EINVAL);
23960Sstevel@tonic-gate 		default:
23970Sstevel@tonic-gate 			*outlenp = 0;
23980Sstevel@tonic-gate 			return (EINVAL);
23990Sstevel@tonic-gate 		}
24000Sstevel@tonic-gate 		break;
24010Sstevel@tonic-gate 	case IPPROTO_IPV6: {
24020Sstevel@tonic-gate 		ip6_pkt_t		*ipp;
24030Sstevel@tonic-gate 		boolean_t		sticky;
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
24060Sstevel@tonic-gate 			*outlenp = 0;
24070Sstevel@tonic-gate 			return (ENOPROTOOPT);
24080Sstevel@tonic-gate 		}
24090Sstevel@tonic-gate 		/*
24100Sstevel@tonic-gate 		 * Deal with both sticky options and ancillary data
24110Sstevel@tonic-gate 		 */
24120Sstevel@tonic-gate 		if (thisdg_attrs == NULL) {
24130Sstevel@tonic-gate 			/* sticky options, or none */
24140Sstevel@tonic-gate 			ipp = &icmp->icmp_sticky_ipp;
24150Sstevel@tonic-gate 			sticky = B_TRUE;
24160Sstevel@tonic-gate 		} else {
24170Sstevel@tonic-gate 			/* ancillary data */
24180Sstevel@tonic-gate 			ipp = (ip6_pkt_t *)thisdg_attrs;
24190Sstevel@tonic-gate 			sticky = B_FALSE;
24200Sstevel@tonic-gate 		}
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 		switch (name) {
24230Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
24240Sstevel@tonic-gate 			if (!checkonly)
24250Sstevel@tonic-gate 				icmp->icmp_multicast_if_index = *i1;
24260Sstevel@tonic-gate 			break;
24270Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
24280Sstevel@tonic-gate 			/* -1 means use default */
24290Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
24300Sstevel@tonic-gate 				*outlenp = 0;
24310Sstevel@tonic-gate 				return (EINVAL);
24320Sstevel@tonic-gate 			}
24330Sstevel@tonic-gate 			if (!checkonly) {
24340Sstevel@tonic-gate 				if (*i1 == -1) {
2435679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
24363448Sdh155122 					    is->is_ipv6_hoplimit;
2437679Sseb 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
24380Sstevel@tonic-gate 					/* Pass modified value to IP. */
24390Sstevel@tonic-gate 					*i1 = ipp->ipp_hoplimit;
24400Sstevel@tonic-gate 				} else {
2441679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
24420Sstevel@tonic-gate 					    (uint8_t)*i1;
2443679Sseb 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
24440Sstevel@tonic-gate 				}
24450Sstevel@tonic-gate 				/* Rebuild the header template */
24465240Snordmark 				error = icmp_build_hdrs(icmp);
24470Sstevel@tonic-gate 				if (error != 0) {
24480Sstevel@tonic-gate 					*outlenp = 0;
24490Sstevel@tonic-gate 					return (error);
24500Sstevel@tonic-gate 				}
24510Sstevel@tonic-gate 			}
24520Sstevel@tonic-gate 			break;
24530Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
24540Sstevel@tonic-gate 			/* -1 means use default */
24550Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
24560Sstevel@tonic-gate 				*outlenp = 0;
24570Sstevel@tonic-gate 				return (EINVAL);
24580Sstevel@tonic-gate 			}
24590Sstevel@tonic-gate 			if (!checkonly) {
24600Sstevel@tonic-gate 				if (*i1 == -1) {
24610Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2462679Sseb 					    ipp->ipp_multicast_hops =
24630Sstevel@tonic-gate 					    IP_DEFAULT_MULTICAST_TTL;
2464679Sseb 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
24650Sstevel@tonic-gate 					/* Pass modified value to IP. */
2466679Sseb 					*i1 = icmp->icmp_multicast_ttl;
24670Sstevel@tonic-gate 				} else {
24680Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2469679Sseb 					    ipp->ipp_multicast_hops =
24700Sstevel@tonic-gate 					    (uint8_t)*i1;
2471679Sseb 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
24720Sstevel@tonic-gate 				}
24730Sstevel@tonic-gate 			}
24740Sstevel@tonic-gate 			break;
24750Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
24760Sstevel@tonic-gate 			if (*i1 != 0 && *i1 != 1) {
24770Sstevel@tonic-gate 				*outlenp = 0;
24780Sstevel@tonic-gate 				return (EINVAL);
24790Sstevel@tonic-gate 			}
24800Sstevel@tonic-gate 			if (!checkonly)
24815240Snordmark 				connp->conn_multicast_loop = *i1;
24820Sstevel@tonic-gate 			break;
24830Sstevel@tonic-gate 		case IPV6_CHECKSUM:
24840Sstevel@tonic-gate 			/*
24850Sstevel@tonic-gate 			 * Integer offset into the user data of where the
24860Sstevel@tonic-gate 			 * checksum is located.
24870Sstevel@tonic-gate 			 * Offset of -1 disables option.
24880Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6.
24890Sstevel@tonic-gate 			 */
24900Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
24910Sstevel@tonic-gate 				*outlenp = 0;
24920Sstevel@tonic-gate 				return (EINVAL);
24930Sstevel@tonic-gate 			}
24940Sstevel@tonic-gate 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
24950Sstevel@tonic-gate 				/* Negative or not 16 bit aligned offset */
24960Sstevel@tonic-gate 				*outlenp = 0;
24970Sstevel@tonic-gate 				return (EINVAL);
24980Sstevel@tonic-gate 			}
24990Sstevel@tonic-gate 			if (checkonly)
25000Sstevel@tonic-gate 				break;
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 			if (*i1 == -1) {
25030Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
25040Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
25050Sstevel@tonic-gate 			} else {
25060Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 1;
25070Sstevel@tonic-gate 				icmp->icmp_checksum_off = *i1;
25080Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
25090Sstevel@tonic-gate 			}
25100Sstevel@tonic-gate 			/* Rebuild the header template */
25115240Snordmark 			error = icmp_build_hdrs(icmp);
25120Sstevel@tonic-gate 			if (error != 0) {
25130Sstevel@tonic-gate 				*outlenp = 0;
25140Sstevel@tonic-gate 				return (error);
25150Sstevel@tonic-gate 			}
25160Sstevel@tonic-gate 			break;
25170Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
25180Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
25190Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
25200Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
25210Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
25220Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
25230Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
25240Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
25250Sstevel@tonic-gate 			/*
25260Sstevel@tonic-gate 			 * "soft" error (negative)
25270Sstevel@tonic-gate 			 * option not handled at this level
25280Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
25290Sstevel@tonic-gate 			 */
25300Sstevel@tonic-gate 			return (-EINVAL);
25310Sstevel@tonic-gate 		case IPV6_BOUND_IF:
25320Sstevel@tonic-gate 			if (!checkonly)
25330Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
25340Sstevel@tonic-gate 			break;
25350Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
25360Sstevel@tonic-gate 			if (!checkonly)
25370Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
25380Sstevel@tonic-gate 			break;
25390Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
25400Sstevel@tonic-gate 			if (!checkonly)
25410Sstevel@tonic-gate 				icmp->icmp_ipv6_recvtclass = onoff;
25420Sstevel@tonic-gate 			break;
25430Sstevel@tonic-gate 		/*
25440Sstevel@tonic-gate 		 * Set boolean switches for ancillary data delivery
25450Sstevel@tonic-gate 		 */
25460Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
25470Sstevel@tonic-gate 			if (!checkonly)
25483318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
25490Sstevel@tonic-gate 			break;
25500Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
25510Sstevel@tonic-gate 			if (!checkonly)
25520Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpathmtu = onoff;
25530Sstevel@tonic-gate 			break;
25540Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
25550Sstevel@tonic-gate 			if (!checkonly)
25560Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhoplimit = onoff;
25570Sstevel@tonic-gate 			break;
25580Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
25590Sstevel@tonic-gate 			if (!checkonly)
25600Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhopopts = onoff;
25610Sstevel@tonic-gate 			break;
25620Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
25630Sstevel@tonic-gate 			if (!checkonly)
25640Sstevel@tonic-gate 				icmp->icmp_ipv6_recvdstopts = onoff;
25650Sstevel@tonic-gate 			break;
25660Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
25670Sstevel@tonic-gate 			if (!checkonly)
25680Sstevel@tonic-gate 				icmp->icmp_old_ipv6_recvdstopts = onoff;
25690Sstevel@tonic-gate 			break;
25700Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
25710Sstevel@tonic-gate 			if (!checkonly)
25720Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrtdstopts = onoff;
25730Sstevel@tonic-gate 			break;
25740Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
25750Sstevel@tonic-gate 			if (!checkonly)
25760Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrthdr = onoff;
25770Sstevel@tonic-gate 			break;
25780Sstevel@tonic-gate 		/*
25790Sstevel@tonic-gate 		 * Set sticky options or ancillary data.
25800Sstevel@tonic-gate 		 * If sticky options, (re)build any extension headers
25810Sstevel@tonic-gate 		 * that might be needed as a result.
25820Sstevel@tonic-gate 		 */
25830Sstevel@tonic-gate 		case IPV6_PKTINFO:
25840Sstevel@tonic-gate 			/*
25850Sstevel@tonic-gate 			 * The source address and ifindex are verified
25860Sstevel@tonic-gate 			 * in ip_opt_set(). For ancillary data the
25870Sstevel@tonic-gate 			 * source address is checked in ip_wput_v6.
25880Sstevel@tonic-gate 			 */
25890Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
25900Sstevel@tonic-gate 				return (EINVAL);
25910Sstevel@tonic-gate 			if (checkonly)
25920Sstevel@tonic-gate 				break;
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 			if (inlen == 0) {
25950Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
25960Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |=
25970Sstevel@tonic-gate 				    (IPPF_IFINDEX|IPPF_ADDR);
25980Sstevel@tonic-gate 			} else {
25990Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
26020Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
26030Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
26040Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
26050Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
26060Sstevel@tonic-gate 				else
26070Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
26080Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
26090Sstevel@tonic-gate 				    &ipp->ipp_addr))
26100Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
26110Sstevel@tonic-gate 				else
26120Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
26130Sstevel@tonic-gate 			}
26140Sstevel@tonic-gate 			if (sticky) {
26155240Snordmark 				error = icmp_build_hdrs(icmp);
26160Sstevel@tonic-gate 				if (error != 0)
26170Sstevel@tonic-gate 					return (error);
26180Sstevel@tonic-gate 			}
26190Sstevel@tonic-gate 			break;
26200Sstevel@tonic-gate 		case IPV6_HOPLIMIT:
2621679Sseb 			/* This option can only be used as ancillary data. */
2622679Sseb 			if (sticky)
2623679Sseb 				return (EINVAL);
26240Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
26250Sstevel@tonic-gate 				return (EINVAL);
26260Sstevel@tonic-gate 			if (checkonly)
26270Sstevel@tonic-gate 				break;
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 			if (inlen == 0) {
26300Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
26310Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
26320Sstevel@tonic-gate 			} else {
26330Sstevel@tonic-gate 				if (*i1 > 255 || *i1 < -1)
26340Sstevel@tonic-gate 					return (EINVAL);
26350Sstevel@tonic-gate 				if (*i1 == -1)
26363448Sdh155122 					ipp->ipp_hoplimit =
26373448Sdh155122 					    is->is_ipv6_hoplimit;
26380Sstevel@tonic-gate 				else
26390Sstevel@tonic-gate 					ipp->ipp_hoplimit = *i1;
26400Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPLIMIT;
26410Sstevel@tonic-gate 			}
26420Sstevel@tonic-gate 			break;
26430Sstevel@tonic-gate 		case IPV6_TCLASS:
26440Sstevel@tonic-gate 			/*
26450Sstevel@tonic-gate 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
26460Sstevel@tonic-gate 			 * and [0, 255] as the actualy traffic class.
26470Sstevel@tonic-gate 			 */
26480Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
26490Sstevel@tonic-gate 				return (EINVAL);
26500Sstevel@tonic-gate 			if (checkonly)
26510Sstevel@tonic-gate 				break;
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 			if (inlen == 0) {
26540Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_TCLASS;
26550Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
26560Sstevel@tonic-gate 			} else {
26570Sstevel@tonic-gate 				if (*i1 >= 256 || *i1 < -1)
26580Sstevel@tonic-gate 					return (EINVAL);
26590Sstevel@tonic-gate 				if (*i1 == -1) {
26600Sstevel@tonic-gate 					ipp->ipp_tclass =
26610Sstevel@tonic-gate 					    IPV6_FLOW_TCLASS(
26620Sstevel@tonic-gate 					    IPV6_DEFAULT_VERS_AND_FLOW);
26630Sstevel@tonic-gate 				} else {
26640Sstevel@tonic-gate 					ipp->ipp_tclass = *i1;
26650Sstevel@tonic-gate 				}
26660Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_TCLASS;
26670Sstevel@tonic-gate 			}
26680Sstevel@tonic-gate 			if (sticky) {
26695240Snordmark 				error = icmp_build_hdrs(icmp);
26700Sstevel@tonic-gate 				if (error != 0)
26710Sstevel@tonic-gate 					return (error);
26720Sstevel@tonic-gate 			}
26730Sstevel@tonic-gate 			break;
26740Sstevel@tonic-gate 		case IPV6_NEXTHOP:
26750Sstevel@tonic-gate 			/*
26760Sstevel@tonic-gate 			 * IP will verify that the nexthop is reachable
26770Sstevel@tonic-gate 			 * and fail for sticky options.
26780Sstevel@tonic-gate 			 */
26790Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t))
26800Sstevel@tonic-gate 				return (EINVAL);
26810Sstevel@tonic-gate 			if (checkonly)
26820Sstevel@tonic-gate 				break;
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 			if (inlen == 0) {
26850Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
26860Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
26870Sstevel@tonic-gate 			} else {
26880Sstevel@tonic-gate 				sin6_t *sin6 = (sin6_t *)invalp;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6)
26910Sstevel@tonic-gate 					return (EAFNOSUPPORT);
26920Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
26930Sstevel@tonic-gate 					return (EADDRNOTAVAIL);
26940Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
26950Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
26960Sstevel@tonic-gate 				    &ipp->ipp_nexthop))
26970Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
26980Sstevel@tonic-gate 				else
26990Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
27000Sstevel@tonic-gate 			}
27010Sstevel@tonic-gate 			if (sticky) {
27025240Snordmark 				error = icmp_build_hdrs(icmp);
27030Sstevel@tonic-gate 				if (error != 0)
27040Sstevel@tonic-gate 					return (error);
27050Sstevel@tonic-gate 			}
27060Sstevel@tonic-gate 			break;
27070Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
27080Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
27090Sstevel@tonic-gate 			/*
27100Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27110Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27120Sstevel@tonic-gate 			 */
27130Sstevel@tonic-gate 			if (inlen != 0 &&
27140Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1)))
27150Sstevel@tonic-gate 				return (EINVAL);
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 			if (checkonly)
27180Sstevel@tonic-gate 				break;
27191676Sjpk 			error = optcom_pkt_set(invalp, inlen, sticky,
27201676Sjpk 			    (uchar_t **)&ipp->ipp_hopopts,
27211676Sjpk 			    &ipp->ipp_hopoptslen,
27221676Sjpk 			    sticky ? icmp->icmp_label_len_v6 : 0);
27231676Sjpk 			if (error != 0)
27241676Sjpk 				return (error);
27251676Sjpk 			if (ipp->ipp_hopoptslen == 0) {
27260Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
27270Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
27280Sstevel@tonic-gate 			} else {
27290Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
27300Sstevel@tonic-gate 			}
27310Sstevel@tonic-gate 			if (sticky) {
27325240Snordmark 				error = icmp_build_hdrs(icmp);
27330Sstevel@tonic-gate 				if (error != 0)
27340Sstevel@tonic-gate 					return (error);
27350Sstevel@tonic-gate 			}
27360Sstevel@tonic-gate 			break;
27370Sstevel@tonic-gate 		}
27380Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
27390Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 			/*
27420Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27430Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27440Sstevel@tonic-gate 			 */
27450Sstevel@tonic-gate 			if (inlen != 0 &&
27460Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
27470Sstevel@tonic-gate 				return (EINVAL);
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 			if (checkonly)
27500Sstevel@tonic-gate 				break;
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 			if (inlen == 0) {
27530Sstevel@tonic-gate 				if (sticky &&
27540Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
27550Sstevel@tonic-gate 					kmem_free(ipp->ipp_rtdstopts,
27560Sstevel@tonic-gate 					    ipp->ipp_rtdstoptslen);
27570Sstevel@tonic-gate 					ipp->ipp_rtdstopts = NULL;
27580Sstevel@tonic-gate 					ipp->ipp_rtdstoptslen = 0;
27590Sstevel@tonic-gate 				}
27600Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
27610Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
27620Sstevel@tonic-gate 			} else {
27631676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
27640Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rtdstopts,
27651676Sjpk 				    &ipp->ipp_rtdstoptslen, 0);
27660Sstevel@tonic-gate 				if (error != 0)
27670Sstevel@tonic-gate 					return (error);
27680Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
27690Sstevel@tonic-gate 			}
27700Sstevel@tonic-gate 			if (sticky) {
27715240Snordmark 				error = icmp_build_hdrs(icmp);
27720Sstevel@tonic-gate 				if (error != 0)
27730Sstevel@tonic-gate 					return (error);
27740Sstevel@tonic-gate 			}
27750Sstevel@tonic-gate 			break;
27760Sstevel@tonic-gate 		}
27770Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
27780Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 			/*
27810Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27820Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27830Sstevel@tonic-gate 			 */
27840Sstevel@tonic-gate 			if (inlen != 0 &&
27850Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
27860Sstevel@tonic-gate 				return (EINVAL);
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 			if (checkonly)
27890Sstevel@tonic-gate 				break;
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 			if (inlen == 0) {
27920Sstevel@tonic-gate 				if (sticky &&
27930Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
27940Sstevel@tonic-gate 					kmem_free(ipp->ipp_dstopts,
27950Sstevel@tonic-gate 					    ipp->ipp_dstoptslen);
27960Sstevel@tonic-gate 					ipp->ipp_dstopts = NULL;
27970Sstevel@tonic-gate 					ipp->ipp_dstoptslen = 0;
27980Sstevel@tonic-gate 				}
27990Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
28000Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
28010Sstevel@tonic-gate 			} else {
28021676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
28030Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_dstopts,
28041676Sjpk 				    &ipp->ipp_dstoptslen, 0);
28050Sstevel@tonic-gate 				if (error != 0)
28060Sstevel@tonic-gate 					return (error);
28070Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
28080Sstevel@tonic-gate 			}
28090Sstevel@tonic-gate 			if (sticky) {
28105240Snordmark 				error = icmp_build_hdrs(icmp);
28110Sstevel@tonic-gate 				if (error != 0)
28120Sstevel@tonic-gate 					return (error);
28130Sstevel@tonic-gate 			}
28140Sstevel@tonic-gate 			break;
28150Sstevel@tonic-gate 		}
28160Sstevel@tonic-gate 		case IPV6_RTHDR: {
28170Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 			/*
28200Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
28210Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
28220Sstevel@tonic-gate 			 */
28230Sstevel@tonic-gate 			if (inlen != 0 &&
28240Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1)))
28250Sstevel@tonic-gate 				return (EINVAL);
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 			if (checkonly)
28280Sstevel@tonic-gate 				break;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 			if (inlen == 0) {
28310Sstevel@tonic-gate 				if (sticky &&
28320Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
28330Sstevel@tonic-gate 					kmem_free(ipp->ipp_rthdr,
28340Sstevel@tonic-gate 					    ipp->ipp_rthdrlen);
28350Sstevel@tonic-gate 					ipp->ipp_rthdr = NULL;
28360Sstevel@tonic-gate 					ipp->ipp_rthdrlen = 0;
28370Sstevel@tonic-gate 				}
28380Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
28390Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
28400Sstevel@tonic-gate 			} else {
28411676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
28420Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rthdr,
28431676Sjpk 				    &ipp->ipp_rthdrlen, 0);
28440Sstevel@tonic-gate 				if (error != 0)
28450Sstevel@tonic-gate 					return (error);
28460Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
28470Sstevel@tonic-gate 			}
28480Sstevel@tonic-gate 			if (sticky) {
28495240Snordmark 				error = icmp_build_hdrs(icmp);
28500Sstevel@tonic-gate 				if (error != 0)
28510Sstevel@tonic-gate 					return (error);
28520Sstevel@tonic-gate 			}
28530Sstevel@tonic-gate 			break;
28540Sstevel@tonic-gate 		}
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 		case IPV6_DONTFRAG:
28570Sstevel@tonic-gate 			if (checkonly)
28580Sstevel@tonic-gate 				break;
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 			if (onoff) {
28610Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DONTFRAG;
28620Sstevel@tonic-gate 			} else {
28630Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
28640Sstevel@tonic-gate 			}
28650Sstevel@tonic-gate 			break;
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 		case IPV6_USE_MIN_MTU:
28680Sstevel@tonic-gate 			if (inlen != sizeof (int))
28690Sstevel@tonic-gate 				return (EINVAL);
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > 1)
28720Sstevel@tonic-gate 				return (EINVAL);
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 			if (checkonly)
28750Sstevel@tonic-gate 				break;
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
28780Sstevel@tonic-gate 			ipp->ipp_use_min_mtu = *i1;
28790Sstevel@tonic-gate 			break;
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 		/*
28820Sstevel@tonic-gate 		 * This option can't be set.  Its only returned via
28830Sstevel@tonic-gate 		 * getsockopt() or ancillary data.
28840Sstevel@tonic-gate 		 */
28850Sstevel@tonic-gate 		case IPV6_PATHMTU:
28860Sstevel@tonic-gate 			return (EINVAL);
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 		case IPV6_BOUND_PIF:
28890Sstevel@tonic-gate 		case IPV6_SEC_OPT:
28900Sstevel@tonic-gate 		case IPV6_DONTFAILOVER_IF:
28910Sstevel@tonic-gate 		case IPV6_SRC_PREFERENCES:
28920Sstevel@tonic-gate 		case IPV6_V6ONLY:
28930Sstevel@tonic-gate 			/* Handled at IP level */
28940Sstevel@tonic-gate 			return (-EINVAL);
28950Sstevel@tonic-gate 		default:
28960Sstevel@tonic-gate 			*outlenp = 0;
28970Sstevel@tonic-gate 			return (EINVAL);
28980Sstevel@tonic-gate 		}
28990Sstevel@tonic-gate 		break;
29000Sstevel@tonic-gate 	}		/* end IPPROTO_IPV6 */
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
29030Sstevel@tonic-gate 		/*
29040Sstevel@tonic-gate 		 * Only allow IPv6 option processing on IPv6 sockets.
29050Sstevel@tonic-gate 		 */
29060Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
29070Sstevel@tonic-gate 			*outlenp = 0;
29080Sstevel@tonic-gate 			return (ENOPROTOOPT);
29090Sstevel@tonic-gate 		}
29100Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
29110Sstevel@tonic-gate 			*outlenp = 0;
29120Sstevel@tonic-gate 			return (ENOPROTOOPT);
29130Sstevel@tonic-gate 		}
29140Sstevel@tonic-gate 		switch (name) {
29150Sstevel@tonic-gate 		case ICMP6_FILTER:
29160Sstevel@tonic-gate 			if (!checkonly) {
29170Sstevel@tonic-gate 				if ((inlen != 0) &&
29180Sstevel@tonic-gate 				    (inlen != sizeof (icmp6_filter_t)))
29190Sstevel@tonic-gate 					return (EINVAL);
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate 				if (inlen == 0) {
29220Sstevel@tonic-gate 					if (icmp->icmp_filter != NULL) {
29230Sstevel@tonic-gate 						kmem_free(icmp->icmp_filter,
29240Sstevel@tonic-gate 						    sizeof (icmp6_filter_t));
29250Sstevel@tonic-gate 						icmp->icmp_filter = NULL;
29260Sstevel@tonic-gate 					}
29270Sstevel@tonic-gate 				} else {
29280Sstevel@tonic-gate 					if (icmp->icmp_filter == NULL) {
29290Sstevel@tonic-gate 						icmp->icmp_filter = kmem_alloc(
29300Sstevel@tonic-gate 						    sizeof (icmp6_filter_t),
29310Sstevel@tonic-gate 						    KM_NOSLEEP);
29320Sstevel@tonic-gate 						if (icmp->icmp_filter == NULL) {
29330Sstevel@tonic-gate 							*outlenp = 0;
29340Sstevel@tonic-gate 							return (ENOBUFS);
29350Sstevel@tonic-gate 						}
29360Sstevel@tonic-gate 					}
29370Sstevel@tonic-gate 					(void) bcopy(invalp, icmp->icmp_filter,
29380Sstevel@tonic-gate 					    inlen);
29390Sstevel@tonic-gate 				}
29400Sstevel@tonic-gate 			}
29410Sstevel@tonic-gate 			break;
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 		default:
29440Sstevel@tonic-gate 			*outlenp = 0;
29450Sstevel@tonic-gate 			return (EINVAL);
29460Sstevel@tonic-gate 		}
29470Sstevel@tonic-gate 		break;
29480Sstevel@tonic-gate 	default:
29490Sstevel@tonic-gate 		*outlenp = 0;
29500Sstevel@tonic-gate 		return (EINVAL);
29510Sstevel@tonic-gate 	}
29520Sstevel@tonic-gate 	/*
29530Sstevel@tonic-gate 	 * Common case of OK return with outval same as inval.
29540Sstevel@tonic-gate 	 */
29550Sstevel@tonic-gate 	if (invalp != outvalp) {
29560Sstevel@tonic-gate 		/* don't trust bcopy for identical src/dst */
29570Sstevel@tonic-gate 		(void) bcopy(invalp, outvalp, inlen);
29580Sstevel@tonic-gate 	}
29590Sstevel@tonic-gate 	*outlenp = inlen;
29600Sstevel@tonic-gate 	return (0);
29610Sstevel@tonic-gate }
29625240Snordmark /* This routine sets socket options. */
29635240Snordmark /* ARGSUSED */
29645240Snordmark int
29655240Snordmark icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
29665240Snordmark     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
29675240Snordmark     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
29685240Snordmark {
29695240Snordmark 	icmp_t	*icmp;
29705240Snordmark 	int	err;
29715240Snordmark 
29725240Snordmark 	icmp = Q_TO_ICMP(q);
29735240Snordmark 
29745240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
29755240Snordmark 	err = icmp_opt_set_locked(q, optset_context, level, name, inlen, invalp,
29765240Snordmark 	    outlenp, outvalp, thisdg_attrs, cr, mblk);
29775240Snordmark 	rw_exit(&icmp->icmp_rwlock);
29785240Snordmark 	return (err);
29795240Snordmark }
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate /*
29820Sstevel@tonic-gate  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
29830Sstevel@tonic-gate  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
29840Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
29850Sstevel@tonic-gate  * headers.
29860Sstevel@tonic-gate  * Returns failure if can't allocate memory.
29870Sstevel@tonic-gate  */
29880Sstevel@tonic-gate static int
29895240Snordmark icmp_build_hdrs(icmp_t *icmp)
29900Sstevel@tonic-gate {
29913448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
29920Sstevel@tonic-gate 	uchar_t	*hdrs;
29930Sstevel@tonic-gate 	uint_t	hdrs_len;
29940Sstevel@tonic-gate 	ip6_t	*ip6h;
29950Sstevel@tonic-gate 	ip6i_t	*ip6i;
29960Sstevel@tonic-gate 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
29970Sstevel@tonic-gate 
29985240Snordmark 	ASSERT(RW_WRITE_HELD(&icmp->icmp_rwlock));
29990Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp);
30000Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
30010Sstevel@tonic-gate 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
30020Sstevel@tonic-gate 		/* Need to reallocate */
30030Sstevel@tonic-gate 		if (hdrs_len != 0) {
30040Sstevel@tonic-gate 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
30050Sstevel@tonic-gate 			if (hdrs == NULL)
30060Sstevel@tonic-gate 				return (ENOMEM);
30070Sstevel@tonic-gate 		} else {
30080Sstevel@tonic-gate 			hdrs = NULL;
30090Sstevel@tonic-gate 		}
30100Sstevel@tonic-gate 		if (icmp->icmp_sticky_hdrs_len != 0) {
30110Sstevel@tonic-gate 			kmem_free(icmp->icmp_sticky_hdrs,
30120Sstevel@tonic-gate 			    icmp->icmp_sticky_hdrs_len);
30130Sstevel@tonic-gate 		}
30140Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = hdrs;
30150Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = hdrs_len;
30160Sstevel@tonic-gate 	}
30170Sstevel@tonic-gate 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
30180Sstevel@tonic-gate 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 	/* Set header fields not in ipp */
30210Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
30220Sstevel@tonic-gate 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
30230Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
30260Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
30270Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
30280Sstevel@tonic-gate 		}
30290Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
30300Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
30310Sstevel@tonic-gate 		}
30320Sstevel@tonic-gate 	} else {
30330Sstevel@tonic-gate 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
30340Sstevel@tonic-gate 	}
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_ADDR))
30370Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 	/* Try to get everything in a single mblk */
30400Sstevel@tonic-gate 	if (hdrs_len > icmp->icmp_max_hdr_len) {
30410Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = hdrs_len;
30425240Snordmark 		rw_exit(&icmp->icmp_rwlock);
30435240Snordmark 		(void) mi_set_sth_wroff(icmp->icmp_connp->conn_rq,
30445240Snordmark 		    icmp->icmp_max_hdr_len + is->is_wroff_extra);
30455240Snordmark 		rw_enter(&icmp->icmp_rwlock, RW_WRITER);
30460Sstevel@tonic-gate 	}
30470Sstevel@tonic-gate 	return (0);
30480Sstevel@tonic-gate }
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate /*
30510Sstevel@tonic-gate  * This routine retrieves the value of an ND variable in a icmpparam_t
30520Sstevel@tonic-gate  * structure.  It is called through nd_getset when a user reads the
30530Sstevel@tonic-gate  * variable.
30540Sstevel@tonic-gate  */
30550Sstevel@tonic-gate /* ARGSUSED */
30560Sstevel@tonic-gate static int
30570Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
30580Sstevel@tonic-gate {
30590Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
30620Sstevel@tonic-gate 	return (0);
30630Sstevel@tonic-gate }
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate /*
30660Sstevel@tonic-gate  * Walk through the param array specified registering each element with the
30670Sstevel@tonic-gate  * named dispatch (ND) handler.
30680Sstevel@tonic-gate  */
30690Sstevel@tonic-gate static boolean_t
30703448Sdh155122 icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt)
30710Sstevel@tonic-gate {
30720Sstevel@tonic-gate 	for (; cnt-- > 0; icmppa++) {
30730Sstevel@tonic-gate 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
30743448Sdh155122 			if (!nd_load(ndp, icmppa->icmp_param_name,
30750Sstevel@tonic-gate 			    icmp_param_get, icmp_param_set,
30760Sstevel@tonic-gate 			    (caddr_t)icmppa)) {
30773448Sdh155122 				nd_free(ndp);
30780Sstevel@tonic-gate 				return (B_FALSE);
30790Sstevel@tonic-gate 			}
30800Sstevel@tonic-gate 		}
30810Sstevel@tonic-gate 	}
30823448Sdh155122 	if (!nd_load(ndp, "icmp_status", icmp_status_report, NULL,
30830Sstevel@tonic-gate 	    NULL)) {
30843448Sdh155122 		nd_free(ndp);
30850Sstevel@tonic-gate 		return (B_FALSE);
30860Sstevel@tonic-gate 	}
30870Sstevel@tonic-gate 	return (B_TRUE);
30880Sstevel@tonic-gate }
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */
30910Sstevel@tonic-gate /* ARGSUSED */
30920Sstevel@tonic-gate static int
30930Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
30940Sstevel@tonic-gate {
30950Sstevel@tonic-gate 	long		new_value;
30960Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 	/*
30990Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
31000Sstevel@tonic-gate 	 * required bounds.
31010Sstevel@tonic-gate 	 */
31020Sstevel@tonic-gate 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
31030Sstevel@tonic-gate 	    new_value < icmppa->icmp_param_min ||
31040Sstevel@tonic-gate 	    new_value > icmppa->icmp_param_max) {
31050Sstevel@tonic-gate 		return (EINVAL);
31060Sstevel@tonic-gate 	}
31070Sstevel@tonic-gate 	/* Set the new value */
31080Sstevel@tonic-gate 	icmppa->icmp_param_value = new_value;
31090Sstevel@tonic-gate 	return (0);
31100Sstevel@tonic-gate }
31115240Snordmark /*ARGSUSED2*/
31120Sstevel@tonic-gate static void
31135240Snordmark icmp_input(void *arg1, mblk_t *mp, void *arg2)
31140Sstevel@tonic-gate {
31155240Snordmark 	conn_t *connp = (conn_t *)arg1;
31160Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
31170Sstevel@tonic-gate 	uchar_t			*rptr;
31185240Snordmark 	icmp_t			*icmp;
31195240Snordmark 	icmp_stack_t		*is;
31200Sstevel@tonic-gate 	sin_t			*sin;
31210Sstevel@tonic-gate 	sin6_t			*sin6;
31220Sstevel@tonic-gate 	ip6_t			*ip6h;
31230Sstevel@tonic-gate 	ip6i_t			*ip6i;
31240Sstevel@tonic-gate 	mblk_t			*mp1;
31250Sstevel@tonic-gate 	int			hdr_len;
31260Sstevel@tonic-gate 	ipha_t			*ipha;
31270Sstevel@tonic-gate 	int			udi_size;	/* Size of T_unitdata_ind */
31280Sstevel@tonic-gate 	uint_t			ipvers;
31290Sstevel@tonic-gate 	ip6_pkt_t		ipp;
31300Sstevel@tonic-gate 	uint8_t			nexthdr;
31313318Srshoaib 	ip_pktinfo_t		*pinfo = NULL;
31320Sstevel@tonic-gate 	mblk_t			*options_mp = NULL;
31330Sstevel@tonic-gate 	uint_t			icmp_opt = 0;
31340Sstevel@tonic-gate 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
31351676Sjpk 	uint_t			hopstrip;
31360Sstevel@tonic-gate 
31375240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
31385240Snordmark 
31395240Snordmark 	icmp = connp->conn_icmp;
31405240Snordmark 	is = icmp->icmp_is;
31415240Snordmark 	rptr = mp->b_rptr;
31425240Snordmark 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_CTL);
31435240Snordmark 	ASSERT(OK_32PTR(rptr));
31445240Snordmark 
31455240Snordmark 	/*
31465240Snordmark 	 * IP should have prepended the options data in an M_CTL
31475240Snordmark 	 * Check M_CTL "type" to make sure are not here bcos of
31485240Snordmark 	 * a valid ICMP message
31495240Snordmark 	 */
31505240Snordmark 	if (DB_TYPE(mp) == M_CTL) {
31510Sstevel@tonic-gate 		/*
31525240Snordmark 		 * FIXME: does IP still do this?
31530Sstevel@tonic-gate 		 * IP sends up the IPSEC_IN message for handling IPSEC
31540Sstevel@tonic-gate 		 * policy at the TCP level. We don't need it here.
31550Sstevel@tonic-gate 		 */
31560Sstevel@tonic-gate 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
31570Sstevel@tonic-gate 			mp1 = mp->b_cont;
31580Sstevel@tonic-gate 			freeb(mp);
31590Sstevel@tonic-gate 			mp = mp1;
31605240Snordmark 			rptr = mp->b_rptr;
31615240Snordmark 		} else if (MBLKL(mp) == sizeof (ip_pktinfo_t) &&
31625240Snordmark 		    ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type ==
31635240Snordmark 		    IN_PKTINFO) {
31645240Snordmark 			/*
31655240Snordmark 			 * IP_RECVIF or IP_RECVSLLA or IPF_RECVADDR information
31665240Snordmark 			 * has been prepended to the packet by IP. We need to
31675240Snordmark 			 * extract the mblk and adjust the rptr
31685240Snordmark 			 */
31693318Srshoaib 			pinfo = (ip_pktinfo_t *)mp->b_rptr;
31705240Snordmark 			options_mp = mp;
31715240Snordmark 			mp = mp->b_cont;
31725240Snordmark 			rptr = mp->b_rptr;
31735240Snordmark 		} else {
31745240Snordmark 			/*
31755240Snordmark 			 * ICMP messages.
31765240Snordmark 			 */
31775240Snordmark 			icmp_icmp_error(connp->conn_rq, mp);
31780Sstevel@tonic-gate 			return;
31790Sstevel@tonic-gate 		}
31800Sstevel@tonic-gate 	}
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate 	/*
31830Sstevel@tonic-gate 	 * Discard message if it is misaligned or smaller than the IP header.
31840Sstevel@tonic-gate 	 */
31850Sstevel@tonic-gate 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
31860Sstevel@tonic-gate 		freemsg(mp);
31870Sstevel@tonic-gate 		if (options_mp != NULL)
31880Sstevel@tonic-gate 			freeb(options_mp);
31895240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
31900Sstevel@tonic-gate 		return;
31910Sstevel@tonic-gate 	}
31920Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	/* Handle M_DATA messages containing IP packets messages */
31950Sstevel@tonic-gate 	if (ipvers == IPV4_VERSION) {
31960Sstevel@tonic-gate 		/*
31970Sstevel@tonic-gate 		 * Special case where IP attaches
31980Sstevel@tonic-gate 		 * the IRE needs to be handled so that we don't send up
31990Sstevel@tonic-gate 		 * IRE to the user land.
32000Sstevel@tonic-gate 		 */
32010Sstevel@tonic-gate 		ipha = (ipha_t *)rptr;
32020Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
32030Sstevel@tonic-gate 
32040Sstevel@tonic-gate 		if (ipha->ipha_protocol == IPPROTO_TCP) {
32050Sstevel@tonic-gate 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
32080Sstevel@tonic-gate 			    TH_SYN) && mp->b_cont != NULL) {
32090Sstevel@tonic-gate 				mp1 = mp->b_cont;
32100Sstevel@tonic-gate 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
32110Sstevel@tonic-gate 					freeb(mp1);
32120Sstevel@tonic-gate 					mp->b_cont = NULL;
32130Sstevel@tonic-gate 				}
32140Sstevel@tonic-gate 			}
32150Sstevel@tonic-gate 		}
32163448Sdh155122 		if (is->is_bsd_compat) {
32170Sstevel@tonic-gate 			ushort_t len;
32180Sstevel@tonic-gate 			len = ntohs(ipha->ipha_length);
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 			if (mp->b_datap->db_ref > 1) {
32210Sstevel@tonic-gate 				/*
32220Sstevel@tonic-gate 				 * Allocate a new IP header so that we can
32230Sstevel@tonic-gate 				 * modify ipha_length.
32240Sstevel@tonic-gate 				 */
32250Sstevel@tonic-gate 				mblk_t	*mp1;
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 				mp1 = allocb(hdr_len, BPRI_MED);
32280Sstevel@tonic-gate 				if (!mp1) {
32290Sstevel@tonic-gate 					freemsg(mp);
32300Sstevel@tonic-gate 					if (options_mp != NULL)
32310Sstevel@tonic-gate 						freeb(options_mp);
32325240Snordmark 					BUMP_MIB(&is->is_rawip_mib,
32333448Sdh155122 					    rawipInErrors);
32340Sstevel@tonic-gate 					return;
32350Sstevel@tonic-gate 				}
32360Sstevel@tonic-gate 				bcopy(rptr, mp1->b_rptr, hdr_len);
32370Sstevel@tonic-gate 				mp->b_rptr = rptr + hdr_len;
32380Sstevel@tonic-gate 				rptr = mp1->b_rptr;
32390Sstevel@tonic-gate 				ipha = (ipha_t *)rptr;
32400Sstevel@tonic-gate 				mp1->b_cont = mp;
32410Sstevel@tonic-gate 				mp1->b_wptr = rptr + hdr_len;
32420Sstevel@tonic-gate 				mp = mp1;
32430Sstevel@tonic-gate 			}
32440Sstevel@tonic-gate 			len -= hdr_len;
32450Sstevel@tonic-gate 			ipha->ipha_length = htons(len);
32460Sstevel@tonic-gate 		}
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 	/*
32500Sstevel@tonic-gate 	 * This is the inbound data path.  Packets are passed upstream as
32510Sstevel@tonic-gate 	 * T_UNITDATA_IND messages with full IP headers still attached.
32520Sstevel@tonic-gate 	 */
32530Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
32540Sstevel@tonic-gate 		ASSERT(ipvers == IPV4_VERSION);
32550Sstevel@tonic-gate 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
32565267Snordmark 		if (icmp->icmp_recvif && (pinfo != NULL) &&
32573318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
32580Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
32590Sstevel@tonic-gate 			    sizeof (uint_t);
32600Sstevel@tonic-gate 		}
32613318Srshoaib 
32625267Snordmark 		if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) &&
32633318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
32643318Srshoaib 			udi_size += sizeof (struct T_opthdr) +
32653318Srshoaib 			    sizeof (struct in_pktinfo);
32663318Srshoaib 		}
32673318Srshoaib 
32681673Sgt145670 		/*
32691673Sgt145670 		 * If SO_TIMESTAMP is set allocate the appropriate sized
32701673Sgt145670 		 * buffer. Since gethrestime() expects a pointer aligned
32711673Sgt145670 		 * argument, we allocate space necessary for extra
32721673Sgt145670 		 * alignment (even though it might not be used).
32731673Sgt145670 		 */
32741673Sgt145670 		if (icmp->icmp_timestamp) {
32751673Sgt145670 			udi_size += sizeof (struct T_opthdr) +
32761673Sgt145670 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
32771673Sgt145670 		}
32780Sstevel@tonic-gate 		mp1 = allocb(udi_size, BPRI_MED);
32790Sstevel@tonic-gate 		if (mp1 == NULL) {
32800Sstevel@tonic-gate 			freemsg(mp);
32810Sstevel@tonic-gate 			if (options_mp != NULL)
32820Sstevel@tonic-gate 				freeb(options_mp);
32835240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
32840Sstevel@tonic-gate 			return;
32850Sstevel@tonic-gate 		}
32860Sstevel@tonic-gate 		mp1->b_cont = mp;
32870Sstevel@tonic-gate 		mp = mp1;
32880Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
32890Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
32900Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)tudi + udi_size;
32910Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
32920Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin_t);
32930Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
32940Sstevel@tonic-gate 		sin = (sin_t *)&tudi[1];
32950Sstevel@tonic-gate 		*sin = sin_null;
32960Sstevel@tonic-gate 		sin->sin_family = AF_INET;
32970Sstevel@tonic-gate 		sin->sin_addr.s_addr = ipha->ipha_src;
32980Sstevel@tonic-gate 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
32990Sstevel@tonic-gate 		    sizeof (sin_t);
33000Sstevel@tonic-gate 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
33010Sstevel@tonic-gate 		tudi->OPT_length = udi_size;
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 		/*
33040Sstevel@tonic-gate 		 * Add options if IP_RECVIF is set
33050Sstevel@tonic-gate 		 */
33060Sstevel@tonic-gate 		if (udi_size != 0) {
33070Sstevel@tonic-gate 			char *dstopt;
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 			dstopt = (char *)&sin[1];
33105267Snordmark 			if (icmp->icmp_recvif && (pinfo != NULL) &&
33113318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 				struct T_opthdr *toh;
33140Sstevel@tonic-gate 				uint_t		*dstptr;
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 				toh = (struct T_opthdr *)dstopt;
33170Sstevel@tonic-gate 				toh->level = IPPROTO_IP;
33180Sstevel@tonic-gate 				toh->name = IP_RECVIF;
33190Sstevel@tonic-gate 				toh->len = sizeof (struct T_opthdr) +
33205240Snordmark 				    sizeof (uint_t);
33210Sstevel@tonic-gate 				toh->status = 0;
33220Sstevel@tonic-gate 				dstopt += sizeof (struct T_opthdr);
33230Sstevel@tonic-gate 				dstptr = (uint_t *)dstopt;
33243318Srshoaib 				*dstptr = pinfo->ip_pkt_ifindex;
33250Sstevel@tonic-gate 				dstopt += sizeof (uint_t);
33260Sstevel@tonic-gate 				udi_size -= toh->len;
33270Sstevel@tonic-gate 			}
33281673Sgt145670 			if (icmp->icmp_timestamp) {
33291673Sgt145670 				struct	T_opthdr *toh;
33301673Sgt145670 
33311673Sgt145670 				toh = (struct T_opthdr *)dstopt;
33321673Sgt145670 				toh->level = SOL_SOCKET;
33331673Sgt145670 				toh->name = SCM_TIMESTAMP;
33341673Sgt145670 				toh->len = sizeof (struct T_opthdr) +
33351673Sgt145670 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
33361673Sgt145670 				toh->status = 0;
33371673Sgt145670 				dstopt += sizeof (struct T_opthdr);
33381673Sgt145670 				/* Align for gethrestime() */
33391673Sgt145670 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
33401673Sgt145670 				    sizeof (intptr_t));
33411673Sgt145670 				gethrestime((timestruc_t *)dstopt);
33423318Srshoaib 				dstopt = (char *)toh + toh->len;
33433318Srshoaib 				udi_size -= toh->len;
33443318Srshoaib 			}
33455267Snordmark 			if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) &&
33463318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
33473318Srshoaib 				struct	T_opthdr *toh;
33483318Srshoaib 				struct	in_pktinfo *pktinfop;
33493318Srshoaib 
33503318Srshoaib 				toh = (struct T_opthdr *)dstopt;
33513318Srshoaib 				toh->level = IPPROTO_IP;
33523318Srshoaib 				toh->name = IP_PKTINFO;
33533318Srshoaib 				toh->len = sizeof (struct T_opthdr) +
33543318Srshoaib 				    sizeof (in_pktinfo_t);
33553318Srshoaib 				toh->status = 0;
33563318Srshoaib 				dstopt += sizeof (struct T_opthdr);
33573318Srshoaib 				pktinfop = (struct in_pktinfo *)dstopt;
33583318Srshoaib 				pktinfop->ipi_ifindex = pinfo->ip_pkt_ifindex;
33593318Srshoaib 				pktinfop->ipi_spec_dst =
33603318Srshoaib 				    pinfo->ip_pkt_match_addr;
33613318Srshoaib 
33623318Srshoaib 				pktinfop->ipi_addr.s_addr = ipha->ipha_dst;
33633318Srshoaib 
33643318Srshoaib 				dstopt += sizeof (struct in_pktinfo);
33651673Sgt145670 				udi_size -= toh->len;
33661673Sgt145670 			}
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 			/* Consumed all of allocated space */
33690Sstevel@tonic-gate 			ASSERT(udi_size == 0);
33700Sstevel@tonic-gate 		}
33710Sstevel@tonic-gate 
33725267Snordmark 		if (options_mp != NULL)
33735267Snordmark 			freeb(options_mp);
33745267Snordmark 
33755240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
33765240Snordmark 		putnext(connp->conn_rq, mp);
33770Sstevel@tonic-gate 		return;
33780Sstevel@tonic-gate 	}
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate 	/*
33810Sstevel@tonic-gate 	 * We don't need options_mp in the IPv6 path.
33820Sstevel@tonic-gate 	 */
33830Sstevel@tonic-gate 	if (options_mp != NULL) {
33840Sstevel@tonic-gate 		freeb(options_mp);
33850Sstevel@tonic-gate 		options_mp = NULL;
33860Sstevel@tonic-gate 	}
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 	/*
33890Sstevel@tonic-gate 	 * Discard message if it is smaller than the IPv6 header
33900Sstevel@tonic-gate 	 * or if the header is malformed.
33910Sstevel@tonic-gate 	 */
33920Sstevel@tonic-gate 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
33930Sstevel@tonic-gate 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
33940Sstevel@tonic-gate 	    icmp->icmp_family != AF_INET6) {
33950Sstevel@tonic-gate 		freemsg(mp);
33965240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
33970Sstevel@tonic-gate 		return;
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	/* Initialize */
34010Sstevel@tonic-gate 	ipp.ipp_fields = 0;
34021676Sjpk 	hopstrip = 0;
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate 	ip6h = (ip6_t *)rptr;
34050Sstevel@tonic-gate 	/*
34060Sstevel@tonic-gate 	 * Call on ip_find_hdr_v6 which gets the total hdr len
34070Sstevel@tonic-gate 	 * as well as individual lenghts of ext hdrs (and ptrs to
34080Sstevel@tonic-gate 	 * them).
34090Sstevel@tonic-gate 	 */
34100Sstevel@tonic-gate 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
34110Sstevel@tonic-gate 		/* Look for ifindex information */
34120Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
34130Sstevel@tonic-gate 			ip6i = (ip6i_t *)ip6h;
34140Sstevel@tonic-gate 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
34150Sstevel@tonic-gate 				ASSERT(ip6i->ip6i_ifindex != 0);
34160Sstevel@tonic-gate 				ipp.ipp_fields |= IPPF_IFINDEX;
34170Sstevel@tonic-gate 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
34180Sstevel@tonic-gate 			}
34190Sstevel@tonic-gate 			rptr = (uchar_t *)&ip6i[1];
34200Sstevel@tonic-gate 			mp->b_rptr = rptr;
34210Sstevel@tonic-gate 			if (rptr == mp->b_wptr) {
34220Sstevel@tonic-gate 				mp1 = mp->b_cont;
34230Sstevel@tonic-gate 				freeb(mp);
34240Sstevel@tonic-gate 				mp = mp1;
34250Sstevel@tonic-gate 				rptr = mp->b_rptr;
34260Sstevel@tonic-gate 			}
34270Sstevel@tonic-gate 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
34280Sstevel@tonic-gate 			ip6h = (ip6_t *)rptr;
34290Sstevel@tonic-gate 		}
34300Sstevel@tonic-gate 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
34311676Sjpk 
34321676Sjpk 		/*
34331676Sjpk 		 * We need to lie a bit to the user because users inside
34341676Sjpk 		 * labeled compartments should not see their own labels.  We
34351676Sjpk 		 * assume that in all other respects IP has checked the label,
34361676Sjpk 		 * and that the label is always first among the options.  (If
34371676Sjpk 		 * it's not first, then this code won't see it, and the option
34381676Sjpk 		 * will be passed along to the user.)
34391676Sjpk 		 *
34401676Sjpk 		 * If we had multilevel ICMP sockets, then the following code
34411676Sjpk 		 * should be skipped for them to allow the user to see the
34421676Sjpk 		 * label.
34431676Sjpk 		 *
34441676Sjpk 		 * Alignment restrictions in the definition of IP options
34451676Sjpk 		 * (namely, the requirement that the 4-octet DOI goes on a
34461676Sjpk 		 * 4-octet boundary) mean that we know exactly where the option
34471676Sjpk 		 * should start, but we're lenient for other hosts.
34481676Sjpk 		 *
34491676Sjpk 		 * Note that there are no multilevel ICMP or raw IP sockets
34501676Sjpk 		 * yet, thus nobody ever sees the IP6OPT_LS option.
34511676Sjpk 		 */
34521676Sjpk 		if ((ipp.ipp_fields & IPPF_HOPOPTS) &&
34531676Sjpk 		    ipp.ipp_hopoptslen > 5 && is_system_labeled()) {
34541676Sjpk 			const uchar_t *ucp =
34551676Sjpk 			    (const uchar_t *)ipp.ipp_hopopts + 2;
34561676Sjpk 			int remlen = ipp.ipp_hopoptslen - 2;
34571676Sjpk 
34581676Sjpk 			while (remlen > 0) {
34591676Sjpk 				if (*ucp == IP6OPT_PAD1) {
34601676Sjpk 					remlen--;
34611676Sjpk 					ucp++;
34621676Sjpk 				} else if (*ucp == IP6OPT_PADN) {
34631676Sjpk 					remlen -= ucp[1] + 2;
34641676Sjpk 					ucp += ucp[1] + 2;
34651676Sjpk 				} else if (*ucp == ip6opt_ls) {
34661676Sjpk 					hopstrip = (ucp -
34671676Sjpk 					    (const uchar_t *)ipp.ipp_hopopts) +
34681676Sjpk 					    ucp[1] + 2;
34691676Sjpk 					hopstrip = (hopstrip + 7) & ~7;
34701676Sjpk 					break;
34711676Sjpk 				} else {
34721676Sjpk 					/* label option must be first */
34731676Sjpk 					break;
34741676Sjpk 				}
34751676Sjpk 			}
34761676Sjpk 		}
34770Sstevel@tonic-gate 	} else {
34780Sstevel@tonic-gate 		hdr_len = IPV6_HDR_LEN;
34790Sstevel@tonic-gate 		ip6i = NULL;
34800Sstevel@tonic-gate 		nexthdr = ip6h->ip6_nxt;
34810Sstevel@tonic-gate 	}
34820Sstevel@tonic-gate 	/*
34830Sstevel@tonic-gate 	 * One special case where IP attaches the IRE needs to
34840Sstevel@tonic-gate 	 * be handled so that we don't send up IRE to the user land.
34850Sstevel@tonic-gate 	 */
34860Sstevel@tonic-gate 	if (nexthdr == IPPROTO_TCP) {
34870Sstevel@tonic-gate 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
34900Sstevel@tonic-gate 		    mp->b_cont != NULL) {
34910Sstevel@tonic-gate 			mp1 = mp->b_cont;
34920Sstevel@tonic-gate 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
34930Sstevel@tonic-gate 				freeb(mp1);
34940Sstevel@tonic-gate 				mp->b_cont = NULL;
34950Sstevel@tonic-gate 			}
34960Sstevel@tonic-gate 		}
34970Sstevel@tonic-gate 	}
34980Sstevel@tonic-gate 	/*
34990Sstevel@tonic-gate 	 * Check a filter for ICMPv6 types if needed.
35000Sstevel@tonic-gate 	 * Verify raw checksums if needed.
35010Sstevel@tonic-gate 	 */
35020Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
35030Sstevel@tonic-gate 		if (icmp->icmp_filter != NULL) {
35040Sstevel@tonic-gate 			int type;
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 			/* Assumes that IP has done the pullupmsg */
35070Sstevel@tonic-gate 			type = mp->b_rptr[hdr_len];
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
35100Sstevel@tonic-gate 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
35110Sstevel@tonic-gate 				freemsg(mp);
35120Sstevel@tonic-gate 				return;
35130Sstevel@tonic-gate 			}
35140Sstevel@tonic-gate 		} else {
35150Sstevel@tonic-gate 			/* Checksum */
35160Sstevel@tonic-gate 			uint16_t	*up;
35170Sstevel@tonic-gate 			uint32_t	sum;
35180Sstevel@tonic-gate 			int		remlen;
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 			up = (uint16_t *)&ip6h->ip6_src;
35210Sstevel@tonic-gate 
35220Sstevel@tonic-gate 			remlen = msgdsize(mp) - hdr_len;
35230Sstevel@tonic-gate 			sum = htons(icmp->icmp_proto + remlen)
35240Sstevel@tonic-gate 			    + up[0] + up[1] + up[2] + up[3]
35250Sstevel@tonic-gate 			    + up[4] + up[5] + up[6] + up[7]
35260Sstevel@tonic-gate 			    + up[8] + up[9] + up[10] + up[11]
35270Sstevel@tonic-gate 			    + up[12] + up[13] + up[14] + up[15];
35280Sstevel@tonic-gate 			sum = (sum & 0xffff) + (sum >> 16);
35290Sstevel@tonic-gate 			sum = IP_CSUM(mp, hdr_len, sum);
35300Sstevel@tonic-gate 			if (sum != 0) {
35310Sstevel@tonic-gate 				/* IPv6 RAW checksum failed */
35320Sstevel@tonic-gate 				ip0dbg(("icmp_rput: RAW checksum "
35330Sstevel@tonic-gate 				    "failed %x\n", sum));
35340Sstevel@tonic-gate 				freemsg(mp);
35355240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
35363448Sdh155122 				    rawipInCksumErrs);
35370Sstevel@tonic-gate 				return;
35380Sstevel@tonic-gate 			}
35390Sstevel@tonic-gate 		}
35400Sstevel@tonic-gate 	}
35410Sstevel@tonic-gate 	/* Skip all the IPv6 headers per API */
35420Sstevel@tonic-gate 	mp->b_rptr += hdr_len;
35430Sstevel@tonic-gate 
35440Sstevel@tonic-gate 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 	/*
35470Sstevel@tonic-gate 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
35480Sstevel@tonic-gate 	 * maintain state information, instead of relying on icmp_t
35490Sstevel@tonic-gate 	 * structure, since there arent any locks protecting these members
35500Sstevel@tonic-gate 	 * and there is a window where there might be a race between a
35510Sstevel@tonic-gate 	 * thread setting options on the write side and a thread reading
35520Sstevel@tonic-gate 	 * these options on the read size.
35530Sstevel@tonic-gate 	 */
35540Sstevel@tonic-gate 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
35550Sstevel@tonic-gate 	    IPPF_RTHDR|IPPF_IFINDEX)) {
35560Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvhopopts &&
35571676Sjpk 		    (ipp.ipp_fields & IPPF_HOPOPTS) &&
35581676Sjpk 		    ipp.ipp_hopoptslen > hopstrip) {
35590Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35601676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
35610Sstevel@tonic-gate 			icmp_opt |= IPPF_HOPOPTS;
35620Sstevel@tonic-gate 		}
35630Sstevel@tonic-gate 		if ((icmp->icmp_ipv6_recvdstopts ||
35645240Snordmark 		    icmp->icmp_old_ipv6_recvdstopts) &&
35650Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
35660Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35670Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
35680Sstevel@tonic-gate 			icmp_opt |= IPPF_DSTOPTS;
35690Sstevel@tonic-gate 		}
35700Sstevel@tonic-gate 		if (((icmp->icmp_ipv6_recvdstopts &&
35710Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrthdr &&
35720Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
35730Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrtdstopts) &&
35740Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
35750Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35760Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
35770Sstevel@tonic-gate 			icmp_opt |= IPPF_RTDSTOPTS;
35780Sstevel@tonic-gate 		}
35790Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvrthdr &&
35800Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) {
35810Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35820Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
35830Sstevel@tonic-gate 			icmp_opt |= IPPF_RTHDR;
35840Sstevel@tonic-gate 		}
35853318Srshoaib 		if (icmp->icmp_ip_recvpktinfo &&
35860Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
35870Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35880Sstevel@tonic-gate 			    sizeof (struct in6_pktinfo);
35890Sstevel@tonic-gate 			icmp_opt |= IPPF_IFINDEX;
35900Sstevel@tonic-gate 		}
35910Sstevel@tonic-gate 	}
35920Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvhoplimit) {
35930Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
35940Sstevel@tonic-gate 		icmp_ipv6_recvhoplimit = B_TRUE;
35950Sstevel@tonic-gate 	}
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvtclass)
35980Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 	mp1 = allocb(udi_size, BPRI_MED);
36010Sstevel@tonic-gate 	if (mp1 == NULL) {
36020Sstevel@tonic-gate 		freemsg(mp);
36035240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
36040Sstevel@tonic-gate 		return;
36050Sstevel@tonic-gate 	}
36060Sstevel@tonic-gate 	mp1->b_cont = mp;
36070Sstevel@tonic-gate 	mp = mp1;
36080Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
36090Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
36100Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)tudi + udi_size;
36110Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
36120Sstevel@tonic-gate 	tudi->SRC_length = sizeof (sin6_t);
36130Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
36140Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
36150Sstevel@tonic-gate 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
36160Sstevel@tonic-gate 	tudi->OPT_length = udi_size;
36170Sstevel@tonic-gate 	sin6 = (sin6_t *)&tudi[1];
36180Sstevel@tonic-gate 	sin6->sin6_port = 0;
36190Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 	sin6->sin6_addr = ip6h->ip6_src;
36220Sstevel@tonic-gate 	/* No sin6_flowinfo per API */
36230Sstevel@tonic-gate 	sin6->sin6_flowinfo = 0;
36240Sstevel@tonic-gate 	/* For link-scope source pass up scope id */
36250Sstevel@tonic-gate 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
36260Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
36270Sstevel@tonic-gate 		sin6->sin6_scope_id = ipp.ipp_ifindex;
36280Sstevel@tonic-gate 	else
36290Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
36323448Sdh155122 	    icmp->icmp_zoneid, is->is_netstack);
36330Sstevel@tonic-gate 
36340Sstevel@tonic-gate 	if (udi_size != 0) {
36350Sstevel@tonic-gate 		uchar_t *dstopt;
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 		dstopt = (uchar_t *)&sin6[1];
36380Sstevel@tonic-gate 		if (icmp_opt & IPPF_IFINDEX) {
36390Sstevel@tonic-gate 			struct T_opthdr *toh;
36400Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36430Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36440Sstevel@tonic-gate 			toh->name = IPV6_PKTINFO;
36450Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36460Sstevel@tonic-gate 			    sizeof (*pkti);
36470Sstevel@tonic-gate 			toh->status = 0;
36480Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36490Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)dstopt;
36500Sstevel@tonic-gate 			pkti->ipi6_addr = ip6h->ip6_dst;
36510Sstevel@tonic-gate 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
36520Sstevel@tonic-gate 			dstopt += sizeof (*pkti);
36530Sstevel@tonic-gate 			udi_size -= toh->len;
36540Sstevel@tonic-gate 		}
36550Sstevel@tonic-gate 		if (icmp_ipv6_recvhoplimit) {
36560Sstevel@tonic-gate 			struct T_opthdr *toh;
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36590Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36600Sstevel@tonic-gate 			toh->name = IPV6_HOPLIMIT;
36610Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36620Sstevel@tonic-gate 			    sizeof (uint_t);
36630Sstevel@tonic-gate 			toh->status = 0;
36640Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36650Sstevel@tonic-gate 			*(uint_t *)dstopt = ip6h->ip6_hops;
36660Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
36670Sstevel@tonic-gate 			udi_size -= toh->len;
36680Sstevel@tonic-gate 		}
36690Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvtclass) {
36700Sstevel@tonic-gate 			struct T_opthdr *toh;
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36730Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36740Sstevel@tonic-gate 			toh->name = IPV6_TCLASS;
36750Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36760Sstevel@tonic-gate 			    sizeof (uint_t);
36770Sstevel@tonic-gate 			toh->status = 0;
36780Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36790Sstevel@tonic-gate 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
36800Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
36810Sstevel@tonic-gate 			udi_size -= toh->len;
36820Sstevel@tonic-gate 		}
36830Sstevel@tonic-gate 		if (icmp_opt & IPPF_HOPOPTS) {
36840Sstevel@tonic-gate 			struct T_opthdr *toh;
36850Sstevel@tonic-gate 
36860Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36870Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36880Sstevel@tonic-gate 			toh->name = IPV6_HOPOPTS;
36890Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36901676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
36910Sstevel@tonic-gate 			toh->status = 0;
36920Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36931676Sjpk 			bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt,
36941676Sjpk 			    ipp.ipp_hopoptslen - hopstrip);
36951676Sjpk 			if (hopstrip > 0) {
36961676Sjpk 				/* copy next header value and fake length */
36971676Sjpk 				dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0];
36981676Sjpk 				dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] -
36991676Sjpk 				    hopstrip / 8;
37001676Sjpk 			}
37011676Sjpk 			dstopt += ipp.ipp_hopoptslen - hopstrip;
37020Sstevel@tonic-gate 			udi_size -= toh->len;
37030Sstevel@tonic-gate 		}
37040Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTDSTOPTS) {
37050Sstevel@tonic-gate 			struct T_opthdr *toh;
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37080Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37090Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
37100Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37110Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
37120Sstevel@tonic-gate 			toh->status = 0;
37130Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37140Sstevel@tonic-gate 			bcopy(ipp.ipp_rtdstopts, dstopt,
37150Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen);
37160Sstevel@tonic-gate 			dstopt += ipp.ipp_rtdstoptslen;
37170Sstevel@tonic-gate 			udi_size -= toh->len;
37180Sstevel@tonic-gate 		}
37190Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTHDR) {
37200Sstevel@tonic-gate 			struct T_opthdr *toh;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37230Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37240Sstevel@tonic-gate 			toh->name = IPV6_RTHDR;
37250Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37260Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
37270Sstevel@tonic-gate 			toh->status = 0;
37280Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37290Sstevel@tonic-gate 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
37300Sstevel@tonic-gate 			dstopt += ipp.ipp_rthdrlen;
37310Sstevel@tonic-gate 			udi_size -= toh->len;
37320Sstevel@tonic-gate 		}
37330Sstevel@tonic-gate 		if (icmp_opt & IPPF_DSTOPTS) {
37340Sstevel@tonic-gate 			struct T_opthdr *toh;
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37370Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37380Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
37390Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37400Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
37410Sstevel@tonic-gate 			toh->status = 0;
37420Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37430Sstevel@tonic-gate 			bcopy(ipp.ipp_dstopts, dstopt,
37440Sstevel@tonic-gate 			    ipp.ipp_dstoptslen);
37450Sstevel@tonic-gate 			dstopt += ipp.ipp_dstoptslen;
37460Sstevel@tonic-gate 			udi_size -= toh->len;
37470Sstevel@tonic-gate 		}
37480Sstevel@tonic-gate 		/* Consumed all of allocated space */
37490Sstevel@tonic-gate 		ASSERT(udi_size == 0);
37500Sstevel@tonic-gate 	}
37515240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
37525240Snordmark 	putnext(connp->conn_rq, mp);
37535240Snordmark }
37545240Snordmark 
37555240Snordmark /*
37565240Snordmark  * Handle the results of a T_BIND_REQ whether deferred by IP or handled
37575240Snordmark  * immediately.
37585240Snordmark  */
37595240Snordmark static void
37605240Snordmark icmp_bind_result(conn_t *connp, mblk_t *mp)
37615240Snordmark {
37625240Snordmark 	struct T_error_ack	*tea;
37635240Snordmark 
37645240Snordmark 	switch (mp->b_datap->db_type) {
37655240Snordmark 	case M_PROTO:
37665240Snordmark 	case M_PCPROTO:
37675240Snordmark 		/* M_PROTO messages contain some type of TPI message. */
37685240Snordmark 		if ((mp->b_wptr - mp->b_rptr) < sizeof (t_scalar_t)) {
37695240Snordmark 			freemsg(mp);
37705240Snordmark 			return;
37715240Snordmark 		}
37725240Snordmark 		tea = (struct T_error_ack *)mp->b_rptr;
37735240Snordmark 
37745240Snordmark 		switch (tea->PRIM_type) {
37755240Snordmark 		case T_ERROR_ACK:
37765240Snordmark 			switch (tea->ERROR_prim) {
37775240Snordmark 			case O_T_BIND_REQ:
37785240Snordmark 			case T_BIND_REQ:
37795240Snordmark 				icmp_bind_error(connp, mp);
37805240Snordmark 				return;
37815240Snordmark 			default:
37825240Snordmark 				break;
37835240Snordmark 			}
37845240Snordmark 			ASSERT(0);
37855240Snordmark 			freemsg(mp);
37865240Snordmark 			return;
37875240Snordmark 
37885240Snordmark 		case T_BIND_ACK:
37895240Snordmark 			icmp_bind_ack(connp, mp);
37905240Snordmark 			return;
37915240Snordmark 
37925240Snordmark 		default:
37935240Snordmark 			break;
37945240Snordmark 		}
37955240Snordmark 		freemsg(mp);
37965240Snordmark 		return;
37975240Snordmark 	default:
37985240Snordmark 		/* FIXME: other cases? */
37995240Snordmark 		ASSERT(0);
38005240Snordmark 		freemsg(mp);
38015240Snordmark 		return;
38025240Snordmark 	}
38030Sstevel@tonic-gate }
38040Sstevel@tonic-gate 
38050Sstevel@tonic-gate /*
38060Sstevel@tonic-gate  * Process a T_BIND_ACK
38070Sstevel@tonic-gate  */
38080Sstevel@tonic-gate static void
38095240Snordmark icmp_bind_ack(conn_t *connp, mblk_t *mp)
38100Sstevel@tonic-gate {
38115240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
38120Sstevel@tonic-gate 	mblk_t	*mp1;
38130Sstevel@tonic-gate 	ire_t	*ire;
38140Sstevel@tonic-gate 	struct T_bind_ack *tba;
38150Sstevel@tonic-gate 	uchar_t *addrp;
38160Sstevel@tonic-gate 	ipa_conn_t	*ac;
38170Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
38180Sstevel@tonic-gate 
38195240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
38200Sstevel@tonic-gate 	/*
38210Sstevel@tonic-gate 	 * We know if headers are included or not so we can
38220Sstevel@tonic-gate 	 * safely do this.
38230Sstevel@tonic-gate 	 */
38240Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
38250Sstevel@tonic-gate 		/*
38260Sstevel@tonic-gate 		 * TPI has not yet bound - bind sent by
38270Sstevel@tonic-gate 		 * icmp_bind_proto.
38280Sstevel@tonic-gate 		 */
38290Sstevel@tonic-gate 		freemsg(mp);
38305240Snordmark 		rw_exit(&icmp->icmp_rwlock);
38310Sstevel@tonic-gate 		return;
38320Sstevel@tonic-gate 	}
38335240Snordmark 	ASSERT(icmp->icmp_pending_op != -1);
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 	/*
38360Sstevel@tonic-gate 	 * If a broadcast/multicast address was bound set
38370Sstevel@tonic-gate 	 * the source address to 0.
38380Sstevel@tonic-gate 	 * This ensures no datagrams with broadcast address
38390Sstevel@tonic-gate 	 * as source address are emitted (which would violate
38400Sstevel@tonic-gate 	 * RFC1122 - Hosts requirements)
38410Sstevel@tonic-gate 	 *
38420Sstevel@tonic-gate 	 * Note that when connecting the returned IRE is
38430Sstevel@tonic-gate 	 * for the destination address and we only perform
38440Sstevel@tonic-gate 	 * the broadcast check for the source address (it
38450Sstevel@tonic-gate 	 * is OK to connect to a broadcast/multicast address.)
38460Sstevel@tonic-gate 	 */
38470Sstevel@tonic-gate 	mp1 = mp->b_cont;
38480Sstevel@tonic-gate 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
38490Sstevel@tonic-gate 		ire = (ire_t *)mp1->b_rptr;
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 		/*
38520Sstevel@tonic-gate 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
38530Sstevel@tonic-gate 		 * local address.
38540Sstevel@tonic-gate 		 */
38550Sstevel@tonic-gate 		if (ire->ire_type == IRE_BROADCAST &&
38560Sstevel@tonic-gate 		    icmp->icmp_state != TS_DATA_XFER) {
38575240Snordmark 			ASSERT(icmp->icmp_pending_op == T_BIND_REQ ||
38585240Snordmark 			    icmp->icmp_pending_op == O_T_BIND_REQ);
38590Sstevel@tonic-gate 			/* This was just a local bind to a MC/broadcast addr */
38600Sstevel@tonic-gate 			V6_SET_ZERO(icmp->icmp_v6src);
38610Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6)
38625240Snordmark 				(void) icmp_build_hdrs(icmp);
38630Sstevel@tonic-gate 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
38640Sstevel@tonic-gate 			/*
38650Sstevel@tonic-gate 			 * Local address not yet set - pick it from the
38660Sstevel@tonic-gate 			 * T_bind_ack
38670Sstevel@tonic-gate 			 */
38680Sstevel@tonic-gate 			tba = (struct T_bind_ack *)mp->b_rptr;
38690Sstevel@tonic-gate 			addrp = &mp->b_rptr[tba->ADDR_offset];
38700Sstevel@tonic-gate 			switch (icmp->icmp_family) {
38710Sstevel@tonic-gate 			case AF_INET:
38720Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
38730Sstevel@tonic-gate 					ac = (ipa_conn_t *)addrp;
38740Sstevel@tonic-gate 				} else {
38750Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
38760Sstevel@tonic-gate 					    sizeof (ipa_conn_x_t));
38770Sstevel@tonic-gate 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
38780Sstevel@tonic-gate 				}
38790Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
38800Sstevel@tonic-gate 				    &icmp->icmp_v6src);
38810Sstevel@tonic-gate 				break;
38820Sstevel@tonic-gate 			case AF_INET6:
38830Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
38840Sstevel@tonic-gate 					ac6 = (ipa6_conn_t *)addrp;
38850Sstevel@tonic-gate 				} else {
38860Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
38870Sstevel@tonic-gate 					    sizeof (ipa6_conn_x_t));
38880Sstevel@tonic-gate 					ac6 = &((ipa6_conn_x_t *)
38890Sstevel@tonic-gate 					    addrp)->ac6x_conn;
38900Sstevel@tonic-gate 				}
38910Sstevel@tonic-gate 				icmp->icmp_v6src = ac6->ac6_laddr;
38925240Snordmark 				(void) icmp_build_hdrs(icmp);
38930Sstevel@tonic-gate 			}
38940Sstevel@tonic-gate 		}
38950Sstevel@tonic-gate 		mp1 = mp1->b_cont;
38960Sstevel@tonic-gate 	}
38975240Snordmark 	icmp->icmp_pending_op = -1;
38985240Snordmark 	rw_exit(&icmp->icmp_rwlock);
38990Sstevel@tonic-gate 	/*
39000Sstevel@tonic-gate 	 * Look for one or more appended ACK message added by
39010Sstevel@tonic-gate 	 * icmp_connect or icmp_disconnect.
39020Sstevel@tonic-gate 	 * If none found just send up the T_BIND_ACK.
39030Sstevel@tonic-gate 	 * icmp_connect has appended a T_OK_ACK and a
39040Sstevel@tonic-gate 	 * T_CONN_CON.
39050Sstevel@tonic-gate 	 * icmp_disconnect has appended a T_OK_ACK.
39060Sstevel@tonic-gate 	 */
39070Sstevel@tonic-gate 	if (mp1 != NULL) {
39080Sstevel@tonic-gate 		if (mp->b_cont == mp1)
39090Sstevel@tonic-gate 			mp->b_cont = NULL;
39100Sstevel@tonic-gate 		else {
39110Sstevel@tonic-gate 			ASSERT(mp->b_cont->b_cont == mp1);
39120Sstevel@tonic-gate 			mp->b_cont->b_cont = NULL;
39130Sstevel@tonic-gate 		}
39140Sstevel@tonic-gate 		freemsg(mp);
39150Sstevel@tonic-gate 		mp = mp1;
39160Sstevel@tonic-gate 		while (mp != NULL) {
39170Sstevel@tonic-gate 			mp1 = mp->b_cont;
39180Sstevel@tonic-gate 			mp->b_cont = NULL;
39195240Snordmark 			putnext(connp->conn_rq, mp);
39200Sstevel@tonic-gate 			mp = mp1;
39210Sstevel@tonic-gate 		}
39220Sstevel@tonic-gate 		return;
39230Sstevel@tonic-gate 	}
39240Sstevel@tonic-gate 	freemsg(mp->b_cont);
39250Sstevel@tonic-gate 	mp->b_cont = NULL;
39265240Snordmark 	putnext(connp->conn_rq, mp);
39275240Snordmark }
39285240Snordmark 
39295240Snordmark static void
39305240Snordmark icmp_bind_error(conn_t *connp, mblk_t *mp)
39315240Snordmark {
39325240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
39335240Snordmark 	struct T_error_ack *tea;
39345240Snordmark 
39355240Snordmark 	tea = (struct T_error_ack *)mp->b_rptr;
39365240Snordmark 	/*
39375240Snordmark 	 * If our O_T_BIND_REQ/T_BIND_REQ fails,
39385240Snordmark 	 * clear out the source address before
39395240Snordmark 	 * passing the message upstream.
39405240Snordmark 	 * If this was caused by a T_CONN_REQ
39415240Snordmark 	 * revert back to bound state.
39425240Snordmark 	 */
39435240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
39445240Snordmark 	if (icmp->icmp_state == TS_UNBND) {
39455240Snordmark 		/*
39465240Snordmark 		 * TPI has not yet bound - bind sent by icmp_bind_proto.
39475240Snordmark 		 */
39485240Snordmark 		freemsg(mp);
39495240Snordmark 		rw_exit(&icmp->icmp_rwlock);
39505240Snordmark 		return;
39515240Snordmark 	}
39525240Snordmark 	ASSERT(icmp->icmp_pending_op != -1);
39535240Snordmark 	tea->ERROR_prim = icmp->icmp_pending_op;
39545240Snordmark 	icmp->icmp_pending_op = -1;
39555240Snordmark 
39565240Snordmark 	switch (tea->ERROR_prim) {
39575240Snordmark 	case T_CONN_REQ:
39585240Snordmark 		ASSERT(icmp->icmp_state == TS_DATA_XFER);
39595240Snordmark 		/* Connect failed */
39605240Snordmark 		/* Revert back to the bound source */
39615240Snordmark 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
39625240Snordmark 		icmp->icmp_state = TS_IDLE;
39635240Snordmark 		if (icmp->icmp_family == AF_INET6)
39645240Snordmark 			(void) icmp_build_hdrs(icmp);
39655240Snordmark 		break;
39665240Snordmark 
39675240Snordmark 	case T_DISCON_REQ:
39685240Snordmark 	case T_BIND_REQ:
39695240Snordmark 	case O_T_BIND_REQ:
39705240Snordmark 		V6_SET_ZERO(icmp->icmp_v6src);
39715240Snordmark 		V6_SET_ZERO(icmp->icmp_bound_v6src);
39725240Snordmark 		icmp->icmp_state = TS_UNBND;
39735240Snordmark 		if (icmp->icmp_family == AF_INET6)
39745240Snordmark 			(void) icmp_build_hdrs(icmp);
39755240Snordmark 		break;
39765240Snordmark 	default:
39775240Snordmark 		break;
39785240Snordmark 	}
39795240Snordmark 	rw_exit(&icmp->icmp_rwlock);
39805240Snordmark 	putnext(connp->conn_rq, mp);
39810Sstevel@tonic-gate }
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate /*
39840Sstevel@tonic-gate  * return SNMP stuff in buffer in mpdata
39850Sstevel@tonic-gate  */
39865240Snordmark mblk_t *
39870Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl)
39880Sstevel@tonic-gate {
39890Sstevel@tonic-gate 	mblk_t			*mpdata;
39900Sstevel@tonic-gate 	struct opthdr		*optp;
39915240Snordmark 	conn_t			*connp = Q_TO_CONN(q);
39925240Snordmark 	icmp_stack_t		*is = connp->conn_netstack->netstack_icmp;
39935240Snordmark 	mblk_t			*mp2ctl;
39945240Snordmark 
39955240Snordmark 	/*
39965240Snordmark 	 * make a copy of the original message
39975240Snordmark 	 */
39985240Snordmark 	mp2ctl = copymsg(mpctl);
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate 	if (mpctl == NULL ||
40010Sstevel@tonic-gate 	    (mpdata = mpctl->b_cont) == NULL) {
40025240Snordmark 		freemsg(mpctl);
40035240Snordmark 		freemsg(mp2ctl);
40040Sstevel@tonic-gate 		return (0);
40050Sstevel@tonic-gate 	}
40060Sstevel@tonic-gate 
40070Sstevel@tonic-gate 	/* fixed length structure for IPv4 and IPv6 counters */
40080Sstevel@tonic-gate 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
40090Sstevel@tonic-gate 	optp->level = EXPER_RAWIP;
40100Sstevel@tonic-gate 	optp->name = 0;
40115240Snordmark 	(void) snmp_append_data(mpdata, (char *)&is->is_rawip_mib,
40125240Snordmark 	    sizeof (is->is_rawip_mib));
40130Sstevel@tonic-gate 	optp->len = msgdsize(mpdata);
40140Sstevel@tonic-gate 	qreply(q, mpctl);
40150Sstevel@tonic-gate 
40165240Snordmark 	return (mp2ctl);
40170Sstevel@tonic-gate }
40180Sstevel@tonic-gate 
40190Sstevel@tonic-gate /*
40200Sstevel@tonic-gate  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
40210Sstevel@tonic-gate  * TODO:  If this ever actually tries to set anything, it needs to be
40220Sstevel@tonic-gate  * to do the appropriate locking.
40230Sstevel@tonic-gate  */
40240Sstevel@tonic-gate /* ARGSUSED */
40255240Snordmark int
40260Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
40270Sstevel@tonic-gate     uchar_t *ptr, int len)
40280Sstevel@tonic-gate {
40290Sstevel@tonic-gate 	switch (level) {
40300Sstevel@tonic-gate 	case EXPER_RAWIP:
40310Sstevel@tonic-gate 		return (0);
40320Sstevel@tonic-gate 	default:
40330Sstevel@tonic-gate 		return (1);
40340Sstevel@tonic-gate 	}
40350Sstevel@tonic-gate }
40360Sstevel@tonic-gate 
40370Sstevel@tonic-gate /* Report for ndd "icmp_status" */
40380Sstevel@tonic-gate /* ARGSUSED */
40390Sstevel@tonic-gate static int
40400Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
40410Sstevel@tonic-gate {
40425240Snordmark 	conn_t  *connp;
40435240Snordmark 	ip_stack_t *ipst;
40440Sstevel@tonic-gate 	char	laddrbuf[INET6_ADDRSTRLEN];
40450Sstevel@tonic-gate 	char	faddrbuf[INET6_ADDRSTRLEN];
40465240Snordmark 	int	i;
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
40490Sstevel@tonic-gate 	    "RAWIP    " MI_COL_HDRPAD_STR
40500Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
40510Sstevel@tonic-gate 	    "  src addr        dest addr       state");
40520Sstevel@tonic-gate 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
40530Sstevel@tonic-gate 
40545240Snordmark 	connp = Q_TO_CONN(q);
40555240Snordmark 	ipst = connp->conn_netstack->netstack_ip;
40565240Snordmark 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
40575240Snordmark 		connf_t *connfp;
40585240Snordmark 		char	*state;
40595240Snordmark 
40605240Snordmark 		connfp = &ipst->ips_ipcl_globalhash_fanout[i];
40615240Snordmark 		connp = NULL;
40625240Snordmark 
40635240Snordmark 		while ((connp = ipcl_get_next_conn(connfp, connp,
40645240Snordmark 		    IPCL_RAWIPCONN)) != NULL) {
40655240Snordmark 			icmp_t  *icmp;
40665240Snordmark 
40675240Snordmark 			mutex_enter(&(connp)->conn_lock);
40685240Snordmark 			icmp = connp->conn_icmp;
40695240Snordmark 
40705240Snordmark 			if (icmp->icmp_state == TS_UNBND)
40715240Snordmark 				state = "UNBOUND";
40725240Snordmark 			else if (icmp->icmp_state == TS_IDLE)
40735240Snordmark 				state = "IDLE";
40745240Snordmark 			else if (icmp->icmp_state == TS_DATA_XFER)
40755240Snordmark 				state = "CONNECTED";
40765240Snordmark 			else
40775240Snordmark 				state = "UnkState";
40785240Snordmark 
40795240Snordmark 			(void) mi_mpprintf(mp, MI_COL_PTRFMT_STR "%s %s %s",
40805240Snordmark 			    (void *)icmp,
40815240Snordmark 			    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
40825240Snordmark 			    sizeof (faddrbuf)),
40835240Snordmark 			    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
40845240Snordmark 			    sizeof (laddrbuf)),
40855240Snordmark 			    state);
40865240Snordmark 			mutex_exit(&(connp)->conn_lock);
40875240Snordmark 		}
40880Sstevel@tonic-gate 	}
40890Sstevel@tonic-gate 	return (0);
40900Sstevel@tonic-gate }
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate /*
40930Sstevel@tonic-gate  * This routine creates a T_UDERROR_IND message and passes it upstream.
40940Sstevel@tonic-gate  * The address and options are copied from the T_UNITDATA_REQ message
40950Sstevel@tonic-gate  * passed in mp.  This message is freed.
40960Sstevel@tonic-gate  */
40970Sstevel@tonic-gate static void
40980Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
40990Sstevel@tonic-gate {
41000Sstevel@tonic-gate 	mblk_t	*mp1;
41010Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
41020Sstevel@tonic-gate 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
41030Sstevel@tonic-gate 
41040Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
41050Sstevel@tonic-gate 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
41060Sstevel@tonic-gate 	    tudr->OPT_length, err);
41070Sstevel@tonic-gate 	if (mp1)
41080Sstevel@tonic-gate 		qreply(q, mp1);
41090Sstevel@tonic-gate 	freemsg(mp);
41100Sstevel@tonic-gate }
41110Sstevel@tonic-gate 
41120Sstevel@tonic-gate /*
41130Sstevel@tonic-gate  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
41140Sstevel@tonic-gate  * After some error checking, the message is passed downstream to ip.
41150Sstevel@tonic-gate  */
41160Sstevel@tonic-gate static void
41170Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp)
41180Sstevel@tonic-gate {
41195240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
41205240Snordmark 
41215240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
41220Sstevel@tonic-gate 	/* If a bind has not been done, we can't unbind. */
41235240Snordmark 	if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) {
41245240Snordmark 		rw_exit(&icmp->icmp_rwlock);
41250Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
41260Sstevel@tonic-gate 		return;
41270Sstevel@tonic-gate 	}
41285240Snordmark 	icmp->icmp_pending_op = T_UNBIND_REQ;
41295240Snordmark 	rw_exit(&icmp->icmp_rwlock);
41305240Snordmark 
41315240Snordmark 	/*
41325240Snordmark 	 * Pass the unbind to IP; T_UNBIND_REQ is larger than T_OK_ACK
41335240Snordmark 	 * and therefore ip_unbind must never return NULL.
41345240Snordmark 	 */
41355240Snordmark 	mp = ip_unbind(q, mp);
41365240Snordmark 	ASSERT(mp != NULL);
41375240Snordmark 	ASSERT(((struct T_ok_ack *)mp->b_rptr)->PRIM_type == T_OK_ACK);
41385240Snordmark 
41395240Snordmark 	/*
41405240Snordmark 	 * Once we're unbound from IP, the pending operation may be cleared
41415240Snordmark 	 * here.
41425240Snordmark 	 */
41435240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
41440Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_v6src);
41450Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_bound_v6src);
41465240Snordmark 	icmp->icmp_pending_op = -1;
41470Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
41485240Snordmark 	if (icmp->icmp_family == AF_INET6)
41495240Snordmark 		(void) icmp_build_hdrs(icmp);
41505240Snordmark 	rw_exit(&icmp->icmp_rwlock);
41515240Snordmark 
41525240Snordmark 	qreply(q, mp);
41530Sstevel@tonic-gate }
41540Sstevel@tonic-gate 
41550Sstevel@tonic-gate /*
41560Sstevel@tonic-gate  * Process IPv4 packets that already include an IP header.
41570Sstevel@tonic-gate  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
41580Sstevel@tonic-gate  * IPPROTO_IGMP).
41590Sstevel@tonic-gate  */
41600Sstevel@tonic-gate static void
41615240Snordmark icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp, ip4_pkt_t *pktinfop)
41620Sstevel@tonic-gate {
41633448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
41640Sstevel@tonic-gate 	ipha_t	*ipha;
41650Sstevel@tonic-gate 	int	ip_hdr_length;
41660Sstevel@tonic-gate 	int	tp_hdr_len;
41670Sstevel@tonic-gate 	mblk_t	*mp1;
41680Sstevel@tonic-gate 	uint_t	pkt_len;
41693318Srshoaib 	ip_opt_info_t optinfo;
41705240Snordmark 	conn_t	*connp = icmp->icmp_connp;
41713318Srshoaib 
41723318Srshoaib 	optinfo.ip_opt_flags = 0;
41733318Srshoaib 	optinfo.ip_opt_ill_index = 0;
41740Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
41750Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
41760Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
41770Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
41783448Sdh155122 			ASSERT(icmp != NULL);
41795240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
41800Sstevel@tonic-gate 			freemsg(mp);
41810Sstevel@tonic-gate 			return;
41820Sstevel@tonic-gate 		}
41830Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
41840Sstevel@tonic-gate 	}
41850Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length =
41860Sstevel@tonic-gate 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
41870Sstevel@tonic-gate 
41880Sstevel@tonic-gate 	/*
41890Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
41900Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
41910Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
41920Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
41930Sstevel@tonic-gate 	 */
41940Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
41950Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
41960Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
41970Sstevel@tonic-gate 	/* Insert options if any */
41980Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
41990Sstevel@tonic-gate 		/*
42000Sstevel@tonic-gate 		 * Put the IP header plus any transport header that is
42010Sstevel@tonic-gate 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
42020Sstevel@tonic-gate 		 * that at least the checksum field is in the first mblk.)
42030Sstevel@tonic-gate 		 */
42040Sstevel@tonic-gate 		switch (ipha->ipha_protocol) {
42050Sstevel@tonic-gate 		case IPPROTO_UDP:
42060Sstevel@tonic-gate 			tp_hdr_len = 8;
42070Sstevel@tonic-gate 			break;
42080Sstevel@tonic-gate 		case IPPROTO_TCP:
42090Sstevel@tonic-gate 			tp_hdr_len = 20;
42100Sstevel@tonic-gate 			break;
42110Sstevel@tonic-gate 		default:
42120Sstevel@tonic-gate 			tp_hdr_len = 0;
42130Sstevel@tonic-gate 			break;
42140Sstevel@tonic-gate 		}
42150Sstevel@tonic-gate 		/*
42160Sstevel@tonic-gate 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
42170Sstevel@tonic-gate 		 * tp_hdr_len bytes will be in a single mblk.
42180Sstevel@tonic-gate 		 */
42190Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
42200Sstevel@tonic-gate 		    tp_hdr_len)) {
42210Sstevel@tonic-gate 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
42220Sstevel@tonic-gate 			    tp_hdr_len)) {
42235240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
42243448Sdh155122 				    rawipOutErrors);
42250Sstevel@tonic-gate 				freemsg(mp);
42260Sstevel@tonic-gate 				return;
42270Sstevel@tonic-gate 			}
42280Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
42290Sstevel@tonic-gate 		}
42300Sstevel@tonic-gate 
42310Sstevel@tonic-gate 		/*
42320Sstevel@tonic-gate 		 * if the length is larger then the max allowed IP packet,
42330Sstevel@tonic-gate 		 * then send an error and abort the processing.
42340Sstevel@tonic-gate 		 */
42350Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length)
42360Sstevel@tonic-gate 		    + icmp->icmp_ip_snd_options_len;
42370Sstevel@tonic-gate 		if (pkt_len > IP_MAXPACKET) {
42380Sstevel@tonic-gate 			icmp_ud_err(q, mp, EMSGSIZE);
42390Sstevel@tonic-gate 			return;
42400Sstevel@tonic-gate 		}
42413448Sdh155122 		if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra +
42420Sstevel@tonic-gate 		    tp_hdr_len, BPRI_LO))) {
42430Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
42440Sstevel@tonic-gate 			return;
42450Sstevel@tonic-gate 		}
42463448Sdh155122 		mp1->b_rptr += is->is_wroff_extra;
42470Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
42480Sstevel@tonic-gate 
42490Sstevel@tonic-gate 		ipha->ipha_length = htons((uint16_t)pkt_len);
42500Sstevel@tonic-gate 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
42510Sstevel@tonic-gate 
42520Sstevel@tonic-gate 		/* Copy transport header if any */
42530Sstevel@tonic-gate 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
42540Sstevel@tonic-gate 		mp1->b_wptr += tp_hdr_len;
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate 		/* Add options */
42570Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_rptr;
42580Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
42590Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len);
42600Sstevel@tonic-gate 
42610Sstevel@tonic-gate 		/* Drop IP header and transport header from original */
42620Sstevel@tonic-gate 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
42630Sstevel@tonic-gate 
42640Sstevel@tonic-gate 		mp1->b_cont = mp;
42650Sstevel@tonic-gate 		mp = mp1;
42660Sstevel@tonic-gate 		/*
42670Sstevel@tonic-gate 		 * Massage source route putting first source
42680Sstevel@tonic-gate 		 * route in ipha_dst.
42690Sstevel@tonic-gate 		 */
42705240Snordmark 		(void) ip_massage_options(ipha, is->is_netstack);
42710Sstevel@tonic-gate 	}
42723318Srshoaib 
42733318Srshoaib 	if (pktinfop != NULL) {
42743318Srshoaib 		/*
42753318Srshoaib 		 * Over write the source address provided in the header
42763318Srshoaib 		 */
42773318Srshoaib 		if (pktinfop->ip4_addr != INADDR_ANY) {
42783318Srshoaib 			ipha->ipha_src = pktinfop->ip4_addr;
42793318Srshoaib 			optinfo.ip_opt_flags = IP_VERIFY_SRC;
42803318Srshoaib 		}
42813318Srshoaib 
42823318Srshoaib 		if (pktinfop->ip4_ill_index != 0) {
42833318Srshoaib 			optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
42843318Srshoaib 		}
42853318Srshoaib 	}
42863318Srshoaib 
42875240Snordmark 	mblk_setcred(mp, connp->conn_cred);
42885240Snordmark 	ip_output_options(connp, mp, q, IP_WPUT,
42895240Snordmark 	    &optinfo);
42900Sstevel@tonic-gate }
42910Sstevel@tonic-gate 
42921676Sjpk static boolean_t
42931676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst)
42941676Sjpk {
42951676Sjpk 	int err;
42961676Sjpk 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
42975240Snordmark 	icmp_stack_t		*is = icmp->icmp_is;
42985240Snordmark 	conn_t	*connp = icmp->icmp_connp;
42995240Snordmark 
43005240Snordmark 	err = tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst,
43013448Sdh155122 	    opt_storage, icmp->icmp_mac_exempt,
43025240Snordmark 	    is->is_netstack->netstack_ip);
43031676Sjpk 	if (err == 0) {
43041676Sjpk 		err = tsol_update_options(&icmp->icmp_ip_snd_options,
43051676Sjpk 		    &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len,
43061676Sjpk 		    opt_storage);
43071676Sjpk 	}
43081676Sjpk 	if (err != 0) {
43095240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
43101676Sjpk 		DTRACE_PROBE4(
43111676Sjpk 		    tx__ip__log__drop__updatelabel__icmp,
43121676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
43131676Sjpk 		    queue_t *, q, char *, opt_storage, mblk_t *, mp);
43141676Sjpk 		icmp_ud_err(q, mp, err);
43151676Sjpk 		return (B_FALSE);
43161676Sjpk 	}
43171676Sjpk 	IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst);
43181676Sjpk 	return (B_TRUE);
43191676Sjpk }
43201676Sjpk 
43210Sstevel@tonic-gate /*
43220Sstevel@tonic-gate  * This routine handles all messages passed downstream.  It either
43230Sstevel@tonic-gate  * consumes the message or passes it downstream; it never queues a
43240Sstevel@tonic-gate  * a message.
43250Sstevel@tonic-gate  */
43260Sstevel@tonic-gate static void
43270Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp)
43280Sstevel@tonic-gate {
43290Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
43300Sstevel@tonic-gate 	ipha_t	*ipha;
43310Sstevel@tonic-gate 	mblk_t	*mp1;
43320Sstevel@tonic-gate 	int	ip_hdr_length;
43330Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
43340Sstevel@tonic-gate 	size_t	ip_len;
43355240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
43365240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
43373448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
43380Sstevel@tonic-gate 	sin6_t	*sin6;
43390Sstevel@tonic-gate 	sin_t	*sin;
43400Sstevel@tonic-gate 	ipaddr_t	v4dst;
43413318Srshoaib 	ip4_pkt_t	pktinfo;
43423318Srshoaib 	ip4_pkt_t	*pktinfop = &pktinfo;
43433318Srshoaib 	ip_opt_info_t	optinfo;
43440Sstevel@tonic-gate 
43450Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
43460Sstevel@tonic-gate 	case M_DATA:
43470Sstevel@tonic-gate 		if (icmp->icmp_hdrincl) {
43480Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
43491676Sjpk 			ipha = (ipha_t *)mp->b_rptr;
43501676Sjpk 			if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
43511676Sjpk 				if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
43525240Snordmark 					BUMP_MIB(&is->is_rawip_mib,
43533448Sdh155122 					    rawipOutErrors);
43541676Sjpk 					freemsg(mp);
43551676Sjpk 					return;
43561676Sjpk 				}
43571676Sjpk 				ipha = (ipha_t *)mp->b_rptr;
43581676Sjpk 			}
43591676Sjpk 			/*
43601676Sjpk 			 * If this connection was used for v6 (inconceivable!)
43611676Sjpk 			 * or if we have a new destination, then it's time to
43621676Sjpk 			 * figure a new label.
43631676Sjpk 			 */
43641676Sjpk 			if (is_system_labeled() &&
43651676Sjpk 			    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
43661676Sjpk 			    V4_PART_OF_V6(icmp->icmp_v6lastdst) !=
43671676Sjpk 			    ipha->ipha_dst) &&
43681676Sjpk 			    !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) {
43691676Sjpk 				return;
43701676Sjpk 			}
43715240Snordmark 			icmp_wput_hdrincl(q, mp, icmp, NULL);
43720Sstevel@tonic-gate 			return;
43730Sstevel@tonic-gate 		}
43740Sstevel@tonic-gate 		freemsg(mp);
43750Sstevel@tonic-gate 		return;
43760Sstevel@tonic-gate 	case M_PROTO:
43770Sstevel@tonic-gate 	case M_PCPROTO:
43780Sstevel@tonic-gate 		ip_len = mp->b_wptr - rptr;
43790Sstevel@tonic-gate 		if (ip_len >= sizeof (struct T_unitdata_req)) {
43800Sstevel@tonic-gate 			/* Expedite valid T_UNITDATA_REQ to below the switch */
43810Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type
43820Sstevel@tonic-gate 			    == T_UNITDATA_REQ)
43830Sstevel@tonic-gate 				break;
43840Sstevel@tonic-gate 		}
43850Sstevel@tonic-gate 		/* FALLTHRU */
43860Sstevel@tonic-gate 	default:
43870Sstevel@tonic-gate 		icmp_wput_other(q, mp);
43880Sstevel@tonic-gate 		return;
43890Sstevel@tonic-gate 	}
43900Sstevel@tonic-gate 
43910Sstevel@tonic-gate 	/* Handle T_UNITDATA_REQ messages here. */
43920Sstevel@tonic-gate 
43933318Srshoaib 
43943318Srshoaib 
43950Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
43960Sstevel@tonic-gate 		/* If a port has not been bound to the stream, fail. */
43975240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
43980Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
43990Sstevel@tonic-gate 		return;
44000Sstevel@tonic-gate 	}
44010Sstevel@tonic-gate 	mp1 = mp->b_cont;
44020Sstevel@tonic-gate 	if (mp1 == NULL) {
44035240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44040Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
44050Sstevel@tonic-gate 		return;
44060Sstevel@tonic-gate 	}
44070Sstevel@tonic-gate 
44080Sstevel@tonic-gate 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
44095240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44100Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
44110Sstevel@tonic-gate 		return;
44120Sstevel@tonic-gate 	}
44130Sstevel@tonic-gate 
44140Sstevel@tonic-gate 	switch (icmp->icmp_family) {
44150Sstevel@tonic-gate 	case AF_INET6:
44160Sstevel@tonic-gate 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
44170Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin6) ||
44180Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin6_t) ||
44190Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
44205240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44210Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44220Sstevel@tonic-gate 			return;
44230Sstevel@tonic-gate 		}
44240Sstevel@tonic-gate 
44250Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
44260Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
44275240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44280Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44290Sstevel@tonic-gate 			return;
44300Sstevel@tonic-gate 		}
44310Sstevel@tonic-gate 
44320Sstevel@tonic-gate 		/*
44330Sstevel@tonic-gate 		 * Destination is a native IPv6 address.
44340Sstevel@tonic-gate 		 * Send out an IPv6 format packet.
44350Sstevel@tonic-gate 		 */
44360Sstevel@tonic-gate 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
44370Sstevel@tonic-gate 		return;
44380Sstevel@tonic-gate 
44390Sstevel@tonic-gate 	case AF_INET:
44400Sstevel@tonic-gate 		sin = (sin_t *)&rptr[tudr->DEST_offset];
44410Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin) ||
44420Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin_t) ||
44430Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
44445240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44450Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44460Sstevel@tonic-gate 			return;
44470Sstevel@tonic-gate 		}
44480Sstevel@tonic-gate 		/* Extract and ipaddr */
44490Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
44500Sstevel@tonic-gate 		break;
44511676Sjpk 
44521676Sjpk 	default:
44531676Sjpk 		ASSERT(0);
44540Sstevel@tonic-gate 	}
44550Sstevel@tonic-gate 
44563318Srshoaib 	pktinfop->ip4_ill_index = 0;
44573318Srshoaib 	pktinfop->ip4_addr = INADDR_ANY;
44583318Srshoaib 	optinfo.ip_opt_flags = 0;
44593318Srshoaib 	optinfo.ip_opt_ill_index = 0;
44603318Srshoaib 
44613318Srshoaib 
44620Sstevel@tonic-gate 	/*
44630Sstevel@tonic-gate 	 * If options passed in, feed it for verification and handling
44640Sstevel@tonic-gate 	 */
44650Sstevel@tonic-gate 	if (tudr->OPT_length != 0) {
44660Sstevel@tonic-gate 		int error;
44670Sstevel@tonic-gate 
44683318Srshoaib 		error = 0;
44690Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
44703318Srshoaib 		    (void *)pktinfop) < 0) {
44710Sstevel@tonic-gate 			/* failure */
44725240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44730Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
44740Sstevel@tonic-gate 			return;
44750Sstevel@tonic-gate 		}
44763318Srshoaib 		ASSERT(error == 0);
44770Sstevel@tonic-gate 		/*
44780Sstevel@tonic-gate 		 * Note: Success in processing options.
44790Sstevel@tonic-gate 		 * mp option buffer represented by
44800Sstevel@tonic-gate 		 * OPT_length/offset now potentially modified
44810Sstevel@tonic-gate 		 * and contain option setting results
44820Sstevel@tonic-gate 		 */
44833318Srshoaib 
44840Sstevel@tonic-gate 	}
44850Sstevel@tonic-gate 
44861676Sjpk 	if (v4dst == INADDR_ANY)
44871676Sjpk 		v4dst = htonl(INADDR_LOOPBACK);
44881676Sjpk 
44891676Sjpk 	/* Check if our saved options are valid; update if not */
44901676Sjpk 	if (is_system_labeled() &&
44911676Sjpk 	    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
44921676Sjpk 	    V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) &&
44931676Sjpk 	    !icmp_update_label(q, icmp, mp, v4dst)) {
44941676Sjpk 		return;
44951676Sjpk 	}
44961676Sjpk 
44970Sstevel@tonic-gate 	/* Protocol 255 contains full IP headers */
44980Sstevel@tonic-gate 	if (icmp->icmp_hdrincl) {
44990Sstevel@tonic-gate 		freeb(mp);
45005240Snordmark 		icmp_wput_hdrincl(q, mp1, icmp, pktinfop);
45010Sstevel@tonic-gate 		return;
45020Sstevel@tonic-gate 	}
45031676Sjpk 
45043318Srshoaib 
45050Sstevel@tonic-gate 	/* Add an IP header */
45060Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
45070Sstevel@tonic-gate 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
45080Sstevel@tonic-gate 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
45090Sstevel@tonic-gate 	    mp1->b_datap->db_ref != 1 ||
45100Sstevel@tonic-gate 	    !OK_32PTR(ipha)) {
45113448Sdh155122 		if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra,
45120Sstevel@tonic-gate 		    BPRI_LO))) {
45135240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
45141289Sja97890 			icmp_ud_err(q, mp, ENOMEM);
45150Sstevel@tonic-gate 			return;
45160Sstevel@tonic-gate 		}
45170Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
45180Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_datap->db_lim;
45190Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)ipha;
45200Sstevel@tonic-gate 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
45210Sstevel@tonic-gate 	}
45220Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
45230Sstevel@tonic-gate 	/* Set version, header length, and tos */
45240Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
45250Sstevel@tonic-gate 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
45265240Snordmark 	    icmp->icmp_type_of_service);
45270Sstevel@tonic-gate 	/* Set ttl and protocol */
45280Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
45290Sstevel@tonic-gate #else
45300Sstevel@tonic-gate 	/* Set version, header length, and tos */
45310Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
45320Sstevel@tonic-gate 	    ((icmp->icmp_type_of_service << 8) |
45335240Snordmark 	    ((IP_VERSION << 4) | (ip_hdr_length>>2)));
45340Sstevel@tonic-gate 	/* Set ttl and protocol */
45350Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
45360Sstevel@tonic-gate #endif
45373318Srshoaib 	if (pktinfop->ip4_addr != INADDR_ANY) {
45383318Srshoaib 		ipha->ipha_src = pktinfop->ip4_addr;
45393318Srshoaib 		optinfo.ip_opt_flags = IP_VERIFY_SRC;
45403318Srshoaib 	} else {
45413318Srshoaib 
45423318Srshoaib 		/*
45433318Srshoaib 		 * Copy our address into the packet.  If this is zero,
45443318Srshoaib 		 * ip will fill in the real source address.
45453318Srshoaib 		 */
45463318Srshoaib 		IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
45473318Srshoaib 	}
45483318Srshoaib 
45490Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
45500Sstevel@tonic-gate 
45513318Srshoaib 	if (pktinfop->ip4_ill_index != 0) {
45523318Srshoaib 		optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
45533318Srshoaib 	}
45543318Srshoaib 
45553318Srshoaib 
45560Sstevel@tonic-gate 	/*
45570Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
45580Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
45590Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
45600Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
45610Sstevel@tonic-gate 	 */
45620Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
45630Sstevel@tonic-gate 
45640Sstevel@tonic-gate 	/* Finish common formatting of the packet. */
45650Sstevel@tonic-gate 	mp1->b_rptr = (uchar_t *)ipha;
45660Sstevel@tonic-gate 
45670Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
45680Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
45690Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
45700Sstevel@tonic-gate 
45710Sstevel@tonic-gate 	/*
45720Sstevel@tonic-gate 	 * Set the length into the IP header.
45730Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
45740Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
45750Sstevel@tonic-gate 	 * as this can cause problems in layers below.
45760Sstevel@tonic-gate 	 */
45770Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
45785240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
45790Sstevel@tonic-gate 		icmp_ud_err(q, mp, EMSGSIZE);
45800Sstevel@tonic-gate 		return;
45810Sstevel@tonic-gate 	}
45820Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)ip_len);
45830Sstevel@tonic-gate 	/*
45840Sstevel@tonic-gate 	 * Copy in the destination address from the T_UNITDATA
45850Sstevel@tonic-gate 	 * request
45860Sstevel@tonic-gate 	 */
45871676Sjpk 	ipha->ipha_dst = v4dst;
45880Sstevel@tonic-gate 
45890Sstevel@tonic-gate 	/*
45900Sstevel@tonic-gate 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
45910Sstevel@tonic-gate 	 */
45920Sstevel@tonic-gate 	if (CLASSD(v4dst))
45930Sstevel@tonic-gate 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
45940Sstevel@tonic-gate 
45950Sstevel@tonic-gate 	/* Copy in options if any */
45960Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
45970Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options,
45980Sstevel@tonic-gate 		    &ipha[1], icmp->icmp_ip_snd_options_len);
45990Sstevel@tonic-gate 		/*
46000Sstevel@tonic-gate 		 * Massage source route putting first source route in ipha_dst.
46010Sstevel@tonic-gate 		 * Ignore the destination in the T_unitdata_req.
46020Sstevel@tonic-gate 		 */
46035240Snordmark 		(void) ip_massage_options(ipha, is->is_netstack);
46040Sstevel@tonic-gate 	}
46053318Srshoaib 
46060Sstevel@tonic-gate 	freeb(mp);
46075240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
46085240Snordmark 	mblk_setcred(mp1, connp->conn_cred);
46095240Snordmark 	ip_output_options(Q_TO_CONN(q), mp1, q, IP_WPUT, &optinfo);
46100Sstevel@tonic-gate #undef	ipha
46110Sstevel@tonic-gate #undef tudr
46120Sstevel@tonic-gate }
46130Sstevel@tonic-gate 
46141676Sjpk static boolean_t
46151676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst)
46161676Sjpk {
46171676Sjpk 	int err;
46181676Sjpk 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
46195240Snordmark 	icmp_stack_t		*is = icmp->icmp_is;
46205240Snordmark 	conn_t	*connp = icmp->icmp_connp;
46215240Snordmark 
46225240Snordmark 	err = tsol_compute_label_v6(DB_CREDDEF(mp, connp->conn_cred), dst,
46233448Sdh155122 	    opt_storage, icmp->icmp_mac_exempt,
46245240Snordmark 	    is->is_netstack->netstack_ip);
46251676Sjpk 	if (err == 0) {
46261676Sjpk 		err = tsol_update_sticky(&icmp->icmp_sticky_ipp,
46271676Sjpk 		    &icmp->icmp_label_len_v6, opt_storage);
46281676Sjpk 	}
46291676Sjpk 	if (err != 0) {
46305240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46311676Sjpk 		DTRACE_PROBE4(
46321676Sjpk 		    tx__ip__log__drop__updatelabel__icmp6,
46331676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
46341676Sjpk 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
46351676Sjpk 		icmp_ud_err(wq, mp, err);
46361676Sjpk 		return (B_FALSE);
46371676Sjpk 	}
46381676Sjpk 
46391676Sjpk 	icmp->icmp_v6lastdst = *dst;
46401676Sjpk 	return (B_TRUE);
46411676Sjpk }
46421676Sjpk 
46430Sstevel@tonic-gate /*
46440Sstevel@tonic-gate  * icmp_wput_ipv6():
46450Sstevel@tonic-gate  * Assumes that icmp_wput did some sanity checking on the destination
46461676Sjpk  * address, but that the label may not yet be correct.
46470Sstevel@tonic-gate  */
46480Sstevel@tonic-gate void
46490Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
46500Sstevel@tonic-gate {
46510Sstevel@tonic-gate 	ip6_t			*ip6h;
46520Sstevel@tonic-gate 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
46530Sstevel@tonic-gate 	mblk_t			*mp1;
46540Sstevel@tonic-gate 	int			ip_hdr_len = IPV6_HDR_LEN;
46550Sstevel@tonic-gate 	size_t			ip_len;
46565240Snordmark 	icmp_t			*icmp = Q_TO_ICMP(q);
46573448Sdh155122 	icmp_stack_t		*is = icmp->icmp_is;
46580Sstevel@tonic-gate 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
46590Sstevel@tonic-gate 	ip6_pkt_t		*ipp = &ipp_s;
46600Sstevel@tonic-gate 	ip6_pkt_t		*tipp;
46610Sstevel@tonic-gate 	uint32_t		csum = 0;
46620Sstevel@tonic-gate 	uint_t			ignore = 0;
46630Sstevel@tonic-gate 	uint_t			option_exists = 0, is_sticky = 0;
46640Sstevel@tonic-gate 	uint8_t			*cp;
46650Sstevel@tonic-gate 	uint8_t			*nxthdr_ptr;
46661676Sjpk 	in6_addr_t		ip6_dst;
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 	/*
46690Sstevel@tonic-gate 	 * If the local address is a mapped address return
46700Sstevel@tonic-gate 	 * an error.
46710Sstevel@tonic-gate 	 * It would be possible to send an IPv6 packet but the
46720Sstevel@tonic-gate 	 * response would never make it back to the application
46730Sstevel@tonic-gate 	 * since it is bound to a mapped address.
46740Sstevel@tonic-gate 	 */
46750Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
46765240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46770Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
46780Sstevel@tonic-gate 		return;
46790Sstevel@tonic-gate 	}
46800Sstevel@tonic-gate 
46810Sstevel@tonic-gate 	ipp->ipp_fields = 0;
46820Sstevel@tonic-gate 	ipp->ipp_sticky_ignored = 0;
46830Sstevel@tonic-gate 
46840Sstevel@tonic-gate 	/*
46850Sstevel@tonic-gate 	 * If TPI options passed in, feed it for verification and handling
46860Sstevel@tonic-gate 	 */
46870Sstevel@tonic-gate 	if (tudr_optlen != 0) {
46880Sstevel@tonic-gate 		int error;
46890Sstevel@tonic-gate 
46900Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
46910Sstevel@tonic-gate 		    (void *)ipp) < 0) {
46920Sstevel@tonic-gate 			/* failure */
46935240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46940Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
46950Sstevel@tonic-gate 			return;
46960Sstevel@tonic-gate 		}
46970Sstevel@tonic-gate 		ignore = ipp->ipp_sticky_ignored;
46980Sstevel@tonic-gate 		ASSERT(error == 0);
46990Sstevel@tonic-gate 	}
47000Sstevel@tonic-gate 
47010Sstevel@tonic-gate 	if (sin6->sin6_scope_id != 0 &&
47020Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
47030Sstevel@tonic-gate 		/*
47040Sstevel@tonic-gate 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
47050Sstevel@tonic-gate 		 * option nor ancillary data.  It needs to be
47060Sstevel@tonic-gate 		 * explicitly set in options_exists.
47070Sstevel@tonic-gate 		 */
47080Sstevel@tonic-gate 		option_exists |= IPPF_SCOPE_ID;
47090Sstevel@tonic-gate 	}
47100Sstevel@tonic-gate 
47111676Sjpk 	/*
47121676Sjpk 	 * Compute the destination address
47131676Sjpk 	 */
47141676Sjpk 	ip6_dst = sin6->sin6_addr;
47151676Sjpk 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
47161676Sjpk 		ip6_dst = ipv6_loopback;
47171676Sjpk 
47181676Sjpk 	/*
47191676Sjpk 	 * If we're not going to the same destination as last time, then
47201676Sjpk 	 * recompute the label required.  This is done in a separate routine to
47211676Sjpk 	 * avoid blowing up our stack here.
47221676Sjpk 	 */
47231676Sjpk 	if (is_system_labeled() &&
47241676Sjpk 	    !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) &&
47251676Sjpk 	    !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) {
47261676Sjpk 		return;
47271676Sjpk 	}
47281676Sjpk 
47291676Sjpk 	/*
47301676Sjpk 	 * If there's a security label here, then we ignore any options the
47311676Sjpk 	 * user may try to set.  We keep the peer's label as a hidden sticky
47321676Sjpk 	 * option.
47331676Sjpk 	 */
47341676Sjpk 	if (icmp->icmp_label_len_v6 > 0) {
47351676Sjpk 		ignore &= ~IPPF_HOPOPTS;
47361676Sjpk 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
47371676Sjpk 	}
47381676Sjpk 
47390Sstevel@tonic-gate 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
47400Sstevel@tonic-gate 	    (ipp->ipp_fields == 0)) {
47410Sstevel@tonic-gate 		/* No sticky options nor ancillary data. */
47420Sstevel@tonic-gate 		goto no_options;
47430Sstevel@tonic-gate 	}
47440Sstevel@tonic-gate 
47450Sstevel@tonic-gate 	/*
47460Sstevel@tonic-gate 	 * Go through the options figuring out where each is going to
47470Sstevel@tonic-gate 	 * come from and build two masks.  The first mask indicates if
47480Sstevel@tonic-gate 	 * the option exists at all.  The second mask indicates if the
47490Sstevel@tonic-gate 	 * option is sticky or ancillary.
47500Sstevel@tonic-gate 	 */
47510Sstevel@tonic-gate 	if (!(ignore & IPPF_HOPOPTS)) {
47520Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
47530Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
47540Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_hopoptslen;
47550Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
47560Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
47570Sstevel@tonic-gate 			is_sticky |= IPPF_HOPOPTS;
47580Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
47590Sstevel@tonic-gate 		}
47600Sstevel@tonic-gate 	}
47610Sstevel@tonic-gate 
47620Sstevel@tonic-gate 	if (!(ignore & IPPF_RTHDR)) {
47630Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTHDR) {
47640Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
47650Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rthdrlen;
47660Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
47670Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
47680Sstevel@tonic-gate 			is_sticky |= IPPF_RTHDR;
47690Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
47700Sstevel@tonic-gate 		}
47710Sstevel@tonic-gate 	}
47720Sstevel@tonic-gate 
47730Sstevel@tonic-gate 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
47740Sstevel@tonic-gate 		/*
47750Sstevel@tonic-gate 		 * Need to have a router header to use these.
47760Sstevel@tonic-gate 		 */
47770Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
47780Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
47790Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rtdstoptslen;
47800Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
47810Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
47820Sstevel@tonic-gate 			is_sticky |= IPPF_RTDSTOPTS;
47830Sstevel@tonic-gate 			ip_hdr_len +=
47840Sstevel@tonic-gate 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
47850Sstevel@tonic-gate 		}
47860Sstevel@tonic-gate 	}
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 	if (!(ignore & IPPF_DSTOPTS)) {
47890Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
47900Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
47910Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_dstoptslen;
47920Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
47930Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
47940Sstevel@tonic-gate 			is_sticky |= IPPF_DSTOPTS;
47950Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
47960Sstevel@tonic-gate 		}
47970Sstevel@tonic-gate 	}
47980Sstevel@tonic-gate 
47990Sstevel@tonic-gate 	if (!(ignore & IPPF_IFINDEX)) {
48000Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_IFINDEX) {
48010Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
48020Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
48030Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
48040Sstevel@tonic-gate 			is_sticky |= IPPF_IFINDEX;
48050Sstevel@tonic-gate 		}
48060Sstevel@tonic-gate 	}
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate 	if (!(ignore & IPPF_ADDR)) {
48090Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_ADDR) {
48100Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
48110Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
48120Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
48130Sstevel@tonic-gate 			is_sticky |= IPPF_ADDR;
48140Sstevel@tonic-gate 		}
48150Sstevel@tonic-gate 	}
48160Sstevel@tonic-gate 
48170Sstevel@tonic-gate 	if (!(ignore & IPPF_DONTFRAG)) {
48180Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
48190Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
48200Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
48210Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
48220Sstevel@tonic-gate 			is_sticky |= IPPF_DONTFRAG;
48230Sstevel@tonic-gate 		}
48240Sstevel@tonic-gate 	}
48250Sstevel@tonic-gate 
48260Sstevel@tonic-gate 	if (!(ignore & IPPF_USE_MIN_MTU)) {
48270Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
48280Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
48290Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
48300Sstevel@tonic-gate 		    IPPF_USE_MIN_MTU) {
48310Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
48320Sstevel@tonic-gate 			is_sticky |= IPPF_USE_MIN_MTU;
48330Sstevel@tonic-gate 		}
48340Sstevel@tonic-gate 	}
48350Sstevel@tonic-gate 
48360Sstevel@tonic-gate 	if (!(ignore & IPPF_NEXTHOP)) {
48370Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
48380Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
48390Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
48400Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
48410Sstevel@tonic-gate 			is_sticky |= IPPF_NEXTHOP;
48420Sstevel@tonic-gate 		}
48430Sstevel@tonic-gate 	}
48440Sstevel@tonic-gate 
4845679Sseb 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4846679Sseb 		option_exists |= IPPF_HOPLIMIT;
4847679Sseb 	/* IPV6_HOPLIMIT can never be sticky */
4848679Sseb 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4849679Sseb 
4850679Sseb 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4851679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4852679Sseb 		option_exists |= IPPF_UNICAST_HOPS;
4853679Sseb 		is_sticky |= IPPF_UNICAST_HOPS;
4854679Sseb 	}
4855679Sseb 
4856679Sseb 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4857679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4858679Sseb 		option_exists |= IPPF_MULTICAST_HOPS;
4859679Sseb 		is_sticky |= IPPF_MULTICAST_HOPS;
48600Sstevel@tonic-gate 	}
48610Sstevel@tonic-gate 
48620Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
48630Sstevel@tonic-gate 		/* This is a sticky socket option only */
48640Sstevel@tonic-gate 		option_exists |= IPPF_NO_CKSUM;
48650Sstevel@tonic-gate 		is_sticky |= IPPF_NO_CKSUM;
48660Sstevel@tonic-gate 	}
48670Sstevel@tonic-gate 
48680Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
48690Sstevel@tonic-gate 		/* This is a sticky socket option only */
48700Sstevel@tonic-gate 		option_exists |= IPPF_RAW_CKSUM;
48710Sstevel@tonic-gate 		is_sticky |= IPPF_RAW_CKSUM;
48720Sstevel@tonic-gate 	}
48730Sstevel@tonic-gate 
48740Sstevel@tonic-gate 	if (!(ignore & IPPF_TCLASS)) {
48750Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_TCLASS) {
48760Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
48770Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
48780Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
48790Sstevel@tonic-gate 			is_sticky |= IPPF_TCLASS;
48800Sstevel@tonic-gate 		}
48810Sstevel@tonic-gate 	}
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate no_options:
48840Sstevel@tonic-gate 
48850Sstevel@tonic-gate 	/*
48860Sstevel@tonic-gate 	 * If any options carried in the ip6i_t were specified, we
48870Sstevel@tonic-gate 	 * need to account for the ip6i_t in the data we'll be sending
48880Sstevel@tonic-gate 	 * down.
48890Sstevel@tonic-gate 	 */
48900Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I)
48910Sstevel@tonic-gate 		ip_hdr_len += sizeof (ip6i_t);
48920Sstevel@tonic-gate 
48930Sstevel@tonic-gate 	/* check/fix buffer config, setup pointers into it */
48940Sstevel@tonic-gate 	mp1 = mp->b_cont;
48950Sstevel@tonic-gate 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
48960Sstevel@tonic-gate 	if ((mp1->b_datap->db_ref != 1) ||
48970Sstevel@tonic-gate 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
48980Sstevel@tonic-gate 	    !OK_32PTR(ip6h)) {
48990Sstevel@tonic-gate 		/* Try to get everything in a single mblk next time */
49000Sstevel@tonic-gate 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
49010Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = ip_hdr_len;
49020Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q),
49033448Sdh155122 			    icmp->icmp_max_hdr_len + is->is_wroff_extra);
49040Sstevel@tonic-gate 		}
49053448Sdh155122 		mp1 = allocb(ip_hdr_len + is->is_wroff_extra, BPRI_LO);
49060Sstevel@tonic-gate 		if (!mp1) {
49075240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
49080Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
49090Sstevel@tonic-gate 			return;
49100Sstevel@tonic-gate 		}
49110Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
49120Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_datap->db_lim;
49130Sstevel@tonic-gate 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
49140Sstevel@tonic-gate 	}
49150Sstevel@tonic-gate 	mp1->b_rptr = (unsigned char *)ip6h;
49160Sstevel@tonic-gate 	ip6i = (ip6i_t *)ip6h;
49170Sstevel@tonic-gate 
49180Sstevel@tonic-gate #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
49190Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I) {
49200Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
49210Sstevel@tonic-gate 		ip6i->ip6i_flags = 0;
49220Sstevel@tonic-gate 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
49230Sstevel@tonic-gate 
49240Sstevel@tonic-gate 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
49250Sstevel@tonic-gate 		if (option_exists & IPPF_SCOPE_ID) {
49260Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
49270Sstevel@tonic-gate 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
49280Sstevel@tonic-gate 		} else if (option_exists & IPPF_IFINDEX) {
49290Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
49300Sstevel@tonic-gate 			ASSERT(tipp->ipp_ifindex != 0);
49310Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
49320Sstevel@tonic-gate 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
49330Sstevel@tonic-gate 		}
49340Sstevel@tonic-gate 
49350Sstevel@tonic-gate 		if (option_exists & IPPF_RAW_CKSUM) {
49360Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
49370Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
49380Sstevel@tonic-gate 		}
49390Sstevel@tonic-gate 
49400Sstevel@tonic-gate 		if (option_exists & IPPF_NO_CKSUM) {
49410Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
49420Sstevel@tonic-gate 		}
49430Sstevel@tonic-gate 
49440Sstevel@tonic-gate 		if (option_exists & IPPF_ADDR) {
49450Sstevel@tonic-gate 			/*
49460Sstevel@tonic-gate 			 * Enable per-packet source address verification if
49470Sstevel@tonic-gate 			 * IPV6_PKTINFO specified the source address.
49480Sstevel@tonic-gate 			 * ip6_src is set in the transport's _wput function.
49490Sstevel@tonic-gate 			 */
49500Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
49510Sstevel@tonic-gate 		}
49520Sstevel@tonic-gate 
49530Sstevel@tonic-gate 		if (option_exists & IPPF_DONTFRAG) {
49540Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
49550Sstevel@tonic-gate 		}
49560Sstevel@tonic-gate 
49570Sstevel@tonic-gate 		if (option_exists & IPPF_USE_MIN_MTU) {
49580Sstevel@tonic-gate 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
49590Sstevel@tonic-gate 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
49600Sstevel@tonic-gate 		}
49610Sstevel@tonic-gate 
49620Sstevel@tonic-gate 		if (option_exists & IPPF_NEXTHOP) {
49630Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
49640Sstevel@tonic-gate 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
49650Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
49660Sstevel@tonic-gate 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
49670Sstevel@tonic-gate 		}
49680Sstevel@tonic-gate 
49690Sstevel@tonic-gate 		/*
49700Sstevel@tonic-gate 		 * tell IP this is an ip6i_t private header
49710Sstevel@tonic-gate 		 */
49720Sstevel@tonic-gate 		ip6i->ip6i_nxt = IPPROTO_RAW;
49730Sstevel@tonic-gate 	}
49740Sstevel@tonic-gate 
49750Sstevel@tonic-gate 	/* Initialize IPv6 header */
49760Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
49770Sstevel@tonic-gate 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
49780Sstevel@tonic-gate 
4979679Sseb 	/* Set the hoplimit of the outgoing packet. */
49800Sstevel@tonic-gate 	if (option_exists & IPPF_HOPLIMIT) {
4981679Sseb 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4982679Sseb 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4983679Sseb 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4984679Sseb 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
49850Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4986679Sseb 		if (option_exists & IPPF_MULTICAST_HOPS)
4987679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
49880Sstevel@tonic-gate 	} else {
49890Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_ttl;
4990679Sseb 		if (option_exists & IPPF_UNICAST_HOPS)
4991679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
49920Sstevel@tonic-gate 	}
49930Sstevel@tonic-gate 
49940Sstevel@tonic-gate 	if (option_exists & IPPF_ADDR) {
49950Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
49960Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
49970Sstevel@tonic-gate 		ip6h->ip6_src = tipp->ipp_addr;
49980Sstevel@tonic-gate 	} else {
49990Sstevel@tonic-gate 		/*
50000Sstevel@tonic-gate 		 * The source address was not set using IPV6_PKTINFO.
50010Sstevel@tonic-gate 		 * First look at the bound source.
50020Sstevel@tonic-gate 		 * If unspecified fallback to __sin6_src_id.
50030Sstevel@tonic-gate 		 */
50040Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
50050Sstevel@tonic-gate 		if (sin6->__sin6_src_id != 0 &&
50060Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
50070Sstevel@tonic-gate 			ip_srcid_find_id(sin6->__sin6_src_id,
50083448Sdh155122 			    &ip6h->ip6_src, icmp->icmp_zoneid,
50093448Sdh155122 			    is->is_netstack);
50100Sstevel@tonic-gate 		}
50110Sstevel@tonic-gate 	}
50120Sstevel@tonic-gate 
50130Sstevel@tonic-gate 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
50140Sstevel@tonic-gate 	cp = (uint8_t *)&ip6h[1];
50150Sstevel@tonic-gate 
50160Sstevel@tonic-gate 	/*
50170Sstevel@tonic-gate 	 * Here's where we have to start stringing together
50180Sstevel@tonic-gate 	 * any extension headers in the right order:
50190Sstevel@tonic-gate 	 * Hop-by-hop, destination, routing, and final destination opts.
50200Sstevel@tonic-gate 	 */
50210Sstevel@tonic-gate 	if (option_exists & IPPF_HOPOPTS) {
50220Sstevel@tonic-gate 		/* Hop-by-hop options */
50230Sstevel@tonic-gate 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
50240Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
50250Sstevel@tonic-gate 
50260Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_HOPOPTS;
50270Sstevel@tonic-gate 		nxthdr_ptr = &hbh->ip6h_nxt;
50280Sstevel@tonic-gate 
50290Sstevel@tonic-gate 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
50300Sstevel@tonic-gate 		cp += tipp->ipp_hopoptslen;
50310Sstevel@tonic-gate 	}
50320Sstevel@tonic-gate 	/*
50330Sstevel@tonic-gate 	 * En-route destination options
50340Sstevel@tonic-gate 	 * Only do them if there's a routing header as well
50350Sstevel@tonic-gate 	 */
50360Sstevel@tonic-gate 	if (option_exists & IPPF_RTDSTOPTS) {
50370Sstevel@tonic-gate 		ip6_dest_t *dst = (ip6_dest_t *)cp;
50380Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
50390Sstevel@tonic-gate 
50400Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
50410Sstevel@tonic-gate 		nxthdr_ptr = &dst->ip6d_nxt;
50420Sstevel@tonic-gate 
50430Sstevel@tonic-gate 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
50440Sstevel@tonic-gate 		cp += tipp->ipp_rtdstoptslen;
50450Sstevel@tonic-gate 	}
50460Sstevel@tonic-gate 	/*
50470Sstevel@tonic-gate 	 * Routing header next
50480Sstevel@tonic-gate 	 */
50490Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
50500Sstevel@tonic-gate 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
50510Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
50520Sstevel@tonic-gate 
50530Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_ROUTING;
50540Sstevel@tonic-gate 		nxthdr_ptr = &rt->ip6r_nxt;
50550Sstevel@tonic-gate 
50560Sstevel@tonic-gate 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
50570Sstevel@tonic-gate 		cp += tipp->ipp_rthdrlen;
50580Sstevel@tonic-gate 	}
50590Sstevel@tonic-gate 	/*
50600Sstevel@tonic-gate 	 * Do ultimate destination options
50610Sstevel@tonic-gate 	 */
50620Sstevel@tonic-gate 	if (option_exists & IPPF_DSTOPTS) {
50630Sstevel@tonic-gate 		ip6_dest_t *dest = (ip6_dest_t *)cp;
50640Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
50670Sstevel@tonic-gate 		nxthdr_ptr = &dest->ip6d_nxt;
50680Sstevel@tonic-gate 
50690Sstevel@tonic-gate 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
50700Sstevel@tonic-gate 		cp += tipp->ipp_dstoptslen;
50710Sstevel@tonic-gate 	}
50720Sstevel@tonic-gate 
50730Sstevel@tonic-gate 	/*
50740Sstevel@tonic-gate 	 * Now set the last header pointer to the proto passed in
50750Sstevel@tonic-gate 	 */
50760Sstevel@tonic-gate 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
50770Sstevel@tonic-gate 	*nxthdr_ptr = icmp->icmp_proto;
50780Sstevel@tonic-gate 
50790Sstevel@tonic-gate 	/*
50800Sstevel@tonic-gate 	 * Copy in the destination address
50810Sstevel@tonic-gate 	 */
50821676Sjpk 	ip6h->ip6_dst = ip6_dst;
50830Sstevel@tonic-gate 
50840Sstevel@tonic-gate 	ip6h->ip6_vcf =
50855240Snordmark 	    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
50865240Snordmark 	    (sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
50870Sstevel@tonic-gate 
50880Sstevel@tonic-gate 	if (option_exists & IPPF_TCLASS) {
50890Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
50900Sstevel@tonic-gate 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
50910Sstevel@tonic-gate 		    tipp->ipp_tclass);
50920Sstevel@tonic-gate 	}
50930Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
50940Sstevel@tonic-gate 		ip6_rthdr_t	*rth;
50950Sstevel@tonic-gate 
50960Sstevel@tonic-gate 		/*
50970Sstevel@tonic-gate 		 * Perform any processing needed for source routing.
50980Sstevel@tonic-gate 		 * We know that all extension headers will be in the same mblk
50990Sstevel@tonic-gate 		 * as the IPv6 header.
51000Sstevel@tonic-gate 		 */
51010Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
51020Sstevel@tonic-gate 		if (rth != NULL && rth->ip6r_segleft != 0) {
51030Sstevel@tonic-gate 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
51040Sstevel@tonic-gate 				/*
51050Sstevel@tonic-gate 				 * Drop packet - only support Type 0 routing.
51060Sstevel@tonic-gate 				 * Notify the application as well.
51070Sstevel@tonic-gate 				 */
51080Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
51095240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51103448Sdh155122 				    rawipOutErrors);
51110Sstevel@tonic-gate 				return;
51120Sstevel@tonic-gate 			}
51130Sstevel@tonic-gate 			/*
51140Sstevel@tonic-gate 			 * rth->ip6r_len is twice the number of
51150Sstevel@tonic-gate 			 * addresses in the header
51160Sstevel@tonic-gate 			 */
51170Sstevel@tonic-gate 			if (rth->ip6r_len & 0x1) {
51180Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
51195240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51203448Sdh155122 				    rawipOutErrors);
51210Sstevel@tonic-gate 				return;
51220Sstevel@tonic-gate 			}
51230Sstevel@tonic-gate 			/*
51240Sstevel@tonic-gate 			 * Shuffle the routing header and ip6_dst
51250Sstevel@tonic-gate 			 * addresses, and get the checksum difference
51260Sstevel@tonic-gate 			 * between the first hop (in ip6_dst) and
51270Sstevel@tonic-gate 			 * the destination (in the last routing hdr entry).
51280Sstevel@tonic-gate 			 */
51293448Sdh155122 			csum = ip_massage_options_v6(ip6h, rth,
51305240Snordmark 			    is->is_netstack);
51310Sstevel@tonic-gate 			/*
51320Sstevel@tonic-gate 			 * Verify that the first hop isn't a mapped address.
51330Sstevel@tonic-gate 			 * Routers along the path need to do this verification
51340Sstevel@tonic-gate 			 * for subsequent hops.
51350Sstevel@tonic-gate 			 */
51360Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
51370Sstevel@tonic-gate 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
51385240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51393448Sdh155122 				    rawipOutErrors);
51400Sstevel@tonic-gate 				return;
51410Sstevel@tonic-gate 			}
51420Sstevel@tonic-gate 		}
51430Sstevel@tonic-gate 	}
51440Sstevel@tonic-gate 
51450Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
51460Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
51470Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
51480Sstevel@tonic-gate 
51490Sstevel@tonic-gate 	/*
51500Sstevel@tonic-gate 	 * Set the length into the IP header.
51510Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
51520Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
51530Sstevel@tonic-gate 	 * as this can cause problems in layers below.
51540Sstevel@tonic-gate 	 */
51550Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
51565240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
51571289Sja97890 		icmp_ud_err(q, mp, EMSGSIZE);
51580Sstevel@tonic-gate 		return;
51590Sstevel@tonic-gate 	}
51600Sstevel@tonic-gate 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
51610Sstevel@tonic-gate 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
51620Sstevel@tonic-gate 		uint16_t *cksum_ptr;
51630Sstevel@tonic-gate 		uint_t	ext_hdrs_len;
51640Sstevel@tonic-gate 
51650Sstevel@tonic-gate 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
51660Sstevel@tonic-gate 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
51670Sstevel@tonic-gate 		    icmp->icmp_checksum_off == 2);
51680Sstevel@tonic-gate 
51690Sstevel@tonic-gate 		/*
51700Sstevel@tonic-gate 		 * We make it easy for IP to include our pseudo header
51710Sstevel@tonic-gate 		 * by putting our length in uh_checksum, modified (if
51720Sstevel@tonic-gate 		 * we have a routing header) by the checksum difference
51730Sstevel@tonic-gate 		 * between the ultimate destination and first hop addresses.
51740Sstevel@tonic-gate 		 * Note: ICMPv6 must always checksum the packet.
51750Sstevel@tonic-gate 		 */
51760Sstevel@tonic-gate 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
51770Sstevel@tonic-gate 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
51780Sstevel@tonic-gate 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
51795240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51803448Sdh155122 				    rawipOutErrors);
51810Sstevel@tonic-gate 				freemsg(mp);
51820Sstevel@tonic-gate 				return;
51830Sstevel@tonic-gate 			}
51840Sstevel@tonic-gate 			ip6i = (ip6i_t *)mp1->b_rptr;
51850Sstevel@tonic-gate 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
51860Sstevel@tonic-gate 				ip6h = (ip6_t *)&ip6i[1];
51870Sstevel@tonic-gate 			else
51880Sstevel@tonic-gate 				ip6h = (ip6_t *)ip6i;
51890Sstevel@tonic-gate 		}
51900Sstevel@tonic-gate 		/* Add payload length to checksum */
51910Sstevel@tonic-gate 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
51920Sstevel@tonic-gate 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
51930Sstevel@tonic-gate 		csum += htons(ip_len - ext_hdrs_len);
51940Sstevel@tonic-gate 
51950Sstevel@tonic-gate 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
51960Sstevel@tonic-gate 		csum = (csum & 0xFFFF) + (csum >> 16);
51970Sstevel@tonic-gate 		*cksum_ptr = (uint16_t)csum;
51980Sstevel@tonic-gate 	}
51990Sstevel@tonic-gate 
52000Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
52010Sstevel@tonic-gate 	ip_len = htons(ip_len);
52020Sstevel@tonic-gate #endif
52030Sstevel@tonic-gate 	ip6h->ip6_plen = (uint16_t)ip_len;
52040Sstevel@tonic-gate 
52050Sstevel@tonic-gate 	freeb(mp);
52060Sstevel@tonic-gate 
52070Sstevel@tonic-gate 	/* We're done. Pass the packet to IP */
52085240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
52095240Snordmark 	ip_output_v6(icmp->icmp_connp, mp1, q, IP_WPUT);
52100Sstevel@tonic-gate }
52110Sstevel@tonic-gate 
52120Sstevel@tonic-gate static void
52130Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp)
52140Sstevel@tonic-gate {
52150Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
52160Sstevel@tonic-gate 	struct iocblk *iocp;
52170Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
52185240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
52195240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
52205240Snordmark 	icmp_stack_t *is = icmp->icmp_is;
52210Sstevel@tonic-gate 	cred_t *cr;
52220Sstevel@tonic-gate 
52235240Snordmark 	cr = DB_CREDDEF(mp, connp->conn_cred);
52240Sstevel@tonic-gate 
52250Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
52260Sstevel@tonic-gate 	case M_PROTO:
52270Sstevel@tonic-gate 	case M_PCPROTO:
52280Sstevel@tonic-gate 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
52290Sstevel@tonic-gate 			/*
52300Sstevel@tonic-gate 			 * If the message does not contain a PRIM_type,
52310Sstevel@tonic-gate 			 * throw it away.
52320Sstevel@tonic-gate 			 */
52330Sstevel@tonic-gate 			freemsg(mp);
52340Sstevel@tonic-gate 			return;
52350Sstevel@tonic-gate 		}
52360Sstevel@tonic-gate 		switch (((union T_primitives *)rptr)->type) {
52370Sstevel@tonic-gate 		case T_ADDR_REQ:
52380Sstevel@tonic-gate 			icmp_addr_req(q, mp);
52390Sstevel@tonic-gate 			return;
52400Sstevel@tonic-gate 		case O_T_BIND_REQ:
52410Sstevel@tonic-gate 		case T_BIND_REQ:
52425240Snordmark 			icmp_bind(q, mp);
52430Sstevel@tonic-gate 			return;
52440Sstevel@tonic-gate 		case T_CONN_REQ:
52450Sstevel@tonic-gate 			icmp_connect(q, mp);
52460Sstevel@tonic-gate 			return;
52470Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
52480Sstevel@tonic-gate 			icmp_capability_req(q, mp);
52490Sstevel@tonic-gate 			return;
52500Sstevel@tonic-gate 		case T_INFO_REQ:
52510Sstevel@tonic-gate 			icmp_info_req(q, mp);
52520Sstevel@tonic-gate 			return;
52530Sstevel@tonic-gate 		case T_UNITDATA_REQ:
52540Sstevel@tonic-gate 			/*
52550Sstevel@tonic-gate 			 * If a T_UNITDATA_REQ gets here, the address must
52560Sstevel@tonic-gate 			 * be bad.  Valid T_UNITDATA_REQs are found above
52570Sstevel@tonic-gate 			 * and break to below this switch.
52580Sstevel@tonic-gate 			 */
52590Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
52600Sstevel@tonic-gate 			return;
52610Sstevel@tonic-gate 		case T_UNBIND_REQ:
52620Sstevel@tonic-gate 			icmp_unbind(q, mp);
52630Sstevel@tonic-gate 			return;
52640Sstevel@tonic-gate 
52650Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
52665240Snordmark 			if (!snmpcom_req(q, mp, icmp_snmp_set, ip_snmp_get,
52675240Snordmark 			    cr)) {
52680Sstevel@tonic-gate 				/* Only IP can return anything meaningful */
52690Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
52705240Snordmark 				    &icmp_opt_obj, B_TRUE);
52715240Snordmark 			}
52720Sstevel@tonic-gate 			return;
52730Sstevel@tonic-gate 
52740Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
52750Sstevel@tonic-gate 			/* Only IP can return anything meaningful */
52765240Snordmark 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj, B_TRUE);
52770Sstevel@tonic-gate 			return;
52780Sstevel@tonic-gate 
52790Sstevel@tonic-gate 		case T_DISCON_REQ:
52800Sstevel@tonic-gate 			icmp_disconnect(q, mp);
52810Sstevel@tonic-gate 			return;
52820Sstevel@tonic-gate 
52830Sstevel@tonic-gate 		/* The following TPI message is not supported by icmp. */
52840Sstevel@tonic-gate 		case O_T_CONN_RES:
52850Sstevel@tonic-gate 		case T_CONN_RES:
52860Sstevel@tonic-gate 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
52870Sstevel@tonic-gate 			return;
52880Sstevel@tonic-gate 
52890Sstevel@tonic-gate 		/* The following 3 TPI requests are illegal for icmp. */
52900Sstevel@tonic-gate 		case T_DATA_REQ:
52910Sstevel@tonic-gate 		case T_EXDATA_REQ:
52920Sstevel@tonic-gate 		case T_ORDREL_REQ:
52930Sstevel@tonic-gate 			freemsg(mp);
52940Sstevel@tonic-gate 			(void) putctl1(RD(q), M_ERROR, EPROTO);
52950Sstevel@tonic-gate 			return;
52960Sstevel@tonic-gate 		default:
52970Sstevel@tonic-gate 			break;
52980Sstevel@tonic-gate 		}
52990Sstevel@tonic-gate 		break;
53000Sstevel@tonic-gate 	case M_IOCTL:
53010Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
53020Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
53030Sstevel@tonic-gate 		case TI_GETPEERNAME:
53040Sstevel@tonic-gate 			if (icmp->icmp_state != TS_DATA_XFER) {
53050Sstevel@tonic-gate 				/*
53060Sstevel@tonic-gate 				 * If a default destination address has not
53070Sstevel@tonic-gate 				 * been associated with the stream, then we
53080Sstevel@tonic-gate 				 * don't know the peer's name.
53090Sstevel@tonic-gate 				 */
53100Sstevel@tonic-gate 				iocp->ioc_error = ENOTCONN;
53115240Snordmark 		err_ret:;
53120Sstevel@tonic-gate 				iocp->ioc_count = 0;
53130Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
53140Sstevel@tonic-gate 				qreply(q, mp);
53150Sstevel@tonic-gate 				return;
53160Sstevel@tonic-gate 			}
53170Sstevel@tonic-gate 			/* FALLTHRU */
53180Sstevel@tonic-gate 		case TI_GETMYNAME:
53190Sstevel@tonic-gate 			/*
53200Sstevel@tonic-gate 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
53210Sstevel@tonic-gate 			 * need to copyin the user's strbuf structure.
53220Sstevel@tonic-gate 			 * Processing will continue in the M_IOCDATA case
53230Sstevel@tonic-gate 			 * below.
53240Sstevel@tonic-gate 			 */
53250Sstevel@tonic-gate 			mi_copyin(q, mp, NULL,
53260Sstevel@tonic-gate 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
53270Sstevel@tonic-gate 			return;
53280Sstevel@tonic-gate 		case ND_SET:
53290Sstevel@tonic-gate 			/* nd_getset performs the necessary error checking */
53300Sstevel@tonic-gate 		case ND_GET:
53315240Snordmark 			if (nd_getset(q, is->is_nd, mp)) {
53320Sstevel@tonic-gate 				qreply(q, mp);
53330Sstevel@tonic-gate 				return;
53340Sstevel@tonic-gate 			}
53350Sstevel@tonic-gate 			break;
53360Sstevel@tonic-gate 		default:
53370Sstevel@tonic-gate 			break;
53380Sstevel@tonic-gate 		}
53390Sstevel@tonic-gate 		break;
53400Sstevel@tonic-gate 	case M_IOCDATA:
53410Sstevel@tonic-gate 		icmp_wput_iocdata(q, mp);
53420Sstevel@tonic-gate 		return;
53430Sstevel@tonic-gate 	default:
53440Sstevel@tonic-gate 		break;
53450Sstevel@tonic-gate 	}
53465240Snordmark 	ip_wput(q, mp);
53470Sstevel@tonic-gate }
53480Sstevel@tonic-gate 
53490Sstevel@tonic-gate /*
53500Sstevel@tonic-gate  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
53510Sstevel@tonic-gate  * messages.
53520Sstevel@tonic-gate  */
53530Sstevel@tonic-gate static void
53540Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp)
53550Sstevel@tonic-gate {
53560Sstevel@tonic-gate 	mblk_t	*mp1;
53570Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
53580Sstevel@tonic-gate 	icmp_t	*icmp;
53590Sstevel@tonic-gate 	in6_addr_t	v6addr;
53600Sstevel@tonic-gate 	ipaddr_t	v4addr;
53610Sstevel@tonic-gate 	uint32_t	flowinfo = 0;
53620Sstevel@tonic-gate 	int		addrlen;
53630Sstevel@tonic-gate 
53640Sstevel@tonic-gate 	/* Make sure it is one of ours. */
53650Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
53660Sstevel@tonic-gate 	case TI_GETMYNAME:
53670Sstevel@tonic-gate 	case TI_GETPEERNAME:
53680Sstevel@tonic-gate 		break;
53690Sstevel@tonic-gate 	default:
53705240Snordmark 		icmp = Q_TO_ICMP(q);
53715240Snordmark 		ip_output(icmp->icmp_connp, mp, q, IP_WPUT);
53720Sstevel@tonic-gate 		return;
53730Sstevel@tonic-gate 	}
53740Sstevel@tonic-gate 	switch (mi_copy_state(q, mp, &mp1)) {
53750Sstevel@tonic-gate 	case -1:
53760Sstevel@tonic-gate 		return;
53770Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_IN, 1):
53780Sstevel@tonic-gate 		break;
53790Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 1):
53800Sstevel@tonic-gate 		/*
53810Sstevel@tonic-gate 		 * The address has been copied out, so now
53820Sstevel@tonic-gate 		 * copyout the strbuf.
53830Sstevel@tonic-gate 		 */
53840Sstevel@tonic-gate 		mi_copyout(q, mp);
53850Sstevel@tonic-gate 		return;
53860Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 2):
53870Sstevel@tonic-gate 		/*
53880Sstevel@tonic-gate 		 * The address and strbuf have been copied out.
53890Sstevel@tonic-gate 		 * We're done, so just acknowledge the original
53900Sstevel@tonic-gate 		 * M_IOCTL.
53910Sstevel@tonic-gate 		 */
53920Sstevel@tonic-gate 		mi_copy_done(q, mp, 0);
53930Sstevel@tonic-gate 		return;
53940Sstevel@tonic-gate 	default:
53950Sstevel@tonic-gate 		/*
53960Sstevel@tonic-gate 		 * Something strange has happened, so acknowledge
53970Sstevel@tonic-gate 		 * the original M_IOCTL with an EPROTO error.
53980Sstevel@tonic-gate 		 */
53990Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
54000Sstevel@tonic-gate 		return;
54010Sstevel@tonic-gate 	}
54020Sstevel@tonic-gate 	/*
54030Sstevel@tonic-gate 	 * Now we have the strbuf structure for TI_GETMYNAME
54040Sstevel@tonic-gate 	 * and TI_GETPEERNAME.  Next we copyout the requested
54050Sstevel@tonic-gate 	 * address and then we'll copyout the strbuf.
54060Sstevel@tonic-gate 	 */
54070Sstevel@tonic-gate 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
54080Sstevel@tonic-gate 	    (void *)mp1->b_rptr);
54095240Snordmark 	icmp = Q_TO_ICMP(q);
54100Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET)
54110Sstevel@tonic-gate 		addrlen = sizeof (sin_t);
54120Sstevel@tonic-gate 	else
54130Sstevel@tonic-gate 		addrlen = sizeof (sin6_t);
54140Sstevel@tonic-gate 
54150Sstevel@tonic-gate 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
54160Sstevel@tonic-gate 		mi_copy_done(q, mp, EINVAL);
54170Sstevel@tonic-gate 		return;
54180Sstevel@tonic-gate 	}
54190Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
54200Sstevel@tonic-gate 	case TI_GETMYNAME:
54210Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
54220Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
54230Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
54240Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
54250Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
54260Sstevel@tonic-gate 			} else {
54270Sstevel@tonic-gate 				/*
54280Sstevel@tonic-gate 				 * INADDR_ANY
54290Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
54300Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
54310Sstevel@tonic-gate 				 * local address instead (that could
54320Sstevel@tonic-gate 				 * also still be INADDR_ANY)
54330Sstevel@tonic-gate 				 */
54340Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
54350Sstevel@tonic-gate 			}
54360Sstevel@tonic-gate 		} else {
54370Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6 */
54380Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
54390Sstevel@tonic-gate 				v6addr = icmp->icmp_v6src;
54400Sstevel@tonic-gate 			} else {
54410Sstevel@tonic-gate 				/*
54420Sstevel@tonic-gate 				 * UNSPECIFIED
54430Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
54440Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
54450Sstevel@tonic-gate 				 * local address instead (that could
54460Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
54470Sstevel@tonic-gate 				 */
54480Sstevel@tonic-gate 				v6addr = icmp->icmp_bound_v6src;
54490Sstevel@tonic-gate 			}
54500Sstevel@tonic-gate 		}
54510Sstevel@tonic-gate 		break;
54520Sstevel@tonic-gate 	case TI_GETPEERNAME:
54530Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
54540Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
54550Sstevel@tonic-gate 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
54560Sstevel@tonic-gate 		} else {
54570Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6) */
54580Sstevel@tonic-gate 			v6addr = icmp->icmp_v6dst;
54590Sstevel@tonic-gate 			flowinfo = icmp->icmp_flowinfo;
54600Sstevel@tonic-gate 		}
54610Sstevel@tonic-gate 		break;
54620Sstevel@tonic-gate 	default:
54630Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
54640Sstevel@tonic-gate 		return;
54650Sstevel@tonic-gate 	}
54660Sstevel@tonic-gate 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
54670Sstevel@tonic-gate 	if (!mp1)
54680Sstevel@tonic-gate 		return;
54690Sstevel@tonic-gate 
54700Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
54710Sstevel@tonic-gate 		sin_t *sin;
54720Sstevel@tonic-gate 
54730Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
54740Sstevel@tonic-gate 		sin = (sin_t *)mp1->b_rptr;
54750Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin[1];
54760Sstevel@tonic-gate 		*sin = sin_null;
54770Sstevel@tonic-gate 		sin->sin_family = AF_INET;
54780Sstevel@tonic-gate 		sin->sin_addr.s_addr = v4addr;
54790Sstevel@tonic-gate 	} else {
54800Sstevel@tonic-gate 		/* icmp->icmp_family == AF_INET6 */
54810Sstevel@tonic-gate 		sin6_t *sin6;
54820Sstevel@tonic-gate 
54830Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
54840Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
54850Sstevel@tonic-gate 		sin6 = (sin6_t *)mp1->b_rptr;
54860Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin6[1];
54870Sstevel@tonic-gate 		*sin6 = sin6_null;
54880Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
54890Sstevel@tonic-gate 		sin6->sin6_flowinfo = flowinfo;
54900Sstevel@tonic-gate 		sin6->sin6_addr = v6addr;
54910Sstevel@tonic-gate 	}
54920Sstevel@tonic-gate 	/* Copy out the address */
54930Sstevel@tonic-gate 	mi_copyout(q, mp);
54940Sstevel@tonic-gate }
54950Sstevel@tonic-gate 
54960Sstevel@tonic-gate static int
54970Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
54980Sstevel@tonic-gate     void *thisdg_attrs)
54990Sstevel@tonic-gate {
55005240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
55010Sstevel@tonic-gate 	struct T_unitdata_req *udreqp;
55020Sstevel@tonic-gate 	int is_absreq_failure;
55030Sstevel@tonic-gate 	cred_t *cr;
55040Sstevel@tonic-gate 
55050Sstevel@tonic-gate 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
55060Sstevel@tonic-gate 	*errorp = 0;
55070Sstevel@tonic-gate 
55085240Snordmark 	cr = DB_CREDDEF(mp, connp->conn_cred);
55090Sstevel@tonic-gate 
55100Sstevel@tonic-gate 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
55110Sstevel@tonic-gate 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
55120Sstevel@tonic-gate 	    thisdg_attrs, &is_absreq_failure);
55130Sstevel@tonic-gate 
55140Sstevel@tonic-gate 	if (*errorp != 0) {
55150Sstevel@tonic-gate 		/*
55160Sstevel@tonic-gate 		 * Note: No special action needed in this
55170Sstevel@tonic-gate 		 * module for "is_absreq_failure"
55180Sstevel@tonic-gate 		 */
55190Sstevel@tonic-gate 		return (-1);		/* failure */
55200Sstevel@tonic-gate 	}
55210Sstevel@tonic-gate 	ASSERT(is_absreq_failure == 0);
55220Sstevel@tonic-gate 	return (0);	/* success */
55230Sstevel@tonic-gate }
55240Sstevel@tonic-gate 
55250Sstevel@tonic-gate void
55260Sstevel@tonic-gate icmp_ddi_init(void)
55270Sstevel@tonic-gate {
55280Sstevel@tonic-gate 	icmp_max_optsize =
55290Sstevel@tonic-gate 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
55305240Snordmark 	    icmp_opt_obj.odb_opt_arr_cnt);
55310Sstevel@tonic-gate 
55323448Sdh155122 	/*
55333448Sdh155122 	 * We want to be informed each time a stack is created or
55343448Sdh155122 	 * destroyed in the kernel, so we can maintain the
55353448Sdh155122 	 * set of icmp_stack_t's.
55363448Sdh155122 	 */
55373448Sdh155122 	netstack_register(NS_ICMP, rawip_stack_init, NULL, rawip_stack_fini);
55380Sstevel@tonic-gate }
55390Sstevel@tonic-gate 
55400Sstevel@tonic-gate void
55410Sstevel@tonic-gate icmp_ddi_destroy(void)
55420Sstevel@tonic-gate {
55433448Sdh155122 	netstack_unregister(NS_ICMP);
55440Sstevel@tonic-gate }
55450Sstevel@tonic-gate 
55463448Sdh155122 /*
55473448Sdh155122  * Initialize the ICMP stack instance.
55483448Sdh155122  */
55493448Sdh155122 static void *
55503448Sdh155122 rawip_stack_init(netstackid_t stackid, netstack_t *ns)
55513448Sdh155122 {
55523448Sdh155122 	icmp_stack_t	*is;
55533448Sdh155122 	icmpparam_t	*pa;
55543448Sdh155122 
55553448Sdh155122 	is = (icmp_stack_t *)kmem_zalloc(sizeof (*is), KM_SLEEP);
55563448Sdh155122 	is->is_netstack = ns;
55573448Sdh155122 
55583448Sdh155122 	pa = (icmpparam_t *)kmem_alloc(sizeof (icmp_param_arr), KM_SLEEP);
55593448Sdh155122 	is->is_param_arr = pa;
55603448Sdh155122 	bcopy(icmp_param_arr, is->is_param_arr, sizeof (icmp_param_arr));
55613448Sdh155122 
55623448Sdh155122 	(void) icmp_param_register(&is->is_nd,
55633448Sdh155122 	    is->is_param_arr, A_CNT(icmp_param_arr));
55643448Sdh155122 	is->is_ksp = rawip_kstat_init(stackid);
55653448Sdh155122 	return (is);
55663448Sdh155122 }
55673448Sdh155122 
55683448Sdh155122 /*
55693448Sdh155122  * Free the ICMP stack instance.
55703448Sdh155122  */
55710Sstevel@tonic-gate static void
55723448Sdh155122 rawip_stack_fini(netstackid_t stackid, void *arg)
55733448Sdh155122 {
55743448Sdh155122 	icmp_stack_t *is = (icmp_stack_t *)arg;
55753448Sdh155122 
55763448Sdh155122 	nd_free(&is->is_nd);
55773448Sdh155122 	kmem_free(is->is_param_arr, sizeof (icmp_param_arr));
55783448Sdh155122 	is->is_param_arr = NULL;
55793448Sdh155122 
55803448Sdh155122 	rawip_kstat_fini(stackid, is->is_ksp);
55813448Sdh155122 	is->is_ksp = NULL;
55823448Sdh155122 	kmem_free(is, sizeof (*is));
55833448Sdh155122 }
55843448Sdh155122 
55853448Sdh155122 static void *
55863448Sdh155122 rawip_kstat_init(netstackid_t stackid) {
55873448Sdh155122 	kstat_t	*ksp;
55880Sstevel@tonic-gate 
55890Sstevel@tonic-gate 	rawip_named_kstat_t template = {
55900Sstevel@tonic-gate 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
55910Sstevel@tonic-gate 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
55920Sstevel@tonic-gate 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
55930Sstevel@tonic-gate 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
55940Sstevel@tonic-gate 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
55950Sstevel@tonic-gate 	};
55960Sstevel@tonic-gate 
55973448Sdh155122 	ksp = kstat_create_netstack("icmp", 0, "rawip", "mib2",
55980Sstevel@tonic-gate 					KSTAT_TYPE_NAMED,
55990Sstevel@tonic-gate 					NUM_OF_FIELDS(rawip_named_kstat_t),
56003448Sdh155122 					0, stackid);
56013448Sdh155122 	if (ksp == NULL || ksp->ks_data == NULL)
56023448Sdh155122 		return (NULL);
56033448Sdh155122 
56043448Sdh155122 	bcopy(&template, ksp->ks_data, sizeof (template));
56053448Sdh155122 	ksp->ks_update = rawip_kstat_update;
56063448Sdh155122 	ksp->ks_private = (void *)(uintptr_t)stackid;
56073448Sdh155122 
56083448Sdh155122 	kstat_install(ksp);
56093448Sdh155122 	return (ksp);
56100Sstevel@tonic-gate }
56110Sstevel@tonic-gate 
56120Sstevel@tonic-gate static void
56133448Sdh155122 rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
56143448Sdh155122 {
56153448Sdh155122 	if (ksp != NULL) {
56163448Sdh155122 		ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
56173448Sdh155122 		kstat_delete_netstack(ksp, stackid);
56180Sstevel@tonic-gate 	}
56190Sstevel@tonic-gate }
56200Sstevel@tonic-gate 
56210Sstevel@tonic-gate static int
56223448Sdh155122 rawip_kstat_update(kstat_t *ksp, int rw)
56233448Sdh155122 {
56240Sstevel@tonic-gate 	rawip_named_kstat_t *rawipkp;
56253448Sdh155122 	netstackid_t	stackid = (netstackid_t)(uintptr_t)ksp->ks_private;
56263448Sdh155122 	netstack_t	*ns;
56273448Sdh155122 	icmp_stack_t	*is;
56283448Sdh155122 
56293448Sdh155122 	if ((ksp == NULL) || (ksp->ks_data == NULL))
56300Sstevel@tonic-gate 		return (EIO);
56310Sstevel@tonic-gate 
56320Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
56330Sstevel@tonic-gate 		return (EACCES);
56340Sstevel@tonic-gate 
56353448Sdh155122 	rawipkp = (rawip_named_kstat_t *)ksp->ks_data;
56363448Sdh155122 
56373448Sdh155122 	ns = netstack_find_by_stackid(stackid);
56383448Sdh155122 	if (ns == NULL)
56393448Sdh155122 		return (-1);
56403448Sdh155122 	is = ns->netstack_icmp;
56413448Sdh155122 	if (is == NULL) {
56423448Sdh155122 		netstack_rele(ns);
56433448Sdh155122 		return (-1);
56443448Sdh155122 	}
56453448Sdh155122 	rawipkp->inDatagrams.value.ui32 =  is->is_rawip_mib.rawipInDatagrams;
56463448Sdh155122 	rawipkp->inCksumErrs.value.ui32 =  is->is_rawip_mib.rawipInCksumErrs;
56473448Sdh155122 	rawipkp->inErrors.value.ui32 =	   is->is_rawip_mib.rawipInErrors;
56483448Sdh155122 	rawipkp->outDatagrams.value.ui32 = is->is_rawip_mib.rawipOutDatagrams;
56493448Sdh155122 	rawipkp->outErrors.value.ui32 =	   is->is_rawip_mib.rawipOutErrors;
56503448Sdh155122 	netstack_rele(ns);
56510Sstevel@tonic-gate 	return (0);
56520Sstevel@tonic-gate }
5653