xref: /onnv-gate/usr/src/uts/common/inet/ip/icmp.c (revision 5401:6b7c8b988fd6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51289Sja97890  * Common Development and Distribution License (the "License").
61289Sja97890  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223388Skcpoon  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/stropts.h>
330Sstevel@tonic-gate #include <sys/strlog.h>
340Sstevel@tonic-gate #include <sys/strsun.h>
350Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
360Sstevel@tonic-gate #include <sys/tihdr.h>
370Sstevel@tonic-gate #include <sys/timod.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
401676Sjpk #include <sys/strsubr.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/policy.h>
451676Sjpk #include <sys/priv.h>
460Sstevel@tonic-gate #include <sys/zone.h>
471673Sgt145670 #include <sys/time.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <sys/isa_defs.h>
510Sstevel@tonic-gate #include <sys/suntpi.h>
520Sstevel@tonic-gate #include <sys/xti_inet.h>
533448Sdh155122 #include <sys/netstack.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <net/route.h>
560Sstevel@tonic-gate #include <net/if.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <netinet/in.h>
590Sstevel@tonic-gate #include <netinet/ip6.h>
600Sstevel@tonic-gate #include <netinet/icmp6.h>
610Sstevel@tonic-gate #include <inet/common.h>
620Sstevel@tonic-gate #include <inet/ip.h>
630Sstevel@tonic-gate #include <inet/ip6.h>
640Sstevel@tonic-gate #include <inet/mi.h>
650Sstevel@tonic-gate #include <inet/nd.h>
660Sstevel@tonic-gate #include <inet/optcom.h>
670Sstevel@tonic-gate #include <inet/snmpcom.h>
680Sstevel@tonic-gate #include <inet/kstatcom.h>
690Sstevel@tonic-gate #include <inet/rawip_impl.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <netinet/ip_mroute.h>
720Sstevel@tonic-gate #include <inet/tcp.h>
730Sstevel@tonic-gate #include <net/pfkeyv2.h>
740Sstevel@tonic-gate #include <inet/ipsec_info.h>
750Sstevel@tonic-gate #include <inet/ipclassifier.h>
760Sstevel@tonic-gate 
771676Sjpk #include <sys/tsol/label.h>
781676Sjpk #include <sys/tsol/tnet.h>
791676Sjpk 
803318Srshoaib #include <inet/ip_ire.h>
813318Srshoaib #include <inet/ip_if.h>
823318Srshoaib 
833318Srshoaib #include <inet/ip_impl.h>
843318Srshoaib 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * Synchronization notes:
870Sstevel@tonic-gate  *
885240Snordmark  * RAWIP is MT and uses the usual kernel synchronization primitives. There is
895240Snordmark  * locks, which is icmp_rwlock. We also use conn_lock when updating things
905240Snordmark  * which affect the IP classifier lookup.
915240Snordmark  * The lock order is icmp_rwlock -> conn_lock.
925240Snordmark  *
935240Snordmark  * The icmp_rwlock:
945240Snordmark  * This protects most of the other fields in the icmp_t. The exact list of
955240Snordmark  * fields which are protected by each of the above locks is documented in
965240Snordmark  * the icmp_t structure definition.
975240Snordmark  *
985240Snordmark  * Plumbing notes:
995240Snordmark  * ICMP is always a device driver. For compatibility with mibopen() code
1005240Snordmark  * it is possible to I_PUSH "icmp", but that results in pushing a passthrough
1015240Snordmark  * dummy module.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static void	icmp_addr_req(queue_t *q, mblk_t *mp);
1050Sstevel@tonic-gate static void	icmp_bind(queue_t *q, mblk_t *mp);
1060Sstevel@tonic-gate static void	icmp_bind_proto(queue_t *q);
1075240Snordmark static void	icmp_bind_result(conn_t *, mblk_t *);
1085240Snordmark static void	icmp_bind_ack(conn_t *, mblk_t *mp);
1095240Snordmark static void	icmp_bind_error(conn_t *, mblk_t *mp);
1105240Snordmark static int	icmp_build_hdrs(icmp_t *icmp);
1110Sstevel@tonic-gate static void	icmp_capability_req(queue_t *q, mblk_t *mp);
1120Sstevel@tonic-gate static int	icmp_close(queue_t *q);
1130Sstevel@tonic-gate static void	icmp_connect(queue_t *q, mblk_t *mp);
1140Sstevel@tonic-gate static void	icmp_disconnect(queue_t *q, mblk_t *mp);
1150Sstevel@tonic-gate static void	icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
1160Sstevel@tonic-gate 		    int sys_error);
1170Sstevel@tonic-gate static void	icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
1180Sstevel@tonic-gate 		    t_scalar_t t_error, int sys_error);
1190Sstevel@tonic-gate static void	icmp_icmp_error(queue_t *q, mblk_t *mp);
1200Sstevel@tonic-gate static void	icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp);
1210Sstevel@tonic-gate static void	icmp_info_req(queue_t *q, mblk_t *mp);
1225240Snordmark static void	icmp_input(void *, mblk_t *, void *);
1230Sstevel@tonic-gate static mblk_t	*icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim,
1240Sstevel@tonic-gate 		    t_scalar_t addr_length, in_port_t);
1255240Snordmark static int	icmp_open(queue_t *q, dev_t *devp, int flag, int sflag,
1265240Snordmark 		    cred_t *credp, boolean_t isv6);
1275240Snordmark static int	icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag,
1285240Snordmark 		    cred_t *credp);
1295240Snordmark static int	icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag,
1305240Snordmark 		    cred_t *credp);
1315240Snordmark static void	icmp_output(queue_t *q, mblk_t *mp);
1320Sstevel@tonic-gate static int	icmp_unitdata_opt_process(queue_t *q, mblk_t *mp,
1330Sstevel@tonic-gate 		    int *errorp, void *thisdg_attrs);
1340Sstevel@tonic-gate static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
1350Sstevel@tonic-gate int		icmp_opt_set(queue_t *q, uint_t optset_context,
1360Sstevel@tonic-gate 		    int level, int name, uint_t inlen,
1370Sstevel@tonic-gate 		    uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
1380Sstevel@tonic-gate 		    void *thisdg_attrs, cred_t *cr, mblk_t *mblk);
1390Sstevel@tonic-gate int		icmp_opt_get(queue_t *q, int level, int name,
1400Sstevel@tonic-gate 		    uchar_t *ptr);
1410Sstevel@tonic-gate static int	icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
1423448Sdh155122 static boolean_t icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt);
1430Sstevel@tonic-gate static int	icmp_param_set(queue_t *q, mblk_t *mp, char *value,
1440Sstevel@tonic-gate 		    caddr_t cp, cred_t *cr);
1450Sstevel@tonic-gate static int	icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
1460Sstevel@tonic-gate 		    uchar_t *ptr, int len);
1470Sstevel@tonic-gate static int	icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp,
1480Sstevel@tonic-gate 		    cred_t *cr);
1490Sstevel@tonic-gate static void	icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err);
1500Sstevel@tonic-gate static void	icmp_unbind(queue_t *q, mblk_t *mp);
1510Sstevel@tonic-gate static void	icmp_wput(queue_t *q, mblk_t *mp);
1520Sstevel@tonic-gate static void	icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6,
1530Sstevel@tonic-gate 		    t_scalar_t tudr_optlen);
1540Sstevel@tonic-gate static void	icmp_wput_other(queue_t *q, mblk_t *mp);
1550Sstevel@tonic-gate static void	icmp_wput_iocdata(queue_t *q, mblk_t *mp);
1560Sstevel@tonic-gate static void	icmp_wput_restricted(queue_t *q, mblk_t *mp);
1570Sstevel@tonic-gate 
1583448Sdh155122 static void	*rawip_stack_init(netstackid_t stackid, netstack_t *ns);
1593448Sdh155122 static void	rawip_stack_fini(netstackid_t stackid, void *arg);
1603448Sdh155122 
1613448Sdh155122 static void	*rawip_kstat_init(netstackid_t stackid);
1623448Sdh155122 static void	rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp);
1630Sstevel@tonic-gate static int	rawip_kstat_update(kstat_t *kp, int rw);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 
1665240Snordmark static struct module_info icmp_mod_info =  {
1670Sstevel@tonic-gate 	5707, "icmp", 1, INFPSZ, 512, 128
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1705240Snordmark /*
1715240Snordmark  * Entry points for ICMP as a device.
1725240Snordmark  * We have separate open functions for the /dev/icmp and /dev/icmp6 devices.
1735240Snordmark  */
1745240Snordmark static struct qinit icmprinitv4 = {
1755240Snordmark 	NULL, NULL, icmp_openv4, icmp_close, NULL, &icmp_mod_info
1765240Snordmark };
1775240Snordmark 
1785240Snordmark static struct qinit icmprinitv6 = {
1795240Snordmark 	NULL, NULL, icmp_openv6, icmp_close, NULL, &icmp_mod_info
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1825240Snordmark static struct qinit icmpwinit = {
1835277Snordmark 	(pfi_t)icmp_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, &icmp_mod_info
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate 
1865240Snordmark /* For AF_INET aka /dev/icmp */
1875240Snordmark struct streamtab icmpinfov4 = {
1885240Snordmark 	&icmprinitv4, &icmpwinit
1895240Snordmark };
1905240Snordmark 
1915240Snordmark /* For AF_INET6 aka /dev/icmp6 */
1925240Snordmark struct streamtab icmpinfov6 = {
1935240Snordmark 	&icmprinitv6, &icmpwinit
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static sin_t	sin_null;	/* Zero address for quick clears */
1970Sstevel@tonic-gate static sin6_t	sin6_null;	/* Zero address for quick clears */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages */
2000Sstevel@tonic-gate static struct T_info_ack icmp_g_t_info_ack = {
2010Sstevel@tonic-gate 	T_INFO_ACK,
2020Sstevel@tonic-gate 	IP_MAXPACKET,	 /* TSDU_size.  icmp allows maximum size messages. */
2030Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size.  icmp does not support expedited data. */
2040Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. icmp does not support connect data. */
2050Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. icmp does not support disconnect data. */
2060Sstevel@tonic-gate 	0,		/* ADDR_size - filled in later. */
2070Sstevel@tonic-gate 	0,		/* OPT_size - not initialized here */
2080Sstevel@tonic-gate 	IP_MAXPACKET,	/* TIDU_size.  icmp allows maximum size messages. */
2090Sstevel@tonic-gate 	T_CLTS,		/* SERV_type.  icmp supports connection-less. */
2100Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state.  This is set from icmp_state. */
2110Sstevel@tonic-gate 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
2120Sstevel@tonic-gate };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2153448Sdh155122  * Table of ND variables supported by icmp.  These are loaded into is_nd
2163448Sdh155122  * when the stack instance is created.
2170Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate static icmpparam_t	icmp_param_arr[] = {
2200Sstevel@tonic-gate 	/* min	max	value	name */
2210Sstevel@tonic-gate 	{ 0,	128,	32,	"icmp_wroff_extra" },
2220Sstevel@tonic-gate 	{ 1,	255,	255,	"icmp_ipv4_ttl" },
2230Sstevel@tonic-gate 	{ 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS,	"icmp_ipv6_hoplimit"},
2240Sstevel@tonic-gate 	{ 0,	1,	1,	"icmp_bsd_compat" },
2250Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_xmit_hiwat"},
2260Sstevel@tonic-gate 	{ 0,	65536,	1024,	"icmp_xmit_lowat"},
2270Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_recv_hiwat"},
2280Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"icmp_max_buf"},
2290Sstevel@tonic-gate };
2303448Sdh155122 #define	is_wroff_extra			is_param_arr[0].icmp_param_value
2313448Sdh155122 #define	is_ipv4_ttl			is_param_arr[1].icmp_param_value
2323448Sdh155122 #define	is_ipv6_hoplimit		is_param_arr[2].icmp_param_value
2333448Sdh155122 #define	is_bsd_compat			is_param_arr[3].icmp_param_value
2343448Sdh155122 #define	is_xmit_hiwat			is_param_arr[4].icmp_param_value
2353448Sdh155122 #define	is_xmit_lowat			is_param_arr[5].icmp_param_value
2363448Sdh155122 #define	is_recv_hiwat			is_param_arr[6].icmp_param_value
2373448Sdh155122 #define	is_max_buf			is_param_arr[7].icmp_param_value
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
2410Sstevel@tonic-gate  * passed to icmp_wput.
2420Sstevel@tonic-gate  * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP
2430Sstevel@tonic-gate  * protocol type placed in the message following the address. A T_BIND_ACK
2445240Snordmark  * message is returned by ip_bind_v4/v6.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static void
2470Sstevel@tonic-gate icmp_bind(queue_t *q, mblk_t *mp)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	sin_t	*sin;
2500Sstevel@tonic-gate 	sin6_t	*sin6;
2510Sstevel@tonic-gate 	mblk_t	*mp1;
2520Sstevel@tonic-gate 	struct T_bind_req	*tbr;
2530Sstevel@tonic-gate 	icmp_t	*icmp;
2545240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
2555240Snordmark 
2565240Snordmark 	icmp = connp->conn_icmp;
2570Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
2580Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2590Sstevel@tonic-gate 		    "icmp_bind: bad req, len %u",
2600Sstevel@tonic-gate 		    (uint_t)(mp->b_wptr - mp->b_rptr));
2610Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
2620Sstevel@tonic-gate 		return;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
2650Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2660Sstevel@tonic-gate 		    "icmp_bind: bad state, %d", icmp->icmp_state);
2670Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
2680Sstevel@tonic-gate 		return;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * Reallocate the message to make sure we have enough room for an
2720Sstevel@tonic-gate 	 * address and the protocol type.
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1);
2750Sstevel@tonic-gate 	if (!mp1) {
2760Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
2770Sstevel@tonic-gate 		return;
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 	mp = mp1;
2800Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
2810Sstevel@tonic-gate 	switch (tbr->ADDR_length) {
2820Sstevel@tonic-gate 	case 0:			/* Generic request */
2830Sstevel@tonic-gate 		tbr->ADDR_offset = sizeof (struct T_bind_req);
2840Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
2850Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin_t);
2860Sstevel@tonic-gate 			sin = (sin_t *)&tbr[1];
2870Sstevel@tonic-gate 			*sin = sin_null;
2880Sstevel@tonic-gate 			sin->sin_family = AF_INET;
2890Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin[1];
2900Sstevel@tonic-gate 		} else {
2910Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
2920Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin6_t);
2930Sstevel@tonic-gate 			sin6 = (sin6_t *)&tbr[1];
2940Sstevel@tonic-gate 			*sin6 = sin6_null;
2950Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
2960Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin6[1];
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		break;
2990Sstevel@tonic-gate 	case sizeof (sin_t):	/* Complete IP address */
3000Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset,
3010Sstevel@tonic-gate 		    sizeof (sin_t));
3020Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
3030Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
3040Sstevel@tonic-gate 			return;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
3070Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
3080Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3090Sstevel@tonic-gate 			return;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 		break;
3120Sstevel@tonic-gate 	case sizeof (sin6_t):	/* Complete IP address */
3130Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset,
3140Sstevel@tonic-gate 		    sizeof (sin6_t));
3150Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
3160Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
3170Sstevel@tonic-gate 			return;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
3200Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
3210Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3220Sstevel@tonic-gate 			return;
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
3250Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3260Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
3270Sstevel@tonic-gate 			return;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 	default:
3310Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
3320Sstevel@tonic-gate 		    "icmp_bind: bad ADDR_length %d", tbr->ADDR_length);
3330Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
3340Sstevel@tonic-gate 		return;
3350Sstevel@tonic-gate 	}
3365240Snordmark 
3375240Snordmark 	/*
3385240Snordmark 	 * The state must be TS_UNBND. TPI mandates that users must send
3395240Snordmark 	 * TPI primitives only 1 at a time and wait for the response before
3405240Snordmark 	 * sending the next primitive.
3415240Snordmark 	 */
3425240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
3435240Snordmark 	if (icmp->icmp_state != TS_UNBND || icmp->icmp_pending_op != -1) {
3445240Snordmark 		rw_exit(&icmp->icmp_rwlock);
3455240Snordmark 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
3465240Snordmark 		    "icmp_bind: bad state, %d", icmp->icmp_state);
3475240Snordmark 		icmp_err_ack(q, mp, TOUTSTATE, 0);
3485240Snordmark 		return;
3495240Snordmark 	}
3505240Snordmark 
3515240Snordmark 	icmp->icmp_pending_op = tbr->PRIM_type;
3525240Snordmark 
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * Copy the source address into our icmp structure.  This address
3550Sstevel@tonic-gate 	 * may still be zero; if so, ip will fill in the correct address
3560Sstevel@tonic-gate 	 * each time an outbound packet is passed to it.
3575240Snordmark 	 * If we are binding to a broadcast or multicast address then
3585240Snordmark 	 * icmp_bind_ack will clear the source address when it receives
3595240Snordmark 	 * the T_BIND_ACK.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
3640Sstevel@tonic-gate 		ASSERT(sin != NULL);
3650Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3660Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr,
3670Sstevel@tonic-gate 		    &icmp->icmp_v6src);
3680Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
3690Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
3700Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3710Sstevel@tonic-gate 	} else {
3720Sstevel@tonic-gate 		int error;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		ASSERT(sin6 != NULL);
3750Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
3760Sstevel@tonic-gate 		icmp->icmp_v6src = sin6->sin6_addr;
3770Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
3780Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		/* Rebuild the header template */
3815240Snordmark 		error = icmp_build_hdrs(icmp);
3820Sstevel@tonic-gate 		if (error != 0) {
3835240Snordmark 			icmp->icmp_pending_op = -1;
3845240Snordmark 			rw_exit(&icmp->icmp_rwlock);
3850Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
3860Sstevel@tonic-gate 			return;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	/*
3900Sstevel@tonic-gate 	 * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following
3910Sstevel@tonic-gate 	 * the address.
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
3940Sstevel@tonic-gate 	if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) {
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * Append a request for an IRE if src not 0 (INADDR_ANY)
3970Sstevel@tonic-gate 		 */
3980Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
3990Sstevel@tonic-gate 		if (!mp->b_cont) {
4005240Snordmark 			icmp->icmp_pending_op = -1;
4015240Snordmark 			rw_exit(&icmp->icmp_rwlock);
4020Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOMEM);
4030Sstevel@tonic-gate 			return;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
4060Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
4070Sstevel@tonic-gate 	}
4085240Snordmark 	rw_exit(&icmp->icmp_rwlock);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */
4115240Snordmark 	if (icmp->icmp_family == AF_INET6)
4125240Snordmark 		mp = ip_bind_v6(q, mp, connp, NULL);
4135240Snordmark 	else
4145240Snordmark 		mp = ip_bind_v4(q, mp, connp);
4155240Snordmark 
4165240Snordmark 	/* The above return NULL if the bind needs to be deferred */
4175240Snordmark 	if (mp != NULL)
4185240Snordmark 		icmp_bind_result(connp, mp);
4195240Snordmark 	else
4205240Snordmark 		CONN_INC_REF(connp);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate  * Send message to IP to just bind to the protocol.
4250Sstevel@tonic-gate  */
4260Sstevel@tonic-gate static void
4270Sstevel@tonic-gate icmp_bind_proto(queue_t *q)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	mblk_t	*mp;
4300Sstevel@tonic-gate 	struct T_bind_req	*tbr;
4310Sstevel@tonic-gate 	icmp_t	*icmp;
4325240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
4335240Snordmark 
4345240Snordmark 	icmp = connp->conn_icmp;
4355240Snordmark 
4360Sstevel@tonic-gate 	mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1,
4370Sstevel@tonic-gate 	    BPRI_MED);
4380Sstevel@tonic-gate 	if (!mp) {
4390Sstevel@tonic-gate 		return;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
4420Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
4430Sstevel@tonic-gate 	tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */
4440Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (struct T_bind_req);
4455240Snordmark 
4465240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
4470Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
4480Sstevel@tonic-gate 		sin_t	*sin;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin_t);
4510Sstevel@tonic-gate 		sin = (sin_t *)&tbr[1];
4520Sstevel@tonic-gate 		*sin = sin_null;
4530Sstevel@tonic-gate 		sin->sin_family = AF_INET;
4540Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin[1];
4550Sstevel@tonic-gate 	} else {
4560Sstevel@tonic-gate 		sin6_t	*sin6;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
4590Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin6_t);
4600Sstevel@tonic-gate 		sin6 = (sin6_t *)&tbr[1];
4610Sstevel@tonic-gate 		*sin6 = sin6_null;
4620Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
4630Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin6[1];
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* Place protocol type in the O_T_BIND_REQ following the address. */
4670Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
4685240Snordmark 	rw_exit(&icmp->icmp_rwlock);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ to ip. */
4715240Snordmark 	if (icmp->icmp_family == AF_INET6)
4725240Snordmark 		mp = ip_bind_v6(q, mp, connp, NULL);
4735240Snordmark 	else
4745240Snordmark 		mp = ip_bind_v4(q, mp, connp);
4755240Snordmark 
4765240Snordmark 	/* The above return NULL if the bind needs to be deferred */
4775240Snordmark 	if (mp != NULL)
4785240Snordmark 		icmp_bind_result(connp, mp);
4795240Snordmark 	else
4805240Snordmark 		CONN_INC_REF(connp);
4815240Snordmark }
4825240Snordmark 
4835240Snordmark /*
4845240Snordmark  * This is called from ip_wput_nondata to handle the results of a
4855240Snordmark  * deferred RAWIP bind.  It is called once the bind has been completed.
4865240Snordmark  */
4875240Snordmark void
4885240Snordmark rawip_resume_bind(conn_t *connp, mblk_t *mp)
4895240Snordmark {
4905240Snordmark 	ASSERT(connp != NULL && IPCL_IS_RAWIP(connp));
4915240Snordmark 
4925240Snordmark 	icmp_bind_result(connp, mp);
4935240Snordmark 
4945240Snordmark 	CONN_OPER_PENDING_DONE(connp);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * This routine handles each T_CONN_REQ message passed to icmp.  It
4990Sstevel@tonic-gate  * associates a default destination address with the stream.
5000Sstevel@tonic-gate  *
5010Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
5020Sstevel@tonic-gate  *	T_BIND_REQ	- specifying local and remote address.
5030Sstevel@tonic-gate  *	IRE_DB_REQ_TYPE	- to get an IRE back containing ire_type and src
5040Sstevel@tonic-gate  *	T_OK_ACK	- for the T_CONN_REQ
5050Sstevel@tonic-gate  *	T_CONN_CON	- to keep the TPI user happy
5060Sstevel@tonic-gate  *
5075240Snordmark  * The connect completes in icmp_bind_result.
5080Sstevel@tonic-gate  * When a T_BIND_ACK is received information is extracted from the IRE
5090Sstevel@tonic-gate  * and the two appended messages are sent to the TPI user.
5105240Snordmark  * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will
5115240Snordmark  * convert it to an error ack for the appropriate primitive.
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate static void
5140Sstevel@tonic-gate icmp_connect(queue_t *q, mblk_t *mp)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	sin_t	*sin;
5170Sstevel@tonic-gate 	sin6_t	*sin6;
5180Sstevel@tonic-gate 	mblk_t	*mp1, *mp2;
5190Sstevel@tonic-gate 	struct T_conn_req	*tcr;
5200Sstevel@tonic-gate 	icmp_t	*icmp;
5210Sstevel@tonic-gate 	ipaddr_t	v4dst;
5220Sstevel@tonic-gate 	in6_addr_t	v6dst;
5230Sstevel@tonic-gate 	uint32_t	flowinfo;
5245240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
5255240Snordmark 
5265240Snordmark 	icmp = connp->conn_icmp;
5270Sstevel@tonic-gate 	tcr = (struct T_conn_req *)mp->b_rptr;
5280Sstevel@tonic-gate 	/* Sanity checks */
5295240Snordmark 	if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) {
5300Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
5310Sstevel@tonic-gate 		return;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (tcr->OPT_length != 0) {
5350Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADOPT, 0);
5360Sstevel@tonic-gate 		return;
5370Sstevel@tonic-gate 	}
5385240Snordmark 
5390Sstevel@tonic-gate 	switch (tcr->DEST_length) {
5400Sstevel@tonic-gate 	default:
5410Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
5420Sstevel@tonic-gate 		return;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	case sizeof (sin_t):
5450Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset,
5460Sstevel@tonic-gate 		    sizeof (sin_t));
5470Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
5480Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
5490Sstevel@tonic-gate 			return;
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
5520Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
5530Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
5540Sstevel@tonic-gate 			return;
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
5570Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
5580Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
5590Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
5600Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
5610Sstevel@tonic-gate 		break;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	case sizeof (sin6_t):
5640Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset,
5650Sstevel@tonic-gate 		    sizeof (sin6_t));
5660Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
5670Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
5680Sstevel@tonic-gate 			return;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
5710Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
5720Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
5730Sstevel@tonic-gate 			return;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
5760Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5770Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
5780Sstevel@tonic-gate 			return;
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 		v6dst = sin6->sin6_addr;
5810Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
5820Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
5830Sstevel@tonic-gate 		flowinfo = sin6->sin6_flowinfo;
5840Sstevel@tonic-gate 		break;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
5870Sstevel@tonic-gate 		/*
5880Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
5890Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
5900Sstevel@tonic-gate 		 * generate the T_CONN_CON.
5910Sstevel@tonic-gate 		 */
5920Sstevel@tonic-gate 		if (v4dst == INADDR_ANY) {
5930Sstevel@tonic-gate 			v4dst = htonl(INADDR_LOOPBACK);
5940Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
5950Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET) {
5960Sstevel@tonic-gate 				sin->sin_addr.s_addr = v4dst;
5970Sstevel@tonic-gate 			} else {
5980Sstevel@tonic-gate 				sin6->sin6_addr = v6dst;
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
6020Sstevel@tonic-gate 		icmp->icmp_flowinfo = 0;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		/*
6050Sstevel@tonic-gate 		 * If the destination address is multicast and
6060Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
6070Sstevel@tonic-gate 		 * use the address of that interface as our
6080Sstevel@tonic-gate 		 * source address if no source address has been set.
6090Sstevel@tonic-gate 		 */
6100Sstevel@tonic-gate 		if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY &&
6110Sstevel@tonic-gate 		    CLASSD(v4dst) &&
6120Sstevel@tonic-gate 		    icmp->icmp_multicast_if_addr != INADDR_ANY) {
6130Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr,
6140Sstevel@tonic-gate 			    &icmp->icmp_v6src);
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 	} else {
6170Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
6180Sstevel@tonic-gate 		/*
6190Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
6200Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
6210Sstevel@tonic-gate 		 * generate the T_CONN_CON.
6220Sstevel@tonic-gate 		 */
6230Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
6240Sstevel@tonic-gate 			v6dst = ipv6_loopback;
6250Sstevel@tonic-gate 			sin6->sin6_addr = v6dst;
6260Sstevel@tonic-gate 		}
6270Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
6280Sstevel@tonic-gate 		icmp->icmp_flowinfo = flowinfo;
6290Sstevel@tonic-gate 		/*
6300Sstevel@tonic-gate 		 * If the destination address is multicast and
6310Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
6320Sstevel@tonic-gate 		 * then the ip bind logic will pick the correct source
6330Sstevel@tonic-gate 		 * address (i.e. matching the outgoing multicast interface).
6340Sstevel@tonic-gate 		 */
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6375240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
6385240Snordmark 	if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) {
6395240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6405240Snordmark 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
6415240Snordmark 		    "icmp_connect: bad state, %d", icmp->icmp_state);
6425240Snordmark 		icmp_err_ack(q, mp, TOUTSTATE, 0);
6435240Snordmark 		return;
6445240Snordmark 	}
6455240Snordmark 	icmp->icmp_pending_op = T_CONN_REQ;
6465240Snordmark 
6475240Snordmark 	if (icmp->icmp_state == TS_DATA_XFER) {
6485240Snordmark 		/* Already connected - clear out state */
6495240Snordmark 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
6505240Snordmark 		icmp->icmp_state = TS_IDLE;
6515240Snordmark 	}
6525240Snordmark 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * Send down bind to IP to verify that there is a route
6550Sstevel@tonic-gate 	 * and to determine the source address.
6560Sstevel@tonic-gate 	 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
6590Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t),
6600Sstevel@tonic-gate 		    sin->sin_port);
6610Sstevel@tonic-gate 	} else {
6620Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
6630Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t),
6640Sstevel@tonic-gate 		    sin6->sin6_port);
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 	if (mp1 == NULL) {
6675240Snordmark 		icmp->icmp_pending_op = -1;
6685240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6690Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
6700Sstevel@tonic-gate 		return;
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * We also have to send a connection confirmation to
6755240Snordmark 	 * keep TLI happy. Prepare it for icmp_bind_result.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
6780Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL,
6790Sstevel@tonic-gate 		    0);
6800Sstevel@tonic-gate 	} else {
6810Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
6820Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL,
6830Sstevel@tonic-gate 		    0);
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 	if (mp2 == NULL) {
6860Sstevel@tonic-gate 		freemsg(mp1);
6875240Snordmark 		icmp->icmp_pending_op = -1;
6885240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6890Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
6900Sstevel@tonic-gate 		return;
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
6940Sstevel@tonic-gate 	if (mp == NULL) {
6950Sstevel@tonic-gate 		/* Unable to reuse the T_CONN_REQ for the ack. */
6960Sstevel@tonic-gate 		freemsg(mp2);
6975240Snordmark 		icmp->icmp_pending_op = -1;
6985240Snordmark 		rw_exit(&icmp->icmp_rwlock);
6990Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
7000Sstevel@tonic-gate 		return;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	icmp->icmp_state = TS_DATA_XFER;
7045240Snordmark 	rw_exit(&icmp->icmp_rwlock);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/* Hang onto the T_OK_ACK and T_CONN_CON for later. */
7070Sstevel@tonic-gate 	linkb(mp1, mp);
7080Sstevel@tonic-gate 	linkb(mp1, mp2);
7090Sstevel@tonic-gate 
7105240Snordmark 	mblk_setcred(mp1, connp->conn_cred);
7115240Snordmark 	if (icmp->icmp_family == AF_INET)
7125240Snordmark 		mp1 = ip_bind_v4(q, mp1, connp);
7135240Snordmark 	else
7145240Snordmark 		mp1 = ip_bind_v6(q, mp1, connp, NULL);
7155240Snordmark 
7165240Snordmark 	/* The above return NULL if the bind needs to be deferred */
7175240Snordmark 	if (mp1 != NULL)
7185240Snordmark 		icmp_bind_result(connp, mp1);
7195240Snordmark 	else
7205240Snordmark 		CONN_INC_REF(connp);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7235240Snordmark static void
7245240Snordmark icmp_close_free(conn_t *connp)
7250Sstevel@tonic-gate {
7265240Snordmark 	icmp_t *icmp = connp->conn_icmp;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/* If there are any options associated with the stream, free them. */
7295315Snordmark 	if (icmp->icmp_ip_snd_options != NULL) {
7300Sstevel@tonic-gate 		mi_free((char *)icmp->icmp_ip_snd_options);
7315315Snordmark 		icmp->icmp_ip_snd_options = NULL;
7325330Snordmark 		icmp->icmp_ip_snd_options_len = 0;
7335315Snordmark 	}
7345315Snordmark 
7355315Snordmark 	if (icmp->icmp_filter != NULL) {
7360Sstevel@tonic-gate 		kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
7375315Snordmark 		icmp->icmp_filter = NULL;
7385315Snordmark 	}
7390Sstevel@tonic-gate 	/* Free memory associated with sticky options */
7400Sstevel@tonic-gate 	if (icmp->icmp_sticky_hdrs_len != 0) {
7410Sstevel@tonic-gate 		kmem_free(icmp->icmp_sticky_hdrs,
7420Sstevel@tonic-gate 		    icmp->icmp_sticky_hdrs_len);
7430Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = NULL;
7440Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = 0;
7450Sstevel@tonic-gate 	}
7461676Sjpk 	ip6_pkt_free(&icmp->icmp_sticky_ipp);
7475330Snordmark 
7485330Snordmark 	/*
7495330Snordmark 	 * Clear any fields which the kmem_cache constructor clears.
7505330Snordmark 	 * Only icmp_connp needs to be preserved.
7515330Snordmark 	 * TBD: We should make this more efficient to avoid clearing
7525330Snordmark 	 * everything.
7535330Snordmark 	 */
7545330Snordmark 	ASSERT(icmp->icmp_connp == connp);
7555330Snordmark 	bzero(icmp, sizeof (icmp_t));
7565330Snordmark 	icmp->icmp_connp = connp;
7575240Snordmark }
7585240Snordmark 
7595240Snordmark static int
7605240Snordmark icmp_close(queue_t *q)
7615240Snordmark {
7625240Snordmark 	conn_t	*connp = (conn_t *)q->q_ptr;
7635240Snordmark 
7645240Snordmark 	ASSERT(connp != NULL && IPCL_IS_RAWIP(connp));
7655240Snordmark 
7665240Snordmark 	ip_quiesce_conn(connp);
7675240Snordmark 
7685240Snordmark 	qprocsoff(connp->conn_rq);
7695240Snordmark 
7705240Snordmark 	icmp_close_free(connp);
7715240Snordmark 
7725240Snordmark 	/*
7735240Snordmark 	 * Now we are truly single threaded on this stream, and can
7745240Snordmark 	 * delete the things hanging off the connp, and finally the connp.
7755240Snordmark 	 * We removed this connp from the fanout list, it cannot be
7765240Snordmark 	 * accessed thru the fanouts, and we already waited for the
7775240Snordmark 	 * conn_ref to drop to 0. We are already in close, so
7785240Snordmark 	 * there cannot be any other thread from the top. qprocsoff
7795240Snordmark 	 * has completed, and service has completed or won't run in
7805240Snordmark 	 * future.
7815240Snordmark 	 */
7825240Snordmark 	ASSERT(connp->conn_ref == 1);
7835240Snordmark 
7845240Snordmark 	inet_minor_free(ip_minor_arena, connp->conn_dev);
7855240Snordmark 
7865240Snordmark 	connp->conn_ref--;
7875240Snordmark 	ipcl_conn_destroy(connp);
7885240Snordmark 
7895240Snordmark 	q->q_ptr = WR(q)->q_ptr = NULL;
7905240Snordmark 	return (0);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate /*
7940Sstevel@tonic-gate  * This routine handles each T_DISCON_REQ message passed to icmp
7950Sstevel@tonic-gate  * as an indicating that ICMP is no longer connected. This results
7960Sstevel@tonic-gate  * in sending a T_BIND_REQ to IP to restore the binding to just
7970Sstevel@tonic-gate  * the local address.
7980Sstevel@tonic-gate  *
7990Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
8000Sstevel@tonic-gate  *	T_BIND_REQ	- specifying just the local address.
8010Sstevel@tonic-gate  *	T_OK_ACK	- for the T_DISCON_REQ
8020Sstevel@tonic-gate  *
8035240Snordmark  * The disconnect completes in icmp_bind_result.
8040Sstevel@tonic-gate  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
8055240Snordmark  * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will
8065240Snordmark  * convert it to an error ack for the appropriate primitive.
8070Sstevel@tonic-gate  */
8080Sstevel@tonic-gate static void
8090Sstevel@tonic-gate icmp_disconnect(queue_t *q, mblk_t *mp)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	icmp_t	*icmp;
8120Sstevel@tonic-gate 	mblk_t	*mp1;
8135240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
8145240Snordmark 
8155240Snordmark 	icmp = connp->conn_icmp;
8165240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
8175240Snordmark 	if (icmp->icmp_state != TS_DATA_XFER || icmp->icmp_pending_op != -1) {
8185240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8190Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
8200Sstevel@tonic-gate 		    "icmp_disconnect: bad state, %d", icmp->icmp_state);
8210Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
8220Sstevel@tonic-gate 		return;
8230Sstevel@tonic-gate 	}
8245240Snordmark 	icmp->icmp_pending_op = T_DISCON_REQ;
8250Sstevel@tonic-gate 	icmp->icmp_v6src = icmp->icmp_bound_v6src;
8260Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/*
8290Sstevel@tonic-gate 	 * Send down bind to IP to remove the full binding and revert
8300Sstevel@tonic-gate 	 * to the local address binding.
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
8330Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0);
8340Sstevel@tonic-gate 	} else {
8350Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
8360Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0);
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 	if (mp1 == NULL) {
8395240Snordmark 		icmp->icmp_pending_op = -1;
8405240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8410Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
8420Sstevel@tonic-gate 		return;
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
8450Sstevel@tonic-gate 	if (mp == NULL) {
8460Sstevel@tonic-gate 		/* Unable to reuse the T_DISCON_REQ for the ack. */
8475240Snordmark 		icmp->icmp_pending_op = -1;
8485240Snordmark 		rw_exit(&icmp->icmp_rwlock);
8490Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
8500Sstevel@tonic-gate 		return;
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
8540Sstevel@tonic-gate 		int error;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		/* Rebuild the header template */
8575240Snordmark 		error = icmp_build_hdrs(icmp);
8580Sstevel@tonic-gate 		if (error != 0) {
8595240Snordmark 			icmp->icmp_pending_op = -1;
8605240Snordmark 			rw_exit(&icmp->icmp_rwlock);
8610Sstevel@tonic-gate 			icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
8620Sstevel@tonic-gate 			freemsg(mp1);
8630Sstevel@tonic-gate 			return;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 	}
8665240Snordmark 
8675240Snordmark 	rw_exit(&icmp->icmp_rwlock);
8685240Snordmark 	/* Append the T_OK_ACK to the T_BIND_REQ for icmp_bind_result */
8690Sstevel@tonic-gate 	linkb(mp1, mp);
8705240Snordmark 
8715240Snordmark 	if (icmp->icmp_family == AF_INET6)
8725240Snordmark 		mp1 = ip_bind_v6(q, mp1, connp, NULL);
8735240Snordmark 	else
8745240Snordmark 		mp1 = ip_bind_v4(q, mp1, connp);
8755240Snordmark 
8765240Snordmark 	/* The above return NULL if the bind needs to be deferred */
8775240Snordmark 	if (mp1 != NULL)
8785240Snordmark 		icmp_bind_result(connp, mp1);
8795240Snordmark 	else
8805240Snordmark 		CONN_INC_REF(connp);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate /* This routine creates a T_ERROR_ACK message and passes it upstream. */
8840Sstevel@tonic-gate static void
8850Sstevel@tonic-gate icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
8880Sstevel@tonic-gate 		qreply(q, mp);
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /* Shorthand to generate and send TPI error acks to our client */
8920Sstevel@tonic-gate static void
8930Sstevel@tonic-gate icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
8940Sstevel@tonic-gate     t_scalar_t t_error, int sys_error)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	struct T_error_ack	*teackp;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
8990Sstevel@tonic-gate 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
9000Sstevel@tonic-gate 		teackp = (struct T_error_ack *)mp->b_rptr;
9010Sstevel@tonic-gate 		teackp->ERROR_prim = primitive;
9020Sstevel@tonic-gate 		teackp->TLI_error = t_error;
9030Sstevel@tonic-gate 		teackp->UNIX_error = sys_error;
9040Sstevel@tonic-gate 		qreply(q, mp);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate /*
9095240Snordmark  * icmp_icmp_error is called by icmp_input to process ICMP
9100Sstevel@tonic-gate  * messages passed up by IP.
9110Sstevel@tonic-gate  * Generates the appropriate T_UDERROR_IND for permanent
9120Sstevel@tonic-gate  * (non-transient) errors.
9130Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
9140Sstevel@tonic-gate  * the ICMP header.
9150Sstevel@tonic-gate  */
9160Sstevel@tonic-gate static void
9170Sstevel@tonic-gate icmp_icmp_error(queue_t *q, mblk_t *mp)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	icmph_t *icmph;
9200Sstevel@tonic-gate 	ipha_t	*ipha;
9210Sstevel@tonic-gate 	int	iph_hdr_length;
9220Sstevel@tonic-gate 	sin_t	sin;
9230Sstevel@tonic-gate 	sin6_t	sin6;
9240Sstevel@tonic-gate 	mblk_t	*mp1;
9250Sstevel@tonic-gate 	int	error = 0;
9265240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
9290Sstevel@tonic-gate 
9305240Snordmark 	ASSERT(OK_32PTR(mp->b_rptr));
9315240Snordmark 
9320Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
9330Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
9340Sstevel@tonic-gate 		icmp_icmp_error_ipv6(q, mp);
9350Sstevel@tonic-gate 		return;
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 	ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
9380Sstevel@tonic-gate 
9395240Snordmark 	/* Skip past the outer IP and ICMP headers */
9400Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
9410Sstevel@tonic-gate 	icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]);
9420Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
9430Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	switch (icmph->icmph_type) {
9460Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
9470Sstevel@tonic-gate 		switch (icmph->icmph_code) {
9480Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
9490Sstevel@tonic-gate 			/*
9500Sstevel@tonic-gate 			 * IP has already adjusted the path MTU.
9510Sstevel@tonic-gate 			 */
9520Sstevel@tonic-gate 			break;
9530Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
9540Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
9550Sstevel@tonic-gate 			error = ECONNREFUSED;
9560Sstevel@tonic-gate 			break;
9570Sstevel@tonic-gate 		default:
9580Sstevel@tonic-gate 			/* Transient errors */
9590Sstevel@tonic-gate 			break;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 		break;
9620Sstevel@tonic-gate 	default:
9630Sstevel@tonic-gate 		/* Transient errors */
9640Sstevel@tonic-gate 		break;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 	if (error == 0) {
9670Sstevel@tonic-gate 		freemsg(mp);
9680Sstevel@tonic-gate 		return;
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9715240Snordmark 	/*
9725240Snordmark 	 * Deliver T_UDERROR_IND when the application has asked for it.
9735240Snordmark 	 * The socket layer enables this automatically when connected.
9745240Snordmark 	 */
9755240Snordmark 	if (!icmp->icmp_dgram_errind) {
9765240Snordmark 		freemsg(mp);
9775240Snordmark 		return;
9785240Snordmark 	}
9795240Snordmark 
9800Sstevel@tonic-gate 	switch (icmp->icmp_family) {
9810Sstevel@tonic-gate 	case AF_INET:
9820Sstevel@tonic-gate 		sin = sin_null;
9830Sstevel@tonic-gate 		sin.sin_family = AF_INET;
9840Sstevel@tonic-gate 		sin.sin_addr.s_addr = ipha->ipha_dst;
9850Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
9860Sstevel@tonic-gate 		    error);
9870Sstevel@tonic-gate 		break;
9880Sstevel@tonic-gate 	case AF_INET6:
9890Sstevel@tonic-gate 		sin6 = sin6_null;
9900Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
9910Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
9940Sstevel@tonic-gate 		    NULL, 0, error);
9950Sstevel@tonic-gate 		break;
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 	if (mp1)
9980Sstevel@tonic-gate 		putnext(q, mp1);
9990Sstevel@tonic-gate 	freemsg(mp);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate /*
10030Sstevel@tonic-gate  * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6
10040Sstevel@tonic-gate  * for IPv6 packets.
10050Sstevel@tonic-gate  * Send permanent (non-transient) errors upstream.
10060Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
10070Sstevel@tonic-gate  * as the ICMPv6 header.
10080Sstevel@tonic-gate  */
10090Sstevel@tonic-gate static void
10100Sstevel@tonic-gate icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	icmp6_t		*icmp6;
10130Sstevel@tonic-gate 	ip6_t		*ip6h, *outer_ip6h;
10140Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
10150Sstevel@tonic-gate 	uint8_t		*nexthdrp;
10160Sstevel@tonic-gate 	sin6_t		sin6;
10170Sstevel@tonic-gate 	mblk_t		*mp1;
10180Sstevel@tonic-gate 	int		error = 0;
10195240Snordmark 	icmp_t		*icmp = Q_TO_ICMP(q);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	outer_ip6h = (ip6_t *)mp->b_rptr;
10220Sstevel@tonic-gate 	if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
10230Sstevel@tonic-gate 		iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
10240Sstevel@tonic-gate 	else
10250Sstevel@tonic-gate 		iph_hdr_length = IPV6_HDR_LEN;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
10280Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
10290Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
10300Sstevel@tonic-gate 		freemsg(mp);
10310Sstevel@tonic-gate 		return;
10320Sstevel@tonic-gate 	}
10335240Snordmark 
10340Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
10350Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
10360Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
10370Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
10380Sstevel@tonic-gate 			error = ECONNREFUSED;
10390Sstevel@tonic-gate 			break;
10400Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
10410Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
10420Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
10430Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
10440Sstevel@tonic-gate 			/* Transient errors */
10450Sstevel@tonic-gate 			break;
10460Sstevel@tonic-gate 		default:
10470Sstevel@tonic-gate 			break;
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 		break;
10500Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG: {
10510Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi;
10520Sstevel@tonic-gate 		struct T_opthdr		*toh;
10530Sstevel@tonic-gate 		size_t			udi_size;
10540Sstevel@tonic-gate 		mblk_t			*newmp;
10550Sstevel@tonic-gate 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
10560Sstevel@tonic-gate 		    sizeof (struct ip6_mtuinfo);
10570Sstevel@tonic-gate 		sin6_t			*sin6;
10580Sstevel@tonic-gate 		struct ip6_mtuinfo	*mtuinfo;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		/*
10610Sstevel@tonic-gate 		 * If the application has requested to receive path mtu
10620Sstevel@tonic-gate 		 * information, send up an empty message containing an
10630Sstevel@tonic-gate 		 * IPV6_PATHMTU ancillary data item.
10640Sstevel@tonic-gate 		 */
10650Sstevel@tonic-gate 		if (!icmp->icmp_ipv6_recvpathmtu)
10660Sstevel@tonic-gate 			break;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
10690Sstevel@tonic-gate 		    opt_length;
10700Sstevel@tonic-gate 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
10715240Snordmark 			BUMP_MIB(&icmp->icmp_is->is_rawip_mib, rawipInErrors);
10720Sstevel@tonic-gate 			break;
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		/*
10760Sstevel@tonic-gate 		 * newmp->b_cont is left to NULL on purpose.  This is an
10770Sstevel@tonic-gate 		 * empty message containing only ancillary data.
10780Sstevel@tonic-gate 		 */
10790Sstevel@tonic-gate 		newmp->b_datap->db_type = M_PROTO;
10800Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
10810Sstevel@tonic-gate 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
10820Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
10830Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin6_t);
10840Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
10850Sstevel@tonic-gate 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
10860Sstevel@tonic-gate 		tudi->OPT_length = opt_length;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		sin6 = (sin6_t *)&tudi[1];
10890Sstevel@tonic-gate 		bzero(sin6, sizeof (sin6_t));
10900Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
10910Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_v6dst;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		toh = (struct T_opthdr *)&sin6[1];
10940Sstevel@tonic-gate 		toh->level = IPPROTO_IPV6;
10950Sstevel@tonic-gate 		toh->name = IPV6_PATHMTU;
10960Sstevel@tonic-gate 		toh->len = opt_length;
10970Sstevel@tonic-gate 		toh->status = 0;
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
11000Sstevel@tonic-gate 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
11010Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
11020Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
11030Sstevel@tonic-gate 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
11040Sstevel@tonic-gate 		/*
11050Sstevel@tonic-gate 		 * We've consumed everything we need from the original
11060Sstevel@tonic-gate 		 * message.  Free it, then send our empty message.
11070Sstevel@tonic-gate 		 */
11080Sstevel@tonic-gate 		freemsg(mp);
11090Sstevel@tonic-gate 		putnext(q, newmp);
11100Sstevel@tonic-gate 		return;
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
11130Sstevel@tonic-gate 		/* Transient errors */
11140Sstevel@tonic-gate 		break;
11150Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
11160Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
11170Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
11180Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
11190Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
11200Sstevel@tonic-gate 			error = ECONNREFUSED;
11210Sstevel@tonic-gate 			break;
11220Sstevel@tonic-gate 		}
11230Sstevel@tonic-gate 		break;
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 	if (error == 0) {
11260Sstevel@tonic-gate 		freemsg(mp);
11270Sstevel@tonic-gate 		return;
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11305240Snordmark 	/*
11315240Snordmark 	 * Deliver T_UDERROR_IND when the application has asked for it.
11325240Snordmark 	 * The socket layer enables this automatically when connected.
11335240Snordmark 	 */
11345240Snordmark 	if (!icmp->icmp_dgram_errind) {
11355240Snordmark 		freemsg(mp);
11365240Snordmark 		return;
11375240Snordmark 	}
11385240Snordmark 
11390Sstevel@tonic-gate 	sin6 = sin6_null;
11400Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
11410Sstevel@tonic-gate 	sin6.sin6_addr = ip6h->ip6_dst;
11420Sstevel@tonic-gate 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
11450Sstevel@tonic-gate 	    error);
11460Sstevel@tonic-gate 	if (mp1)
11470Sstevel@tonic-gate 		putnext(q, mp1);
11480Sstevel@tonic-gate 	freemsg(mp);
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate  * This routine responds to T_ADDR_REQ messages.  It is called by icmp_wput.
11530Sstevel@tonic-gate  * The local address is filled in if endpoint is bound. The remote address
11540Sstevel@tonic-gate  * is filled in if remote address has been precified ("connected endpoint")
11550Sstevel@tonic-gate  * (The concept of connected CLTS sockets is alien to published TPI
11560Sstevel@tonic-gate  *  but we support it anyway).
11570Sstevel@tonic-gate  */
11580Sstevel@tonic-gate static void
11590Sstevel@tonic-gate icmp_addr_req(queue_t *q, mblk_t *mp)
11600Sstevel@tonic-gate {
11615240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
11620Sstevel@tonic-gate 	mblk_t	*ackmp;
11630Sstevel@tonic-gate 	struct T_addr_ack *taa;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	/* Make it large enough for worst case */
11660Sstevel@tonic-gate 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
11670Sstevel@tonic-gate 	    2 * sizeof (sin6_t), 1);
11680Sstevel@tonic-gate 	if (ackmp == NULL) {
11690Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
11700Sstevel@tonic-gate 		return;
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 	taa = (struct T_addr_ack *)ackmp->b_rptr;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	bzero(taa, sizeof (struct T_addr_ack));
11750Sstevel@tonic-gate 	ackmp->b_wptr = (uchar_t *)&taa[1];
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	taa->PRIM_type = T_ADDR_ACK;
11780Sstevel@tonic-gate 	ackmp->b_datap->db_type = M_PCPROTO;
11795240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_READER);
11800Sstevel@tonic-gate 	/*
11810Sstevel@tonic-gate 	 * Note: Following code assumes 32 bit alignment of basic
11820Sstevel@tonic-gate 	 * data structures like sin_t and struct T_addr_ack.
11830Sstevel@tonic-gate 	 */
11840Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
11850Sstevel@tonic-gate 		/*
11860Sstevel@tonic-gate 		 * Fill in local address
11870Sstevel@tonic-gate 		 */
11880Sstevel@tonic-gate 		taa->LOCADDR_offset = sizeof (*taa);
11890Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
11900Sstevel@tonic-gate 			sin_t	*sin;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin_t);
11930Sstevel@tonic-gate 			sin = (sin_t *)&taa[1];
11940Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
11950Sstevel@tonic-gate 			*sin = sin_null;
11960Sstevel@tonic-gate 			sin->sin_family = AF_INET;
11970Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
11980Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
11990Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src,
12000Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
12010Sstevel@tonic-gate 			} else {
12020Sstevel@tonic-gate 				/*
12030Sstevel@tonic-gate 				 * INADDR_ANY
12040Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
12050Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
12060Sstevel@tonic-gate 				 * local address instead (that could
12070Sstevel@tonic-gate 				 * also still be INADDR_ANY)
12080Sstevel@tonic-gate 				 */
12090Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src,
12100Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
12110Sstevel@tonic-gate 			}
12120Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin[1];
12130Sstevel@tonic-gate 		} else {
12140Sstevel@tonic-gate 			sin6_t	*sin6;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
12170Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin6_t);
12180Sstevel@tonic-gate 			sin6 = (sin6_t *)&taa[1];
12190Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
12200Sstevel@tonic-gate 			*sin6 = sin6_null;
12210Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
12220Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
12230Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_v6src;
12240Sstevel@tonic-gate 			} else {
12250Sstevel@tonic-gate 				/*
12260Sstevel@tonic-gate 				 * UNSPECIFIED
12270Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
12280Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
12290Sstevel@tonic-gate 				 * local address instead (that could
12300Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
12310Sstevel@tonic-gate 				 */
12320Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_bound_v6src;
12330Sstevel@tonic-gate 			}
12340Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin6[1];
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 	}
12375240Snordmark 	rw_exit(&icmp->icmp_rwlock);
12380Sstevel@tonic-gate 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
12390Sstevel@tonic-gate 	qreply(q, ackmp);
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate static void
12430Sstevel@tonic-gate icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
12440Sstevel@tonic-gate {
12450Sstevel@tonic-gate 	*tap = icmp_g_t_info_ack;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6)
12480Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin6_t);
12490Sstevel@tonic-gate 	else
12500Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin_t);
12510Sstevel@tonic-gate 	tap->CURRENT_state = icmp->icmp_state;
12520Sstevel@tonic-gate 	tap->OPT_size = icmp_max_optsize;
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate /*
12560Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
12570Sstevel@tonic-gate  * icmp_wput.  Much of the T_CAPABILITY_ACK information is copied from
12580Sstevel@tonic-gate  * icmp_g_t_info_ack.  The current state of the stream is copied from
12590Sstevel@tonic-gate  * icmp_state.
12600Sstevel@tonic-gate  */
12610Sstevel@tonic-gate static void
12620Sstevel@tonic-gate icmp_capability_req(queue_t *q, mblk_t *mp)
12630Sstevel@tonic-gate {
12645240Snordmark 	icmp_t			*icmp = Q_TO_ICMP(q);
12650Sstevel@tonic-gate 	t_uscalar_t		cap_bits1;
12660Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
12715240Snordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
12720Sstevel@tonic-gate 	if (!mp)
12730Sstevel@tonic-gate 		return;
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
12760Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
12790Sstevel@tonic-gate 		icmp_copy_info(&tcap->INFO_ack, icmp);
12800Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	qreply(q, mp);
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages.  It is called by icmp_wput.
12880Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
12890Sstevel@tonic-gate  * The current state of the stream is copied from icmp_state.
12900Sstevel@tonic-gate  */
12910Sstevel@tonic-gate static void
12920Sstevel@tonic-gate icmp_info_req(queue_t *q, mblk_t *mp)
12930Sstevel@tonic-gate {
12945240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
12970Sstevel@tonic-gate 	    T_INFO_ACK);
12980Sstevel@tonic-gate 	if (!mp)
12990Sstevel@tonic-gate 		return;
13000Sstevel@tonic-gate 	icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
13010Sstevel@tonic-gate 	qreply(q, mp);
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate /*
13050Sstevel@tonic-gate  * IP recognizes seven kinds of bind requests:
13060Sstevel@tonic-gate  *
13070Sstevel@tonic-gate  * - A zero-length address binds only to the protocol number.
13080Sstevel@tonic-gate  *
13090Sstevel@tonic-gate  * - A 4-byte address is treated as a request to
13100Sstevel@tonic-gate  * validate that the address is a valid local IPv4
13110Sstevel@tonic-gate  * address, appropriate for an application to bind to.
13120Sstevel@tonic-gate  * IP does the verification, but does not make any note
13130Sstevel@tonic-gate  * of the address at this time.
13140Sstevel@tonic-gate  *
13150Sstevel@tonic-gate  * - A 16-byte address contains is treated as a request
13160Sstevel@tonic-gate  * to validate a local IPv6 address, as the 4-byte
13170Sstevel@tonic-gate  * address case above.
13180Sstevel@tonic-gate  *
13190Sstevel@tonic-gate  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
13200Sstevel@tonic-gate  * use it for the inbound fanout of packets.
13210Sstevel@tonic-gate  *
13220Sstevel@tonic-gate  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
13230Sstevel@tonic-gate  * use it for the inbound fanout of packets.
13240Sstevel@tonic-gate  *
13250Sstevel@tonic-gate  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
13260Sstevel@tonic-gate  * information consisting of local and remote addresses
13270Sstevel@tonic-gate  * and ports (unused for raw sockets).  In this case, the addresses are both
13280Sstevel@tonic-gate  * validated as appropriate for this operation, and, if
13290Sstevel@tonic-gate  * so, the information is retained for use in the
13300Sstevel@tonic-gate  * inbound fanout.
13310Sstevel@tonic-gate  *
13320Sstevel@tonic-gate  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
13330Sstevel@tonic-gate  * fanout information, like the 12-byte case above.
13340Sstevel@tonic-gate  *
13350Sstevel@tonic-gate  * IP will also fill in the IRE request mblk with information
13360Sstevel@tonic-gate  * regarding our peer.  In all cases, we notify IP of our protocol
13370Sstevel@tonic-gate  * type by appending a single protocol byte to the bind request.
13380Sstevel@tonic-gate  */
13390Sstevel@tonic-gate static mblk_t *
13400Sstevel@tonic-gate icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length,
13410Sstevel@tonic-gate     in_port_t fport)
13420Sstevel@tonic-gate {
13430Sstevel@tonic-gate 	char	*cp;
13440Sstevel@tonic-gate 	mblk_t	*mp;
13450Sstevel@tonic-gate 	struct T_bind_req *tbr;
13460Sstevel@tonic-gate 	ipa_conn_t	*ac;
13470Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
13480Sstevel@tonic-gate 	sin_t		*sin;
13490Sstevel@tonic-gate 	sin6_t		*sin6;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
13525240Snordmark 	ASSERT(RW_LOCK_HELD(&icmp->icmp_rwlock));
13530Sstevel@tonic-gate 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
13540Sstevel@tonic-gate 	if (mp == NULL)
13550Sstevel@tonic-gate 		return (NULL);
13560Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
13570Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
13580Sstevel@tonic-gate 	tbr->PRIM_type = bind_prim;
13590Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (*tbr);
13600Sstevel@tonic-gate 	tbr->CONIND_number = 0;
13610Sstevel@tonic-gate 	tbr->ADDR_length = addr_length;
13620Sstevel@tonic-gate 	cp = (char *)&tbr[1];
13630Sstevel@tonic-gate 	switch (addr_length) {
13640Sstevel@tonic-gate 	case sizeof (ipa_conn_t):
13650Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
13660Sstevel@tonic-gate 		/* Append a request for an IRE */
13670Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13680Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13690Sstevel@tonic-gate 			freemsg(mp);
13700Sstevel@tonic-gate 			return (NULL);
13710Sstevel@tonic-gate 		}
13720Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13730Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
13760Sstevel@tonic-gate 		ac = (ipa_conn_t *)cp;
13770Sstevel@tonic-gate 		ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src);
13780Sstevel@tonic-gate 		ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst);
13790Sstevel@tonic-gate 		ac->ac_fport = fport;
13800Sstevel@tonic-gate 		ac->ac_lport = 0;
13810Sstevel@tonic-gate 		break;
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	case sizeof (ipa6_conn_t):
13840Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
13850Sstevel@tonic-gate 		/* Append a request for an IRE */
13860Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
13870Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
13880Sstevel@tonic-gate 			freemsg(mp);
13890Sstevel@tonic-gate 			return (NULL);
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
13920Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
13950Sstevel@tonic-gate 		ac6 = (ipa6_conn_t *)cp;
13960Sstevel@tonic-gate 		ac6->ac6_laddr = icmp->icmp_v6src;
13970Sstevel@tonic-gate 		ac6->ac6_faddr = icmp->icmp_v6dst;
13980Sstevel@tonic-gate 		ac6->ac6_fport = fport;
13990Sstevel@tonic-gate 		ac6->ac6_lport = 0;
14000Sstevel@tonic-gate 		break;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	case sizeof (sin_t):
14030Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
14040Sstevel@tonic-gate 		/* Append a request for an IRE */
14050Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
14060Sstevel@tonic-gate 		if (!mp->b_cont) {
14070Sstevel@tonic-gate 			freemsg(mp);
14080Sstevel@tonic-gate 			return (NULL);
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
14110Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 		sin = (sin_t *)cp;
14140Sstevel@tonic-gate 		*sin = sin_null;
14150Sstevel@tonic-gate 		sin->sin_family = AF_INET;
14160Sstevel@tonic-gate 		sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
14170Sstevel@tonic-gate 		break;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	case sizeof (sin6_t):
14200Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
14210Sstevel@tonic-gate 		/* Append a request for an IRE */
14220Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
14230Sstevel@tonic-gate 		if (!mp->b_cont) {
14240Sstevel@tonic-gate 			freemsg(mp);
14250Sstevel@tonic-gate 			return (NULL);
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
14280Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
14310Sstevel@tonic-gate 		*sin6 = sin6_null;
14320Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
14330Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_bound_v6src;
14340Sstevel@tonic-gate 		break;
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 	/* Add protocol number to end */
14370Sstevel@tonic-gate 	cp[addr_length] = icmp->icmp_proto;
14380Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
14390Sstevel@tonic-gate 	return (mp);
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14425240Snordmark /* For /dev/icmp aka AF_INET open */
14435240Snordmark static int
14445240Snordmark icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
14451676Sjpk {
14465240Snordmark 	return (icmp_open(q, devp, flag, sflag, credp, B_FALSE));
14475240Snordmark }
14485240Snordmark 
14495240Snordmark /* For /dev/icmp6 aka AF_INET6 open */
14505240Snordmark static int
14515240Snordmark icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
14525240Snordmark {
14535240Snordmark 	return (icmp_open(q, devp, flag, sflag, credp, B_TRUE));
14541676Sjpk }
14551676Sjpk 
14560Sstevel@tonic-gate /*
14570Sstevel@tonic-gate  * This is the open routine for icmp.  It allocates a icmp_t structure for
14580Sstevel@tonic-gate  * the stream and, on the first open of the module, creates an ND table.
14590Sstevel@tonic-gate  */
14605240Snordmark /*ARGSUSED2*/
14610Sstevel@tonic-gate static int
14625240Snordmark icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
14635240Snordmark     boolean_t isv6)
14640Sstevel@tonic-gate {
14650Sstevel@tonic-gate 	int	err;
14660Sstevel@tonic-gate 	icmp_t	*icmp;
14675240Snordmark 	conn_t *connp;
14685240Snordmark 	dev_t	conn_dev;
14695240Snordmark 	zoneid_t zoneid;
14703448Sdh155122 	netstack_t *ns;
14713448Sdh155122 	icmp_stack_t *is;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	/* If the stream is already open, return immediately. */
14740Sstevel@tonic-gate 	if (q->q_ptr != NULL)
14750Sstevel@tonic-gate 		return (0);
14760Sstevel@tonic-gate 
14775240Snordmark 	if (sflag == MODOPEN)
14780Sstevel@tonic-gate 		return (EINVAL);
14790Sstevel@tonic-gate 
14803448Sdh155122 	ns = netstack_find_by_cred(credp);
14813448Sdh155122 	ASSERT(ns != NULL);
14823448Sdh155122 	is = ns->netstack_icmp;
14833448Sdh155122 	ASSERT(is != NULL);
14843448Sdh155122 
14853448Sdh155122 	/*
14863448Sdh155122 	 * For exclusive stacks we set the zoneid to zero
14873448Sdh155122 	 * to make ICMP operate as if in the global zone.
14883448Sdh155122 	 */
14895240Snordmark 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
14903448Sdh155122 		zoneid = GLOBAL_ZONEID;
14913448Sdh155122 	else
14923448Sdh155122 		zoneid = crgetzoneid(credp);
14933448Sdh155122 
14945240Snordmark 	if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) {
14955240Snordmark 		netstack_rele(ns);
14965240Snordmark 		return (EBUSY);
14973448Sdh155122 	}
14985240Snordmark 	*devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
14995240Snordmark 
15005240Snordmark 	connp = ipcl_conn_create(IPCL_RAWIPCONN, KM_SLEEP, ns);
15015240Snordmark 	connp->conn_dev = conn_dev;
15025240Snordmark 	icmp = connp->conn_icmp;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15055240Snordmark 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
15065240Snordmark 	 * done by netstack_find_by_cred()
15075240Snordmark 	 */
15085240Snordmark 	netstack_rele(ns);
15095240Snordmark 
15105240Snordmark 	/*
15115240Snordmark 	 * Initialize the icmp_t structure for this stream.
15120Sstevel@tonic-gate 	 */
15135240Snordmark 	q->q_ptr = connp;
15145240Snordmark 	WR(q)->q_ptr = connp;
15155240Snordmark 	connp->conn_rq = q;
15165240Snordmark 	connp->conn_wq = WR(q);
15175240Snordmark 
15185240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
15195240Snordmark 	ASSERT(connp->conn_ulp == IPPROTO_ICMP);
15205240Snordmark 	ASSERT(connp->conn_icmp == icmp);
15215240Snordmark 	ASSERT(icmp->icmp_connp == connp);
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/* Set the initial state of the stream and the privilege status. */
15240Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
15255240Snordmark 	if (isv6) {
15260Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV6_VERSION;
15270Sstevel@tonic-gate 		icmp->icmp_family = AF_INET6;
15285240Snordmark 		connp->conn_ulp = IPPROTO_ICMPV6;
15290Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
15300Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMPV6;
15310Sstevel@tonic-gate 		icmp->icmp_checksum_off = 2;	/* Offset for icmp6_cksum */
15320Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IPV6_HDR_LEN;
15333448Sdh155122 		icmp->icmp_ttl = (uint8_t)is->is_ipv6_hoplimit;
15345240Snordmark 		connp->conn_af_isv6 = B_TRUE;
15355240Snordmark 		connp->conn_flags |= IPCL_ISV6;
15360Sstevel@tonic-gate 	} else {
15370Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV4_VERSION;
15380Sstevel@tonic-gate 		icmp->icmp_family = AF_INET;
15390Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
15400Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMP;
15410Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH;
15423448Sdh155122 		icmp->icmp_ttl = (uint8_t)is->is_ipv4_ttl;
15435240Snordmark 		connp->conn_af_isv6 = B_FALSE;
15445240Snordmark 		connp->conn_flags &= ~IPCL_ISV6;
15450Sstevel@tonic-gate 	}
15465240Snordmark 	icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
15475240Snordmark 	icmp->icmp_pending_op = -1;
15485240Snordmark 	connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
15495240Snordmark 	connp->conn_zoneid = zoneid;
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	/*
15525240Snordmark 	 * If the caller has the process-wide flag set, then default to MAC
15535240Snordmark 	 * exempt mode.  This allows read-down to unlabeled hosts.
15540Sstevel@tonic-gate 	 */
15555240Snordmark 	if (getpflags(NET_MAC_AWARE, credp) != 0)
15565240Snordmark 		icmp->icmp_mac_exempt = B_TRUE;
15575240Snordmark 
15585240Snordmark 	connp->conn_ulp_labeled = is_system_labeled();
15595240Snordmark 
15605240Snordmark 	icmp->icmp_is = is;
15615240Snordmark 
15625240Snordmark 	q->q_hiwat = is->is_recv_hiwat;
15633448Sdh155122 	WR(q)->q_hiwat = is->is_xmit_hiwat;
15643448Sdh155122 	WR(q)->q_lowat = is->is_xmit_lowat;
15655240Snordmark 
15665240Snordmark 	connp->conn_recv = icmp_input;
15675240Snordmark 	crhold(credp);
15685240Snordmark 	connp->conn_cred = credp;
15695240Snordmark 
15705240Snordmark 	mutex_enter(&connp->conn_lock);
15715240Snordmark 	connp->conn_state_flags &= ~CONN_INCIPIENT;
15725240Snordmark 	mutex_exit(&connp->conn_lock);
15735240Snordmark 
15745240Snordmark 	qprocson(q);
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
15770Sstevel@tonic-gate 		/* Build initial header template for transmit */
15785240Snordmark 		if ((err = icmp_build_hdrs(icmp)) != 0) {
15795240Snordmark 			rw_exit(&icmp->icmp_rwlock);
15805240Snordmark 			qprocsoff(q);
15815240Snordmark 			ipcl_conn_destroy(connp);
15825240Snordmark 			return (err);
15835240Snordmark 		}
15840Sstevel@tonic-gate 	}
15855240Snordmark 	rw_exit(&icmp->icmp_rwlock);
15865240Snordmark 
15870Sstevel@tonic-gate 	/* Set the Stream head write offset. */
15883448Sdh155122 	(void) mi_set_sth_wroff(q,
15893448Sdh155122 	    icmp->icmp_max_hdr_len + is->is_wroff_extra);
15900Sstevel@tonic-gate 	(void) mi_set_sth_hiwat(q, q->q_hiwat);
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	return (0);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*
15960Sstevel@tonic-gate  * Which ICMP options OK to set through T_UNITDATA_REQ...
15970Sstevel@tonic-gate  */
15980Sstevel@tonic-gate /* ARGSUSED */
15990Sstevel@tonic-gate static boolean_t
16000Sstevel@tonic-gate icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
16010Sstevel@tonic-gate {
16020Sstevel@tonic-gate 	return (B_TRUE);
16030Sstevel@tonic-gate }
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate /*
16060Sstevel@tonic-gate  * This routine gets default values of certain options whose default
16070Sstevel@tonic-gate  * values are maintained by protcol specific code
16080Sstevel@tonic-gate  */
16090Sstevel@tonic-gate /* ARGSUSED */
16100Sstevel@tonic-gate int
16110Sstevel@tonic-gate icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr)
16120Sstevel@tonic-gate {
16135240Snordmark 	icmp_t *icmp = Q_TO_ICMP(q);
16143448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
16150Sstevel@tonic-gate 	int *i1 = (int *)ptr;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	switch (level) {
16180Sstevel@tonic-gate 	case IPPROTO_IP:
16190Sstevel@tonic-gate 		switch (name) {
16200Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
16210Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
16220Sstevel@tonic-gate 			return (sizeof (uchar_t));
16230Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
16240Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
16250Sstevel@tonic-gate 			return (sizeof (uchar_t));
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 		break;
16280Sstevel@tonic-gate 	case IPPROTO_IPV6:
16290Sstevel@tonic-gate 		switch (name) {
16300Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
16310Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_TTL;
16320Sstevel@tonic-gate 			return (sizeof (int));
16330Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
16340Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
16350Sstevel@tonic-gate 			return (sizeof (int));
16360Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
16373448Sdh155122 			*i1 = is->is_ipv6_hoplimit;
16380Sstevel@tonic-gate 			return (sizeof (int));
16390Sstevel@tonic-gate 		}
16400Sstevel@tonic-gate 		break;
16410Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
16420Sstevel@tonic-gate 		switch (name) {
16430Sstevel@tonic-gate 		case ICMP6_FILTER:
16440Sstevel@tonic-gate 			/* Make it look like "pass all" */
16450Sstevel@tonic-gate 			ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
16460Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
16470Sstevel@tonic-gate 		}
16480Sstevel@tonic-gate 		break;
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate 	return (-1);
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
16550Sstevel@tonic-gate  * It returns the size of the option retrieved.
16560Sstevel@tonic-gate  */
16570Sstevel@tonic-gate int
16585240Snordmark icmp_opt_get_locked(queue_t *q, int level, int name, uchar_t *ptr)
16590Sstevel@tonic-gate {
16605240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
16615240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
16625240Snordmark 	icmp_stack_t *is = icmp->icmp_is;
16630Sstevel@tonic-gate 	int	*i1 = (int *)ptr;
16640Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &icmp->icmp_sticky_ipp;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	switch (level) {
16670Sstevel@tonic-gate 	case SOL_SOCKET:
16680Sstevel@tonic-gate 		switch (name) {
16690Sstevel@tonic-gate 		case SO_DEBUG:
16700Sstevel@tonic-gate 			*i1 = icmp->icmp_debug;
16710Sstevel@tonic-gate 			break;
16720Sstevel@tonic-gate 		case SO_TYPE:
16730Sstevel@tonic-gate 			*i1 = SOCK_RAW;
16740Sstevel@tonic-gate 			break;
16750Sstevel@tonic-gate 		case SO_PROTOTYPE:
16760Sstevel@tonic-gate 			*i1 = icmp->icmp_proto;
16770Sstevel@tonic-gate 			break;
16780Sstevel@tonic-gate 		case SO_REUSEADDR:
16790Sstevel@tonic-gate 			*i1 = icmp->icmp_reuseaddr;
16800Sstevel@tonic-gate 			break;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 		/*
16830Sstevel@tonic-gate 		 * The following three items are available here,
16840Sstevel@tonic-gate 		 * but are only meaningful to IP.
16850Sstevel@tonic-gate 		 */
16860Sstevel@tonic-gate 		case SO_DONTROUTE:
16870Sstevel@tonic-gate 			*i1 = icmp->icmp_dontroute;
16880Sstevel@tonic-gate 			break;
16890Sstevel@tonic-gate 		case SO_USELOOPBACK:
16900Sstevel@tonic-gate 			*i1 = icmp->icmp_useloopback;
16910Sstevel@tonic-gate 			break;
16920Sstevel@tonic-gate 		case SO_BROADCAST:
16930Sstevel@tonic-gate 			*i1 = icmp->icmp_broadcast;
16940Sstevel@tonic-gate 			break;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 		case SO_SNDBUF:
16970Sstevel@tonic-gate 			ASSERT(q->q_hiwat <= INT_MAX);
16980Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
16990Sstevel@tonic-gate 			break;
17000Sstevel@tonic-gate 		case SO_RCVBUF:
17010Sstevel@tonic-gate 			ASSERT(RD(q)->q_hiwat <= INT_MAX);
17020Sstevel@tonic-gate 			*i1 = (int)RD(q)->q_hiwat;
17030Sstevel@tonic-gate 			break;
17040Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
17050Sstevel@tonic-gate 			*i1 = icmp->icmp_dgram_errind;
17060Sstevel@tonic-gate 			break;
17071673Sgt145670 		case SO_TIMESTAMP:
17081673Sgt145670 			*i1 = icmp->icmp_timestamp;
17091673Sgt145670 			break;
17101676Sjpk 		case SO_MAC_EXEMPT:
17111676Sjpk 			*i1 = icmp->icmp_mac_exempt;
17121676Sjpk 			break;
17133388Skcpoon 		case SO_DOMAIN:
17143388Skcpoon 			*i1 = icmp->icmp_family;
17153388Skcpoon 			break;
17163388Skcpoon 
17170Sstevel@tonic-gate 		/*
17182263Ssommerfe 		 * Following four not meaningful for icmp
17190Sstevel@tonic-gate 		 * Action is same as "default" to which we fallthrough
17200Sstevel@tonic-gate 		 * so we keep them in comments.
17210Sstevel@tonic-gate 		 * case SO_LINGER:
17220Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
17230Sstevel@tonic-gate 		 * case SO_OOBINLINE:
17242263Ssommerfe 		 * case SO_ALLZONES:
17250Sstevel@tonic-gate 		 */
17260Sstevel@tonic-gate 		default:
17270Sstevel@tonic-gate 			return (-1);
17280Sstevel@tonic-gate 		}
17290Sstevel@tonic-gate 		break;
17300Sstevel@tonic-gate 	case IPPROTO_IP:
17310Sstevel@tonic-gate 		/*
17320Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
17330Sstevel@tonic-gate 		 */
17340Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET)
17350Sstevel@tonic-gate 			return (-1);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		switch (name) {
17380Sstevel@tonic-gate 		case IP_OPTIONS:
17390Sstevel@tonic-gate 		case T_IP_OPTIONS:
17400Sstevel@tonic-gate 			/* Options are passed up with each packet */
17410Sstevel@tonic-gate 			return (0);
17420Sstevel@tonic-gate 		case IP_HDRINCL:
17430Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_hdrincl;
17440Sstevel@tonic-gate 			break;
17450Sstevel@tonic-gate 		case IP_TOS:
17460Sstevel@tonic-gate 		case T_IP_TOS:
17470Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_type_of_service;
17480Sstevel@tonic-gate 			break;
17490Sstevel@tonic-gate 		case IP_TTL:
17500Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_ttl;
17510Sstevel@tonic-gate 			break;
17520Sstevel@tonic-gate 		case IP_MULTICAST_IF:
17530Sstevel@tonic-gate 			/* 0 address if not set */
17540Sstevel@tonic-gate 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
17550Sstevel@tonic-gate 			return (sizeof (ipaddr_t));
17560Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
17570Sstevel@tonic-gate 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
17580Sstevel@tonic-gate 			return (sizeof (uchar_t));
17590Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
17605240Snordmark 			*ptr = connp->conn_multicast_loop;
17610Sstevel@tonic-gate 			return (sizeof (uint8_t));
17620Sstevel@tonic-gate 		case IP_BOUND_IF:
17630Sstevel@tonic-gate 			/* Zero if not set */
17640Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
17650Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17660Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
17670Sstevel@tonic-gate 			*ptr = icmp->icmp_unspec_source;
17680Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17690Sstevel@tonic-gate 		case IP_RECVIF:
17700Sstevel@tonic-gate 			*ptr = icmp->icmp_recvif;
17710Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
17723318Srshoaib 		case IP_RECVPKTINFO:
17733318Srshoaib 			/*
17743318Srshoaib 			 * This also handles IP_PKTINFO.
17753318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
17763318Srshoaib 			 * Differentiation is based on the size of the argument
17773318Srshoaib 			 * passed in.
17783318Srshoaib 			 * This option is handled in IP which will return an
17793318Srshoaib 			 * error for IP_PKTINFO as it's not supported as a
17803318Srshoaib 			 * sticky option.
17813318Srshoaib 			 */
17823318Srshoaib 			return (-EINVAL);
17830Sstevel@tonic-gate 		/*
17840Sstevel@tonic-gate 		 * Cannot "get" the value of following options
17850Sstevel@tonic-gate 		 * at this level. Action is same as "default" to
17860Sstevel@tonic-gate 		 * which we fallthrough so we keep them in comments.
17870Sstevel@tonic-gate 		 *
17880Sstevel@tonic-gate 		 * case IP_ADD_MEMBERSHIP:
17890Sstevel@tonic-gate 		 * case IP_DROP_MEMBERSHIP:
17900Sstevel@tonic-gate 		 * case IP_BLOCK_SOURCE:
17910Sstevel@tonic-gate 		 * case IP_UNBLOCK_SOURCE:
17920Sstevel@tonic-gate 		 * case IP_ADD_SOURCE_MEMBERSHIP:
17930Sstevel@tonic-gate 		 * case IP_DROP_SOURCE_MEMBERSHIP:
17940Sstevel@tonic-gate 		 * case MCAST_JOIN_GROUP:
17950Sstevel@tonic-gate 		 * case MCAST_LEAVE_GROUP:
17960Sstevel@tonic-gate 		 * case MCAST_BLOCK_SOURCE:
17970Sstevel@tonic-gate 		 * case MCAST_UNBLOCK_SOURCE:
17980Sstevel@tonic-gate 		 * case MCAST_JOIN_SOURCE_GROUP:
17990Sstevel@tonic-gate 		 * case MCAST_LEAVE_SOURCE_GROUP:
18000Sstevel@tonic-gate 		 * case MRT_INIT:
18010Sstevel@tonic-gate 		 * case MRT_DONE:
18020Sstevel@tonic-gate 		 * case MRT_ADD_VIF:
18030Sstevel@tonic-gate 		 * case MRT_DEL_VIF:
18040Sstevel@tonic-gate 		 * case MRT_ADD_MFC:
18050Sstevel@tonic-gate 		 * case MRT_DEL_MFC:
18060Sstevel@tonic-gate 		 * case MRT_VERSION:
18070Sstevel@tonic-gate 		 * case MRT_ASSERT:
18080Sstevel@tonic-gate 		 * case IP_SEC_OPT:
18090Sstevel@tonic-gate 		 * case IP_DONTFAILOVER_IF:
18101663Spriyanka 		 * case IP_NEXTHOP:
18110Sstevel@tonic-gate 		 */
18120Sstevel@tonic-gate 		default:
18130Sstevel@tonic-gate 			return (-1);
18140Sstevel@tonic-gate 		}
18150Sstevel@tonic-gate 		break;
18160Sstevel@tonic-gate 	case IPPROTO_IPV6:
18170Sstevel@tonic-gate 		/*
18180Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
18190Sstevel@tonic-gate 		 */
18200Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
18210Sstevel@tonic-gate 			return (-1);
18220Sstevel@tonic-gate 		switch (name) {
18230Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
18240Sstevel@tonic-gate 			*i1 = (unsigned int)icmp->icmp_ttl;
18250Sstevel@tonic-gate 			break;
18260Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
18270Sstevel@tonic-gate 			/* 0 index if not set */
18280Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_if_index;
18290Sstevel@tonic-gate 			break;
18300Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
18310Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_ttl;
18320Sstevel@tonic-gate 			break;
18330Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
18345240Snordmark 			*i1 = connp->conn_multicast_loop;
18350Sstevel@tonic-gate 			break;
18360Sstevel@tonic-gate 		case IPV6_BOUND_IF:
18370Sstevel@tonic-gate 			/* Zero if not set */
18380Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
18390Sstevel@tonic-gate 			break;
18400Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
18410Sstevel@tonic-gate 			*i1 = icmp->icmp_unspec_source;
18420Sstevel@tonic-gate 			break;
18430Sstevel@tonic-gate 		case IPV6_CHECKSUM:
18440Sstevel@tonic-gate 			/*
18450Sstevel@tonic-gate 			 * Return offset or -1 if no checksum offset.
18460Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6
18470Sstevel@tonic-gate 			 */
18480Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
18490Sstevel@tonic-gate 				return (-1);
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 			if (icmp->icmp_raw_checksum) {
18520Sstevel@tonic-gate 				*i1 = icmp->icmp_checksum_off;
18530Sstevel@tonic-gate 			} else {
18540Sstevel@tonic-gate 				*i1 = -1;
18550Sstevel@tonic-gate 			}
18560Sstevel@tonic-gate 			break;
18570Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
18580Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
18590Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
18600Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
18610Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
18620Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
18630Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
18640Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
18650Sstevel@tonic-gate 			/* cannot "get" the value for these */
18660Sstevel@tonic-gate 			return (-1);
18670Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
18683318Srshoaib 			*i1 = icmp->icmp_ip_recvpktinfo;
18690Sstevel@tonic-gate 			break;
18700Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
18710Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvtclass;
18720Sstevel@tonic-gate 			break;
18730Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
18740Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpathmtu;
18750Sstevel@tonic-gate 			break;
18760Sstevel@tonic-gate 		case IPV6_V6ONLY:
18770Sstevel@tonic-gate 			*i1 = 1;
18780Sstevel@tonic-gate 			break;
18790Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
18800Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhoplimit;
18810Sstevel@tonic-gate 			break;
18820Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
18830Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhopopts;
18840Sstevel@tonic-gate 			break;
18850Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
18860Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvdstopts;
18870Sstevel@tonic-gate 			break;
18880Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
18890Sstevel@tonic-gate 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
18900Sstevel@tonic-gate 			break;
18910Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
18920Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
18930Sstevel@tonic-gate 			break;
18940Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
18950Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrthdr;
18960Sstevel@tonic-gate 			break;
18970Sstevel@tonic-gate 		case IPV6_PKTINFO: {
18980Sstevel@tonic-gate 			/* XXX assumes that caller has room for max size! */
18990Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
19020Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
19030Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
19040Sstevel@tonic-gate 			else
19050Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
19060Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
19070Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
19080Sstevel@tonic-gate 			else
19090Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
19100Sstevel@tonic-gate 			return (sizeof (struct in6_pktinfo));
19110Sstevel@tonic-gate 		}
19120Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
19130Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)ptr;
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
19160Sstevel@tonic-gate 				return (0);
19170Sstevel@tonic-gate 			*sin6 = sin6_null;
19180Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
19190Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
19200Sstevel@tonic-gate 			return (sizeof (sin6_t));
19210Sstevel@tonic-gate 		}
19220Sstevel@tonic-gate 		case IPV6_HOPOPTS:
19230Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
19240Sstevel@tonic-gate 				return (0);
19251676Sjpk 			if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6)
19261676Sjpk 				return (0);
19271676Sjpk 			bcopy((char *)ipp->ipp_hopopts +
19281676Sjpk 			    icmp->icmp_label_len_v6, ptr,
19291676Sjpk 			    ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
19301676Sjpk 			if (icmp->icmp_label_len_v6 > 0) {
19311676Sjpk 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
19321676Sjpk 				ptr[1] = (ipp->ipp_hopoptslen -
19331676Sjpk 				    icmp->icmp_label_len_v6 + 7) / 8 - 1;
19341676Sjpk 			}
19351676Sjpk 			return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
19360Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
19370Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
19380Sstevel@tonic-gate 				return (0);
19390Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
19400Sstevel@tonic-gate 			return (ipp->ipp_rtdstoptslen);
19410Sstevel@tonic-gate 		case IPV6_RTHDR:
19420Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
19430Sstevel@tonic-gate 				return (0);
19440Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
19450Sstevel@tonic-gate 			return (ipp->ipp_rthdrlen);
19460Sstevel@tonic-gate 		case IPV6_DSTOPTS:
19470Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
19480Sstevel@tonic-gate 				return (0);
19490Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
19500Sstevel@tonic-gate 			return (ipp->ipp_dstoptslen);
19510Sstevel@tonic-gate 		case IPV6_PATHMTU:
19520Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
19530Sstevel@tonic-gate 				return (0);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
19565381Smeem 			    (struct ip6_mtuinfo *)ptr, is->is_netstack));
19570Sstevel@tonic-gate 		case IPV6_TCLASS:
19580Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_TCLASS)
19590Sstevel@tonic-gate 				*i1 = ipp->ipp_tclass;
19600Sstevel@tonic-gate 			else
19610Sstevel@tonic-gate 				*i1 = IPV6_FLOW_TCLASS(
19620Sstevel@tonic-gate 				    IPV6_DEFAULT_VERS_AND_FLOW);
19630Sstevel@tonic-gate 			break;
19640Sstevel@tonic-gate 		default:
19650Sstevel@tonic-gate 			return (-1);
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 		break;
19680Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
19690Sstevel@tonic-gate 		/*
19700Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
19710Sstevel@tonic-gate 		 */
19720Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
19730Sstevel@tonic-gate 			return (-1);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
19760Sstevel@tonic-gate 			return (-1);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 		switch (name) {
19790Sstevel@tonic-gate 		case ICMP6_FILTER:
19800Sstevel@tonic-gate 			if (icmp->icmp_filter == NULL) {
19810Sstevel@tonic-gate 				/* Make it look like "pass all" */
19820Sstevel@tonic-gate 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
19830Sstevel@tonic-gate 			} else {
19840Sstevel@tonic-gate 				(void) bcopy(icmp->icmp_filter, ptr,
19850Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
19860Sstevel@tonic-gate 			}
19870Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
19880Sstevel@tonic-gate 		default:
19890Sstevel@tonic-gate 			return (-1);
19900Sstevel@tonic-gate 		}
19910Sstevel@tonic-gate 	default:
19920Sstevel@tonic-gate 		return (-1);
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 	return (sizeof (int));
19950Sstevel@tonic-gate }
19960Sstevel@tonic-gate 
19975240Snordmark /*
19985240Snordmark  * This routine retrieves the current status of socket options.
19995240Snordmark  * It returns the size of the option retrieved.
20005240Snordmark  */
20015240Snordmark int
20025240Snordmark icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
20035240Snordmark {
20045240Snordmark 	icmp_t  *icmp = Q_TO_ICMP(q);
20055240Snordmark 	int 	err;
20065240Snordmark 
20075240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_READER);
20085240Snordmark 	err = icmp_opt_get_locked(q, level, name, ptr);
20095240Snordmark 	rw_exit(&icmp->icmp_rwlock);
20105240Snordmark 	return (err);
20115240Snordmark }
20125240Snordmark 
20135240Snordmark 
20140Sstevel@tonic-gate /* This routine sets socket options. */
20150Sstevel@tonic-gate /* ARGSUSED */
20160Sstevel@tonic-gate int
20175240Snordmark icmp_opt_set_locked(queue_t *q, uint_t optset_context, int level, int name,
20180Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
20190Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
20200Sstevel@tonic-gate {
20215240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
20225240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
20233448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
20240Sstevel@tonic-gate 	int	*i1 = (int *)invalp;
20250Sstevel@tonic-gate 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
20260Sstevel@tonic-gate 	boolean_t checkonly;
20270Sstevel@tonic-gate 	int	error;
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	switch (optset_context) {
20300Sstevel@tonic-gate 	case SETFN_OPTCOM_CHECKONLY:
20310Sstevel@tonic-gate 		checkonly = B_TRUE;
20320Sstevel@tonic-gate 		/*
20330Sstevel@tonic-gate 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
20340Sstevel@tonic-gate 		 * inlen != 0 implies value supplied and
20350Sstevel@tonic-gate 		 * 	we have to "pretend" to set it.
20360Sstevel@tonic-gate 		 * inlen == 0 implies that there is no
20370Sstevel@tonic-gate 		 * 	value part in T_CHECK request and just validation
20380Sstevel@tonic-gate 		 * done elsewhere should be enough, we just return here.
20390Sstevel@tonic-gate 		 */
20400Sstevel@tonic-gate 		if (inlen == 0) {
20410Sstevel@tonic-gate 			*outlenp = 0;
20420Sstevel@tonic-gate 			return (0);
20430Sstevel@tonic-gate 		}
20440Sstevel@tonic-gate 		break;
20450Sstevel@tonic-gate 	case SETFN_OPTCOM_NEGOTIATE:
20460Sstevel@tonic-gate 		checkonly = B_FALSE;
20470Sstevel@tonic-gate 		break;
20480Sstevel@tonic-gate 	case SETFN_UD_NEGOTIATE:
20490Sstevel@tonic-gate 	case SETFN_CONN_NEGOTIATE:
20500Sstevel@tonic-gate 		checkonly = B_FALSE;
20510Sstevel@tonic-gate 		/*
20520Sstevel@tonic-gate 		 * Negotiating local and "association-related" options
20530Sstevel@tonic-gate 		 * through T_UNITDATA_REQ.
20540Sstevel@tonic-gate 		 *
20550Sstevel@tonic-gate 		 * Following routine can filter out ones we do not
20560Sstevel@tonic-gate 		 * want to be "set" this way.
20570Sstevel@tonic-gate 		 */
20580Sstevel@tonic-gate 		if (!icmp_opt_allow_udr_set(level, name)) {
20590Sstevel@tonic-gate 			*outlenp = 0;
20600Sstevel@tonic-gate 			return (EINVAL);
20610Sstevel@tonic-gate 		}
20620Sstevel@tonic-gate 		break;
20630Sstevel@tonic-gate 	default:
20640Sstevel@tonic-gate 		/*
20650Sstevel@tonic-gate 		 * We should never get here
20660Sstevel@tonic-gate 		 */
20670Sstevel@tonic-gate 		*outlenp = 0;
20680Sstevel@tonic-gate 		return (EINVAL);
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
20720Sstevel@tonic-gate 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	/*
20750Sstevel@tonic-gate 	 * For fixed length options, no sanity check
20760Sstevel@tonic-gate 	 * of passed in length is done. It is assumed *_optcom_req()
20770Sstevel@tonic-gate 	 * routines do the right thing.
20780Sstevel@tonic-gate 	 */
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	switch (level) {
20810Sstevel@tonic-gate 	case SOL_SOCKET:
20820Sstevel@tonic-gate 		switch (name) {
20830Sstevel@tonic-gate 		case SO_DEBUG:
20840Sstevel@tonic-gate 			if (!checkonly)
20850Sstevel@tonic-gate 				icmp->icmp_debug = onoff;
20860Sstevel@tonic-gate 			break;
20870Sstevel@tonic-gate 		case SO_PROTOTYPE:
20880Sstevel@tonic-gate 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
20890Sstevel@tonic-gate 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
20900Sstevel@tonic-gate 			    secpolicy_net_rawaccess(cr) != 0) {
20910Sstevel@tonic-gate 				*outlenp = 0;
20920Sstevel@tonic-gate 				return (EACCES);
20930Sstevel@tonic-gate 			}
20940Sstevel@tonic-gate 			/* Can't use IPPROTO_RAW with IPv6 */
20950Sstevel@tonic-gate 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
20960Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET6) {
20970Sstevel@tonic-gate 				*outlenp = 0;
20980Sstevel@tonic-gate 				return (EPROTONOSUPPORT);
20990Sstevel@tonic-gate 			}
21000Sstevel@tonic-gate 			if (checkonly) {
21010Sstevel@tonic-gate 				/* T_CHECK case */
21020Sstevel@tonic-gate 				*(int *)outvalp = (*i1 & 0xFF);
21030Sstevel@tonic-gate 				break;
21040Sstevel@tonic-gate 			}
21050Sstevel@tonic-gate 			icmp->icmp_proto = *i1 & 0xFF;
21060Sstevel@tonic-gate 			if ((icmp->icmp_proto == IPPROTO_RAW ||
21070Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_IGMP) &&
21080Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET)
21090Sstevel@tonic-gate 				icmp->icmp_hdrincl = 1;
21100Sstevel@tonic-gate 			else
21110Sstevel@tonic-gate 				icmp->icmp_hdrincl = 0;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6 &&
21140Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
21150Sstevel@tonic-gate 				/* Set offset for icmp6_cksum */
21160Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
21170Sstevel@tonic-gate 				icmp->icmp_checksum_off = 2;
21180Sstevel@tonic-gate 			}
21190Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_UDP ||
21200Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_TCP ||
21210Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_SCTP) {
21220Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 1;
21230Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields |=
21240Sstevel@tonic-gate 				    IPPF_NO_CKSUM;
21250Sstevel@tonic-gate 			} else {
21260Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 0;
21270Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields &=
21280Sstevel@tonic-gate 				    ~IPPF_NO_CKSUM;
21290Sstevel@tonic-gate 			}
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 			if (icmp->icmp_filter != NULL &&
21320Sstevel@tonic-gate 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
21330Sstevel@tonic-gate 				kmem_free(icmp->icmp_filter,
21340Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
21350Sstevel@tonic-gate 				icmp->icmp_filter = NULL;
21360Sstevel@tonic-gate 			}
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 			/* Rebuild the header template */
21395240Snordmark 			error = icmp_build_hdrs(icmp);
21400Sstevel@tonic-gate 			if (error != 0) {
21410Sstevel@tonic-gate 				*outlenp = 0;
21420Sstevel@tonic-gate 				return (error);
21430Sstevel@tonic-gate 			}
21440Sstevel@tonic-gate 
2145409Skcpoon 			/*
2146409Skcpoon 			 * For SCTP, we don't use icmp_bind_proto() for
2147409Skcpoon 			 * raw socket binding.  Note that we do not need
2148409Skcpoon 			 * to set *outlenp.
21495240Snordmark 			 * FIXME: how does SCTP work?
2150409Skcpoon 			 */
2151409Skcpoon 			if (icmp->icmp_proto == IPPROTO_SCTP)
2152409Skcpoon 				return (0);
2153409Skcpoon 
21540Sstevel@tonic-gate 			*outlenp = sizeof (int);
21550Sstevel@tonic-gate 			*(int *)outvalp = *i1 & 0xFF;
21565240Snordmark 
21575240Snordmark 			/* Drop lock across the bind operation */
21585240Snordmark 			rw_exit(&icmp->icmp_rwlock);
21595240Snordmark 			icmp_bind_proto(q);
21605240Snordmark 			rw_enter(&icmp->icmp_rwlock, RW_WRITER);
21610Sstevel@tonic-gate 			return (0);
21620Sstevel@tonic-gate 		case SO_REUSEADDR:
21630Sstevel@tonic-gate 			if (!checkonly)
21640Sstevel@tonic-gate 				icmp->icmp_reuseaddr = onoff;
21650Sstevel@tonic-gate 			break;
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 		/*
21680Sstevel@tonic-gate 		 * The following three items are available here,
21690Sstevel@tonic-gate 		 * but are only meaningful to IP.
21700Sstevel@tonic-gate 		 */
21710Sstevel@tonic-gate 		case SO_DONTROUTE:
21720Sstevel@tonic-gate 			if (!checkonly)
21730Sstevel@tonic-gate 				icmp->icmp_dontroute = onoff;
21740Sstevel@tonic-gate 			break;
21750Sstevel@tonic-gate 		case SO_USELOOPBACK:
21760Sstevel@tonic-gate 			if (!checkonly)
21770Sstevel@tonic-gate 				icmp->icmp_useloopback = onoff;
21780Sstevel@tonic-gate 			break;
21790Sstevel@tonic-gate 		case SO_BROADCAST:
21800Sstevel@tonic-gate 			if (!checkonly)
21810Sstevel@tonic-gate 				icmp->icmp_broadcast = onoff;
21820Sstevel@tonic-gate 			break;
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 		case SO_SNDBUF:
21853448Sdh155122 			if (*i1 > is->is_max_buf) {
21860Sstevel@tonic-gate 				*outlenp = 0;
21870Sstevel@tonic-gate 				return (ENOBUFS);
21880Sstevel@tonic-gate 			}
21890Sstevel@tonic-gate 			if (!checkonly) {
21900Sstevel@tonic-gate 				q->q_hiwat = *i1;
21910Sstevel@tonic-gate 			}
21920Sstevel@tonic-gate 			break;
21930Sstevel@tonic-gate 		case SO_RCVBUF:
21943448Sdh155122 			if (*i1 > is->is_max_buf) {
21950Sstevel@tonic-gate 				*outlenp = 0;
21960Sstevel@tonic-gate 				return (ENOBUFS);
21970Sstevel@tonic-gate 			}
21980Sstevel@tonic-gate 			if (!checkonly) {
21990Sstevel@tonic-gate 				RD(q)->q_hiwat = *i1;
22005240Snordmark 				rw_exit(&icmp->icmp_rwlock);
22010Sstevel@tonic-gate 				(void) mi_set_sth_hiwat(RD(q), *i1);
22025240Snordmark 				rw_enter(&icmp->icmp_rwlock, RW_WRITER);
22030Sstevel@tonic-gate 			}
22040Sstevel@tonic-gate 			break;
22050Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
22060Sstevel@tonic-gate 			if (!checkonly)
22070Sstevel@tonic-gate 				icmp->icmp_dgram_errind = onoff;
22080Sstevel@tonic-gate 			break;
22092263Ssommerfe 		case SO_ALLZONES:
22102263Ssommerfe 			/*
22112263Ssommerfe 			 * "soft" error (negative)
22122263Ssommerfe 			 * option not handled at this level
22132263Ssommerfe 			 * Note: Do not modify *outlenp
22142263Ssommerfe 			 */
22152263Ssommerfe 			return (-EINVAL);
22161673Sgt145670 		case SO_TIMESTAMP:
22171673Sgt145670 			if (!checkonly) {
22181673Sgt145670 				icmp->icmp_timestamp = onoff;
22191673Sgt145670 			}
22201673Sgt145670 			break;
22211676Sjpk 		case SO_MAC_EXEMPT:
22221676Sjpk 			if (secpolicy_net_mac_aware(cr) != 0 ||
22231676Sjpk 			    icmp->icmp_state != TS_UNBND)
22241676Sjpk 				return (EACCES);
22251676Sjpk 			if (!checkonly)
22261676Sjpk 				icmp->icmp_mac_exempt = onoff;
22271676Sjpk 			break;
22280Sstevel@tonic-gate 		/*
22290Sstevel@tonic-gate 		 * Following three not meaningful for icmp
22300Sstevel@tonic-gate 		 * Action is same as "default" so we keep them
22310Sstevel@tonic-gate 		 * in comments.
22320Sstevel@tonic-gate 		 * case SO_LINGER:
22330Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
22340Sstevel@tonic-gate 		 * case SO_OOBINLINE:
22350Sstevel@tonic-gate 		 */
22360Sstevel@tonic-gate 		default:
22370Sstevel@tonic-gate 			*outlenp = 0;
22380Sstevel@tonic-gate 			return (EINVAL);
22390Sstevel@tonic-gate 		}
22400Sstevel@tonic-gate 		break;
22410Sstevel@tonic-gate 	case IPPROTO_IP:
22420Sstevel@tonic-gate 		/*
22430Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
22440Sstevel@tonic-gate 		 */
22450Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET) {
22460Sstevel@tonic-gate 			*outlenp = 0;
22470Sstevel@tonic-gate 			return (ENOPROTOOPT);
22480Sstevel@tonic-gate 		}
22490Sstevel@tonic-gate 		switch (name) {
22500Sstevel@tonic-gate 		case IP_OPTIONS:
22510Sstevel@tonic-gate 		case T_IP_OPTIONS:
22520Sstevel@tonic-gate 			/* Save options for use by IP. */
22531676Sjpk 			if ((inlen & 0x3) ||
22541676Sjpk 			    inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) {
22550Sstevel@tonic-gate 				*outlenp = 0;
22560Sstevel@tonic-gate 				return (EINVAL);
22570Sstevel@tonic-gate 			}
22580Sstevel@tonic-gate 			if (checkonly)
22590Sstevel@tonic-gate 				break;
22600Sstevel@tonic-gate 
22611676Sjpk 			if (!tsol_option_set(&icmp->icmp_ip_snd_options,
22621676Sjpk 			    &icmp->icmp_ip_snd_options_len,
22631676Sjpk 			    icmp->icmp_label_len, invalp, inlen)) {
22641676Sjpk 				*outlenp = 0;
22651676Sjpk 				return (ENOMEM);
22660Sstevel@tonic-gate 			}
22671676Sjpk 
22680Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
22690Sstevel@tonic-gate 			    icmp->icmp_ip_snd_options_len;
22705240Snordmark 			rw_exit(&icmp->icmp_rwlock);
22710Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
22725240Snordmark 			    is->is_wroff_extra);
22735240Snordmark 			rw_enter(&icmp->icmp_rwlock, RW_WRITER);
22740Sstevel@tonic-gate 			break;
22750Sstevel@tonic-gate 		case IP_HDRINCL:
22760Sstevel@tonic-gate 			if (!checkonly)
22770Sstevel@tonic-gate 				icmp->icmp_hdrincl = onoff;
22780Sstevel@tonic-gate 			break;
22790Sstevel@tonic-gate 		case IP_TOS:
22800Sstevel@tonic-gate 		case T_IP_TOS:
22810Sstevel@tonic-gate 			if (!checkonly) {
22820Sstevel@tonic-gate 				icmp->icmp_type_of_service = (uint8_t)*i1;
22830Sstevel@tonic-gate 			}
22840Sstevel@tonic-gate 			break;
22850Sstevel@tonic-gate 		case IP_TTL:
22860Sstevel@tonic-gate 			if (!checkonly) {
22870Sstevel@tonic-gate 				icmp->icmp_ttl = (uint8_t)*i1;
22880Sstevel@tonic-gate 			}
22890Sstevel@tonic-gate 			break;
22900Sstevel@tonic-gate 		case IP_MULTICAST_IF:
22910Sstevel@tonic-gate 			/*
22920Sstevel@tonic-gate 			 * TODO should check OPTMGMT reply and undo this if
22930Sstevel@tonic-gate 			 * there is an error.
22940Sstevel@tonic-gate 			 */
22950Sstevel@tonic-gate 			if (!checkonly)
22960Sstevel@tonic-gate 				icmp->icmp_multicast_if_addr = *i1;
22970Sstevel@tonic-gate 			break;
22980Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
22990Sstevel@tonic-gate 			if (!checkonly)
23000Sstevel@tonic-gate 				icmp->icmp_multicast_ttl = *invalp;
23010Sstevel@tonic-gate 			break;
23020Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
23030Sstevel@tonic-gate 			if (!checkonly) {
23045240Snordmark 				connp->conn_multicast_loop =
23050Sstevel@tonic-gate 				    (*invalp == 0) ? 0 : 1;
23060Sstevel@tonic-gate 			}
23070Sstevel@tonic-gate 			break;
23080Sstevel@tonic-gate 		case IP_BOUND_IF:
23090Sstevel@tonic-gate 			if (!checkonly)
23100Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
23110Sstevel@tonic-gate 			break;
23120Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
23130Sstevel@tonic-gate 			if (!checkonly)
23140Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
23150Sstevel@tonic-gate 			break;
23160Sstevel@tonic-gate 		case IP_RECVIF:
23170Sstevel@tonic-gate 			if (!checkonly)
23180Sstevel@tonic-gate 				icmp->icmp_recvif = onoff;
23195240Snordmark 			/*
23205240Snordmark 			 * pass to ip
23215240Snordmark 			 */
23225240Snordmark 			return (-EINVAL);
23233318Srshoaib 		case IP_PKTINFO: {
23243318Srshoaib 			/*
23253318Srshoaib 			 * This also handles IP_RECVPKTINFO.
23263318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
23273318Srshoaib 			 * Differentiation is based on the size of the argument
23283318Srshoaib 			 * passed in.
23293318Srshoaib 			 */
23303318Srshoaib 			struct in_pktinfo *pktinfop;
23313318Srshoaib 			ip4_pkt_t *attr_pktinfop;
23323318Srshoaib 
23333318Srshoaib 			if (checkonly)
23343318Srshoaib 				break;
23353318Srshoaib 
23363318Srshoaib 			if (inlen == sizeof (int)) {
23373318Srshoaib 				/*
23383318Srshoaib 				 * This is IP_RECVPKTINFO option.
23393318Srshoaib 				 * Keep a local copy of wether this option is
23403318Srshoaib 				 * set or not and pass it down to IP for
23413318Srshoaib 				 * processing.
23423318Srshoaib 				 */
23433318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
23443318Srshoaib 				return (-EINVAL);
23453318Srshoaib 			}
23463318Srshoaib 
23473318Srshoaib 
23483318Srshoaib 			if (inlen != sizeof (struct in_pktinfo))
23493318Srshoaib 				return (EINVAL);
23503318Srshoaib 
23513318Srshoaib 			if ((attr_pktinfop = (ip4_pkt_t *)thisdg_attrs)
23523318Srshoaib 			    == NULL) {
23533318Srshoaib 				/*
23543318Srshoaib 				 * sticky option is not supported
23553318Srshoaib 				 */
23563318Srshoaib 				return (EINVAL);
23573318Srshoaib 			}
23583318Srshoaib 
23593318Srshoaib 			pktinfop = (struct in_pktinfo *)invalp;
23603318Srshoaib 
23613318Srshoaib 			/*
23623318Srshoaib 			 * Atleast one of the values should be specified
23633318Srshoaib 			 */
23643318Srshoaib 			if (pktinfop->ipi_ifindex == 0 &&
23653318Srshoaib 			    pktinfop->ipi_spec_dst.s_addr == INADDR_ANY) {
23663318Srshoaib 				return (EINVAL);
23673318Srshoaib 			}
23683318Srshoaib 
23693318Srshoaib 			attr_pktinfop->ip4_addr = pktinfop->ipi_spec_dst.s_addr;
23703318Srshoaib 			attr_pktinfop->ip4_ill_index = pktinfop->ipi_ifindex;
23713318Srshoaib 		}
23723318Srshoaib 			break;
23730Sstevel@tonic-gate 		case IP_ADD_MEMBERSHIP:
23740Sstevel@tonic-gate 		case IP_DROP_MEMBERSHIP:
23750Sstevel@tonic-gate 		case IP_BLOCK_SOURCE:
23760Sstevel@tonic-gate 		case IP_UNBLOCK_SOURCE:
23770Sstevel@tonic-gate 		case IP_ADD_SOURCE_MEMBERSHIP:
23780Sstevel@tonic-gate 		case IP_DROP_SOURCE_MEMBERSHIP:
23790Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
23800Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
23810Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
23820Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
23830Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
23840Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
23850Sstevel@tonic-gate 		case MRT_INIT:
23860Sstevel@tonic-gate 		case MRT_DONE:
23870Sstevel@tonic-gate 		case MRT_ADD_VIF:
23880Sstevel@tonic-gate 		case MRT_DEL_VIF:
23890Sstevel@tonic-gate 		case MRT_ADD_MFC:
23900Sstevel@tonic-gate 		case MRT_DEL_MFC:
23910Sstevel@tonic-gate 		case MRT_VERSION:
23920Sstevel@tonic-gate 		case MRT_ASSERT:
23930Sstevel@tonic-gate 		case IP_SEC_OPT:
23940Sstevel@tonic-gate 		case IP_DONTFAILOVER_IF:
23951663Spriyanka 		case IP_NEXTHOP:
23960Sstevel@tonic-gate 			/*
23970Sstevel@tonic-gate 			 * "soft" error (negative)
23980Sstevel@tonic-gate 			 * option not handled at this level
23990Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
24000Sstevel@tonic-gate 			 */
24010Sstevel@tonic-gate 			return (-EINVAL);
24020Sstevel@tonic-gate 		default:
24030Sstevel@tonic-gate 			*outlenp = 0;
24040Sstevel@tonic-gate 			return (EINVAL);
24050Sstevel@tonic-gate 		}
24060Sstevel@tonic-gate 		break;
24070Sstevel@tonic-gate 	case IPPROTO_IPV6: {
24080Sstevel@tonic-gate 		ip6_pkt_t		*ipp;
24090Sstevel@tonic-gate 		boolean_t		sticky;
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
24120Sstevel@tonic-gate 			*outlenp = 0;
24130Sstevel@tonic-gate 			return (ENOPROTOOPT);
24140Sstevel@tonic-gate 		}
24150Sstevel@tonic-gate 		/*
24160Sstevel@tonic-gate 		 * Deal with both sticky options and ancillary data
24170Sstevel@tonic-gate 		 */
24180Sstevel@tonic-gate 		if (thisdg_attrs == NULL) {
24190Sstevel@tonic-gate 			/* sticky options, or none */
24200Sstevel@tonic-gate 			ipp = &icmp->icmp_sticky_ipp;
24210Sstevel@tonic-gate 			sticky = B_TRUE;
24220Sstevel@tonic-gate 		} else {
24230Sstevel@tonic-gate 			/* ancillary data */
24240Sstevel@tonic-gate 			ipp = (ip6_pkt_t *)thisdg_attrs;
24250Sstevel@tonic-gate 			sticky = B_FALSE;
24260Sstevel@tonic-gate 		}
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 		switch (name) {
24290Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
24300Sstevel@tonic-gate 			if (!checkonly)
24310Sstevel@tonic-gate 				icmp->icmp_multicast_if_index = *i1;
24320Sstevel@tonic-gate 			break;
24330Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
24340Sstevel@tonic-gate 			/* -1 means use default */
24350Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
24360Sstevel@tonic-gate 				*outlenp = 0;
24370Sstevel@tonic-gate 				return (EINVAL);
24380Sstevel@tonic-gate 			}
24390Sstevel@tonic-gate 			if (!checkonly) {
24400Sstevel@tonic-gate 				if (*i1 == -1) {
2441679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
24423448Sdh155122 					    is->is_ipv6_hoplimit;
2443679Sseb 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
24440Sstevel@tonic-gate 					/* Pass modified value to IP. */
24450Sstevel@tonic-gate 					*i1 = ipp->ipp_hoplimit;
24460Sstevel@tonic-gate 				} else {
2447679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
24480Sstevel@tonic-gate 					    (uint8_t)*i1;
2449679Sseb 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
24500Sstevel@tonic-gate 				}
24510Sstevel@tonic-gate 				/* Rebuild the header template */
24525240Snordmark 				error = icmp_build_hdrs(icmp);
24530Sstevel@tonic-gate 				if (error != 0) {
24540Sstevel@tonic-gate 					*outlenp = 0;
24550Sstevel@tonic-gate 					return (error);
24560Sstevel@tonic-gate 				}
24570Sstevel@tonic-gate 			}
24580Sstevel@tonic-gate 			break;
24590Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
24600Sstevel@tonic-gate 			/* -1 means use default */
24610Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
24620Sstevel@tonic-gate 				*outlenp = 0;
24630Sstevel@tonic-gate 				return (EINVAL);
24640Sstevel@tonic-gate 			}
24650Sstevel@tonic-gate 			if (!checkonly) {
24660Sstevel@tonic-gate 				if (*i1 == -1) {
24670Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2468679Sseb 					    ipp->ipp_multicast_hops =
24690Sstevel@tonic-gate 					    IP_DEFAULT_MULTICAST_TTL;
2470679Sseb 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
24710Sstevel@tonic-gate 					/* Pass modified value to IP. */
2472679Sseb 					*i1 = icmp->icmp_multicast_ttl;
24730Sstevel@tonic-gate 				} else {
24740Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2475679Sseb 					    ipp->ipp_multicast_hops =
24760Sstevel@tonic-gate 					    (uint8_t)*i1;
2477679Sseb 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
24780Sstevel@tonic-gate 				}
24790Sstevel@tonic-gate 			}
24800Sstevel@tonic-gate 			break;
24810Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
24820Sstevel@tonic-gate 			if (*i1 != 0 && *i1 != 1) {
24830Sstevel@tonic-gate 				*outlenp = 0;
24840Sstevel@tonic-gate 				return (EINVAL);
24850Sstevel@tonic-gate 			}
24860Sstevel@tonic-gate 			if (!checkonly)
24875240Snordmark 				connp->conn_multicast_loop = *i1;
24880Sstevel@tonic-gate 			break;
24890Sstevel@tonic-gate 		case IPV6_CHECKSUM:
24900Sstevel@tonic-gate 			/*
24910Sstevel@tonic-gate 			 * Integer offset into the user data of where the
24920Sstevel@tonic-gate 			 * checksum is located.
24930Sstevel@tonic-gate 			 * Offset of -1 disables option.
24940Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6.
24950Sstevel@tonic-gate 			 */
24960Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
24970Sstevel@tonic-gate 				*outlenp = 0;
24980Sstevel@tonic-gate 				return (EINVAL);
24990Sstevel@tonic-gate 			}
25000Sstevel@tonic-gate 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
25010Sstevel@tonic-gate 				/* Negative or not 16 bit aligned offset */
25020Sstevel@tonic-gate 				*outlenp = 0;
25030Sstevel@tonic-gate 				return (EINVAL);
25040Sstevel@tonic-gate 			}
25050Sstevel@tonic-gate 			if (checkonly)
25060Sstevel@tonic-gate 				break;
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 			if (*i1 == -1) {
25090Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
25100Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
25110Sstevel@tonic-gate 			} else {
25120Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 1;
25130Sstevel@tonic-gate 				icmp->icmp_checksum_off = *i1;
25140Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
25150Sstevel@tonic-gate 			}
25160Sstevel@tonic-gate 			/* Rebuild the header template */
25175240Snordmark 			error = icmp_build_hdrs(icmp);
25180Sstevel@tonic-gate 			if (error != 0) {
25190Sstevel@tonic-gate 				*outlenp = 0;
25200Sstevel@tonic-gate 				return (error);
25210Sstevel@tonic-gate 			}
25220Sstevel@tonic-gate 			break;
25230Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
25240Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
25250Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
25260Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
25270Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
25280Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
25290Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
25300Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
25310Sstevel@tonic-gate 			/*
25320Sstevel@tonic-gate 			 * "soft" error (negative)
25330Sstevel@tonic-gate 			 * option not handled at this level
25340Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
25350Sstevel@tonic-gate 			 */
25360Sstevel@tonic-gate 			return (-EINVAL);
25370Sstevel@tonic-gate 		case IPV6_BOUND_IF:
25380Sstevel@tonic-gate 			if (!checkonly)
25390Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
25400Sstevel@tonic-gate 			break;
25410Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
25420Sstevel@tonic-gate 			if (!checkonly)
25430Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
25440Sstevel@tonic-gate 			break;
25450Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
25460Sstevel@tonic-gate 			if (!checkonly)
25470Sstevel@tonic-gate 				icmp->icmp_ipv6_recvtclass = onoff;
25480Sstevel@tonic-gate 			break;
25490Sstevel@tonic-gate 		/*
25500Sstevel@tonic-gate 		 * Set boolean switches for ancillary data delivery
25510Sstevel@tonic-gate 		 */
25520Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
25530Sstevel@tonic-gate 			if (!checkonly)
25543318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
25550Sstevel@tonic-gate 			break;
25560Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
25570Sstevel@tonic-gate 			if (!checkonly)
25580Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpathmtu = onoff;
25590Sstevel@tonic-gate 			break;
25600Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
25610Sstevel@tonic-gate 			if (!checkonly)
25620Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhoplimit = onoff;
25630Sstevel@tonic-gate 			break;
25640Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
25650Sstevel@tonic-gate 			if (!checkonly)
25660Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhopopts = onoff;
25670Sstevel@tonic-gate 			break;
25680Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
25690Sstevel@tonic-gate 			if (!checkonly)
25700Sstevel@tonic-gate 				icmp->icmp_ipv6_recvdstopts = onoff;
25710Sstevel@tonic-gate 			break;
25720Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
25730Sstevel@tonic-gate 			if (!checkonly)
25740Sstevel@tonic-gate 				icmp->icmp_old_ipv6_recvdstopts = onoff;
25750Sstevel@tonic-gate 			break;
25760Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
25770Sstevel@tonic-gate 			if (!checkonly)
25780Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrtdstopts = onoff;
25790Sstevel@tonic-gate 			break;
25800Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
25810Sstevel@tonic-gate 			if (!checkonly)
25820Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrthdr = onoff;
25830Sstevel@tonic-gate 			break;
25840Sstevel@tonic-gate 		/*
25850Sstevel@tonic-gate 		 * Set sticky options or ancillary data.
25860Sstevel@tonic-gate 		 * If sticky options, (re)build any extension headers
25870Sstevel@tonic-gate 		 * that might be needed as a result.
25880Sstevel@tonic-gate 		 */
25890Sstevel@tonic-gate 		case IPV6_PKTINFO:
25900Sstevel@tonic-gate 			/*
25910Sstevel@tonic-gate 			 * The source address and ifindex are verified
25920Sstevel@tonic-gate 			 * in ip_opt_set(). For ancillary data the
25930Sstevel@tonic-gate 			 * source address is checked in ip_wput_v6.
25940Sstevel@tonic-gate 			 */
25950Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
25960Sstevel@tonic-gate 				return (EINVAL);
25970Sstevel@tonic-gate 			if (checkonly)
25980Sstevel@tonic-gate 				break;
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 			if (inlen == 0) {
26010Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
26020Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |=
26030Sstevel@tonic-gate 				    (IPPF_IFINDEX|IPPF_ADDR);
26040Sstevel@tonic-gate 			} else {
26050Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
26080Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
26090Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
26100Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
26110Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
26120Sstevel@tonic-gate 				else
26130Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
26140Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
26150Sstevel@tonic-gate 				    &ipp->ipp_addr))
26160Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
26170Sstevel@tonic-gate 				else
26180Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
26190Sstevel@tonic-gate 			}
26200Sstevel@tonic-gate 			if (sticky) {
26215240Snordmark 				error = icmp_build_hdrs(icmp);
26220Sstevel@tonic-gate 				if (error != 0)
26230Sstevel@tonic-gate 					return (error);
26240Sstevel@tonic-gate 			}
26250Sstevel@tonic-gate 			break;
26260Sstevel@tonic-gate 		case IPV6_HOPLIMIT:
2627679Sseb 			/* This option can only be used as ancillary data. */
2628679Sseb 			if (sticky)
2629679Sseb 				return (EINVAL);
26300Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
26310Sstevel@tonic-gate 				return (EINVAL);
26320Sstevel@tonic-gate 			if (checkonly)
26330Sstevel@tonic-gate 				break;
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 			if (inlen == 0) {
26360Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
26370Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
26380Sstevel@tonic-gate 			} else {
26390Sstevel@tonic-gate 				if (*i1 > 255 || *i1 < -1)
26400Sstevel@tonic-gate 					return (EINVAL);
26410Sstevel@tonic-gate 				if (*i1 == -1)
26423448Sdh155122 					ipp->ipp_hoplimit =
26433448Sdh155122 					    is->is_ipv6_hoplimit;
26440Sstevel@tonic-gate 				else
26450Sstevel@tonic-gate 					ipp->ipp_hoplimit = *i1;
26460Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPLIMIT;
26470Sstevel@tonic-gate 			}
26480Sstevel@tonic-gate 			break;
26490Sstevel@tonic-gate 		case IPV6_TCLASS:
26500Sstevel@tonic-gate 			/*
26510Sstevel@tonic-gate 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
26520Sstevel@tonic-gate 			 * and [0, 255] as the actualy traffic class.
26530Sstevel@tonic-gate 			 */
26540Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
26550Sstevel@tonic-gate 				return (EINVAL);
26560Sstevel@tonic-gate 			if (checkonly)
26570Sstevel@tonic-gate 				break;
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 			if (inlen == 0) {
26600Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_TCLASS;
26610Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
26620Sstevel@tonic-gate 			} else {
26630Sstevel@tonic-gate 				if (*i1 >= 256 || *i1 < -1)
26640Sstevel@tonic-gate 					return (EINVAL);
26650Sstevel@tonic-gate 				if (*i1 == -1) {
26660Sstevel@tonic-gate 					ipp->ipp_tclass =
26670Sstevel@tonic-gate 					    IPV6_FLOW_TCLASS(
26680Sstevel@tonic-gate 					    IPV6_DEFAULT_VERS_AND_FLOW);
26690Sstevel@tonic-gate 				} else {
26700Sstevel@tonic-gate 					ipp->ipp_tclass = *i1;
26710Sstevel@tonic-gate 				}
26720Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_TCLASS;
26730Sstevel@tonic-gate 			}
26740Sstevel@tonic-gate 			if (sticky) {
26755240Snordmark 				error = icmp_build_hdrs(icmp);
26760Sstevel@tonic-gate 				if (error != 0)
26770Sstevel@tonic-gate 					return (error);
26780Sstevel@tonic-gate 			}
26790Sstevel@tonic-gate 			break;
26800Sstevel@tonic-gate 		case IPV6_NEXTHOP:
26810Sstevel@tonic-gate 			/*
26820Sstevel@tonic-gate 			 * IP will verify that the nexthop is reachable
26830Sstevel@tonic-gate 			 * and fail for sticky options.
26840Sstevel@tonic-gate 			 */
26850Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t))
26860Sstevel@tonic-gate 				return (EINVAL);
26870Sstevel@tonic-gate 			if (checkonly)
26880Sstevel@tonic-gate 				break;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 			if (inlen == 0) {
26910Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
26920Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
26930Sstevel@tonic-gate 			} else {
26940Sstevel@tonic-gate 				sin6_t *sin6 = (sin6_t *)invalp;
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6)
26970Sstevel@tonic-gate 					return (EAFNOSUPPORT);
26980Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
26990Sstevel@tonic-gate 					return (EADDRNOTAVAIL);
27000Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
27010Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
27020Sstevel@tonic-gate 				    &ipp->ipp_nexthop))
27030Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
27040Sstevel@tonic-gate 				else
27050Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
27060Sstevel@tonic-gate 			}
27070Sstevel@tonic-gate 			if (sticky) {
27085240Snordmark 				error = icmp_build_hdrs(icmp);
27090Sstevel@tonic-gate 				if (error != 0)
27100Sstevel@tonic-gate 					return (error);
27110Sstevel@tonic-gate 			}
27120Sstevel@tonic-gate 			break;
27130Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
27140Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
27150Sstevel@tonic-gate 			/*
27160Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27170Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27180Sstevel@tonic-gate 			 */
27190Sstevel@tonic-gate 			if (inlen != 0 &&
27200Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1)))
27210Sstevel@tonic-gate 				return (EINVAL);
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 			if (checkonly)
27240Sstevel@tonic-gate 				break;
27251676Sjpk 			error = optcom_pkt_set(invalp, inlen, sticky,
27261676Sjpk 			    (uchar_t **)&ipp->ipp_hopopts,
27271676Sjpk 			    &ipp->ipp_hopoptslen,
27281676Sjpk 			    sticky ? icmp->icmp_label_len_v6 : 0);
27291676Sjpk 			if (error != 0)
27301676Sjpk 				return (error);
27311676Sjpk 			if (ipp->ipp_hopoptslen == 0) {
27320Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
27330Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
27340Sstevel@tonic-gate 			} else {
27350Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
27360Sstevel@tonic-gate 			}
27370Sstevel@tonic-gate 			if (sticky) {
27385240Snordmark 				error = icmp_build_hdrs(icmp);
27390Sstevel@tonic-gate 				if (error != 0)
27400Sstevel@tonic-gate 					return (error);
27410Sstevel@tonic-gate 			}
27420Sstevel@tonic-gate 			break;
27430Sstevel@tonic-gate 		}
27440Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
27450Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 			/*
27480Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27490Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27500Sstevel@tonic-gate 			 */
27510Sstevel@tonic-gate 			if (inlen != 0 &&
27520Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
27530Sstevel@tonic-gate 				return (EINVAL);
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate 			if (checkonly)
27560Sstevel@tonic-gate 				break;
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 			if (inlen == 0) {
27590Sstevel@tonic-gate 				if (sticky &&
27600Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
27610Sstevel@tonic-gate 					kmem_free(ipp->ipp_rtdstopts,
27620Sstevel@tonic-gate 					    ipp->ipp_rtdstoptslen);
27630Sstevel@tonic-gate 					ipp->ipp_rtdstopts = NULL;
27640Sstevel@tonic-gate 					ipp->ipp_rtdstoptslen = 0;
27650Sstevel@tonic-gate 				}
27660Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
27670Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
27680Sstevel@tonic-gate 			} else {
27691676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
27700Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rtdstopts,
27711676Sjpk 				    &ipp->ipp_rtdstoptslen, 0);
27720Sstevel@tonic-gate 				if (error != 0)
27730Sstevel@tonic-gate 					return (error);
27740Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
27750Sstevel@tonic-gate 			}
27760Sstevel@tonic-gate 			if (sticky) {
27775240Snordmark 				error = icmp_build_hdrs(icmp);
27780Sstevel@tonic-gate 				if (error != 0)
27790Sstevel@tonic-gate 					return (error);
27800Sstevel@tonic-gate 			}
27810Sstevel@tonic-gate 			break;
27820Sstevel@tonic-gate 		}
27830Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
27840Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 			/*
27870Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
27880Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
27890Sstevel@tonic-gate 			 */
27900Sstevel@tonic-gate 			if (inlen != 0 &&
27910Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
27920Sstevel@tonic-gate 				return (EINVAL);
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 			if (checkonly)
27950Sstevel@tonic-gate 				break;
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 			if (inlen == 0) {
27980Sstevel@tonic-gate 				if (sticky &&
27990Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
28000Sstevel@tonic-gate 					kmem_free(ipp->ipp_dstopts,
28010Sstevel@tonic-gate 					    ipp->ipp_dstoptslen);
28020Sstevel@tonic-gate 					ipp->ipp_dstopts = NULL;
28030Sstevel@tonic-gate 					ipp->ipp_dstoptslen = 0;
28040Sstevel@tonic-gate 				}
28050Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
28060Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
28070Sstevel@tonic-gate 			} else {
28081676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
28090Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_dstopts,
28101676Sjpk 				    &ipp->ipp_dstoptslen, 0);
28110Sstevel@tonic-gate 				if (error != 0)
28120Sstevel@tonic-gate 					return (error);
28130Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
28140Sstevel@tonic-gate 			}
28150Sstevel@tonic-gate 			if (sticky) {
28165240Snordmark 				error = icmp_build_hdrs(icmp);
28170Sstevel@tonic-gate 				if (error != 0)
28180Sstevel@tonic-gate 					return (error);
28190Sstevel@tonic-gate 			}
28200Sstevel@tonic-gate 			break;
28210Sstevel@tonic-gate 		}
28220Sstevel@tonic-gate 		case IPV6_RTHDR: {
28230Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 			/*
28260Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
28270Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
28280Sstevel@tonic-gate 			 */
28290Sstevel@tonic-gate 			if (inlen != 0 &&
28300Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1)))
28310Sstevel@tonic-gate 				return (EINVAL);
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 			if (checkonly)
28340Sstevel@tonic-gate 				break;
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 			if (inlen == 0) {
28370Sstevel@tonic-gate 				if (sticky &&
28380Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
28390Sstevel@tonic-gate 					kmem_free(ipp->ipp_rthdr,
28400Sstevel@tonic-gate 					    ipp->ipp_rthdrlen);
28410Sstevel@tonic-gate 					ipp->ipp_rthdr = NULL;
28420Sstevel@tonic-gate 					ipp->ipp_rthdrlen = 0;
28430Sstevel@tonic-gate 				}
28440Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
28450Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
28460Sstevel@tonic-gate 			} else {
28471676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
28480Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rthdr,
28491676Sjpk 				    &ipp->ipp_rthdrlen, 0);
28500Sstevel@tonic-gate 				if (error != 0)
28510Sstevel@tonic-gate 					return (error);
28520Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
28530Sstevel@tonic-gate 			}
28540Sstevel@tonic-gate 			if (sticky) {
28555240Snordmark 				error = icmp_build_hdrs(icmp);
28560Sstevel@tonic-gate 				if (error != 0)
28570Sstevel@tonic-gate 					return (error);
28580Sstevel@tonic-gate 			}
28590Sstevel@tonic-gate 			break;
28600Sstevel@tonic-gate 		}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 		case IPV6_DONTFRAG:
28630Sstevel@tonic-gate 			if (checkonly)
28640Sstevel@tonic-gate 				break;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 			if (onoff) {
28670Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DONTFRAG;
28680Sstevel@tonic-gate 			} else {
28690Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
28700Sstevel@tonic-gate 			}
28710Sstevel@tonic-gate 			break;
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 		case IPV6_USE_MIN_MTU:
28740Sstevel@tonic-gate 			if (inlen != sizeof (int))
28750Sstevel@tonic-gate 				return (EINVAL);
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > 1)
28780Sstevel@tonic-gate 				return (EINVAL);
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 			if (checkonly)
28810Sstevel@tonic-gate 				break;
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
28840Sstevel@tonic-gate 			ipp->ipp_use_min_mtu = *i1;
28850Sstevel@tonic-gate 			break;
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate 		/*
28880Sstevel@tonic-gate 		 * This option can't be set.  Its only returned via
28890Sstevel@tonic-gate 		 * getsockopt() or ancillary data.
28900Sstevel@tonic-gate 		 */
28910Sstevel@tonic-gate 		case IPV6_PATHMTU:
28920Sstevel@tonic-gate 			return (EINVAL);
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 		case IPV6_BOUND_PIF:
28950Sstevel@tonic-gate 		case IPV6_SEC_OPT:
28960Sstevel@tonic-gate 		case IPV6_DONTFAILOVER_IF:
28970Sstevel@tonic-gate 		case IPV6_SRC_PREFERENCES:
28980Sstevel@tonic-gate 		case IPV6_V6ONLY:
28990Sstevel@tonic-gate 			/* Handled at IP level */
29000Sstevel@tonic-gate 			return (-EINVAL);
29010Sstevel@tonic-gate 		default:
29020Sstevel@tonic-gate 			*outlenp = 0;
29030Sstevel@tonic-gate 			return (EINVAL);
29040Sstevel@tonic-gate 		}
29050Sstevel@tonic-gate 		break;
29060Sstevel@tonic-gate 	}		/* end IPPROTO_IPV6 */
29070Sstevel@tonic-gate 
29080Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
29090Sstevel@tonic-gate 		/*
29100Sstevel@tonic-gate 		 * Only allow IPv6 option processing on IPv6 sockets.
29110Sstevel@tonic-gate 		 */
29120Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
29130Sstevel@tonic-gate 			*outlenp = 0;
29140Sstevel@tonic-gate 			return (ENOPROTOOPT);
29150Sstevel@tonic-gate 		}
29160Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
29170Sstevel@tonic-gate 			*outlenp = 0;
29180Sstevel@tonic-gate 			return (ENOPROTOOPT);
29190Sstevel@tonic-gate 		}
29200Sstevel@tonic-gate 		switch (name) {
29210Sstevel@tonic-gate 		case ICMP6_FILTER:
29220Sstevel@tonic-gate 			if (!checkonly) {
29230Sstevel@tonic-gate 				if ((inlen != 0) &&
29240Sstevel@tonic-gate 				    (inlen != sizeof (icmp6_filter_t)))
29250Sstevel@tonic-gate 					return (EINVAL);
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 				if (inlen == 0) {
29280Sstevel@tonic-gate 					if (icmp->icmp_filter != NULL) {
29290Sstevel@tonic-gate 						kmem_free(icmp->icmp_filter,
29300Sstevel@tonic-gate 						    sizeof (icmp6_filter_t));
29310Sstevel@tonic-gate 						icmp->icmp_filter = NULL;
29320Sstevel@tonic-gate 					}
29330Sstevel@tonic-gate 				} else {
29340Sstevel@tonic-gate 					if (icmp->icmp_filter == NULL) {
29350Sstevel@tonic-gate 						icmp->icmp_filter = kmem_alloc(
29360Sstevel@tonic-gate 						    sizeof (icmp6_filter_t),
29370Sstevel@tonic-gate 						    KM_NOSLEEP);
29380Sstevel@tonic-gate 						if (icmp->icmp_filter == NULL) {
29390Sstevel@tonic-gate 							*outlenp = 0;
29400Sstevel@tonic-gate 							return (ENOBUFS);
29410Sstevel@tonic-gate 						}
29420Sstevel@tonic-gate 					}
29430Sstevel@tonic-gate 					(void) bcopy(invalp, icmp->icmp_filter,
29440Sstevel@tonic-gate 					    inlen);
29450Sstevel@tonic-gate 				}
29460Sstevel@tonic-gate 			}
29470Sstevel@tonic-gate 			break;
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 		default:
29500Sstevel@tonic-gate 			*outlenp = 0;
29510Sstevel@tonic-gate 			return (EINVAL);
29520Sstevel@tonic-gate 		}
29530Sstevel@tonic-gate 		break;
29540Sstevel@tonic-gate 	default:
29550Sstevel@tonic-gate 		*outlenp = 0;
29560Sstevel@tonic-gate 		return (EINVAL);
29570Sstevel@tonic-gate 	}
29580Sstevel@tonic-gate 	/*
29590Sstevel@tonic-gate 	 * Common case of OK return with outval same as inval.
29600Sstevel@tonic-gate 	 */
29610Sstevel@tonic-gate 	if (invalp != outvalp) {
29620Sstevel@tonic-gate 		/* don't trust bcopy for identical src/dst */
29630Sstevel@tonic-gate 		(void) bcopy(invalp, outvalp, inlen);
29640Sstevel@tonic-gate 	}
29650Sstevel@tonic-gate 	*outlenp = inlen;
29660Sstevel@tonic-gate 	return (0);
29670Sstevel@tonic-gate }
29685240Snordmark /* This routine sets socket options. */
29695240Snordmark /* ARGSUSED */
29705240Snordmark int
29715240Snordmark icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
29725240Snordmark     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
29735240Snordmark     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
29745240Snordmark {
29755240Snordmark 	icmp_t	*icmp;
29765240Snordmark 	int	err;
29775240Snordmark 
29785240Snordmark 	icmp = Q_TO_ICMP(q);
29795240Snordmark 
29805240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
29815240Snordmark 	err = icmp_opt_set_locked(q, optset_context, level, name, inlen, invalp,
29825240Snordmark 	    outlenp, outvalp, thisdg_attrs, cr, mblk);
29835240Snordmark 	rw_exit(&icmp->icmp_rwlock);
29845240Snordmark 	return (err);
29855240Snordmark }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate /*
29880Sstevel@tonic-gate  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
29890Sstevel@tonic-gate  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
29900Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
29910Sstevel@tonic-gate  * headers.
29920Sstevel@tonic-gate  * Returns failure if can't allocate memory.
29930Sstevel@tonic-gate  */
29940Sstevel@tonic-gate static int
29955240Snordmark icmp_build_hdrs(icmp_t *icmp)
29960Sstevel@tonic-gate {
29973448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
29980Sstevel@tonic-gate 	uchar_t	*hdrs;
29990Sstevel@tonic-gate 	uint_t	hdrs_len;
30000Sstevel@tonic-gate 	ip6_t	*ip6h;
30010Sstevel@tonic-gate 	ip6i_t	*ip6i;
30020Sstevel@tonic-gate 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
30030Sstevel@tonic-gate 
30045240Snordmark 	ASSERT(RW_WRITE_HELD(&icmp->icmp_rwlock));
30050Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp);
30060Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
30070Sstevel@tonic-gate 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
30080Sstevel@tonic-gate 		/* Need to reallocate */
30090Sstevel@tonic-gate 		if (hdrs_len != 0) {
30100Sstevel@tonic-gate 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
30110Sstevel@tonic-gate 			if (hdrs == NULL)
30120Sstevel@tonic-gate 				return (ENOMEM);
30130Sstevel@tonic-gate 		} else {
30140Sstevel@tonic-gate 			hdrs = NULL;
30150Sstevel@tonic-gate 		}
30160Sstevel@tonic-gate 		if (icmp->icmp_sticky_hdrs_len != 0) {
30170Sstevel@tonic-gate 			kmem_free(icmp->icmp_sticky_hdrs,
30180Sstevel@tonic-gate 			    icmp->icmp_sticky_hdrs_len);
30190Sstevel@tonic-gate 		}
30200Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = hdrs;
30210Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = hdrs_len;
30220Sstevel@tonic-gate 	}
30230Sstevel@tonic-gate 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
30240Sstevel@tonic-gate 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
30250Sstevel@tonic-gate 
30260Sstevel@tonic-gate 	/* Set header fields not in ipp */
30270Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
30280Sstevel@tonic-gate 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
30290Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
30320Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
30330Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
30340Sstevel@tonic-gate 		}
30350Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
30360Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
30370Sstevel@tonic-gate 		}
30380Sstevel@tonic-gate 	} else {
30390Sstevel@tonic-gate 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
30400Sstevel@tonic-gate 	}
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_ADDR))
30430Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	/* Try to get everything in a single mblk */
30460Sstevel@tonic-gate 	if (hdrs_len > icmp->icmp_max_hdr_len) {
30470Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = hdrs_len;
30485240Snordmark 		rw_exit(&icmp->icmp_rwlock);
30495240Snordmark 		(void) mi_set_sth_wroff(icmp->icmp_connp->conn_rq,
30505240Snordmark 		    icmp->icmp_max_hdr_len + is->is_wroff_extra);
30515240Snordmark 		rw_enter(&icmp->icmp_rwlock, RW_WRITER);
30520Sstevel@tonic-gate 	}
30530Sstevel@tonic-gate 	return (0);
30540Sstevel@tonic-gate }
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate /*
30570Sstevel@tonic-gate  * This routine retrieves the value of an ND variable in a icmpparam_t
30580Sstevel@tonic-gate  * structure.  It is called through nd_getset when a user reads the
30590Sstevel@tonic-gate  * variable.
30600Sstevel@tonic-gate  */
30610Sstevel@tonic-gate /* ARGSUSED */
30620Sstevel@tonic-gate static int
30630Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
30640Sstevel@tonic-gate {
30650Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
30680Sstevel@tonic-gate 	return (0);
30690Sstevel@tonic-gate }
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate /*
30720Sstevel@tonic-gate  * Walk through the param array specified registering each element with the
30730Sstevel@tonic-gate  * named dispatch (ND) handler.
30740Sstevel@tonic-gate  */
30750Sstevel@tonic-gate static boolean_t
30763448Sdh155122 icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt)
30770Sstevel@tonic-gate {
30780Sstevel@tonic-gate 	for (; cnt-- > 0; icmppa++) {
30790Sstevel@tonic-gate 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
30803448Sdh155122 			if (!nd_load(ndp, icmppa->icmp_param_name,
30810Sstevel@tonic-gate 			    icmp_param_get, icmp_param_set,
30820Sstevel@tonic-gate 			    (caddr_t)icmppa)) {
30833448Sdh155122 				nd_free(ndp);
30840Sstevel@tonic-gate 				return (B_FALSE);
30850Sstevel@tonic-gate 			}
30860Sstevel@tonic-gate 		}
30870Sstevel@tonic-gate 	}
30883448Sdh155122 	if (!nd_load(ndp, "icmp_status", icmp_status_report, NULL,
30890Sstevel@tonic-gate 	    NULL)) {
30903448Sdh155122 		nd_free(ndp);
30910Sstevel@tonic-gate 		return (B_FALSE);
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 	return (B_TRUE);
30940Sstevel@tonic-gate }
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */
30970Sstevel@tonic-gate /* ARGSUSED */
30980Sstevel@tonic-gate static int
30990Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
31000Sstevel@tonic-gate {
31010Sstevel@tonic-gate 	long		new_value;
31020Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate 	/*
31050Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
31060Sstevel@tonic-gate 	 * required bounds.
31070Sstevel@tonic-gate 	 */
31080Sstevel@tonic-gate 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
31090Sstevel@tonic-gate 	    new_value < icmppa->icmp_param_min ||
31100Sstevel@tonic-gate 	    new_value > icmppa->icmp_param_max) {
31110Sstevel@tonic-gate 		return (EINVAL);
31120Sstevel@tonic-gate 	}
31130Sstevel@tonic-gate 	/* Set the new value */
31140Sstevel@tonic-gate 	icmppa->icmp_param_value = new_value;
31150Sstevel@tonic-gate 	return (0);
31160Sstevel@tonic-gate }
31175240Snordmark /*ARGSUSED2*/
31180Sstevel@tonic-gate static void
31195240Snordmark icmp_input(void *arg1, mblk_t *mp, void *arg2)
31200Sstevel@tonic-gate {
31215240Snordmark 	conn_t *connp = (conn_t *)arg1;
31220Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
31230Sstevel@tonic-gate 	uchar_t			*rptr;
31245240Snordmark 	icmp_t			*icmp;
31255240Snordmark 	icmp_stack_t		*is;
31260Sstevel@tonic-gate 	sin_t			*sin;
31270Sstevel@tonic-gate 	sin6_t			*sin6;
31280Sstevel@tonic-gate 	ip6_t			*ip6h;
31290Sstevel@tonic-gate 	ip6i_t			*ip6i;
31300Sstevel@tonic-gate 	mblk_t			*mp1;
31310Sstevel@tonic-gate 	int			hdr_len;
31320Sstevel@tonic-gate 	ipha_t			*ipha;
31330Sstevel@tonic-gate 	int			udi_size;	/* Size of T_unitdata_ind */
31340Sstevel@tonic-gate 	uint_t			ipvers;
31350Sstevel@tonic-gate 	ip6_pkt_t		ipp;
31360Sstevel@tonic-gate 	uint8_t			nexthdr;
31373318Srshoaib 	ip_pktinfo_t		*pinfo = NULL;
31380Sstevel@tonic-gate 	mblk_t			*options_mp = NULL;
31390Sstevel@tonic-gate 	uint_t			icmp_opt = 0;
31400Sstevel@tonic-gate 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
31411676Sjpk 	uint_t			hopstrip;
31420Sstevel@tonic-gate 
31435240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
31445240Snordmark 
31455240Snordmark 	icmp = connp->conn_icmp;
31465240Snordmark 	is = icmp->icmp_is;
31475240Snordmark 	rptr = mp->b_rptr;
31485240Snordmark 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_CTL);
31495240Snordmark 	ASSERT(OK_32PTR(rptr));
31505240Snordmark 
31515240Snordmark 	/*
31525240Snordmark 	 * IP should have prepended the options data in an M_CTL
31535240Snordmark 	 * Check M_CTL "type" to make sure are not here bcos of
31545240Snordmark 	 * a valid ICMP message
31555240Snordmark 	 */
31565240Snordmark 	if (DB_TYPE(mp) == M_CTL) {
31570Sstevel@tonic-gate 		/*
31585240Snordmark 		 * FIXME: does IP still do this?
31590Sstevel@tonic-gate 		 * IP sends up the IPSEC_IN message for handling IPSEC
31600Sstevel@tonic-gate 		 * policy at the TCP level. We don't need it here.
31610Sstevel@tonic-gate 		 */
31620Sstevel@tonic-gate 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
31630Sstevel@tonic-gate 			mp1 = mp->b_cont;
31640Sstevel@tonic-gate 			freeb(mp);
31650Sstevel@tonic-gate 			mp = mp1;
31665240Snordmark 			rptr = mp->b_rptr;
31675240Snordmark 		} else if (MBLKL(mp) == sizeof (ip_pktinfo_t) &&
31685240Snordmark 		    ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type ==
31695240Snordmark 		    IN_PKTINFO) {
31705240Snordmark 			/*
31715240Snordmark 			 * IP_RECVIF or IP_RECVSLLA or IPF_RECVADDR information
31725240Snordmark 			 * has been prepended to the packet by IP. We need to
31735240Snordmark 			 * extract the mblk and adjust the rptr
31745240Snordmark 			 */
31753318Srshoaib 			pinfo = (ip_pktinfo_t *)mp->b_rptr;
31765240Snordmark 			options_mp = mp;
31775240Snordmark 			mp = mp->b_cont;
31785240Snordmark 			rptr = mp->b_rptr;
31795240Snordmark 		} else {
31805240Snordmark 			/*
31815240Snordmark 			 * ICMP messages.
31825240Snordmark 			 */
31835240Snordmark 			icmp_icmp_error(connp->conn_rq, mp);
31840Sstevel@tonic-gate 			return;
31850Sstevel@tonic-gate 		}
31860Sstevel@tonic-gate 	}
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 	/*
31890Sstevel@tonic-gate 	 * Discard message if it is misaligned or smaller than the IP header.
31900Sstevel@tonic-gate 	 */
31910Sstevel@tonic-gate 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
31920Sstevel@tonic-gate 		freemsg(mp);
31930Sstevel@tonic-gate 		if (options_mp != NULL)
31940Sstevel@tonic-gate 			freeb(options_mp);
31955240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
31960Sstevel@tonic-gate 		return;
31970Sstevel@tonic-gate 	}
31980Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 	/* Handle M_DATA messages containing IP packets messages */
32010Sstevel@tonic-gate 	if (ipvers == IPV4_VERSION) {
32020Sstevel@tonic-gate 		/*
32030Sstevel@tonic-gate 		 * Special case where IP attaches
32040Sstevel@tonic-gate 		 * the IRE needs to be handled so that we don't send up
32050Sstevel@tonic-gate 		 * IRE to the user land.
32060Sstevel@tonic-gate 		 */
32070Sstevel@tonic-gate 		ipha = (ipha_t *)rptr;
32080Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 		if (ipha->ipha_protocol == IPPROTO_TCP) {
32110Sstevel@tonic-gate 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
32140Sstevel@tonic-gate 			    TH_SYN) && mp->b_cont != NULL) {
32150Sstevel@tonic-gate 				mp1 = mp->b_cont;
32160Sstevel@tonic-gate 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
32170Sstevel@tonic-gate 					freeb(mp1);
32180Sstevel@tonic-gate 					mp->b_cont = NULL;
32190Sstevel@tonic-gate 				}
32200Sstevel@tonic-gate 			}
32210Sstevel@tonic-gate 		}
32223448Sdh155122 		if (is->is_bsd_compat) {
32230Sstevel@tonic-gate 			ushort_t len;
32240Sstevel@tonic-gate 			len = ntohs(ipha->ipha_length);
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 			if (mp->b_datap->db_ref > 1) {
32270Sstevel@tonic-gate 				/*
32280Sstevel@tonic-gate 				 * Allocate a new IP header so that we can
32290Sstevel@tonic-gate 				 * modify ipha_length.
32300Sstevel@tonic-gate 				 */
32310Sstevel@tonic-gate 				mblk_t	*mp1;
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate 				mp1 = allocb(hdr_len, BPRI_MED);
32340Sstevel@tonic-gate 				if (!mp1) {
32350Sstevel@tonic-gate 					freemsg(mp);
32360Sstevel@tonic-gate 					if (options_mp != NULL)
32370Sstevel@tonic-gate 						freeb(options_mp);
32385240Snordmark 					BUMP_MIB(&is->is_rawip_mib,
32393448Sdh155122 					    rawipInErrors);
32400Sstevel@tonic-gate 					return;
32410Sstevel@tonic-gate 				}
32420Sstevel@tonic-gate 				bcopy(rptr, mp1->b_rptr, hdr_len);
32430Sstevel@tonic-gate 				mp->b_rptr = rptr + hdr_len;
32440Sstevel@tonic-gate 				rptr = mp1->b_rptr;
32450Sstevel@tonic-gate 				ipha = (ipha_t *)rptr;
32460Sstevel@tonic-gate 				mp1->b_cont = mp;
32470Sstevel@tonic-gate 				mp1->b_wptr = rptr + hdr_len;
32480Sstevel@tonic-gate 				mp = mp1;
32490Sstevel@tonic-gate 			}
32500Sstevel@tonic-gate 			len -= hdr_len;
32510Sstevel@tonic-gate 			ipha->ipha_length = htons(len);
32520Sstevel@tonic-gate 		}
32530Sstevel@tonic-gate 	}
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	/*
32560Sstevel@tonic-gate 	 * This is the inbound data path.  Packets are passed upstream as
32570Sstevel@tonic-gate 	 * T_UNITDATA_IND messages with full IP headers still attached.
32580Sstevel@tonic-gate 	 */
32590Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
32600Sstevel@tonic-gate 		ASSERT(ipvers == IPV4_VERSION);
32610Sstevel@tonic-gate 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
32625267Snordmark 		if (icmp->icmp_recvif && (pinfo != NULL) &&
32633318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
32640Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
32650Sstevel@tonic-gate 			    sizeof (uint_t);
32660Sstevel@tonic-gate 		}
32673318Srshoaib 
32685267Snordmark 		if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) &&
32693318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
32703318Srshoaib 			udi_size += sizeof (struct T_opthdr) +
32713318Srshoaib 			    sizeof (struct in_pktinfo);
32723318Srshoaib 		}
32733318Srshoaib 
32741673Sgt145670 		/*
32751673Sgt145670 		 * If SO_TIMESTAMP is set allocate the appropriate sized
32761673Sgt145670 		 * buffer. Since gethrestime() expects a pointer aligned
32771673Sgt145670 		 * argument, we allocate space necessary for extra
32781673Sgt145670 		 * alignment (even though it might not be used).
32791673Sgt145670 		 */
32801673Sgt145670 		if (icmp->icmp_timestamp) {
32811673Sgt145670 			udi_size += sizeof (struct T_opthdr) +
32821673Sgt145670 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
32831673Sgt145670 		}
32840Sstevel@tonic-gate 		mp1 = allocb(udi_size, BPRI_MED);
32850Sstevel@tonic-gate 		if (mp1 == NULL) {
32860Sstevel@tonic-gate 			freemsg(mp);
32870Sstevel@tonic-gate 			if (options_mp != NULL)
32880Sstevel@tonic-gate 				freeb(options_mp);
32895240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
32900Sstevel@tonic-gate 			return;
32910Sstevel@tonic-gate 		}
32920Sstevel@tonic-gate 		mp1->b_cont = mp;
32930Sstevel@tonic-gate 		mp = mp1;
32940Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
32950Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
32960Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)tudi + udi_size;
32970Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
32980Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin_t);
32990Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
33000Sstevel@tonic-gate 		sin = (sin_t *)&tudi[1];
33010Sstevel@tonic-gate 		*sin = sin_null;
33020Sstevel@tonic-gate 		sin->sin_family = AF_INET;
33030Sstevel@tonic-gate 		sin->sin_addr.s_addr = ipha->ipha_src;
33040Sstevel@tonic-gate 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
33050Sstevel@tonic-gate 		    sizeof (sin_t);
33060Sstevel@tonic-gate 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
33070Sstevel@tonic-gate 		tudi->OPT_length = udi_size;
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 		/*
33100Sstevel@tonic-gate 		 * Add options if IP_RECVIF is set
33110Sstevel@tonic-gate 		 */
33120Sstevel@tonic-gate 		if (udi_size != 0) {
33130Sstevel@tonic-gate 			char *dstopt;
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 			dstopt = (char *)&sin[1];
33165267Snordmark 			if (icmp->icmp_recvif && (pinfo != NULL) &&
33173318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 				struct T_opthdr *toh;
33200Sstevel@tonic-gate 				uint_t		*dstptr;
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 				toh = (struct T_opthdr *)dstopt;
33230Sstevel@tonic-gate 				toh->level = IPPROTO_IP;
33240Sstevel@tonic-gate 				toh->name = IP_RECVIF;
33250Sstevel@tonic-gate 				toh->len = sizeof (struct T_opthdr) +
33265240Snordmark 				    sizeof (uint_t);
33270Sstevel@tonic-gate 				toh->status = 0;
33280Sstevel@tonic-gate 				dstopt += sizeof (struct T_opthdr);
33290Sstevel@tonic-gate 				dstptr = (uint_t *)dstopt;
33303318Srshoaib 				*dstptr = pinfo->ip_pkt_ifindex;
33310Sstevel@tonic-gate 				dstopt += sizeof (uint_t);
33320Sstevel@tonic-gate 				udi_size -= toh->len;
33330Sstevel@tonic-gate 			}
33341673Sgt145670 			if (icmp->icmp_timestamp) {
33351673Sgt145670 				struct	T_opthdr *toh;
33361673Sgt145670 
33371673Sgt145670 				toh = (struct T_opthdr *)dstopt;
33381673Sgt145670 				toh->level = SOL_SOCKET;
33391673Sgt145670 				toh->name = SCM_TIMESTAMP;
33401673Sgt145670 				toh->len = sizeof (struct T_opthdr) +
33411673Sgt145670 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
33421673Sgt145670 				toh->status = 0;
33431673Sgt145670 				dstopt += sizeof (struct T_opthdr);
33441673Sgt145670 				/* Align for gethrestime() */
33451673Sgt145670 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
33461673Sgt145670 				    sizeof (intptr_t));
33471673Sgt145670 				gethrestime((timestruc_t *)dstopt);
33483318Srshoaib 				dstopt = (char *)toh + toh->len;
33493318Srshoaib 				udi_size -= toh->len;
33503318Srshoaib 			}
33515267Snordmark 			if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) &&
33523318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
33533318Srshoaib 				struct	T_opthdr *toh;
33543318Srshoaib 				struct	in_pktinfo *pktinfop;
33553318Srshoaib 
33563318Srshoaib 				toh = (struct T_opthdr *)dstopt;
33573318Srshoaib 				toh->level = IPPROTO_IP;
33583318Srshoaib 				toh->name = IP_PKTINFO;
33593318Srshoaib 				toh->len = sizeof (struct T_opthdr) +
33603318Srshoaib 				    sizeof (in_pktinfo_t);
33613318Srshoaib 				toh->status = 0;
33623318Srshoaib 				dstopt += sizeof (struct T_opthdr);
33633318Srshoaib 				pktinfop = (struct in_pktinfo *)dstopt;
33643318Srshoaib 				pktinfop->ipi_ifindex = pinfo->ip_pkt_ifindex;
33653318Srshoaib 				pktinfop->ipi_spec_dst =
33663318Srshoaib 				    pinfo->ip_pkt_match_addr;
33673318Srshoaib 
33683318Srshoaib 				pktinfop->ipi_addr.s_addr = ipha->ipha_dst;
33693318Srshoaib 
33703318Srshoaib 				dstopt += sizeof (struct in_pktinfo);
33711673Sgt145670 				udi_size -= toh->len;
33721673Sgt145670 			}
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 			/* Consumed all of allocated space */
33750Sstevel@tonic-gate 			ASSERT(udi_size == 0);
33760Sstevel@tonic-gate 		}
33770Sstevel@tonic-gate 
33785267Snordmark 		if (options_mp != NULL)
33795267Snordmark 			freeb(options_mp);
33805267Snordmark 
33815240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
33825240Snordmark 		putnext(connp->conn_rq, mp);
33830Sstevel@tonic-gate 		return;
33840Sstevel@tonic-gate 	}
33850Sstevel@tonic-gate 
33860Sstevel@tonic-gate 	/*
33870Sstevel@tonic-gate 	 * We don't need options_mp in the IPv6 path.
33880Sstevel@tonic-gate 	 */
33890Sstevel@tonic-gate 	if (options_mp != NULL) {
33900Sstevel@tonic-gate 		freeb(options_mp);
33910Sstevel@tonic-gate 		options_mp = NULL;
33920Sstevel@tonic-gate 	}
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	/*
33950Sstevel@tonic-gate 	 * Discard message if it is smaller than the IPv6 header
33960Sstevel@tonic-gate 	 * or if the header is malformed.
33970Sstevel@tonic-gate 	 */
33980Sstevel@tonic-gate 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
33990Sstevel@tonic-gate 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
34000Sstevel@tonic-gate 	    icmp->icmp_family != AF_INET6) {
34010Sstevel@tonic-gate 		freemsg(mp);
34025240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
34030Sstevel@tonic-gate 		return;
34040Sstevel@tonic-gate 	}
34050Sstevel@tonic-gate 
34060Sstevel@tonic-gate 	/* Initialize */
34070Sstevel@tonic-gate 	ipp.ipp_fields = 0;
34081676Sjpk 	hopstrip = 0;
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	ip6h = (ip6_t *)rptr;
34110Sstevel@tonic-gate 	/*
34120Sstevel@tonic-gate 	 * Call on ip_find_hdr_v6 which gets the total hdr len
34130Sstevel@tonic-gate 	 * as well as individual lenghts of ext hdrs (and ptrs to
34140Sstevel@tonic-gate 	 * them).
34150Sstevel@tonic-gate 	 */
34160Sstevel@tonic-gate 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
34170Sstevel@tonic-gate 		/* Look for ifindex information */
34180Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
34190Sstevel@tonic-gate 			ip6i = (ip6i_t *)ip6h;
34200Sstevel@tonic-gate 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
34210Sstevel@tonic-gate 				ASSERT(ip6i->ip6i_ifindex != 0);
34220Sstevel@tonic-gate 				ipp.ipp_fields |= IPPF_IFINDEX;
34230Sstevel@tonic-gate 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
34240Sstevel@tonic-gate 			}
34250Sstevel@tonic-gate 			rptr = (uchar_t *)&ip6i[1];
34260Sstevel@tonic-gate 			mp->b_rptr = rptr;
34270Sstevel@tonic-gate 			if (rptr == mp->b_wptr) {
34280Sstevel@tonic-gate 				mp1 = mp->b_cont;
34290Sstevel@tonic-gate 				freeb(mp);
34300Sstevel@tonic-gate 				mp = mp1;
34310Sstevel@tonic-gate 				rptr = mp->b_rptr;
34320Sstevel@tonic-gate 			}
34330Sstevel@tonic-gate 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
34340Sstevel@tonic-gate 			ip6h = (ip6_t *)rptr;
34350Sstevel@tonic-gate 		}
34360Sstevel@tonic-gate 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
34371676Sjpk 
34381676Sjpk 		/*
34391676Sjpk 		 * We need to lie a bit to the user because users inside
34401676Sjpk 		 * labeled compartments should not see their own labels.  We
34411676Sjpk 		 * assume that in all other respects IP has checked the label,
34421676Sjpk 		 * and that the label is always first among the options.  (If
34431676Sjpk 		 * it's not first, then this code won't see it, and the option
34441676Sjpk 		 * will be passed along to the user.)
34451676Sjpk 		 *
34461676Sjpk 		 * If we had multilevel ICMP sockets, then the following code
34471676Sjpk 		 * should be skipped for them to allow the user to see the
34481676Sjpk 		 * label.
34491676Sjpk 		 *
34501676Sjpk 		 * Alignment restrictions in the definition of IP options
34511676Sjpk 		 * (namely, the requirement that the 4-octet DOI goes on a
34521676Sjpk 		 * 4-octet boundary) mean that we know exactly where the option
34531676Sjpk 		 * should start, but we're lenient for other hosts.
34541676Sjpk 		 *
34551676Sjpk 		 * Note that there are no multilevel ICMP or raw IP sockets
34561676Sjpk 		 * yet, thus nobody ever sees the IP6OPT_LS option.
34571676Sjpk 		 */
34581676Sjpk 		if ((ipp.ipp_fields & IPPF_HOPOPTS) &&
34591676Sjpk 		    ipp.ipp_hopoptslen > 5 && is_system_labeled()) {
34601676Sjpk 			const uchar_t *ucp =
34611676Sjpk 			    (const uchar_t *)ipp.ipp_hopopts + 2;
34621676Sjpk 			int remlen = ipp.ipp_hopoptslen - 2;
34631676Sjpk 
34641676Sjpk 			while (remlen > 0) {
34651676Sjpk 				if (*ucp == IP6OPT_PAD1) {
34661676Sjpk 					remlen--;
34671676Sjpk 					ucp++;
34681676Sjpk 				} else if (*ucp == IP6OPT_PADN) {
34691676Sjpk 					remlen -= ucp[1] + 2;
34701676Sjpk 					ucp += ucp[1] + 2;
34711676Sjpk 				} else if (*ucp == ip6opt_ls) {
34721676Sjpk 					hopstrip = (ucp -
34731676Sjpk 					    (const uchar_t *)ipp.ipp_hopopts) +
34741676Sjpk 					    ucp[1] + 2;
34751676Sjpk 					hopstrip = (hopstrip + 7) & ~7;
34761676Sjpk 					break;
34771676Sjpk 				} else {
34781676Sjpk 					/* label option must be first */
34791676Sjpk 					break;
34801676Sjpk 				}
34811676Sjpk 			}
34821676Sjpk 		}
34830Sstevel@tonic-gate 	} else {
34840Sstevel@tonic-gate 		hdr_len = IPV6_HDR_LEN;
34850Sstevel@tonic-gate 		ip6i = NULL;
34860Sstevel@tonic-gate 		nexthdr = ip6h->ip6_nxt;
34870Sstevel@tonic-gate 	}
34880Sstevel@tonic-gate 	/*
34890Sstevel@tonic-gate 	 * One special case where IP attaches the IRE needs to
34900Sstevel@tonic-gate 	 * be handled so that we don't send up IRE to the user land.
34910Sstevel@tonic-gate 	 */
34920Sstevel@tonic-gate 	if (nexthdr == IPPROTO_TCP) {
34930Sstevel@tonic-gate 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
34960Sstevel@tonic-gate 		    mp->b_cont != NULL) {
34970Sstevel@tonic-gate 			mp1 = mp->b_cont;
34980Sstevel@tonic-gate 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
34990Sstevel@tonic-gate 				freeb(mp1);
35000Sstevel@tonic-gate 				mp->b_cont = NULL;
35010Sstevel@tonic-gate 			}
35020Sstevel@tonic-gate 		}
35030Sstevel@tonic-gate 	}
35040Sstevel@tonic-gate 	/*
35050Sstevel@tonic-gate 	 * Check a filter for ICMPv6 types if needed.
35060Sstevel@tonic-gate 	 * Verify raw checksums if needed.
35070Sstevel@tonic-gate 	 */
35080Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
35090Sstevel@tonic-gate 		if (icmp->icmp_filter != NULL) {
35100Sstevel@tonic-gate 			int type;
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 			/* Assumes that IP has done the pullupmsg */
35130Sstevel@tonic-gate 			type = mp->b_rptr[hdr_len];
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
35160Sstevel@tonic-gate 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
35170Sstevel@tonic-gate 				freemsg(mp);
35180Sstevel@tonic-gate 				return;
35190Sstevel@tonic-gate 			}
35200Sstevel@tonic-gate 		} else {
35210Sstevel@tonic-gate 			/* Checksum */
35220Sstevel@tonic-gate 			uint16_t	*up;
35230Sstevel@tonic-gate 			uint32_t	sum;
35240Sstevel@tonic-gate 			int		remlen;
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 			up = (uint16_t *)&ip6h->ip6_src;
35270Sstevel@tonic-gate 
35280Sstevel@tonic-gate 			remlen = msgdsize(mp) - hdr_len;
35290Sstevel@tonic-gate 			sum = htons(icmp->icmp_proto + remlen)
35300Sstevel@tonic-gate 			    + up[0] + up[1] + up[2] + up[3]
35310Sstevel@tonic-gate 			    + up[4] + up[5] + up[6] + up[7]
35320Sstevel@tonic-gate 			    + up[8] + up[9] + up[10] + up[11]
35330Sstevel@tonic-gate 			    + up[12] + up[13] + up[14] + up[15];
35340Sstevel@tonic-gate 			sum = (sum & 0xffff) + (sum >> 16);
35350Sstevel@tonic-gate 			sum = IP_CSUM(mp, hdr_len, sum);
35360Sstevel@tonic-gate 			if (sum != 0) {
35370Sstevel@tonic-gate 				/* IPv6 RAW checksum failed */
35380Sstevel@tonic-gate 				ip0dbg(("icmp_rput: RAW checksum "
35390Sstevel@tonic-gate 				    "failed %x\n", sum));
35400Sstevel@tonic-gate 				freemsg(mp);
35415240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
35423448Sdh155122 				    rawipInCksumErrs);
35430Sstevel@tonic-gate 				return;
35440Sstevel@tonic-gate 			}
35450Sstevel@tonic-gate 		}
35460Sstevel@tonic-gate 	}
35470Sstevel@tonic-gate 	/* Skip all the IPv6 headers per API */
35480Sstevel@tonic-gate 	mp->b_rptr += hdr_len;
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
35510Sstevel@tonic-gate 
35520Sstevel@tonic-gate 	/*
35530Sstevel@tonic-gate 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
35540Sstevel@tonic-gate 	 * maintain state information, instead of relying on icmp_t
35550Sstevel@tonic-gate 	 * structure, since there arent any locks protecting these members
35560Sstevel@tonic-gate 	 * and there is a window where there might be a race between a
35570Sstevel@tonic-gate 	 * thread setting options on the write side and a thread reading
35580Sstevel@tonic-gate 	 * these options on the read size.
35590Sstevel@tonic-gate 	 */
35600Sstevel@tonic-gate 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
35610Sstevel@tonic-gate 	    IPPF_RTHDR|IPPF_IFINDEX)) {
35620Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvhopopts &&
35631676Sjpk 		    (ipp.ipp_fields & IPPF_HOPOPTS) &&
35641676Sjpk 		    ipp.ipp_hopoptslen > hopstrip) {
35650Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35661676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
35670Sstevel@tonic-gate 			icmp_opt |= IPPF_HOPOPTS;
35680Sstevel@tonic-gate 		}
35690Sstevel@tonic-gate 		if ((icmp->icmp_ipv6_recvdstopts ||
35705240Snordmark 		    icmp->icmp_old_ipv6_recvdstopts) &&
35710Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
35720Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35730Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
35740Sstevel@tonic-gate 			icmp_opt |= IPPF_DSTOPTS;
35750Sstevel@tonic-gate 		}
35760Sstevel@tonic-gate 		if (((icmp->icmp_ipv6_recvdstopts &&
35770Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrthdr &&
35780Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
35790Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrtdstopts) &&
35800Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
35810Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35820Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
35830Sstevel@tonic-gate 			icmp_opt |= IPPF_RTDSTOPTS;
35840Sstevel@tonic-gate 		}
35850Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvrthdr &&
35860Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) {
35870Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35880Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
35890Sstevel@tonic-gate 			icmp_opt |= IPPF_RTHDR;
35900Sstevel@tonic-gate 		}
35913318Srshoaib 		if (icmp->icmp_ip_recvpktinfo &&
35920Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
35930Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35940Sstevel@tonic-gate 			    sizeof (struct in6_pktinfo);
35950Sstevel@tonic-gate 			icmp_opt |= IPPF_IFINDEX;
35960Sstevel@tonic-gate 		}
35970Sstevel@tonic-gate 	}
35980Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvhoplimit) {
35990Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
36000Sstevel@tonic-gate 		icmp_ipv6_recvhoplimit = B_TRUE;
36010Sstevel@tonic-gate 	}
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvtclass)
36040Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
36050Sstevel@tonic-gate 
3606*5401Snordmark 	/*
3607*5401Snordmark 	 * If SO_TIMESTAMP is set allocate the appropriate sized
3608*5401Snordmark 	 * buffer. Since gethrestime() expects a pointer aligned
3609*5401Snordmark 	 * argument, we allocate space necessary for extra
3610*5401Snordmark 	 * alignment (even though it might not be used).
3611*5401Snordmark 	 */
3612*5401Snordmark 	if (icmp->icmp_timestamp) {
3613*5401Snordmark 		udi_size += sizeof (struct T_opthdr) +
3614*5401Snordmark 		    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
3615*5401Snordmark 	}
3616*5401Snordmark 
36170Sstevel@tonic-gate 	mp1 = allocb(udi_size, BPRI_MED);
36180Sstevel@tonic-gate 	if (mp1 == NULL) {
36190Sstevel@tonic-gate 		freemsg(mp);
36205240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
36210Sstevel@tonic-gate 		return;
36220Sstevel@tonic-gate 	}
36230Sstevel@tonic-gate 	mp1->b_cont = mp;
36240Sstevel@tonic-gate 	mp = mp1;
36250Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
36260Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
36270Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)tudi + udi_size;
36280Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
36290Sstevel@tonic-gate 	tudi->SRC_length = sizeof (sin6_t);
36300Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
36310Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
36320Sstevel@tonic-gate 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
36330Sstevel@tonic-gate 	tudi->OPT_length = udi_size;
36340Sstevel@tonic-gate 	sin6 = (sin6_t *)&tudi[1];
36350Sstevel@tonic-gate 	sin6->sin6_port = 0;
36360Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
36370Sstevel@tonic-gate 
36380Sstevel@tonic-gate 	sin6->sin6_addr = ip6h->ip6_src;
36390Sstevel@tonic-gate 	/* No sin6_flowinfo per API */
36400Sstevel@tonic-gate 	sin6->sin6_flowinfo = 0;
36410Sstevel@tonic-gate 	/* For link-scope source pass up scope id */
36420Sstevel@tonic-gate 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
36430Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
36440Sstevel@tonic-gate 		sin6->sin6_scope_id = ipp.ipp_ifindex;
36450Sstevel@tonic-gate 	else
36460Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
36493448Sdh155122 	    icmp->icmp_zoneid, is->is_netstack);
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 	if (udi_size != 0) {
36520Sstevel@tonic-gate 		uchar_t *dstopt;
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate 		dstopt = (uchar_t *)&sin6[1];
36550Sstevel@tonic-gate 		if (icmp_opt & IPPF_IFINDEX) {
36560Sstevel@tonic-gate 			struct T_opthdr *toh;
36570Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
36580Sstevel@tonic-gate 
36590Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36600Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36610Sstevel@tonic-gate 			toh->name = IPV6_PKTINFO;
36620Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36630Sstevel@tonic-gate 			    sizeof (*pkti);
36640Sstevel@tonic-gate 			toh->status = 0;
36650Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36660Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)dstopt;
36670Sstevel@tonic-gate 			pkti->ipi6_addr = ip6h->ip6_dst;
36680Sstevel@tonic-gate 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
36690Sstevel@tonic-gate 			dstopt += sizeof (*pkti);
36700Sstevel@tonic-gate 			udi_size -= toh->len;
36710Sstevel@tonic-gate 		}
36720Sstevel@tonic-gate 		if (icmp_ipv6_recvhoplimit) {
36730Sstevel@tonic-gate 			struct T_opthdr *toh;
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36760Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36770Sstevel@tonic-gate 			toh->name = IPV6_HOPLIMIT;
36780Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36790Sstevel@tonic-gate 			    sizeof (uint_t);
36800Sstevel@tonic-gate 			toh->status = 0;
36810Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36820Sstevel@tonic-gate 			*(uint_t *)dstopt = ip6h->ip6_hops;
36830Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
36840Sstevel@tonic-gate 			udi_size -= toh->len;
36850Sstevel@tonic-gate 		}
36860Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvtclass) {
36870Sstevel@tonic-gate 			struct T_opthdr *toh;
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36900Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36910Sstevel@tonic-gate 			toh->name = IPV6_TCLASS;
36920Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36930Sstevel@tonic-gate 			    sizeof (uint_t);
36940Sstevel@tonic-gate 			toh->status = 0;
36950Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36960Sstevel@tonic-gate 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
36970Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
36980Sstevel@tonic-gate 			udi_size -= toh->len;
36990Sstevel@tonic-gate 		}
3700*5401Snordmark 		if (icmp->icmp_timestamp) {
3701*5401Snordmark 			struct	T_opthdr *toh;
3702*5401Snordmark 
3703*5401Snordmark 			toh = (struct T_opthdr *)dstopt;
3704*5401Snordmark 			toh->level = SOL_SOCKET;
3705*5401Snordmark 			toh->name = SCM_TIMESTAMP;
3706*5401Snordmark 			toh->len = sizeof (struct T_opthdr) +
3707*5401Snordmark 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
3708*5401Snordmark 			toh->status = 0;
3709*5401Snordmark 			dstopt += sizeof (struct T_opthdr);
3710*5401Snordmark 			/* Align for gethrestime() */
3711*5401Snordmark 			dstopt = (uchar_t *)P2ROUNDUP((intptr_t)dstopt,
3712*5401Snordmark 			    sizeof (intptr_t));
3713*5401Snordmark 			gethrestime((timestruc_t *)dstopt);
3714*5401Snordmark 			dstopt = (uchar_t *)toh + toh->len;
3715*5401Snordmark 			udi_size -= toh->len;
3716*5401Snordmark 		}
37170Sstevel@tonic-gate 		if (icmp_opt & IPPF_HOPOPTS) {
37180Sstevel@tonic-gate 			struct T_opthdr *toh;
37190Sstevel@tonic-gate 
37200Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37210Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37220Sstevel@tonic-gate 			toh->name = IPV6_HOPOPTS;
37230Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37241676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
37250Sstevel@tonic-gate 			toh->status = 0;
37260Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37271676Sjpk 			bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt,
37281676Sjpk 			    ipp.ipp_hopoptslen - hopstrip);
37291676Sjpk 			if (hopstrip > 0) {
37301676Sjpk 				/* copy next header value and fake length */
37311676Sjpk 				dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0];
37321676Sjpk 				dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] -
37331676Sjpk 				    hopstrip / 8;
37341676Sjpk 			}
37351676Sjpk 			dstopt += ipp.ipp_hopoptslen - hopstrip;
37360Sstevel@tonic-gate 			udi_size -= toh->len;
37370Sstevel@tonic-gate 		}
37380Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTDSTOPTS) {
37390Sstevel@tonic-gate 			struct T_opthdr *toh;
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37420Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37430Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
37440Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37450Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
37460Sstevel@tonic-gate 			toh->status = 0;
37470Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37480Sstevel@tonic-gate 			bcopy(ipp.ipp_rtdstopts, dstopt,
37490Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen);
37500Sstevel@tonic-gate 			dstopt += ipp.ipp_rtdstoptslen;
37510Sstevel@tonic-gate 			udi_size -= toh->len;
37520Sstevel@tonic-gate 		}
37530Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTHDR) {
37540Sstevel@tonic-gate 			struct T_opthdr *toh;
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37570Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37580Sstevel@tonic-gate 			toh->name = IPV6_RTHDR;
37590Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37600Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
37610Sstevel@tonic-gate 			toh->status = 0;
37620Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37630Sstevel@tonic-gate 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
37640Sstevel@tonic-gate 			dstopt += ipp.ipp_rthdrlen;
37650Sstevel@tonic-gate 			udi_size -= toh->len;
37660Sstevel@tonic-gate 		}
37670Sstevel@tonic-gate 		if (icmp_opt & IPPF_DSTOPTS) {
37680Sstevel@tonic-gate 			struct T_opthdr *toh;
37690Sstevel@tonic-gate 
37700Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
37710Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
37720Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
37730Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
37740Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
37750Sstevel@tonic-gate 			toh->status = 0;
37760Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
37770Sstevel@tonic-gate 			bcopy(ipp.ipp_dstopts, dstopt,
37780Sstevel@tonic-gate 			    ipp.ipp_dstoptslen);
37790Sstevel@tonic-gate 			dstopt += ipp.ipp_dstoptslen;
37800Sstevel@tonic-gate 			udi_size -= toh->len;
37810Sstevel@tonic-gate 		}
37820Sstevel@tonic-gate 		/* Consumed all of allocated space */
37830Sstevel@tonic-gate 		ASSERT(udi_size == 0);
37840Sstevel@tonic-gate 	}
37855240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
37865240Snordmark 	putnext(connp->conn_rq, mp);
37875240Snordmark }
37885240Snordmark 
37895240Snordmark /*
37905240Snordmark  * Handle the results of a T_BIND_REQ whether deferred by IP or handled
37915240Snordmark  * immediately.
37925240Snordmark  */
37935240Snordmark static void
37945240Snordmark icmp_bind_result(conn_t *connp, mblk_t *mp)
37955240Snordmark {
37965240Snordmark 	struct T_error_ack	*tea;
37975240Snordmark 
37985240Snordmark 	switch (mp->b_datap->db_type) {
37995240Snordmark 	case M_PROTO:
38005240Snordmark 	case M_PCPROTO:
38015240Snordmark 		/* M_PROTO messages contain some type of TPI message. */
38025240Snordmark 		if ((mp->b_wptr - mp->b_rptr) < sizeof (t_scalar_t)) {
38035240Snordmark 			freemsg(mp);
38045240Snordmark 			return;
38055240Snordmark 		}
38065240Snordmark 		tea = (struct T_error_ack *)mp->b_rptr;
38075240Snordmark 
38085240Snordmark 		switch (tea->PRIM_type) {
38095240Snordmark 		case T_ERROR_ACK:
38105240Snordmark 			switch (tea->ERROR_prim) {
38115240Snordmark 			case O_T_BIND_REQ:
38125240Snordmark 			case T_BIND_REQ:
38135240Snordmark 				icmp_bind_error(connp, mp);
38145240Snordmark 				return;
38155240Snordmark 			default:
38165240Snordmark 				break;
38175240Snordmark 			}
38185240Snordmark 			ASSERT(0);
38195240Snordmark 			freemsg(mp);
38205240Snordmark 			return;
38215240Snordmark 
38225240Snordmark 		case T_BIND_ACK:
38235240Snordmark 			icmp_bind_ack(connp, mp);
38245240Snordmark 			return;
38255240Snordmark 
38265240Snordmark 		default:
38275240Snordmark 			break;
38285240Snordmark 		}
38295240Snordmark 		freemsg(mp);
38305240Snordmark 		return;
38315240Snordmark 	default:
38325240Snordmark 		/* FIXME: other cases? */
38335240Snordmark 		ASSERT(0);
38345240Snordmark 		freemsg(mp);
38355240Snordmark 		return;
38365240Snordmark 	}
38370Sstevel@tonic-gate }
38380Sstevel@tonic-gate 
38390Sstevel@tonic-gate /*
38400Sstevel@tonic-gate  * Process a T_BIND_ACK
38410Sstevel@tonic-gate  */
38420Sstevel@tonic-gate static void
38435240Snordmark icmp_bind_ack(conn_t *connp, mblk_t *mp)
38440Sstevel@tonic-gate {
38455240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
38460Sstevel@tonic-gate 	mblk_t	*mp1;
38470Sstevel@tonic-gate 	ire_t	*ire;
38480Sstevel@tonic-gate 	struct T_bind_ack *tba;
38490Sstevel@tonic-gate 	uchar_t *addrp;
38500Sstevel@tonic-gate 	ipa_conn_t	*ac;
38510Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
38520Sstevel@tonic-gate 
38535240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
38540Sstevel@tonic-gate 	/*
38550Sstevel@tonic-gate 	 * We know if headers are included or not so we can
38560Sstevel@tonic-gate 	 * safely do this.
38570Sstevel@tonic-gate 	 */
38580Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
38590Sstevel@tonic-gate 		/*
38600Sstevel@tonic-gate 		 * TPI has not yet bound - bind sent by
38610Sstevel@tonic-gate 		 * icmp_bind_proto.
38620Sstevel@tonic-gate 		 */
38630Sstevel@tonic-gate 		freemsg(mp);
38645240Snordmark 		rw_exit(&icmp->icmp_rwlock);
38650Sstevel@tonic-gate 		return;
38660Sstevel@tonic-gate 	}
38675240Snordmark 	ASSERT(icmp->icmp_pending_op != -1);
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate 	/*
38700Sstevel@tonic-gate 	 * If a broadcast/multicast address was bound set
38710Sstevel@tonic-gate 	 * the source address to 0.
38720Sstevel@tonic-gate 	 * This ensures no datagrams with broadcast address
38730Sstevel@tonic-gate 	 * as source address are emitted (which would violate
38740Sstevel@tonic-gate 	 * RFC1122 - Hosts requirements)
38750Sstevel@tonic-gate 	 *
38760Sstevel@tonic-gate 	 * Note that when connecting the returned IRE is
38770Sstevel@tonic-gate 	 * for the destination address and we only perform
38780Sstevel@tonic-gate 	 * the broadcast check for the source address (it
38790Sstevel@tonic-gate 	 * is OK to connect to a broadcast/multicast address.)
38800Sstevel@tonic-gate 	 */
38810Sstevel@tonic-gate 	mp1 = mp->b_cont;
38820Sstevel@tonic-gate 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
38830Sstevel@tonic-gate 		ire = (ire_t *)mp1->b_rptr;
38840Sstevel@tonic-gate 
38850Sstevel@tonic-gate 		/*
38860Sstevel@tonic-gate 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
38870Sstevel@tonic-gate 		 * local address.
38880Sstevel@tonic-gate 		 */
38890Sstevel@tonic-gate 		if (ire->ire_type == IRE_BROADCAST &&
38900Sstevel@tonic-gate 		    icmp->icmp_state != TS_DATA_XFER) {
38915240Snordmark 			ASSERT(icmp->icmp_pending_op == T_BIND_REQ ||
38925240Snordmark 			    icmp->icmp_pending_op == O_T_BIND_REQ);
38930Sstevel@tonic-gate 			/* This was just a local bind to a MC/broadcast addr */
38940Sstevel@tonic-gate 			V6_SET_ZERO(icmp->icmp_v6src);
38950Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6)
38965240Snordmark 				(void) icmp_build_hdrs(icmp);
38970Sstevel@tonic-gate 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
38980Sstevel@tonic-gate 			/*
38990Sstevel@tonic-gate 			 * Local address not yet set - pick it from the
39000Sstevel@tonic-gate 			 * T_bind_ack
39010Sstevel@tonic-gate 			 */
39020Sstevel@tonic-gate 			tba = (struct T_bind_ack *)mp->b_rptr;
39030Sstevel@tonic-gate 			addrp = &mp->b_rptr[tba->ADDR_offset];
39040Sstevel@tonic-gate 			switch (icmp->icmp_family) {
39050Sstevel@tonic-gate 			case AF_INET:
39060Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
39070Sstevel@tonic-gate 					ac = (ipa_conn_t *)addrp;
39080Sstevel@tonic-gate 				} else {
39090Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
39100Sstevel@tonic-gate 					    sizeof (ipa_conn_x_t));
39110Sstevel@tonic-gate 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
39120Sstevel@tonic-gate 				}
39130Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
39140Sstevel@tonic-gate 				    &icmp->icmp_v6src);
39150Sstevel@tonic-gate 				break;
39160Sstevel@tonic-gate 			case AF_INET6:
39170Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
39180Sstevel@tonic-gate 					ac6 = (ipa6_conn_t *)addrp;
39190Sstevel@tonic-gate 				} else {
39200Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
39210Sstevel@tonic-gate 					    sizeof (ipa6_conn_x_t));
39220Sstevel@tonic-gate 					ac6 = &((ipa6_conn_x_t *)
39230Sstevel@tonic-gate 					    addrp)->ac6x_conn;
39240Sstevel@tonic-gate 				}
39250Sstevel@tonic-gate 				icmp->icmp_v6src = ac6->ac6_laddr;
39265240Snordmark 				(void) icmp_build_hdrs(icmp);
39270Sstevel@tonic-gate 			}
39280Sstevel@tonic-gate 		}
39290Sstevel@tonic-gate 		mp1 = mp1->b_cont;
39300Sstevel@tonic-gate 	}
39315240Snordmark 	icmp->icmp_pending_op = -1;
39325240Snordmark 	rw_exit(&icmp->icmp_rwlock);
39330Sstevel@tonic-gate 	/*
39340Sstevel@tonic-gate 	 * Look for one or more appended ACK message added by
39350Sstevel@tonic-gate 	 * icmp_connect or icmp_disconnect.
39360Sstevel@tonic-gate 	 * If none found just send up the T_BIND_ACK.
39370Sstevel@tonic-gate 	 * icmp_connect has appended a T_OK_ACK and a
39380Sstevel@tonic-gate 	 * T_CONN_CON.
39390Sstevel@tonic-gate 	 * icmp_disconnect has appended a T_OK_ACK.
39400Sstevel@tonic-gate 	 */
39410Sstevel@tonic-gate 	if (mp1 != NULL) {
39420Sstevel@tonic-gate 		if (mp->b_cont == mp1)
39430Sstevel@tonic-gate 			mp->b_cont = NULL;
39440Sstevel@tonic-gate 		else {
39450Sstevel@tonic-gate 			ASSERT(mp->b_cont->b_cont == mp1);
39460Sstevel@tonic-gate 			mp->b_cont->b_cont = NULL;
39470Sstevel@tonic-gate 		}
39480Sstevel@tonic-gate 		freemsg(mp);
39490Sstevel@tonic-gate 		mp = mp1;
39500Sstevel@tonic-gate 		while (mp != NULL) {
39510Sstevel@tonic-gate 			mp1 = mp->b_cont;
39520Sstevel@tonic-gate 			mp->b_cont = NULL;
39535240Snordmark 			putnext(connp->conn_rq, mp);
39540Sstevel@tonic-gate 			mp = mp1;
39550Sstevel@tonic-gate 		}
39560Sstevel@tonic-gate 		return;
39570Sstevel@tonic-gate 	}
39580Sstevel@tonic-gate 	freemsg(mp->b_cont);
39590Sstevel@tonic-gate 	mp->b_cont = NULL;
39605240Snordmark 	putnext(connp->conn_rq, mp);
39615240Snordmark }
39625240Snordmark 
39635240Snordmark static void
39645240Snordmark icmp_bind_error(conn_t *connp, mblk_t *mp)
39655240Snordmark {
39665240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
39675240Snordmark 	struct T_error_ack *tea;
39685240Snordmark 
39695240Snordmark 	tea = (struct T_error_ack *)mp->b_rptr;
39705240Snordmark 	/*
39715240Snordmark 	 * If our O_T_BIND_REQ/T_BIND_REQ fails,
39725240Snordmark 	 * clear out the source address before
39735240Snordmark 	 * passing the message upstream.
39745240Snordmark 	 * If this was caused by a T_CONN_REQ
39755240Snordmark 	 * revert back to bound state.
39765240Snordmark 	 */
39775240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
39785240Snordmark 	if (icmp->icmp_state == TS_UNBND) {
39795240Snordmark 		/*
39805240Snordmark 		 * TPI has not yet bound - bind sent by icmp_bind_proto.
39815240Snordmark 		 */
39825240Snordmark 		freemsg(mp);
39835240Snordmark 		rw_exit(&icmp->icmp_rwlock);
39845240Snordmark 		return;
39855240Snordmark 	}
39865240Snordmark 	ASSERT(icmp->icmp_pending_op != -1);
39875240Snordmark 	tea->ERROR_prim = icmp->icmp_pending_op;
39885240Snordmark 	icmp->icmp_pending_op = -1;
39895240Snordmark 
39905240Snordmark 	switch (tea->ERROR_prim) {
39915240Snordmark 	case T_CONN_REQ:
39925240Snordmark 		ASSERT(icmp->icmp_state == TS_DATA_XFER);
39935240Snordmark 		/* Connect failed */
39945240Snordmark 		/* Revert back to the bound source */
39955240Snordmark 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
39965240Snordmark 		icmp->icmp_state = TS_IDLE;
39975240Snordmark 		if (icmp->icmp_family == AF_INET6)
39985240Snordmark 			(void) icmp_build_hdrs(icmp);
39995240Snordmark 		break;
40005240Snordmark 
40015240Snordmark 	case T_DISCON_REQ:
40025240Snordmark 	case T_BIND_REQ:
40035240Snordmark 	case O_T_BIND_REQ:
40045240Snordmark 		V6_SET_ZERO(icmp->icmp_v6src);
40055240Snordmark 		V6_SET_ZERO(icmp->icmp_bound_v6src);
40065240Snordmark 		icmp->icmp_state = TS_UNBND;
40075240Snordmark 		if (icmp->icmp_family == AF_INET6)
40085240Snordmark 			(void) icmp_build_hdrs(icmp);
40095240Snordmark 		break;
40105240Snordmark 	default:
40115240Snordmark 		break;
40125240Snordmark 	}
40135240Snordmark 	rw_exit(&icmp->icmp_rwlock);
40145240Snordmark 	putnext(connp->conn_rq, mp);
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate /*
40180Sstevel@tonic-gate  * return SNMP stuff in buffer in mpdata
40190Sstevel@tonic-gate  */
40205240Snordmark mblk_t *
40210Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl)
40220Sstevel@tonic-gate {
40230Sstevel@tonic-gate 	mblk_t			*mpdata;
40240Sstevel@tonic-gate 	struct opthdr		*optp;
40255240Snordmark 	conn_t			*connp = Q_TO_CONN(q);
40265240Snordmark 	icmp_stack_t		*is = connp->conn_netstack->netstack_icmp;
40275240Snordmark 	mblk_t			*mp2ctl;
40285240Snordmark 
40295240Snordmark 	/*
40305240Snordmark 	 * make a copy of the original message
40315240Snordmark 	 */
40325240Snordmark 	mp2ctl = copymsg(mpctl);
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	if (mpctl == NULL ||
40350Sstevel@tonic-gate 	    (mpdata = mpctl->b_cont) == NULL) {
40365240Snordmark 		freemsg(mpctl);
40375240Snordmark 		freemsg(mp2ctl);
40380Sstevel@tonic-gate 		return (0);
40390Sstevel@tonic-gate 	}
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate 	/* fixed length structure for IPv4 and IPv6 counters */
40420Sstevel@tonic-gate 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
40430Sstevel@tonic-gate 	optp->level = EXPER_RAWIP;
40440Sstevel@tonic-gate 	optp->name = 0;
40455240Snordmark 	(void) snmp_append_data(mpdata, (char *)&is->is_rawip_mib,
40465240Snordmark 	    sizeof (is->is_rawip_mib));
40470Sstevel@tonic-gate 	optp->len = msgdsize(mpdata);
40480Sstevel@tonic-gate 	qreply(q, mpctl);
40490Sstevel@tonic-gate 
40505240Snordmark 	return (mp2ctl);
40510Sstevel@tonic-gate }
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate /*
40540Sstevel@tonic-gate  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
40550Sstevel@tonic-gate  * TODO:  If this ever actually tries to set anything, it needs to be
40560Sstevel@tonic-gate  * to do the appropriate locking.
40570Sstevel@tonic-gate  */
40580Sstevel@tonic-gate /* ARGSUSED */
40595240Snordmark int
40600Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
40610Sstevel@tonic-gate     uchar_t *ptr, int len)
40620Sstevel@tonic-gate {
40630Sstevel@tonic-gate 	switch (level) {
40640Sstevel@tonic-gate 	case EXPER_RAWIP:
40650Sstevel@tonic-gate 		return (0);
40660Sstevel@tonic-gate 	default:
40670Sstevel@tonic-gate 		return (1);
40680Sstevel@tonic-gate 	}
40690Sstevel@tonic-gate }
40700Sstevel@tonic-gate 
40710Sstevel@tonic-gate /* Report for ndd "icmp_status" */
40720Sstevel@tonic-gate /* ARGSUSED */
40730Sstevel@tonic-gate static int
40740Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
40750Sstevel@tonic-gate {
40765240Snordmark 	conn_t  *connp;
40775240Snordmark 	ip_stack_t *ipst;
40780Sstevel@tonic-gate 	char	laddrbuf[INET6_ADDRSTRLEN];
40790Sstevel@tonic-gate 	char	faddrbuf[INET6_ADDRSTRLEN];
40805240Snordmark 	int	i;
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
40830Sstevel@tonic-gate 	    "RAWIP    " MI_COL_HDRPAD_STR
40840Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
40850Sstevel@tonic-gate 	    "  src addr        dest addr       state");
40860Sstevel@tonic-gate 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
40870Sstevel@tonic-gate 
40885240Snordmark 	connp = Q_TO_CONN(q);
40895240Snordmark 	ipst = connp->conn_netstack->netstack_ip;
40905240Snordmark 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
40915240Snordmark 		connf_t *connfp;
40925240Snordmark 		char	*state;
40935240Snordmark 
40945240Snordmark 		connfp = &ipst->ips_ipcl_globalhash_fanout[i];
40955240Snordmark 		connp = NULL;
40965240Snordmark 
40975240Snordmark 		while ((connp = ipcl_get_next_conn(connfp, connp,
40985240Snordmark 		    IPCL_RAWIPCONN)) != NULL) {
40995240Snordmark 			icmp_t  *icmp;
41005240Snordmark 
41015240Snordmark 			mutex_enter(&(connp)->conn_lock);
41025240Snordmark 			icmp = connp->conn_icmp;
41035240Snordmark 
41045240Snordmark 			if (icmp->icmp_state == TS_UNBND)
41055240Snordmark 				state = "UNBOUND";
41065240Snordmark 			else if (icmp->icmp_state == TS_IDLE)
41075240Snordmark 				state = "IDLE";
41085240Snordmark 			else if (icmp->icmp_state == TS_DATA_XFER)
41095240Snordmark 				state = "CONNECTED";
41105240Snordmark 			else
41115240Snordmark 				state = "UnkState";
41125240Snordmark 
41135240Snordmark 			(void) mi_mpprintf(mp, MI_COL_PTRFMT_STR "%s %s %s",
41145240Snordmark 			    (void *)icmp,
41155240Snordmark 			    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
41165240Snordmark 			    sizeof (faddrbuf)),
41175240Snordmark 			    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
41185240Snordmark 			    sizeof (laddrbuf)),
41195240Snordmark 			    state);
41205240Snordmark 			mutex_exit(&(connp)->conn_lock);
41215240Snordmark 		}
41220Sstevel@tonic-gate 	}
41230Sstevel@tonic-gate 	return (0);
41240Sstevel@tonic-gate }
41250Sstevel@tonic-gate 
41260Sstevel@tonic-gate /*
41270Sstevel@tonic-gate  * This routine creates a T_UDERROR_IND message and passes it upstream.
41280Sstevel@tonic-gate  * The address and options are copied from the T_UNITDATA_REQ message
41290Sstevel@tonic-gate  * passed in mp.  This message is freed.
41300Sstevel@tonic-gate  */
41310Sstevel@tonic-gate static void
41320Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
41330Sstevel@tonic-gate {
41340Sstevel@tonic-gate 	mblk_t	*mp1;
41350Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
41360Sstevel@tonic-gate 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
41370Sstevel@tonic-gate 
41380Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
41390Sstevel@tonic-gate 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
41400Sstevel@tonic-gate 	    tudr->OPT_length, err);
41410Sstevel@tonic-gate 	if (mp1)
41420Sstevel@tonic-gate 		qreply(q, mp1);
41430Sstevel@tonic-gate 	freemsg(mp);
41440Sstevel@tonic-gate }
41450Sstevel@tonic-gate 
41460Sstevel@tonic-gate /*
41470Sstevel@tonic-gate  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
41480Sstevel@tonic-gate  * After some error checking, the message is passed downstream to ip.
41490Sstevel@tonic-gate  */
41500Sstevel@tonic-gate static void
41510Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp)
41520Sstevel@tonic-gate {
41535240Snordmark 	icmp_t	*icmp = Q_TO_ICMP(q);
41545240Snordmark 
41555240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
41560Sstevel@tonic-gate 	/* If a bind has not been done, we can't unbind. */
41575240Snordmark 	if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) {
41585240Snordmark 		rw_exit(&icmp->icmp_rwlock);
41590Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
41600Sstevel@tonic-gate 		return;
41610Sstevel@tonic-gate 	}
41625240Snordmark 	icmp->icmp_pending_op = T_UNBIND_REQ;
41635240Snordmark 	rw_exit(&icmp->icmp_rwlock);
41645240Snordmark 
41655240Snordmark 	/*
41665240Snordmark 	 * Pass the unbind to IP; T_UNBIND_REQ is larger than T_OK_ACK
41675240Snordmark 	 * and therefore ip_unbind must never return NULL.
41685240Snordmark 	 */
41695240Snordmark 	mp = ip_unbind(q, mp);
41705240Snordmark 	ASSERT(mp != NULL);
41715240Snordmark 	ASSERT(((struct T_ok_ack *)mp->b_rptr)->PRIM_type == T_OK_ACK);
41725240Snordmark 
41735240Snordmark 	/*
41745240Snordmark 	 * Once we're unbound from IP, the pending operation may be cleared
41755240Snordmark 	 * here.
41765240Snordmark 	 */
41775240Snordmark 	rw_enter(&icmp->icmp_rwlock, RW_WRITER);
41780Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_v6src);
41790Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_bound_v6src);
41805240Snordmark 	icmp->icmp_pending_op = -1;
41810Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
41825240Snordmark 	if (icmp->icmp_family == AF_INET6)
41835240Snordmark 		(void) icmp_build_hdrs(icmp);
41845240Snordmark 	rw_exit(&icmp->icmp_rwlock);
41855240Snordmark 
41865240Snordmark 	qreply(q, mp);
41870Sstevel@tonic-gate }
41880Sstevel@tonic-gate 
41890Sstevel@tonic-gate /*
41900Sstevel@tonic-gate  * Process IPv4 packets that already include an IP header.
41910Sstevel@tonic-gate  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
41920Sstevel@tonic-gate  * IPPROTO_IGMP).
41930Sstevel@tonic-gate  */
41940Sstevel@tonic-gate static void
41955240Snordmark icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp, ip4_pkt_t *pktinfop)
41960Sstevel@tonic-gate {
41973448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
41980Sstevel@tonic-gate 	ipha_t	*ipha;
41990Sstevel@tonic-gate 	int	ip_hdr_length;
42000Sstevel@tonic-gate 	int	tp_hdr_len;
42010Sstevel@tonic-gate 	mblk_t	*mp1;
42020Sstevel@tonic-gate 	uint_t	pkt_len;
42033318Srshoaib 	ip_opt_info_t optinfo;
42045240Snordmark 	conn_t	*connp = icmp->icmp_connp;
42053318Srshoaib 
42063318Srshoaib 	optinfo.ip_opt_flags = 0;
42073318Srshoaib 	optinfo.ip_opt_ill_index = 0;
42080Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
42090Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
42100Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
42110Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
42123448Sdh155122 			ASSERT(icmp != NULL);
42135240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
42140Sstevel@tonic-gate 			freemsg(mp);
42150Sstevel@tonic-gate 			return;
42160Sstevel@tonic-gate 		}
42170Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
42180Sstevel@tonic-gate 	}
42190Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length =
42200Sstevel@tonic-gate 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
42210Sstevel@tonic-gate 
42220Sstevel@tonic-gate 	/*
42230Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
42240Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
42250Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
42260Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
42270Sstevel@tonic-gate 	 */
42280Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
42290Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
42300Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
42310Sstevel@tonic-gate 	/* Insert options if any */
42320Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
42330Sstevel@tonic-gate 		/*
42340Sstevel@tonic-gate 		 * Put the IP header plus any transport header that is
42350Sstevel@tonic-gate 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
42360Sstevel@tonic-gate 		 * that at least the checksum field is in the first mblk.)
42370Sstevel@tonic-gate 		 */
42380Sstevel@tonic-gate 		switch (ipha->ipha_protocol) {
42390Sstevel@tonic-gate 		case IPPROTO_UDP:
42400Sstevel@tonic-gate 			tp_hdr_len = 8;
42410Sstevel@tonic-gate 			break;
42420Sstevel@tonic-gate 		case IPPROTO_TCP:
42430Sstevel@tonic-gate 			tp_hdr_len = 20;
42440Sstevel@tonic-gate 			break;
42450Sstevel@tonic-gate 		default:
42460Sstevel@tonic-gate 			tp_hdr_len = 0;
42470Sstevel@tonic-gate 			break;
42480Sstevel@tonic-gate 		}
42490Sstevel@tonic-gate 		/*
42500Sstevel@tonic-gate 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
42510Sstevel@tonic-gate 		 * tp_hdr_len bytes will be in a single mblk.
42520Sstevel@tonic-gate 		 */
42530Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
42540Sstevel@tonic-gate 		    tp_hdr_len)) {
42550Sstevel@tonic-gate 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
42560Sstevel@tonic-gate 			    tp_hdr_len)) {
42575240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
42583448Sdh155122 				    rawipOutErrors);
42590Sstevel@tonic-gate 				freemsg(mp);
42600Sstevel@tonic-gate 				return;
42610Sstevel@tonic-gate 			}
42620Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
42630Sstevel@tonic-gate 		}
42640Sstevel@tonic-gate 
42650Sstevel@tonic-gate 		/*
42660Sstevel@tonic-gate 		 * if the length is larger then the max allowed IP packet,
42670Sstevel@tonic-gate 		 * then send an error and abort the processing.
42680Sstevel@tonic-gate 		 */
42690Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length)
42700Sstevel@tonic-gate 		    + icmp->icmp_ip_snd_options_len;
42710Sstevel@tonic-gate 		if (pkt_len > IP_MAXPACKET) {
42720Sstevel@tonic-gate 			icmp_ud_err(q, mp, EMSGSIZE);
42730Sstevel@tonic-gate 			return;
42740Sstevel@tonic-gate 		}
42753448Sdh155122 		if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra +
42760Sstevel@tonic-gate 		    tp_hdr_len, BPRI_LO))) {
42770Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
42780Sstevel@tonic-gate 			return;
42790Sstevel@tonic-gate 		}
42803448Sdh155122 		mp1->b_rptr += is->is_wroff_extra;
42810Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
42820Sstevel@tonic-gate 
42830Sstevel@tonic-gate 		ipha->ipha_length = htons((uint16_t)pkt_len);
42840Sstevel@tonic-gate 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
42850Sstevel@tonic-gate 
42860Sstevel@tonic-gate 		/* Copy transport header if any */
42870Sstevel@tonic-gate 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
42880Sstevel@tonic-gate 		mp1->b_wptr += tp_hdr_len;
42890Sstevel@tonic-gate 
42900Sstevel@tonic-gate 		/* Add options */
42910Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_rptr;
42920Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
42930Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len);
42940Sstevel@tonic-gate 
42950Sstevel@tonic-gate 		/* Drop IP header and transport header from original */
42960Sstevel@tonic-gate 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
42970Sstevel@tonic-gate 
42980Sstevel@tonic-gate 		mp1->b_cont = mp;
42990Sstevel@tonic-gate 		mp = mp1;
43000Sstevel@tonic-gate 		/*
43010Sstevel@tonic-gate 		 * Massage source route putting first source
43020Sstevel@tonic-gate 		 * route in ipha_dst.
43030Sstevel@tonic-gate 		 */
43045240Snordmark 		(void) ip_massage_options(ipha, is->is_netstack);
43050Sstevel@tonic-gate 	}
43063318Srshoaib 
43073318Srshoaib 	if (pktinfop != NULL) {
43083318Srshoaib 		/*
43093318Srshoaib 		 * Over write the source address provided in the header
43103318Srshoaib 		 */
43113318Srshoaib 		if (pktinfop->ip4_addr != INADDR_ANY) {
43123318Srshoaib 			ipha->ipha_src = pktinfop->ip4_addr;
43133318Srshoaib 			optinfo.ip_opt_flags = IP_VERIFY_SRC;
43143318Srshoaib 		}
43153318Srshoaib 
43163318Srshoaib 		if (pktinfop->ip4_ill_index != 0) {
43173318Srshoaib 			optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
43183318Srshoaib 		}
43193318Srshoaib 	}
43203318Srshoaib 
43215240Snordmark 	mblk_setcred(mp, connp->conn_cred);
43225240Snordmark 	ip_output_options(connp, mp, q, IP_WPUT,
43235240Snordmark 	    &optinfo);
43240Sstevel@tonic-gate }
43250Sstevel@tonic-gate 
43261676Sjpk static boolean_t
43271676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst)
43281676Sjpk {
43291676Sjpk 	int err;
43301676Sjpk 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
43315240Snordmark 	icmp_stack_t		*is = icmp->icmp_is;
43325240Snordmark 	conn_t	*connp = icmp->icmp_connp;
43335240Snordmark 
43345240Snordmark 	err = tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst,
43353448Sdh155122 	    opt_storage, icmp->icmp_mac_exempt,
43365240Snordmark 	    is->is_netstack->netstack_ip);
43371676Sjpk 	if (err == 0) {
43381676Sjpk 		err = tsol_update_options(&icmp->icmp_ip_snd_options,
43391676Sjpk 		    &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len,
43401676Sjpk 		    opt_storage);
43411676Sjpk 	}
43421676Sjpk 	if (err != 0) {
43435240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
43441676Sjpk 		DTRACE_PROBE4(
43451676Sjpk 		    tx__ip__log__drop__updatelabel__icmp,
43461676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
43471676Sjpk 		    queue_t *, q, char *, opt_storage, mblk_t *, mp);
43481676Sjpk 		icmp_ud_err(q, mp, err);
43491676Sjpk 		return (B_FALSE);
43501676Sjpk 	}
43511676Sjpk 	IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst);
43521676Sjpk 	return (B_TRUE);
43531676Sjpk }
43541676Sjpk 
43550Sstevel@tonic-gate /*
43560Sstevel@tonic-gate  * This routine handles all messages passed downstream.  It either
43570Sstevel@tonic-gate  * consumes the message or passes it downstream; it never queues a
43580Sstevel@tonic-gate  * a message.
43590Sstevel@tonic-gate  */
43600Sstevel@tonic-gate static void
43610Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp)
43620Sstevel@tonic-gate {
43630Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
43640Sstevel@tonic-gate 	ipha_t	*ipha;
43650Sstevel@tonic-gate 	mblk_t	*mp1;
43660Sstevel@tonic-gate 	int	ip_hdr_length;
43670Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
43680Sstevel@tonic-gate 	size_t	ip_len;
43695240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
43705240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
43713448Sdh155122 	icmp_stack_t *is = icmp->icmp_is;
43720Sstevel@tonic-gate 	sin6_t	*sin6;
43730Sstevel@tonic-gate 	sin_t	*sin;
43740Sstevel@tonic-gate 	ipaddr_t	v4dst;
43753318Srshoaib 	ip4_pkt_t	pktinfo;
43763318Srshoaib 	ip4_pkt_t	*pktinfop = &pktinfo;
43773318Srshoaib 	ip_opt_info_t	optinfo;
43780Sstevel@tonic-gate 
43790Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
43800Sstevel@tonic-gate 	case M_DATA:
43810Sstevel@tonic-gate 		if (icmp->icmp_hdrincl) {
43820Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
43831676Sjpk 			ipha = (ipha_t *)mp->b_rptr;
43841676Sjpk 			if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
43851676Sjpk 				if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
43865240Snordmark 					BUMP_MIB(&is->is_rawip_mib,
43873448Sdh155122 					    rawipOutErrors);
43881676Sjpk 					freemsg(mp);
43891676Sjpk 					return;
43901676Sjpk 				}
43911676Sjpk 				ipha = (ipha_t *)mp->b_rptr;
43921676Sjpk 			}
43931676Sjpk 			/*
43941676Sjpk 			 * If this connection was used for v6 (inconceivable!)
43951676Sjpk 			 * or if we have a new destination, then it's time to
43961676Sjpk 			 * figure a new label.
43971676Sjpk 			 */
43981676Sjpk 			if (is_system_labeled() &&
43991676Sjpk 			    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
44001676Sjpk 			    V4_PART_OF_V6(icmp->icmp_v6lastdst) !=
44011676Sjpk 			    ipha->ipha_dst) &&
44021676Sjpk 			    !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) {
44031676Sjpk 				return;
44041676Sjpk 			}
44055240Snordmark 			icmp_wput_hdrincl(q, mp, icmp, NULL);
44060Sstevel@tonic-gate 			return;
44070Sstevel@tonic-gate 		}
44080Sstevel@tonic-gate 		freemsg(mp);
44090Sstevel@tonic-gate 		return;
44100Sstevel@tonic-gate 	case M_PROTO:
44110Sstevel@tonic-gate 	case M_PCPROTO:
44120Sstevel@tonic-gate 		ip_len = mp->b_wptr - rptr;
44130Sstevel@tonic-gate 		if (ip_len >= sizeof (struct T_unitdata_req)) {
44140Sstevel@tonic-gate 			/* Expedite valid T_UNITDATA_REQ to below the switch */
44150Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type
44160Sstevel@tonic-gate 			    == T_UNITDATA_REQ)
44170Sstevel@tonic-gate 				break;
44180Sstevel@tonic-gate 		}
44190Sstevel@tonic-gate 		/* FALLTHRU */
44200Sstevel@tonic-gate 	default:
44210Sstevel@tonic-gate 		icmp_wput_other(q, mp);
44220Sstevel@tonic-gate 		return;
44230Sstevel@tonic-gate 	}
44240Sstevel@tonic-gate 
44250Sstevel@tonic-gate 	/* Handle T_UNITDATA_REQ messages here. */
44260Sstevel@tonic-gate 
44273318Srshoaib 
44283318Srshoaib 
44290Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
44300Sstevel@tonic-gate 		/* If a port has not been bound to the stream, fail. */
44315240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44320Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
44330Sstevel@tonic-gate 		return;
44340Sstevel@tonic-gate 	}
44350Sstevel@tonic-gate 	mp1 = mp->b_cont;
44360Sstevel@tonic-gate 	if (mp1 == NULL) {
44375240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44380Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
44390Sstevel@tonic-gate 		return;
44400Sstevel@tonic-gate 	}
44410Sstevel@tonic-gate 
44420Sstevel@tonic-gate 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
44435240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44440Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
44450Sstevel@tonic-gate 		return;
44460Sstevel@tonic-gate 	}
44470Sstevel@tonic-gate 
44480Sstevel@tonic-gate 	switch (icmp->icmp_family) {
44490Sstevel@tonic-gate 	case AF_INET6:
44500Sstevel@tonic-gate 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
44510Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin6) ||
44520Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin6_t) ||
44530Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
44545240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44550Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44560Sstevel@tonic-gate 			return;
44570Sstevel@tonic-gate 		}
44580Sstevel@tonic-gate 
44590Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
44600Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
44615240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44620Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44630Sstevel@tonic-gate 			return;
44640Sstevel@tonic-gate 		}
44650Sstevel@tonic-gate 
44660Sstevel@tonic-gate 		/*
44670Sstevel@tonic-gate 		 * Destination is a native IPv6 address.
44680Sstevel@tonic-gate 		 * Send out an IPv6 format packet.
44690Sstevel@tonic-gate 		 */
44700Sstevel@tonic-gate 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
44710Sstevel@tonic-gate 		return;
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate 	case AF_INET:
44740Sstevel@tonic-gate 		sin = (sin_t *)&rptr[tudr->DEST_offset];
44750Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin) ||
44760Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin_t) ||
44770Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
44785240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
44790Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
44800Sstevel@tonic-gate 			return;
44810Sstevel@tonic-gate 		}
44820Sstevel@tonic-gate 		/* Extract and ipaddr */
44830Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
44840Sstevel@tonic-gate 		break;
44851676Sjpk 
44861676Sjpk 	default:
44871676Sjpk 		ASSERT(0);
44880Sstevel@tonic-gate 	}
44890Sstevel@tonic-gate 
44903318Srshoaib 	pktinfop->ip4_ill_index = 0;
44913318Srshoaib 	pktinfop->ip4_addr = INADDR_ANY;
44923318Srshoaib 	optinfo.ip_opt_flags = 0;
44933318Srshoaib 	optinfo.ip_opt_ill_index = 0;
44943318Srshoaib 
44953318Srshoaib 
44960Sstevel@tonic-gate 	/*
44970Sstevel@tonic-gate 	 * If options passed in, feed it for verification and handling
44980Sstevel@tonic-gate 	 */
44990Sstevel@tonic-gate 	if (tudr->OPT_length != 0) {
45000Sstevel@tonic-gate 		int error;
45010Sstevel@tonic-gate 
45023318Srshoaib 		error = 0;
45030Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
45043318Srshoaib 		    (void *)pktinfop) < 0) {
45050Sstevel@tonic-gate 			/* failure */
45065240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
45070Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
45080Sstevel@tonic-gate 			return;
45090Sstevel@tonic-gate 		}
45103318Srshoaib 		ASSERT(error == 0);
45110Sstevel@tonic-gate 		/*
45120Sstevel@tonic-gate 		 * Note: Success in processing options.
45130Sstevel@tonic-gate 		 * mp option buffer represented by
45140Sstevel@tonic-gate 		 * OPT_length/offset now potentially modified
45150Sstevel@tonic-gate 		 * and contain option setting results
45160Sstevel@tonic-gate 		 */
45173318Srshoaib 
45180Sstevel@tonic-gate 	}
45190Sstevel@tonic-gate 
45201676Sjpk 	if (v4dst == INADDR_ANY)
45211676Sjpk 		v4dst = htonl(INADDR_LOOPBACK);
45221676Sjpk 
45231676Sjpk 	/* Check if our saved options are valid; update if not */
45241676Sjpk 	if (is_system_labeled() &&
45251676Sjpk 	    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
45261676Sjpk 	    V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) &&
45271676Sjpk 	    !icmp_update_label(q, icmp, mp, v4dst)) {
45281676Sjpk 		return;
45291676Sjpk 	}
45301676Sjpk 
45310Sstevel@tonic-gate 	/* Protocol 255 contains full IP headers */
45320Sstevel@tonic-gate 	if (icmp->icmp_hdrincl) {
45330Sstevel@tonic-gate 		freeb(mp);
45345240Snordmark 		icmp_wput_hdrincl(q, mp1, icmp, pktinfop);
45350Sstevel@tonic-gate 		return;
45360Sstevel@tonic-gate 	}
45371676Sjpk 
45383318Srshoaib 
45390Sstevel@tonic-gate 	/* Add an IP header */
45400Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
45410Sstevel@tonic-gate 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
45420Sstevel@tonic-gate 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
45430Sstevel@tonic-gate 	    mp1->b_datap->db_ref != 1 ||
45440Sstevel@tonic-gate 	    !OK_32PTR(ipha)) {
45453448Sdh155122 		if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra,
45460Sstevel@tonic-gate 		    BPRI_LO))) {
45475240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
45481289Sja97890 			icmp_ud_err(q, mp, ENOMEM);
45490Sstevel@tonic-gate 			return;
45500Sstevel@tonic-gate 		}
45510Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
45520Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_datap->db_lim;
45530Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)ipha;
45540Sstevel@tonic-gate 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
45550Sstevel@tonic-gate 	}
45560Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
45570Sstevel@tonic-gate 	/* Set version, header length, and tos */
45580Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
45590Sstevel@tonic-gate 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
45605240Snordmark 	    icmp->icmp_type_of_service);
45610Sstevel@tonic-gate 	/* Set ttl and protocol */
45620Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
45630Sstevel@tonic-gate #else
45640Sstevel@tonic-gate 	/* Set version, header length, and tos */
45650Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
45660Sstevel@tonic-gate 	    ((icmp->icmp_type_of_service << 8) |
45675240Snordmark 	    ((IP_VERSION << 4) | (ip_hdr_length>>2)));
45680Sstevel@tonic-gate 	/* Set ttl and protocol */
45690Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
45700Sstevel@tonic-gate #endif
45713318Srshoaib 	if (pktinfop->ip4_addr != INADDR_ANY) {
45723318Srshoaib 		ipha->ipha_src = pktinfop->ip4_addr;
45733318Srshoaib 		optinfo.ip_opt_flags = IP_VERIFY_SRC;
45743318Srshoaib 	} else {
45753318Srshoaib 
45763318Srshoaib 		/*
45773318Srshoaib 		 * Copy our address into the packet.  If this is zero,
45783318Srshoaib 		 * ip will fill in the real source address.
45793318Srshoaib 		 */
45803318Srshoaib 		IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
45813318Srshoaib 	}
45823318Srshoaib 
45830Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
45840Sstevel@tonic-gate 
45853318Srshoaib 	if (pktinfop->ip4_ill_index != 0) {
45863318Srshoaib 		optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
45873318Srshoaib 	}
45883318Srshoaib 
45893318Srshoaib 
45900Sstevel@tonic-gate 	/*
45910Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
45920Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
45930Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
45940Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
45950Sstevel@tonic-gate 	 */
45960Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
45970Sstevel@tonic-gate 
45980Sstevel@tonic-gate 	/* Finish common formatting of the packet. */
45990Sstevel@tonic-gate 	mp1->b_rptr = (uchar_t *)ipha;
46000Sstevel@tonic-gate 
46010Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
46020Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
46030Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
46040Sstevel@tonic-gate 
46050Sstevel@tonic-gate 	/*
46060Sstevel@tonic-gate 	 * Set the length into the IP header.
46070Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
46080Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
46090Sstevel@tonic-gate 	 * as this can cause problems in layers below.
46100Sstevel@tonic-gate 	 */
46110Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
46125240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46130Sstevel@tonic-gate 		icmp_ud_err(q, mp, EMSGSIZE);
46140Sstevel@tonic-gate 		return;
46150Sstevel@tonic-gate 	}
46160Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)ip_len);
46170Sstevel@tonic-gate 	/*
46180Sstevel@tonic-gate 	 * Copy in the destination address from the T_UNITDATA
46190Sstevel@tonic-gate 	 * request
46200Sstevel@tonic-gate 	 */
46211676Sjpk 	ipha->ipha_dst = v4dst;
46220Sstevel@tonic-gate 
46230Sstevel@tonic-gate 	/*
46240Sstevel@tonic-gate 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
46250Sstevel@tonic-gate 	 */
46260Sstevel@tonic-gate 	if (CLASSD(v4dst))
46270Sstevel@tonic-gate 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
46280Sstevel@tonic-gate 
46290Sstevel@tonic-gate 	/* Copy in options if any */
46300Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
46310Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options,
46320Sstevel@tonic-gate 		    &ipha[1], icmp->icmp_ip_snd_options_len);
46330Sstevel@tonic-gate 		/*
46340Sstevel@tonic-gate 		 * Massage source route putting first source route in ipha_dst.
46350Sstevel@tonic-gate 		 * Ignore the destination in the T_unitdata_req.
46360Sstevel@tonic-gate 		 */
46375240Snordmark 		(void) ip_massage_options(ipha, is->is_netstack);
46380Sstevel@tonic-gate 	}
46393318Srshoaib 
46400Sstevel@tonic-gate 	freeb(mp);
46415240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
46425240Snordmark 	mblk_setcred(mp1, connp->conn_cred);
46435240Snordmark 	ip_output_options(Q_TO_CONN(q), mp1, q, IP_WPUT, &optinfo);
46440Sstevel@tonic-gate #undef	ipha
46450Sstevel@tonic-gate #undef tudr
46460Sstevel@tonic-gate }
46470Sstevel@tonic-gate 
46481676Sjpk static boolean_t
46491676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst)
46501676Sjpk {
46511676Sjpk 	int err;
46521676Sjpk 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
46535240Snordmark 	icmp_stack_t		*is = icmp->icmp_is;
46545240Snordmark 	conn_t	*connp = icmp->icmp_connp;
46555240Snordmark 
46565240Snordmark 	err = tsol_compute_label_v6(DB_CREDDEF(mp, connp->conn_cred), dst,
46573448Sdh155122 	    opt_storage, icmp->icmp_mac_exempt,
46585240Snordmark 	    is->is_netstack->netstack_ip);
46591676Sjpk 	if (err == 0) {
46601676Sjpk 		err = tsol_update_sticky(&icmp->icmp_sticky_ipp,
46611676Sjpk 		    &icmp->icmp_label_len_v6, opt_storage);
46621676Sjpk 	}
46631676Sjpk 	if (err != 0) {
46645240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
46651676Sjpk 		DTRACE_PROBE4(
46661676Sjpk 		    tx__ip__log__drop__updatelabel__icmp6,
46671676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
46681676Sjpk 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
46691676Sjpk 		icmp_ud_err(wq, mp, err);
46701676Sjpk 		return (B_FALSE);
46711676Sjpk 	}
46721676Sjpk 
46731676Sjpk 	icmp->icmp_v6lastdst = *dst;
46741676Sjpk 	return (B_TRUE);
46751676Sjpk }
46761676Sjpk 
46770Sstevel@tonic-gate /*
46780Sstevel@tonic-gate  * icmp_wput_ipv6():
46790Sstevel@tonic-gate  * Assumes that icmp_wput did some sanity checking on the destination
46801676Sjpk  * address, but that the label may not yet be correct.
46810Sstevel@tonic-gate  */
46820Sstevel@tonic-gate void
46830Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
46840Sstevel@tonic-gate {
46850Sstevel@tonic-gate 	ip6_t			*ip6h;
46860Sstevel@tonic-gate 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
46870Sstevel@tonic-gate 	mblk_t			*mp1;
46880Sstevel@tonic-gate 	int			ip_hdr_len = IPV6_HDR_LEN;
46890Sstevel@tonic-gate 	size_t			ip_len;
46905240Snordmark 	icmp_t			*icmp = Q_TO_ICMP(q);
46913448Sdh155122 	icmp_stack_t		*is = icmp->icmp_is;
46920Sstevel@tonic-gate 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
46930Sstevel@tonic-gate 	ip6_pkt_t		*ipp = &ipp_s;
46940Sstevel@tonic-gate 	ip6_pkt_t		*tipp;
46950Sstevel@tonic-gate 	uint32_t		csum = 0;
46960Sstevel@tonic-gate 	uint_t			ignore = 0;
46970Sstevel@tonic-gate 	uint_t			option_exists = 0, is_sticky = 0;
46980Sstevel@tonic-gate 	uint8_t			*cp;
46990Sstevel@tonic-gate 	uint8_t			*nxthdr_ptr;
47001676Sjpk 	in6_addr_t		ip6_dst;
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	/*
47030Sstevel@tonic-gate 	 * If the local address is a mapped address return
47040Sstevel@tonic-gate 	 * an error.
47050Sstevel@tonic-gate 	 * It would be possible to send an IPv6 packet but the
47060Sstevel@tonic-gate 	 * response would never make it back to the application
47070Sstevel@tonic-gate 	 * since it is bound to a mapped address.
47080Sstevel@tonic-gate 	 */
47090Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
47105240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
47110Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
47120Sstevel@tonic-gate 		return;
47130Sstevel@tonic-gate 	}
47140Sstevel@tonic-gate 
47150Sstevel@tonic-gate 	ipp->ipp_fields = 0;
47160Sstevel@tonic-gate 	ipp->ipp_sticky_ignored = 0;
47170Sstevel@tonic-gate 
47180Sstevel@tonic-gate 	/*
47190Sstevel@tonic-gate 	 * If TPI options passed in, feed it for verification and handling
47200Sstevel@tonic-gate 	 */
47210Sstevel@tonic-gate 	if (tudr_optlen != 0) {
47220Sstevel@tonic-gate 		int error;
47230Sstevel@tonic-gate 
47240Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
47250Sstevel@tonic-gate 		    (void *)ipp) < 0) {
47260Sstevel@tonic-gate 			/* failure */
47275240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
47280Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
47290Sstevel@tonic-gate 			return;
47300Sstevel@tonic-gate 		}
47310Sstevel@tonic-gate 		ignore = ipp->ipp_sticky_ignored;
47320Sstevel@tonic-gate 		ASSERT(error == 0);
47330Sstevel@tonic-gate 	}
47340Sstevel@tonic-gate 
47350Sstevel@tonic-gate 	if (sin6->sin6_scope_id != 0 &&
47360Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
47370Sstevel@tonic-gate 		/*
47380Sstevel@tonic-gate 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
47390Sstevel@tonic-gate 		 * option nor ancillary data.  It needs to be
47400Sstevel@tonic-gate 		 * explicitly set in options_exists.
47410Sstevel@tonic-gate 		 */
47420Sstevel@tonic-gate 		option_exists |= IPPF_SCOPE_ID;
47430Sstevel@tonic-gate 	}
47440Sstevel@tonic-gate 
47451676Sjpk 	/*
47461676Sjpk 	 * Compute the destination address
47471676Sjpk 	 */
47481676Sjpk 	ip6_dst = sin6->sin6_addr;
47491676Sjpk 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
47501676Sjpk 		ip6_dst = ipv6_loopback;
47511676Sjpk 
47521676Sjpk 	/*
47531676Sjpk 	 * If we're not going to the same destination as last time, then
47541676Sjpk 	 * recompute the label required.  This is done in a separate routine to
47551676Sjpk 	 * avoid blowing up our stack here.
47561676Sjpk 	 */
47571676Sjpk 	if (is_system_labeled() &&
47581676Sjpk 	    !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) &&
47591676Sjpk 	    !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) {
47601676Sjpk 		return;
47611676Sjpk 	}
47621676Sjpk 
47631676Sjpk 	/*
47641676Sjpk 	 * If there's a security label here, then we ignore any options the
47651676Sjpk 	 * user may try to set.  We keep the peer's label as a hidden sticky
47661676Sjpk 	 * option.
47671676Sjpk 	 */
47681676Sjpk 	if (icmp->icmp_label_len_v6 > 0) {
47691676Sjpk 		ignore &= ~IPPF_HOPOPTS;
47701676Sjpk 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
47711676Sjpk 	}
47721676Sjpk 
47730Sstevel@tonic-gate 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
47740Sstevel@tonic-gate 	    (ipp->ipp_fields == 0)) {
47750Sstevel@tonic-gate 		/* No sticky options nor ancillary data. */
47760Sstevel@tonic-gate 		goto no_options;
47770Sstevel@tonic-gate 	}
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 	/*
47800Sstevel@tonic-gate 	 * Go through the options figuring out where each is going to
47810Sstevel@tonic-gate 	 * come from and build two masks.  The first mask indicates if
47820Sstevel@tonic-gate 	 * the option exists at all.  The second mask indicates if the
47830Sstevel@tonic-gate 	 * option is sticky or ancillary.
47840Sstevel@tonic-gate 	 */
47850Sstevel@tonic-gate 	if (!(ignore & IPPF_HOPOPTS)) {
47860Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
47870Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
47880Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_hopoptslen;
47890Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
47900Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
47910Sstevel@tonic-gate 			is_sticky |= IPPF_HOPOPTS;
47920Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
47930Sstevel@tonic-gate 		}
47940Sstevel@tonic-gate 	}
47950Sstevel@tonic-gate 
47960Sstevel@tonic-gate 	if (!(ignore & IPPF_RTHDR)) {
47970Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTHDR) {
47980Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
47990Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rthdrlen;
48000Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
48010Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
48020Sstevel@tonic-gate 			is_sticky |= IPPF_RTHDR;
48030Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
48040Sstevel@tonic-gate 		}
48050Sstevel@tonic-gate 	}
48060Sstevel@tonic-gate 
48070Sstevel@tonic-gate 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
48080Sstevel@tonic-gate 		/*
48090Sstevel@tonic-gate 		 * Need to have a router header to use these.
48100Sstevel@tonic-gate 		 */
48110Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
48120Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
48130Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rtdstoptslen;
48140Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
48150Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
48160Sstevel@tonic-gate 			is_sticky |= IPPF_RTDSTOPTS;
48170Sstevel@tonic-gate 			ip_hdr_len +=
48180Sstevel@tonic-gate 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
48190Sstevel@tonic-gate 		}
48200Sstevel@tonic-gate 	}
48210Sstevel@tonic-gate 
48220Sstevel@tonic-gate 	if (!(ignore & IPPF_DSTOPTS)) {
48230Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
48240Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
48250Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_dstoptslen;
48260Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
48270Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
48280Sstevel@tonic-gate 			is_sticky |= IPPF_DSTOPTS;
48290Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
48300Sstevel@tonic-gate 		}
48310Sstevel@tonic-gate 	}
48320Sstevel@tonic-gate 
48330Sstevel@tonic-gate 	if (!(ignore & IPPF_IFINDEX)) {
48340Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_IFINDEX) {
48350Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
48360Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
48370Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
48380Sstevel@tonic-gate 			is_sticky |= IPPF_IFINDEX;
48390Sstevel@tonic-gate 		}
48400Sstevel@tonic-gate 	}
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate 	if (!(ignore & IPPF_ADDR)) {
48430Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_ADDR) {
48440Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
48450Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
48460Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
48470Sstevel@tonic-gate 			is_sticky |= IPPF_ADDR;
48480Sstevel@tonic-gate 		}
48490Sstevel@tonic-gate 	}
48500Sstevel@tonic-gate 
48510Sstevel@tonic-gate 	if (!(ignore & IPPF_DONTFRAG)) {
48520Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
48530Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
48540Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
48550Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
48560Sstevel@tonic-gate 			is_sticky |= IPPF_DONTFRAG;
48570Sstevel@tonic-gate 		}
48580Sstevel@tonic-gate 	}
48590Sstevel@tonic-gate 
48600Sstevel@tonic-gate 	if (!(ignore & IPPF_USE_MIN_MTU)) {
48610Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
48620Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
48630Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
48640Sstevel@tonic-gate 		    IPPF_USE_MIN_MTU) {
48650Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
48660Sstevel@tonic-gate 			is_sticky |= IPPF_USE_MIN_MTU;
48670Sstevel@tonic-gate 		}
48680Sstevel@tonic-gate 	}
48690Sstevel@tonic-gate 
48700Sstevel@tonic-gate 	if (!(ignore & IPPF_NEXTHOP)) {
48710Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
48720Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
48730Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
48740Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
48750Sstevel@tonic-gate 			is_sticky |= IPPF_NEXTHOP;
48760Sstevel@tonic-gate 		}
48770Sstevel@tonic-gate 	}
48780Sstevel@tonic-gate 
4879679Sseb 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4880679Sseb 		option_exists |= IPPF_HOPLIMIT;
4881679Sseb 	/* IPV6_HOPLIMIT can never be sticky */
4882679Sseb 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4883679Sseb 
4884679Sseb 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4885679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4886679Sseb 		option_exists |= IPPF_UNICAST_HOPS;
4887679Sseb 		is_sticky |= IPPF_UNICAST_HOPS;
4888679Sseb 	}
4889679Sseb 
4890679Sseb 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4891679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4892679Sseb 		option_exists |= IPPF_MULTICAST_HOPS;
4893679Sseb 		is_sticky |= IPPF_MULTICAST_HOPS;
48940Sstevel@tonic-gate 	}
48950Sstevel@tonic-gate 
48960Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
48970Sstevel@tonic-gate 		/* This is a sticky socket option only */
48980Sstevel@tonic-gate 		option_exists |= IPPF_NO_CKSUM;
48990Sstevel@tonic-gate 		is_sticky |= IPPF_NO_CKSUM;
49000Sstevel@tonic-gate 	}
49010Sstevel@tonic-gate 
49020Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
49030Sstevel@tonic-gate 		/* This is a sticky socket option only */
49040Sstevel@tonic-gate 		option_exists |= IPPF_RAW_CKSUM;
49050Sstevel@tonic-gate 		is_sticky |= IPPF_RAW_CKSUM;
49060Sstevel@tonic-gate 	}
49070Sstevel@tonic-gate 
49080Sstevel@tonic-gate 	if (!(ignore & IPPF_TCLASS)) {
49090Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_TCLASS) {
49100Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
49110Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
49120Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
49130Sstevel@tonic-gate 			is_sticky |= IPPF_TCLASS;
49140Sstevel@tonic-gate 		}
49150Sstevel@tonic-gate 	}
49160Sstevel@tonic-gate 
49170Sstevel@tonic-gate no_options:
49180Sstevel@tonic-gate 
49190Sstevel@tonic-gate 	/*
49200Sstevel@tonic-gate 	 * If any options carried in the ip6i_t were specified, we
49210Sstevel@tonic-gate 	 * need to account for the ip6i_t in the data we'll be sending
49220Sstevel@tonic-gate 	 * down.
49230Sstevel@tonic-gate 	 */
49240Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I)
49250Sstevel@tonic-gate 		ip_hdr_len += sizeof (ip6i_t);
49260Sstevel@tonic-gate 
49270Sstevel@tonic-gate 	/* check/fix buffer config, setup pointers into it */
49280Sstevel@tonic-gate 	mp1 = mp->b_cont;
49290Sstevel@tonic-gate 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
49300Sstevel@tonic-gate 	if ((mp1->b_datap->db_ref != 1) ||
49310Sstevel@tonic-gate 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
49320Sstevel@tonic-gate 	    !OK_32PTR(ip6h)) {
49330Sstevel@tonic-gate 		/* Try to get everything in a single mblk next time */
49340Sstevel@tonic-gate 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
49350Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = ip_hdr_len;
49360Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q),
49373448Sdh155122 			    icmp->icmp_max_hdr_len + is->is_wroff_extra);
49380Sstevel@tonic-gate 		}
49393448Sdh155122 		mp1 = allocb(ip_hdr_len + is->is_wroff_extra, BPRI_LO);
49400Sstevel@tonic-gate 		if (!mp1) {
49415240Snordmark 			BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
49420Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
49430Sstevel@tonic-gate 			return;
49440Sstevel@tonic-gate 		}
49450Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
49460Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_datap->db_lim;
49470Sstevel@tonic-gate 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
49480Sstevel@tonic-gate 	}
49490Sstevel@tonic-gate 	mp1->b_rptr = (unsigned char *)ip6h;
49500Sstevel@tonic-gate 	ip6i = (ip6i_t *)ip6h;
49510Sstevel@tonic-gate 
49520Sstevel@tonic-gate #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
49530Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I) {
49540Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
49550Sstevel@tonic-gate 		ip6i->ip6i_flags = 0;
49560Sstevel@tonic-gate 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
49570Sstevel@tonic-gate 
49580Sstevel@tonic-gate 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
49590Sstevel@tonic-gate 		if (option_exists & IPPF_SCOPE_ID) {
49600Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
49610Sstevel@tonic-gate 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
49620Sstevel@tonic-gate 		} else if (option_exists & IPPF_IFINDEX) {
49630Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
49640Sstevel@tonic-gate 			ASSERT(tipp->ipp_ifindex != 0);
49650Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
49660Sstevel@tonic-gate 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
49670Sstevel@tonic-gate 		}
49680Sstevel@tonic-gate 
49690Sstevel@tonic-gate 		if (option_exists & IPPF_RAW_CKSUM) {
49700Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
49710Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
49720Sstevel@tonic-gate 		}
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate 		if (option_exists & IPPF_NO_CKSUM) {
49750Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
49760Sstevel@tonic-gate 		}
49770Sstevel@tonic-gate 
49780Sstevel@tonic-gate 		if (option_exists & IPPF_ADDR) {
49790Sstevel@tonic-gate 			/*
49800Sstevel@tonic-gate 			 * Enable per-packet source address verification if
49810Sstevel@tonic-gate 			 * IPV6_PKTINFO specified the source address.
49820Sstevel@tonic-gate 			 * ip6_src is set in the transport's _wput function.
49830Sstevel@tonic-gate 			 */
49840Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
49850Sstevel@tonic-gate 		}
49860Sstevel@tonic-gate 
49870Sstevel@tonic-gate 		if (option_exists & IPPF_DONTFRAG) {
49880Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
49890Sstevel@tonic-gate 		}
49900Sstevel@tonic-gate 
49910Sstevel@tonic-gate 		if (option_exists & IPPF_USE_MIN_MTU) {
49920Sstevel@tonic-gate 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
49930Sstevel@tonic-gate 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
49940Sstevel@tonic-gate 		}
49950Sstevel@tonic-gate 
49960Sstevel@tonic-gate 		if (option_exists & IPPF_NEXTHOP) {
49970Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
49980Sstevel@tonic-gate 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
49990Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
50000Sstevel@tonic-gate 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
50010Sstevel@tonic-gate 		}
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate 		/*
50040Sstevel@tonic-gate 		 * tell IP this is an ip6i_t private header
50050Sstevel@tonic-gate 		 */
50060Sstevel@tonic-gate 		ip6i->ip6i_nxt = IPPROTO_RAW;
50070Sstevel@tonic-gate 	}
50080Sstevel@tonic-gate 
50090Sstevel@tonic-gate 	/* Initialize IPv6 header */
50100Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
50110Sstevel@tonic-gate 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
50120Sstevel@tonic-gate 
5013679Sseb 	/* Set the hoplimit of the outgoing packet. */
50140Sstevel@tonic-gate 	if (option_exists & IPPF_HOPLIMIT) {
5015679Sseb 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
5016679Sseb 		ip6h->ip6_hops = ipp->ipp_hoplimit;
5017679Sseb 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
5018679Sseb 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
50190Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
5020679Sseb 		if (option_exists & IPPF_MULTICAST_HOPS)
5021679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
50220Sstevel@tonic-gate 	} else {
50230Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_ttl;
5024679Sseb 		if (option_exists & IPPF_UNICAST_HOPS)
5025679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
50260Sstevel@tonic-gate 	}
50270Sstevel@tonic-gate 
50280Sstevel@tonic-gate 	if (option_exists & IPPF_ADDR) {
50290Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
50300Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
50310Sstevel@tonic-gate 		ip6h->ip6_src = tipp->ipp_addr;
50320Sstevel@tonic-gate 	} else {
50330Sstevel@tonic-gate 		/*
50340Sstevel@tonic-gate 		 * The source address was not set using IPV6_PKTINFO.
50350Sstevel@tonic-gate 		 * First look at the bound source.
50360Sstevel@tonic-gate 		 * If unspecified fallback to __sin6_src_id.
50370Sstevel@tonic-gate 		 */
50380Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
50390Sstevel@tonic-gate 		if (sin6->__sin6_src_id != 0 &&
50400Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
50410Sstevel@tonic-gate 			ip_srcid_find_id(sin6->__sin6_src_id,
50423448Sdh155122 			    &ip6h->ip6_src, icmp->icmp_zoneid,
50433448Sdh155122 			    is->is_netstack);
50440Sstevel@tonic-gate 		}
50450Sstevel@tonic-gate 	}
50460Sstevel@tonic-gate 
50470Sstevel@tonic-gate 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
50480Sstevel@tonic-gate 	cp = (uint8_t *)&ip6h[1];
50490Sstevel@tonic-gate 
50500Sstevel@tonic-gate 	/*
50510Sstevel@tonic-gate 	 * Here's where we have to start stringing together
50520Sstevel@tonic-gate 	 * any extension headers in the right order:
50530Sstevel@tonic-gate 	 * Hop-by-hop, destination, routing, and final destination opts.
50540Sstevel@tonic-gate 	 */
50550Sstevel@tonic-gate 	if (option_exists & IPPF_HOPOPTS) {
50560Sstevel@tonic-gate 		/* Hop-by-hop options */
50570Sstevel@tonic-gate 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
50580Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
50590Sstevel@tonic-gate 
50600Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_HOPOPTS;
50610Sstevel@tonic-gate 		nxthdr_ptr = &hbh->ip6h_nxt;
50620Sstevel@tonic-gate 
50630Sstevel@tonic-gate 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
50640Sstevel@tonic-gate 		cp += tipp->ipp_hopoptslen;
50650Sstevel@tonic-gate 	}
50660Sstevel@tonic-gate 	/*
50670Sstevel@tonic-gate 	 * En-route destination options
50680Sstevel@tonic-gate 	 * Only do them if there's a routing header as well
50690Sstevel@tonic-gate 	 */
50700Sstevel@tonic-gate 	if (option_exists & IPPF_RTDSTOPTS) {
50710Sstevel@tonic-gate 		ip6_dest_t *dst = (ip6_dest_t *)cp;
50720Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
50730Sstevel@tonic-gate 
50740Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
50750Sstevel@tonic-gate 		nxthdr_ptr = &dst->ip6d_nxt;
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
50780Sstevel@tonic-gate 		cp += tipp->ipp_rtdstoptslen;
50790Sstevel@tonic-gate 	}
50800Sstevel@tonic-gate 	/*
50810Sstevel@tonic-gate 	 * Routing header next
50820Sstevel@tonic-gate 	 */
50830Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
50840Sstevel@tonic-gate 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
50850Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
50860Sstevel@tonic-gate 
50870Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_ROUTING;
50880Sstevel@tonic-gate 		nxthdr_ptr = &rt->ip6r_nxt;
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
50910Sstevel@tonic-gate 		cp += tipp->ipp_rthdrlen;
50920Sstevel@tonic-gate 	}
50930Sstevel@tonic-gate 	/*
50940Sstevel@tonic-gate 	 * Do ultimate destination options
50950Sstevel@tonic-gate 	 */
50960Sstevel@tonic-gate 	if (option_exists & IPPF_DSTOPTS) {
50970Sstevel@tonic-gate 		ip6_dest_t *dest = (ip6_dest_t *)cp;
50980Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
50990Sstevel@tonic-gate 
51000Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
51010Sstevel@tonic-gate 		nxthdr_ptr = &dest->ip6d_nxt;
51020Sstevel@tonic-gate 
51030Sstevel@tonic-gate 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
51040Sstevel@tonic-gate 		cp += tipp->ipp_dstoptslen;
51050Sstevel@tonic-gate 	}
51060Sstevel@tonic-gate 
51070Sstevel@tonic-gate 	/*
51080Sstevel@tonic-gate 	 * Now set the last header pointer to the proto passed in
51090Sstevel@tonic-gate 	 */
51100Sstevel@tonic-gate 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
51110Sstevel@tonic-gate 	*nxthdr_ptr = icmp->icmp_proto;
51120Sstevel@tonic-gate 
51130Sstevel@tonic-gate 	/*
51140Sstevel@tonic-gate 	 * Copy in the destination address
51150Sstevel@tonic-gate 	 */
51161676Sjpk 	ip6h->ip6_dst = ip6_dst;
51170Sstevel@tonic-gate 
51180Sstevel@tonic-gate 	ip6h->ip6_vcf =
51195240Snordmark 	    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
51205240Snordmark 	    (sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
51210Sstevel@tonic-gate 
51220Sstevel@tonic-gate 	if (option_exists & IPPF_TCLASS) {
51230Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
51240Sstevel@tonic-gate 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
51250Sstevel@tonic-gate 		    tipp->ipp_tclass);
51260Sstevel@tonic-gate 	}
51270Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
51280Sstevel@tonic-gate 		ip6_rthdr_t	*rth;
51290Sstevel@tonic-gate 
51300Sstevel@tonic-gate 		/*
51310Sstevel@tonic-gate 		 * Perform any processing needed for source routing.
51320Sstevel@tonic-gate 		 * We know that all extension headers will be in the same mblk
51330Sstevel@tonic-gate 		 * as the IPv6 header.
51340Sstevel@tonic-gate 		 */
51350Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
51360Sstevel@tonic-gate 		if (rth != NULL && rth->ip6r_segleft != 0) {
51370Sstevel@tonic-gate 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
51380Sstevel@tonic-gate 				/*
51390Sstevel@tonic-gate 				 * Drop packet - only support Type 0 routing.
51400Sstevel@tonic-gate 				 * Notify the application as well.
51410Sstevel@tonic-gate 				 */
51420Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
51435240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51443448Sdh155122 				    rawipOutErrors);
51450Sstevel@tonic-gate 				return;
51460Sstevel@tonic-gate 			}
51470Sstevel@tonic-gate 			/*
51480Sstevel@tonic-gate 			 * rth->ip6r_len is twice the number of
51490Sstevel@tonic-gate 			 * addresses in the header
51500Sstevel@tonic-gate 			 */
51510Sstevel@tonic-gate 			if (rth->ip6r_len & 0x1) {
51520Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
51535240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51543448Sdh155122 				    rawipOutErrors);
51550Sstevel@tonic-gate 				return;
51560Sstevel@tonic-gate 			}
51570Sstevel@tonic-gate 			/*
51580Sstevel@tonic-gate 			 * Shuffle the routing header and ip6_dst
51590Sstevel@tonic-gate 			 * addresses, and get the checksum difference
51600Sstevel@tonic-gate 			 * between the first hop (in ip6_dst) and
51610Sstevel@tonic-gate 			 * the destination (in the last routing hdr entry).
51620Sstevel@tonic-gate 			 */
51633448Sdh155122 			csum = ip_massage_options_v6(ip6h, rth,
51645240Snordmark 			    is->is_netstack);
51650Sstevel@tonic-gate 			/*
51660Sstevel@tonic-gate 			 * Verify that the first hop isn't a mapped address.
51670Sstevel@tonic-gate 			 * Routers along the path need to do this verification
51680Sstevel@tonic-gate 			 * for subsequent hops.
51690Sstevel@tonic-gate 			 */
51700Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
51710Sstevel@tonic-gate 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
51725240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
51733448Sdh155122 				    rawipOutErrors);
51740Sstevel@tonic-gate 				return;
51750Sstevel@tonic-gate 			}
51760Sstevel@tonic-gate 		}
51770Sstevel@tonic-gate 	}
51780Sstevel@tonic-gate 
51790Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
51800Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
51810Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
51820Sstevel@tonic-gate 
51830Sstevel@tonic-gate 	/*
51840Sstevel@tonic-gate 	 * Set the length into the IP header.
51850Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
51860Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
51870Sstevel@tonic-gate 	 * as this can cause problems in layers below.
51880Sstevel@tonic-gate 	 */
51890Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
51905240Snordmark 		BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
51911289Sja97890 		icmp_ud_err(q, mp, EMSGSIZE);
51920Sstevel@tonic-gate 		return;
51930Sstevel@tonic-gate 	}
51940Sstevel@tonic-gate 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
51950Sstevel@tonic-gate 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
51960Sstevel@tonic-gate 		uint16_t *cksum_ptr;
51970Sstevel@tonic-gate 		uint_t	ext_hdrs_len;
51980Sstevel@tonic-gate 
51990Sstevel@tonic-gate 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
52000Sstevel@tonic-gate 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
52010Sstevel@tonic-gate 		    icmp->icmp_checksum_off == 2);
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 		/*
52040Sstevel@tonic-gate 		 * We make it easy for IP to include our pseudo header
52050Sstevel@tonic-gate 		 * by putting our length in uh_checksum, modified (if
52060Sstevel@tonic-gate 		 * we have a routing header) by the checksum difference
52070Sstevel@tonic-gate 		 * between the ultimate destination and first hop addresses.
52080Sstevel@tonic-gate 		 * Note: ICMPv6 must always checksum the packet.
52090Sstevel@tonic-gate 		 */
52100Sstevel@tonic-gate 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
52110Sstevel@tonic-gate 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
52120Sstevel@tonic-gate 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
52135240Snordmark 				BUMP_MIB(&is->is_rawip_mib,
52143448Sdh155122 				    rawipOutErrors);
52150Sstevel@tonic-gate 				freemsg(mp);
52160Sstevel@tonic-gate 				return;
52170Sstevel@tonic-gate 			}
52180Sstevel@tonic-gate 			ip6i = (ip6i_t *)mp1->b_rptr;
52190Sstevel@tonic-gate 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
52200Sstevel@tonic-gate 				ip6h = (ip6_t *)&ip6i[1];
52210Sstevel@tonic-gate 			else
52220Sstevel@tonic-gate 				ip6h = (ip6_t *)ip6i;
52230Sstevel@tonic-gate 		}
52240Sstevel@tonic-gate 		/* Add payload length to checksum */
52250Sstevel@tonic-gate 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
52260Sstevel@tonic-gate 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
52270Sstevel@tonic-gate 		csum += htons(ip_len - ext_hdrs_len);
52280Sstevel@tonic-gate 
52290Sstevel@tonic-gate 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
52300Sstevel@tonic-gate 		csum = (csum & 0xFFFF) + (csum >> 16);
52310Sstevel@tonic-gate 		*cksum_ptr = (uint16_t)csum;
52320Sstevel@tonic-gate 	}
52330Sstevel@tonic-gate 
52340Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
52350Sstevel@tonic-gate 	ip_len = htons(ip_len);
52360Sstevel@tonic-gate #endif
52370Sstevel@tonic-gate 	ip6h->ip6_plen = (uint16_t)ip_len;
52380Sstevel@tonic-gate 
52390Sstevel@tonic-gate 	freeb(mp);
52400Sstevel@tonic-gate 
52410Sstevel@tonic-gate 	/* We're done. Pass the packet to IP */
52425240Snordmark 	BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
52435240Snordmark 	ip_output_v6(icmp->icmp_connp, mp1, q, IP_WPUT);
52440Sstevel@tonic-gate }
52450Sstevel@tonic-gate 
52460Sstevel@tonic-gate static void
52470Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp)
52480Sstevel@tonic-gate {
52490Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
52500Sstevel@tonic-gate 	struct iocblk *iocp;
52510Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
52525240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
52535240Snordmark 	icmp_t	*icmp = connp->conn_icmp;
52545240Snordmark 	icmp_stack_t *is = icmp->icmp_is;
52550Sstevel@tonic-gate 	cred_t *cr;
52560Sstevel@tonic-gate 
52575240Snordmark 	cr = DB_CREDDEF(mp, connp->conn_cred);
52580Sstevel@tonic-gate 
52590Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
52600Sstevel@tonic-gate 	case M_PROTO:
52610Sstevel@tonic-gate 	case M_PCPROTO:
52620Sstevel@tonic-gate 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
52630Sstevel@tonic-gate 			/*
52640Sstevel@tonic-gate 			 * If the message does not contain a PRIM_type,
52650Sstevel@tonic-gate 			 * throw it away.
52660Sstevel@tonic-gate 			 */
52670Sstevel@tonic-gate 			freemsg(mp);
52680Sstevel@tonic-gate 			return;
52690Sstevel@tonic-gate 		}
52700Sstevel@tonic-gate 		switch (((union T_primitives *)rptr)->type) {
52710Sstevel@tonic-gate 		case T_ADDR_REQ:
52720Sstevel@tonic-gate 			icmp_addr_req(q, mp);
52730Sstevel@tonic-gate 			return;
52740Sstevel@tonic-gate 		case O_T_BIND_REQ:
52750Sstevel@tonic-gate 		case T_BIND_REQ:
52765240Snordmark 			icmp_bind(q, mp);
52770Sstevel@tonic-gate 			return;
52780Sstevel@tonic-gate 		case T_CONN_REQ:
52790Sstevel@tonic-gate 			icmp_connect(q, mp);
52800Sstevel@tonic-gate 			return;
52810Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
52820Sstevel@tonic-gate 			icmp_capability_req(q, mp);
52830Sstevel@tonic-gate 			return;
52840Sstevel@tonic-gate 		case T_INFO_REQ:
52850Sstevel@tonic-gate 			icmp_info_req(q, mp);
52860Sstevel@tonic-gate 			return;
52870Sstevel@tonic-gate 		case T_UNITDATA_REQ:
52880Sstevel@tonic-gate 			/*
52890Sstevel@tonic-gate 			 * If a T_UNITDATA_REQ gets here, the address must
52900Sstevel@tonic-gate 			 * be bad.  Valid T_UNITDATA_REQs are found above
52910Sstevel@tonic-gate 			 * and break to below this switch.
52920Sstevel@tonic-gate 			 */
52930Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
52940Sstevel@tonic-gate 			return;
52950Sstevel@tonic-gate 		case T_UNBIND_REQ:
52960Sstevel@tonic-gate 			icmp_unbind(q, mp);
52970Sstevel@tonic-gate 			return;
52980Sstevel@tonic-gate 
52990Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
53005240Snordmark 			if (!snmpcom_req(q, mp, icmp_snmp_set, ip_snmp_get,
53015240Snordmark 			    cr)) {
53020Sstevel@tonic-gate 				/* Only IP can return anything meaningful */
53030Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
53045240Snordmark 				    &icmp_opt_obj, B_TRUE);
53055240Snordmark 			}
53060Sstevel@tonic-gate 			return;
53070Sstevel@tonic-gate 
53080Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
53090Sstevel@tonic-gate 			/* Only IP can return anything meaningful */
53105240Snordmark 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj, B_TRUE);
53110Sstevel@tonic-gate 			return;
53120Sstevel@tonic-gate 
53130Sstevel@tonic-gate 		case T_DISCON_REQ:
53140Sstevel@tonic-gate 			icmp_disconnect(q, mp);
53150Sstevel@tonic-gate 			return;
53160Sstevel@tonic-gate 
53170Sstevel@tonic-gate 		/* The following TPI message is not supported by icmp. */
53180Sstevel@tonic-gate 		case O_T_CONN_RES:
53190Sstevel@tonic-gate 		case T_CONN_RES:
53200Sstevel@tonic-gate 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
53210Sstevel@tonic-gate 			return;
53220Sstevel@tonic-gate 
53230Sstevel@tonic-gate 		/* The following 3 TPI requests are illegal for icmp. */
53240Sstevel@tonic-gate 		case T_DATA_REQ:
53250Sstevel@tonic-gate 		case T_EXDATA_REQ:
53260Sstevel@tonic-gate 		case T_ORDREL_REQ:
53270Sstevel@tonic-gate 			freemsg(mp);
53280Sstevel@tonic-gate 			(void) putctl1(RD(q), M_ERROR, EPROTO);
53290Sstevel@tonic-gate 			return;
53300Sstevel@tonic-gate 		default:
53310Sstevel@tonic-gate 			break;
53320Sstevel@tonic-gate 		}
53330Sstevel@tonic-gate 		break;
53340Sstevel@tonic-gate 	case M_IOCTL:
53350Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
53360Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
53370Sstevel@tonic-gate 		case TI_GETPEERNAME:
53380Sstevel@tonic-gate 			if (icmp->icmp_state != TS_DATA_XFER) {
53390Sstevel@tonic-gate 				/*
53400Sstevel@tonic-gate 				 * If a default destination address has not
53410Sstevel@tonic-gate 				 * been associated with the stream, then we
53420Sstevel@tonic-gate 				 * don't know the peer's name.
53430Sstevel@tonic-gate 				 */
53440Sstevel@tonic-gate 				iocp->ioc_error = ENOTCONN;
53455240Snordmark 		err_ret:;
53460Sstevel@tonic-gate 				iocp->ioc_count = 0;
53470Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
53480Sstevel@tonic-gate 				qreply(q, mp);
53490Sstevel@tonic-gate 				return;
53500Sstevel@tonic-gate 			}
53510Sstevel@tonic-gate 			/* FALLTHRU */
53520Sstevel@tonic-gate 		case TI_GETMYNAME:
53530Sstevel@tonic-gate 			/*
53540Sstevel@tonic-gate 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
53550Sstevel@tonic-gate 			 * need to copyin the user's strbuf structure.
53560Sstevel@tonic-gate 			 * Processing will continue in the M_IOCDATA case
53570Sstevel@tonic-gate 			 * below.
53580Sstevel@tonic-gate 			 */
53590Sstevel@tonic-gate 			mi_copyin(q, mp, NULL,
53600Sstevel@tonic-gate 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
53610Sstevel@tonic-gate 			return;
53620Sstevel@tonic-gate 		case ND_SET:
53630Sstevel@tonic-gate 			/* nd_getset performs the necessary error checking */
53640Sstevel@tonic-gate 		case ND_GET:
53655240Snordmark 			if (nd_getset(q, is->is_nd, mp)) {
53660Sstevel@tonic-gate 				qreply(q, mp);
53670Sstevel@tonic-gate 				return;
53680Sstevel@tonic-gate 			}
53690Sstevel@tonic-gate 			break;
53700Sstevel@tonic-gate 		default:
53710Sstevel@tonic-gate 			break;
53720Sstevel@tonic-gate 		}
53730Sstevel@tonic-gate 		break;
53740Sstevel@tonic-gate 	case M_IOCDATA:
53750Sstevel@tonic-gate 		icmp_wput_iocdata(q, mp);
53760Sstevel@tonic-gate 		return;
53770Sstevel@tonic-gate 	default:
53780Sstevel@tonic-gate 		break;
53790Sstevel@tonic-gate 	}
53805240Snordmark 	ip_wput(q, mp);
53810Sstevel@tonic-gate }
53820Sstevel@tonic-gate 
53830Sstevel@tonic-gate /*
53840Sstevel@tonic-gate  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
53850Sstevel@tonic-gate  * messages.
53860Sstevel@tonic-gate  */
53870Sstevel@tonic-gate static void
53880Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp)
53890Sstevel@tonic-gate {
53900Sstevel@tonic-gate 	mblk_t	*mp1;
53910Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
53920Sstevel@tonic-gate 	icmp_t	*icmp;
53930Sstevel@tonic-gate 	in6_addr_t	v6addr;
53940Sstevel@tonic-gate 	ipaddr_t	v4addr;
53950Sstevel@tonic-gate 	uint32_t	flowinfo = 0;
53960Sstevel@tonic-gate 	int		addrlen;
53970Sstevel@tonic-gate 
53980Sstevel@tonic-gate 	/* Make sure it is one of ours. */
53990Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
54000Sstevel@tonic-gate 	case TI_GETMYNAME:
54010Sstevel@tonic-gate 	case TI_GETPEERNAME:
54020Sstevel@tonic-gate 		break;
54030Sstevel@tonic-gate 	default:
54045240Snordmark 		icmp = Q_TO_ICMP(q);
54055240Snordmark 		ip_output(icmp->icmp_connp, mp, q, IP_WPUT);
54060Sstevel@tonic-gate 		return;
54070Sstevel@tonic-gate 	}
54080Sstevel@tonic-gate 	switch (mi_copy_state(q, mp, &mp1)) {
54090Sstevel@tonic-gate 	case -1:
54100Sstevel@tonic-gate 		return;
54110Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_IN, 1):
54120Sstevel@tonic-gate 		break;
54130Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 1):
54140Sstevel@tonic-gate 		/*
54150Sstevel@tonic-gate 		 * The address has been copied out, so now
54160Sstevel@tonic-gate 		 * copyout the strbuf.
54170Sstevel@tonic-gate 		 */
54180Sstevel@tonic-gate 		mi_copyout(q, mp);
54190Sstevel@tonic-gate 		return;
54200Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 2):
54210Sstevel@tonic-gate 		/*
54220Sstevel@tonic-gate 		 * The address and strbuf have been copied out.
54230Sstevel@tonic-gate 		 * We're done, so just acknowledge the original
54240Sstevel@tonic-gate 		 * M_IOCTL.
54250Sstevel@tonic-gate 		 */
54260Sstevel@tonic-gate 		mi_copy_done(q, mp, 0);
54270Sstevel@tonic-gate 		return;
54280Sstevel@tonic-gate 	default:
54290Sstevel@tonic-gate 		/*
54300Sstevel@tonic-gate 		 * Something strange has happened, so acknowledge
54310Sstevel@tonic-gate 		 * the original M_IOCTL with an EPROTO error.
54320Sstevel@tonic-gate 		 */
54330Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
54340Sstevel@tonic-gate 		return;
54350Sstevel@tonic-gate 	}
54360Sstevel@tonic-gate 	/*
54370Sstevel@tonic-gate 	 * Now we have the strbuf structure for TI_GETMYNAME
54380Sstevel@tonic-gate 	 * and TI_GETPEERNAME.  Next we copyout the requested
54390Sstevel@tonic-gate 	 * address and then we'll copyout the strbuf.
54400Sstevel@tonic-gate 	 */
54410Sstevel@tonic-gate 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
54420Sstevel@tonic-gate 	    (void *)mp1->b_rptr);
54435240Snordmark 	icmp = Q_TO_ICMP(q);
54440Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET)
54450Sstevel@tonic-gate 		addrlen = sizeof (sin_t);
54460Sstevel@tonic-gate 	else
54470Sstevel@tonic-gate 		addrlen = sizeof (sin6_t);
54480Sstevel@tonic-gate 
54490Sstevel@tonic-gate 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
54500Sstevel@tonic-gate 		mi_copy_done(q, mp, EINVAL);
54510Sstevel@tonic-gate 		return;
54520Sstevel@tonic-gate 	}
54530Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
54540Sstevel@tonic-gate 	case TI_GETMYNAME:
54550Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
54560Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
54570Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
54580Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
54590Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
54600Sstevel@tonic-gate 			} else {
54610Sstevel@tonic-gate 				/*
54620Sstevel@tonic-gate 				 * INADDR_ANY
54630Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
54640Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
54650Sstevel@tonic-gate 				 * local address instead (that could
54660Sstevel@tonic-gate 				 * also still be INADDR_ANY)
54670Sstevel@tonic-gate 				 */
54680Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
54690Sstevel@tonic-gate 			}
54700Sstevel@tonic-gate 		} else {
54710Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6 */
54720Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
54730Sstevel@tonic-gate 				v6addr = icmp->icmp_v6src;
54740Sstevel@tonic-gate 			} else {
54750Sstevel@tonic-gate 				/*
54760Sstevel@tonic-gate 				 * UNSPECIFIED
54770Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
54780Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
54790Sstevel@tonic-gate 				 * local address instead (that could
54800Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
54810Sstevel@tonic-gate 				 */
54820Sstevel@tonic-gate 				v6addr = icmp->icmp_bound_v6src;
54830Sstevel@tonic-gate 			}
54840Sstevel@tonic-gate 		}
54850Sstevel@tonic-gate 		break;
54860Sstevel@tonic-gate 	case TI_GETPEERNAME:
54870Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
54880Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
54890Sstevel@tonic-gate 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
54900Sstevel@tonic-gate 		} else {
54910Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6) */
54920Sstevel@tonic-gate 			v6addr = icmp->icmp_v6dst;
54930Sstevel@tonic-gate 			flowinfo = icmp->icmp_flowinfo;
54940Sstevel@tonic-gate 		}
54950Sstevel@tonic-gate 		break;
54960Sstevel@tonic-gate 	default:
54970Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
54980Sstevel@tonic-gate 		return;
54990Sstevel@tonic-gate 	}
55000Sstevel@tonic-gate 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
55010Sstevel@tonic-gate 	if (!mp1)
55020Sstevel@tonic-gate 		return;
55030Sstevel@tonic-gate 
55040Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
55050Sstevel@tonic-gate 		sin_t *sin;
55060Sstevel@tonic-gate 
55070Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
55080Sstevel@tonic-gate 		sin = (sin_t *)mp1->b_rptr;
55090Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin[1];
55100Sstevel@tonic-gate 		*sin = sin_null;
55110Sstevel@tonic-gate 		sin->sin_family = AF_INET;
55120Sstevel@tonic-gate 		sin->sin_addr.s_addr = v4addr;
55130Sstevel@tonic-gate 	} else {
55140Sstevel@tonic-gate 		/* icmp->icmp_family == AF_INET6 */
55150Sstevel@tonic-gate 		sin6_t *sin6;
55160Sstevel@tonic-gate 
55170Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
55180Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
55190Sstevel@tonic-gate 		sin6 = (sin6_t *)mp1->b_rptr;
55200Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin6[1];
55210Sstevel@tonic-gate 		*sin6 = sin6_null;
55220Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
55230Sstevel@tonic-gate 		sin6->sin6_flowinfo = flowinfo;
55240Sstevel@tonic-gate 		sin6->sin6_addr = v6addr;
55250Sstevel@tonic-gate 	}
55260Sstevel@tonic-gate 	/* Copy out the address */
55270Sstevel@tonic-gate 	mi_copyout(q, mp);
55280Sstevel@tonic-gate }
55290Sstevel@tonic-gate 
55300Sstevel@tonic-gate static int
55310Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
55320Sstevel@tonic-gate     void *thisdg_attrs)
55330Sstevel@tonic-gate {
55345240Snordmark 	conn_t	*connp = Q_TO_CONN(q);
55350Sstevel@tonic-gate 	struct T_unitdata_req *udreqp;
55360Sstevel@tonic-gate 	int is_absreq_failure;
55370Sstevel@tonic-gate 	cred_t *cr;
55380Sstevel@tonic-gate 
55390Sstevel@tonic-gate 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
55400Sstevel@tonic-gate 	*errorp = 0;
55410Sstevel@tonic-gate 
55425240Snordmark 	cr = DB_CREDDEF(mp, connp->conn_cred);
55430Sstevel@tonic-gate 
55440Sstevel@tonic-gate 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
55450Sstevel@tonic-gate 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
55460Sstevel@tonic-gate 	    thisdg_attrs, &is_absreq_failure);
55470Sstevel@tonic-gate 
55480Sstevel@tonic-gate 	if (*errorp != 0) {
55490Sstevel@tonic-gate 		/*
55500Sstevel@tonic-gate 		 * Note: No special action needed in this
55510Sstevel@tonic-gate 		 * module for "is_absreq_failure"
55520Sstevel@tonic-gate 		 */
55530Sstevel@tonic-gate 		return (-1);		/* failure */
55540Sstevel@tonic-gate 	}
55550Sstevel@tonic-gate 	ASSERT(is_absreq_failure == 0);
55560Sstevel@tonic-gate 	return (0);	/* success */
55570Sstevel@tonic-gate }
55580Sstevel@tonic-gate 
55590Sstevel@tonic-gate void
55600Sstevel@tonic-gate icmp_ddi_init(void)
55610Sstevel@tonic-gate {
55625381Smeem 	icmp_max_optsize = optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
55635240Snordmark 	    icmp_opt_obj.odb_opt_arr_cnt);
55640Sstevel@tonic-gate 
55653448Sdh155122 	/*
55663448Sdh155122 	 * We want to be informed each time a stack is created or
55673448Sdh155122 	 * destroyed in the kernel, so we can maintain the
55683448Sdh155122 	 * set of icmp_stack_t's.
55693448Sdh155122 	 */
55703448Sdh155122 	netstack_register(NS_ICMP, rawip_stack_init, NULL, rawip_stack_fini);
55710Sstevel@tonic-gate }
55720Sstevel@tonic-gate 
55730Sstevel@tonic-gate void
55740Sstevel@tonic-gate icmp_ddi_destroy(void)
55750Sstevel@tonic-gate {
55763448Sdh155122 	netstack_unregister(NS_ICMP);
55770Sstevel@tonic-gate }
55780Sstevel@tonic-gate 
55793448Sdh155122 /*
55803448Sdh155122  * Initialize the ICMP stack instance.
55813448Sdh155122  */
55823448Sdh155122 static void *
55833448Sdh155122 rawip_stack_init(netstackid_t stackid, netstack_t *ns)
55843448Sdh155122 {
55853448Sdh155122 	icmp_stack_t	*is;
55863448Sdh155122 	icmpparam_t	*pa;
55873448Sdh155122 
55883448Sdh155122 	is = (icmp_stack_t *)kmem_zalloc(sizeof (*is), KM_SLEEP);
55893448Sdh155122 	is->is_netstack = ns;
55903448Sdh155122 
55913448Sdh155122 	pa = (icmpparam_t *)kmem_alloc(sizeof (icmp_param_arr), KM_SLEEP);
55923448Sdh155122 	is->is_param_arr = pa;
55933448Sdh155122 	bcopy(icmp_param_arr, is->is_param_arr, sizeof (icmp_param_arr));
55943448Sdh155122 
55953448Sdh155122 	(void) icmp_param_register(&is->is_nd,
55963448Sdh155122 	    is->is_param_arr, A_CNT(icmp_param_arr));
55973448Sdh155122 	is->is_ksp = rawip_kstat_init(stackid);
55983448Sdh155122 	return (is);
55993448Sdh155122 }
56003448Sdh155122 
56013448Sdh155122 /*
56023448Sdh155122  * Free the ICMP stack instance.
56033448Sdh155122  */
56040Sstevel@tonic-gate static void
56053448Sdh155122 rawip_stack_fini(netstackid_t stackid, void *arg)
56063448Sdh155122 {
56073448Sdh155122 	icmp_stack_t *is = (icmp_stack_t *)arg;
56083448Sdh155122 
56093448Sdh155122 	nd_free(&is->is_nd);
56103448Sdh155122 	kmem_free(is->is_param_arr, sizeof (icmp_param_arr));
56113448Sdh155122 	is->is_param_arr = NULL;
56123448Sdh155122 
56133448Sdh155122 	rawip_kstat_fini(stackid, is->is_ksp);
56143448Sdh155122 	is->is_ksp = NULL;
56153448Sdh155122 	kmem_free(is, sizeof (*is));
56163448Sdh155122 }
56173448Sdh155122 
56183448Sdh155122 static void *
56193448Sdh155122 rawip_kstat_init(netstackid_t stackid) {
56203448Sdh155122 	kstat_t	*ksp;
56210Sstevel@tonic-gate 
56220Sstevel@tonic-gate 	rawip_named_kstat_t template = {
56230Sstevel@tonic-gate 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
56240Sstevel@tonic-gate 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
56250Sstevel@tonic-gate 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
56260Sstevel@tonic-gate 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
56270Sstevel@tonic-gate 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
56280Sstevel@tonic-gate 	};
56290Sstevel@tonic-gate 
56303448Sdh155122 	ksp = kstat_create_netstack("icmp", 0, "rawip", "mib2",
56310Sstevel@tonic-gate 					KSTAT_TYPE_NAMED,
56320Sstevel@tonic-gate 					NUM_OF_FIELDS(rawip_named_kstat_t),
56333448Sdh155122 					0, stackid);
56343448Sdh155122 	if (ksp == NULL || ksp->ks_data == NULL)
56353448Sdh155122 		return (NULL);
56363448Sdh155122 
56373448Sdh155122 	bcopy(&template, ksp->ks_data, sizeof (template));
56383448Sdh155122 	ksp->ks_update = rawip_kstat_update;
56393448Sdh155122 	ksp->ks_private = (void *)(uintptr_t)stackid;
56403448Sdh155122 
56413448Sdh155122 	kstat_install(ksp);
56423448Sdh155122 	return (ksp);
56430Sstevel@tonic-gate }
56440Sstevel@tonic-gate 
56450Sstevel@tonic-gate static void
56463448Sdh155122 rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
56473448Sdh155122 {
56483448Sdh155122 	if (ksp != NULL) {
56493448Sdh155122 		ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
56503448Sdh155122 		kstat_delete_netstack(ksp, stackid);
56510Sstevel@tonic-gate 	}
56520Sstevel@tonic-gate }
56530Sstevel@tonic-gate 
56540Sstevel@tonic-gate static int
56553448Sdh155122 rawip_kstat_update(kstat_t *ksp, int rw)
56563448Sdh155122 {
56570Sstevel@tonic-gate 	rawip_named_kstat_t *rawipkp;
56583448Sdh155122 	netstackid_t	stackid = (netstackid_t)(uintptr_t)ksp->ks_private;
56593448Sdh155122 	netstack_t	*ns;
56603448Sdh155122 	icmp_stack_t	*is;
56613448Sdh155122 
56623448Sdh155122 	if ((ksp == NULL) || (ksp->ks_data == NULL))
56630Sstevel@tonic-gate 		return (EIO);
56640Sstevel@tonic-gate 
56650Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
56660Sstevel@tonic-gate 		return (EACCES);
56670Sstevel@tonic-gate 
56683448Sdh155122 	rawipkp = (rawip_named_kstat_t *)ksp->ks_data;
56693448Sdh155122 
56703448Sdh155122 	ns = netstack_find_by_stackid(stackid);
56713448Sdh155122 	if (ns == NULL)
56723448Sdh155122 		return (-1);
56733448Sdh155122 	is = ns->netstack_icmp;
56743448Sdh155122 	if (is == NULL) {
56753448Sdh155122 		netstack_rele(ns);
56763448Sdh155122 		return (-1);
56773448Sdh155122 	}
56783448Sdh155122 	rawipkp->inDatagrams.value.ui32 =  is->is_rawip_mib.rawipInDatagrams;
56793448Sdh155122 	rawipkp->inCksumErrs.value.ui32 =  is->is_rawip_mib.rawipInCksumErrs;
56803448Sdh155122 	rawipkp->inErrors.value.ui32 =	   is->is_rawip_mib.rawipInErrors;
56813448Sdh155122 	rawipkp->outDatagrams.value.ui32 = is->is_rawip_mib.rawipOutDatagrams;
56823448Sdh155122 	rawipkp->outErrors.value.ui32 =	   is->is_rawip_mib.rawipOutErrors;
56833448Sdh155122 	netstack_rele(ns);
56840Sstevel@tonic-gate 	return (0);
56850Sstevel@tonic-gate }
5686