xref: /onnv-gate/usr/src/uts/common/inet/ip/icmp.c (revision 2263:fd48046384d0)
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 /*
221289Sja97890  * Copyright 2006 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>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <net/route.h>
550Sstevel@tonic-gate #include <net/if.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <netinet/in.h>
580Sstevel@tonic-gate #include <netinet/ip6.h>
590Sstevel@tonic-gate #include <netinet/icmp6.h>
600Sstevel@tonic-gate #include <inet/common.h>
610Sstevel@tonic-gate #include <inet/ip.h>
620Sstevel@tonic-gate #include <inet/ip6.h>
630Sstevel@tonic-gate #include <inet/mi.h>
640Sstevel@tonic-gate #include <inet/nd.h>
650Sstevel@tonic-gate #include <inet/optcom.h>
660Sstevel@tonic-gate #include <inet/snmpcom.h>
670Sstevel@tonic-gate #include <inet/kstatcom.h>
680Sstevel@tonic-gate #include <inet/rawip_impl.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <netinet/ip_mroute.h>
710Sstevel@tonic-gate #include <inet/tcp.h>
720Sstevel@tonic-gate #include <net/pfkeyv2.h>
730Sstevel@tonic-gate #include <inet/ipsec_info.h>
740Sstevel@tonic-gate #include <inet/ipclassifier.h>
750Sstevel@tonic-gate 
761676Sjpk #include <sys/tsol/label.h>
771676Sjpk #include <sys/tsol/tnet.h>
781676Sjpk 
790Sstevel@tonic-gate #define	ICMP6 "icmp6"
800Sstevel@tonic-gate major_t	ICMP6_MAJ;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Object to represent database of options to search passed to
840Sstevel@tonic-gate  * {sock,tpi}optcom_req() interface routine to take care of option
850Sstevel@tonic-gate  * management and associated methods.
860Sstevel@tonic-gate  * XXX These and other extern's should really move to a icmp header.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate extern optdb_obj_t	icmp_opt_obj;
890Sstevel@tonic-gate extern uint_t		icmp_max_optsize;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Synchronization notes:
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * At all points in this code where exclusive access is required, we
950Sstevel@tonic-gate  * pass a message to a subroutine by invoking qwriter(..., PERIM_OUTER)
960Sstevel@tonic-gate  * which will arrange to call the routine only after all threads have
970Sstevel@tonic-gate  * exited the shared resource.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
1010Sstevel@tonic-gate typedef struct icmpparam_s {
1020Sstevel@tonic-gate 	uint_t	icmp_param_min;
1030Sstevel@tonic-gate 	uint_t	icmp_param_max;
1040Sstevel@tonic-gate 	uint_t	icmp_param_value;
1050Sstevel@tonic-gate 	char	*icmp_param_name;
1060Sstevel@tonic-gate } icmpparam_t;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static void	icmp_addr_req(queue_t *q, mblk_t *mp);
1090Sstevel@tonic-gate static void	icmp_bind(queue_t *q, mblk_t *mp);
1100Sstevel@tonic-gate static void	icmp_bind_proto(queue_t *q);
1110Sstevel@tonic-gate static int	icmp_build_hdrs(queue_t *q, icmp_t *icmp);
1120Sstevel@tonic-gate static void	icmp_capability_req(queue_t *q, mblk_t *mp);
1130Sstevel@tonic-gate static int	icmp_close(queue_t *q);
1140Sstevel@tonic-gate static void	icmp_connect(queue_t *q, mblk_t *mp);
1150Sstevel@tonic-gate static void	icmp_disconnect(queue_t *q, mblk_t *mp);
1160Sstevel@tonic-gate static void	icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
1170Sstevel@tonic-gate 		    int sys_error);
1180Sstevel@tonic-gate static void	icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
1190Sstevel@tonic-gate 		    t_scalar_t t_error, int sys_error);
1200Sstevel@tonic-gate static void	icmp_icmp_error(queue_t *q, mblk_t *mp);
1210Sstevel@tonic-gate static void	icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp);
1220Sstevel@tonic-gate static void	icmp_info_req(queue_t *q, mblk_t *mp);
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);
1250Sstevel@tonic-gate static int	icmp_open(queue_t *q, dev_t *devp, int flag,
1260Sstevel@tonic-gate 		    int sflag, cred_t *credp);
1270Sstevel@tonic-gate static int	icmp_unitdata_opt_process(queue_t *q, mblk_t *mp,
1280Sstevel@tonic-gate 		    int *errorp, void *thisdg_attrs);
1290Sstevel@tonic-gate static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
1300Sstevel@tonic-gate int		icmp_opt_set(queue_t *q, uint_t optset_context,
1310Sstevel@tonic-gate 		    int level, int name, uint_t inlen,
1320Sstevel@tonic-gate 		    uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
1330Sstevel@tonic-gate 		    void *thisdg_attrs, cred_t *cr, mblk_t *mblk);
1340Sstevel@tonic-gate int		icmp_opt_get(queue_t *q, int level, int name,
1350Sstevel@tonic-gate 		    uchar_t *ptr);
1360Sstevel@tonic-gate static int	icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
1370Sstevel@tonic-gate static boolean_t icmp_param_register(icmpparam_t *icmppa, int cnt);
1380Sstevel@tonic-gate static int	icmp_param_set(queue_t *q, mblk_t *mp, char *value,
1390Sstevel@tonic-gate 		    caddr_t cp, cred_t *cr);
1400Sstevel@tonic-gate static void	icmp_rput(queue_t *q, mblk_t *mp);
1410Sstevel@tonic-gate static void	icmp_rput_bind_ack(queue_t *q, mblk_t *mp);
1420Sstevel@tonic-gate static int	icmp_snmp_get(queue_t *q, mblk_t *mpctl);
1430Sstevel@tonic-gate static int	icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
1440Sstevel@tonic-gate 		    uchar_t *ptr, int len);
1450Sstevel@tonic-gate static int	icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp,
1460Sstevel@tonic-gate 		    cred_t *cr);
1470Sstevel@tonic-gate static void	icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err);
1480Sstevel@tonic-gate static void	icmp_unbind(queue_t *q, mblk_t *mp);
1490Sstevel@tonic-gate static void	icmp_wput(queue_t *q, mblk_t *mp);
1500Sstevel@tonic-gate static void	icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6,
1510Sstevel@tonic-gate 		    t_scalar_t tudr_optlen);
1520Sstevel@tonic-gate static void	icmp_wput_other(queue_t *q, mblk_t *mp);
1530Sstevel@tonic-gate static void	icmp_wput_iocdata(queue_t *q, mblk_t *mp);
1540Sstevel@tonic-gate static void	icmp_wput_restricted(queue_t *q, mblk_t *mp);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static void	rawip_kstat_init(void);
1570Sstevel@tonic-gate static void	rawip_kstat_fini(void);
1580Sstevel@tonic-gate static int	rawip_kstat_update(kstat_t *kp, int rw);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static struct module_info info =  {
1620Sstevel@tonic-gate 	5707, "icmp", 1, INFPSZ, 512, 128
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static struct qinit rinit = {
1660Sstevel@tonic-gate 	(pfi_t)icmp_rput, NULL, icmp_open, icmp_close, NULL, &info
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static struct qinit winit = {
1700Sstevel@tonic-gate 	(pfi_t)icmp_wput, NULL, NULL, NULL, NULL, &info
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate struct streamtab icmpinfo = {
1740Sstevel@tonic-gate 	&rinit, &winit
1750Sstevel@tonic-gate };
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static sin_t	sin_null;	/* Zero address for quick clears */
1780Sstevel@tonic-gate static sin6_t	sin6_null;	/* Zero address for quick clears */
1790Sstevel@tonic-gate static void	*icmp_g_head;	/* Head for list of open icmp streams. */
1800Sstevel@tonic-gate static IDP	icmp_g_nd;	/* Points to table of ICMP ND variables. */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /* MIB-2 stuff for SNMP */
1830Sstevel@tonic-gate static mib2_rawip_t	rawip_mib;	/* SNMP fixed size info */
1840Sstevel@tonic-gate static kstat_t		*rawip_mibkp;	/* kstat exporting rawip_mib data */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages */
1870Sstevel@tonic-gate static struct T_info_ack icmp_g_t_info_ack = {
1880Sstevel@tonic-gate 	T_INFO_ACK,
1890Sstevel@tonic-gate 	IP_MAXPACKET,	 /* TSDU_size.  icmp allows maximum size messages. */
1900Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size.  icmp does not support expedited data. */
1910Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. icmp does not support connect data. */
1920Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. icmp does not support disconnect data. */
1930Sstevel@tonic-gate 	0,		/* ADDR_size - filled in later. */
1940Sstevel@tonic-gate 	0,		/* OPT_size - not initialized here */
1950Sstevel@tonic-gate 	IP_MAXPACKET,	/* TIDU_size.  icmp allows maximum size messages. */
1960Sstevel@tonic-gate 	T_CLTS,		/* SERV_type.  icmp supports connection-less. */
1970Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state.  This is set from icmp_state. */
1980Sstevel@tonic-gate 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
1990Sstevel@tonic-gate };
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Table of ND variables supported by icmp.  These are loaded into icmp_g_nd
2030Sstevel@tonic-gate  * in icmp_open.
2040Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate static icmpparam_t	icmp_param_arr[] = {
2070Sstevel@tonic-gate 	/* min	max	value	name */
2080Sstevel@tonic-gate 	{ 0,	128,	32,	"icmp_wroff_extra" },
2090Sstevel@tonic-gate 	{ 1,	255,	255,	"icmp_ipv4_ttl" },
2100Sstevel@tonic-gate 	{ 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS,	"icmp_ipv6_hoplimit"},
2110Sstevel@tonic-gate 	{ 0,	1,	1,	"icmp_bsd_compat" },
2120Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_xmit_hiwat"},
2130Sstevel@tonic-gate 	{ 0,	65536,	1024,	"icmp_xmit_lowat"},
2140Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_recv_hiwat"},
2150Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"icmp_max_buf"},
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate #define	icmp_wroff_extra		icmp_param_arr[0].icmp_param_value
2180Sstevel@tonic-gate #define	icmp_ipv4_ttl			icmp_param_arr[1].icmp_param_value
2190Sstevel@tonic-gate #define	icmp_ipv6_hoplimit		icmp_param_arr[2].icmp_param_value
2200Sstevel@tonic-gate #define	icmp_bsd_compat			icmp_param_arr[3].icmp_param_value
2210Sstevel@tonic-gate #define	icmp_xmit_hiwat			icmp_param_arr[4].icmp_param_value
2220Sstevel@tonic-gate #define	icmp_xmit_lowat			icmp_param_arr[5].icmp_param_value
2230Sstevel@tonic-gate #define	icmp_recv_hiwat			icmp_param_arr[6].icmp_param_value
2240Sstevel@tonic-gate #define	icmp_max_buf			icmp_param_arr[7].icmp_param_value
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
2280Sstevel@tonic-gate  * passed to icmp_wput.
2290Sstevel@tonic-gate  * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP
2300Sstevel@tonic-gate  * protocol type placed in the message following the address. A T_BIND_ACK
2310Sstevel@tonic-gate  * message is passed upstream when ip acknowledges the request.
2320Sstevel@tonic-gate  * (Called as writer.)
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate static void
2350Sstevel@tonic-gate icmp_bind(queue_t *q, mblk_t *mp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	sin_t	*sin;
2380Sstevel@tonic-gate 	sin6_t	*sin6;
2390Sstevel@tonic-gate 	mblk_t	*mp1;
2400Sstevel@tonic-gate 	struct T_bind_req	*tbr;
2410Sstevel@tonic-gate 	icmp_t	*icmp;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
2440Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
2450Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2460Sstevel@tonic-gate 		    "icmp_bind: bad req, len %u",
2470Sstevel@tonic-gate 		    (uint_t)(mp->b_wptr - mp->b_rptr));
2480Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
2490Sstevel@tonic-gate 		return;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
2520Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2530Sstevel@tonic-gate 		    "icmp_bind: bad state, %d", icmp->icmp_state);
2540Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
2550Sstevel@tonic-gate 		return;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Reallocate the message to make sure we have enough room for an
2590Sstevel@tonic-gate 	 * address and the protocol type.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1);
2620Sstevel@tonic-gate 	if (!mp1) {
2630Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
2640Sstevel@tonic-gate 		return;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	mp = mp1;
2670Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
2680Sstevel@tonic-gate 	switch (tbr->ADDR_length) {
2690Sstevel@tonic-gate 	case 0:			/* Generic request */
2700Sstevel@tonic-gate 		tbr->ADDR_offset = sizeof (struct T_bind_req);
2710Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
2720Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin_t);
2730Sstevel@tonic-gate 			sin = (sin_t *)&tbr[1];
2740Sstevel@tonic-gate 			*sin = sin_null;
2750Sstevel@tonic-gate 			sin->sin_family = AF_INET;
2760Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin[1];
2770Sstevel@tonic-gate 		} else {
2780Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
2790Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin6_t);
2800Sstevel@tonic-gate 			sin6 = (sin6_t *)&tbr[1];
2810Sstevel@tonic-gate 			*sin6 = sin6_null;
2820Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
2830Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin6[1];
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 		break;
2860Sstevel@tonic-gate 	case sizeof (sin_t):	/* Complete IP address */
2870Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset,
2880Sstevel@tonic-gate 		    sizeof (sin_t));
2890Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
2900Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
2910Sstevel@tonic-gate 			return;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
2940Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
2950Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
2960Sstevel@tonic-gate 			return;
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		break;
2990Sstevel@tonic-gate 	case sizeof (sin6_t):	/* Complete IP address */
3000Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset,
3010Sstevel@tonic-gate 		    sizeof (sin6_t));
3020Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
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_INET6 ||
3070Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
3080Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3090Sstevel@tonic-gate 			return;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
3120Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3130Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
3140Sstevel@tonic-gate 			return;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 		break;
3170Sstevel@tonic-gate 	default:
3180Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
3190Sstevel@tonic-gate 		    "icmp_bind: bad ADDR_length %d", tbr->ADDR_length);
3200Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
3210Sstevel@tonic-gate 		return;
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * Copy the source address into our icmp structure.  This address
3250Sstevel@tonic-gate 	 * may still be zero; if so, ip will fill in the correct address
3260Sstevel@tonic-gate 	 * each time an outbound packet is passed to it.
3270Sstevel@tonic-gate 	 * If we are binding to a broadcast or multicast address icmp_rput
3280Sstevel@tonic-gate 	 * will clear the source address when it receives the T_BIND_ACK.
3290Sstevel@tonic-gate 	 */
3300Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
3330Sstevel@tonic-gate 		ASSERT(sin != NULL);
3340Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3350Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr,
3360Sstevel@tonic-gate 		    &icmp->icmp_v6src);
3370Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
3380Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
3390Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3400Sstevel@tonic-gate 	} else {
3410Sstevel@tonic-gate 		int error;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		ASSERT(sin6 != NULL);
3440Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
3450Sstevel@tonic-gate 		icmp->icmp_v6src = sin6->sin6_addr;
3460Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
3470Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		/* Rebuild the header template */
3500Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
3510Sstevel@tonic-gate 		if (error != 0) {
3520Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
3530Sstevel@tonic-gate 			return;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following
3580Sstevel@tonic-gate 	 * the address.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
3610Sstevel@tonic-gate 	if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) {
3620Sstevel@tonic-gate 		/*
3630Sstevel@tonic-gate 		 * Append a request for an IRE if src not 0 (INADDR_ANY)
3640Sstevel@tonic-gate 		 */
3650Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
3660Sstevel@tonic-gate 		if (!mp->b_cont) {
3670Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOMEM);
3680Sstevel@tonic-gate 			return;
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
3710Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */
3750Sstevel@tonic-gate 	putnext(q, mp);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * Send message to IP to just bind to the protocol.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate static void
3820Sstevel@tonic-gate icmp_bind_proto(queue_t *q)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	mblk_t	*mp;
3850Sstevel@tonic-gate 	struct T_bind_req	*tbr;
3860Sstevel@tonic-gate 	icmp_t	*icmp;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
3890Sstevel@tonic-gate 	mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1,
3900Sstevel@tonic-gate 	    BPRI_MED);
3910Sstevel@tonic-gate 	if (!mp) {
3920Sstevel@tonic-gate 		return;
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
3950Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
3960Sstevel@tonic-gate 	tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */
3970Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (struct T_bind_req);
3980Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
3990Sstevel@tonic-gate 		sin_t	*sin;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin_t);
4020Sstevel@tonic-gate 		sin = (sin_t *)&tbr[1];
4030Sstevel@tonic-gate 		*sin = sin_null;
4040Sstevel@tonic-gate 		sin->sin_family = AF_INET;
4050Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin[1];
4060Sstevel@tonic-gate 	} else {
4070Sstevel@tonic-gate 		sin6_t	*sin6;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
4100Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin6_t);
4110Sstevel@tonic-gate 		sin6 = (sin6_t *)&tbr[1];
4120Sstevel@tonic-gate 		*sin6 = sin6_null;
4130Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
4140Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin6[1];
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/* Place protocol type in the O_T_BIND_REQ following the address. */
4180Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ to ip. */
4210Sstevel@tonic-gate 	putnext(q, mp);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * This routine handles each T_CONN_REQ message passed to icmp.  It
4260Sstevel@tonic-gate  * associates a default destination address with the stream.
4270Sstevel@tonic-gate  *
4280Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
4290Sstevel@tonic-gate  *	T_BIND_REQ	- specifying local and remote address.
4300Sstevel@tonic-gate  *	IRE_DB_REQ_TYPE	- to get an IRE back containing ire_type and src
4310Sstevel@tonic-gate  *	T_OK_ACK	- for the T_CONN_REQ
4320Sstevel@tonic-gate  *	T_CONN_CON	- to keep the TPI user happy
4330Sstevel@tonic-gate  *
4340Sstevel@tonic-gate  * The connect completes in icmp_rput.
4350Sstevel@tonic-gate  * When a T_BIND_ACK is received information is extracted from the IRE
4360Sstevel@tonic-gate  * and the two appended messages are sent to the TPI user.
4370Sstevel@tonic-gate  * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
4380Sstevel@tonic-gate  * it to an error ack for the appropriate primitive.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate static void
4410Sstevel@tonic-gate icmp_connect(queue_t *q, mblk_t *mp)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	sin_t	*sin;
4440Sstevel@tonic-gate 	sin6_t	*sin6;
4450Sstevel@tonic-gate 	mblk_t	*mp1, *mp2;
4460Sstevel@tonic-gate 	struct T_conn_req	*tcr;
4470Sstevel@tonic-gate 	icmp_t	*icmp;
4480Sstevel@tonic-gate 	ipaddr_t	v4dst;
4490Sstevel@tonic-gate 	in6_addr_t	v6dst;
4500Sstevel@tonic-gate 	uint32_t	flowinfo;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
4530Sstevel@tonic-gate 	tcr = (struct T_conn_req *)mp->b_rptr;
4540Sstevel@tonic-gate 	/* Sanity checks */
4550Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr < sizeof (struct T_conn_req))) {
4560Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
4570Sstevel@tonic-gate 		return;
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (icmp->icmp_state == TS_DATA_XFER) {
4610Sstevel@tonic-gate 		/* Already connected - clear out state */
4620Sstevel@tonic-gate 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
4630Sstevel@tonic-gate 		icmp->icmp_state = TS_IDLE;
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (tcr->OPT_length != 0) {
4680Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADOPT, 0);
4690Sstevel@tonic-gate 		return;
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 	switch (tcr->DEST_length) {
4720Sstevel@tonic-gate 	default:
4730Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
4740Sstevel@tonic-gate 		return;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	case sizeof (sin_t):
4770Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset,
4780Sstevel@tonic-gate 		    sizeof (sin_t));
4790Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
4800Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
4810Sstevel@tonic-gate 			return;
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
4840Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
4850Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
4860Sstevel@tonic-gate 			return;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
4890Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
4900Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
4910Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
4920Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
4930Sstevel@tonic-gate 		break;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	case sizeof (sin6_t):
4960Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset,
4970Sstevel@tonic-gate 		    sizeof (sin6_t));
4980Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
4990Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
5000Sstevel@tonic-gate 			return;
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
5030Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
5040Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
5050Sstevel@tonic-gate 			return;
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
5080Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5090Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
5100Sstevel@tonic-gate 			return;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		v6dst = sin6->sin6_addr;
5130Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
5140Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
5150Sstevel@tonic-gate 		flowinfo = sin6->sin6_flowinfo;
5160Sstevel@tonic-gate 		break;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
5190Sstevel@tonic-gate 		/*
5200Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
5210Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
5220Sstevel@tonic-gate 		 * generate the T_CONN_CON.
5230Sstevel@tonic-gate 		 */
5240Sstevel@tonic-gate 		if (v4dst == INADDR_ANY) {
5250Sstevel@tonic-gate 			v4dst = htonl(INADDR_LOOPBACK);
5260Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
5270Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET) {
5280Sstevel@tonic-gate 				sin->sin_addr.s_addr = v4dst;
5290Sstevel@tonic-gate 			} else {
5300Sstevel@tonic-gate 				sin6->sin6_addr = v6dst;
5310Sstevel@tonic-gate 			}
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
5340Sstevel@tonic-gate 		icmp->icmp_flowinfo = 0;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		/*
5370Sstevel@tonic-gate 		 * If the destination address is multicast and
5380Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
5390Sstevel@tonic-gate 		 * use the address of that interface as our
5400Sstevel@tonic-gate 		 * source address if no source address has been set.
5410Sstevel@tonic-gate 		 */
5420Sstevel@tonic-gate 		if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY &&
5430Sstevel@tonic-gate 		    CLASSD(v4dst) &&
5440Sstevel@tonic-gate 		    icmp->icmp_multicast_if_addr != INADDR_ANY) {
5450Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr,
5460Sstevel@tonic-gate 			    &icmp->icmp_v6src);
5470Sstevel@tonic-gate 		}
5480Sstevel@tonic-gate 	} else {
5490Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
5500Sstevel@tonic-gate 		/*
5510Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
5520Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
5530Sstevel@tonic-gate 		 * generate the T_CONN_CON.
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
5560Sstevel@tonic-gate 			v6dst = ipv6_loopback;
5570Sstevel@tonic-gate 			sin6->sin6_addr = v6dst;
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
5600Sstevel@tonic-gate 		icmp->icmp_flowinfo = flowinfo;
5610Sstevel@tonic-gate 		/*
5620Sstevel@tonic-gate 		 * If the destination address is multicast and
5630Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
5640Sstevel@tonic-gate 		 * then the ip bind logic will pick the correct source
5650Sstevel@tonic-gate 		 * address (i.e. matching the outgoing multicast interface).
5660Sstevel@tonic-gate 		 */
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Send down bind to IP to verify that there is a route
5710Sstevel@tonic-gate 	 * and to determine the source address.
5720Sstevel@tonic-gate 	 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput.
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
5750Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t),
5760Sstevel@tonic-gate 		    sin->sin_port);
5770Sstevel@tonic-gate 	} else {
5780Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
5790Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t),
5800Sstevel@tonic-gate 		    sin6->sin6_port);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 	if (mp1 == NULL) {
5830Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
5840Sstevel@tonic-gate 		return;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * We also have to send a connection confirmation to
5890Sstevel@tonic-gate 	 * keep TLI happy. Prepare it for icmp_rput.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
5920Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL,
5930Sstevel@tonic-gate 		    0);
5940Sstevel@tonic-gate 	} else {
5950Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
5960Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL,
5970Sstevel@tonic-gate 		    0);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 	if (mp2 == NULL) {
6000Sstevel@tonic-gate 		freemsg(mp1);
6010Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
6020Sstevel@tonic-gate 		return;
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
6060Sstevel@tonic-gate 	if (mp == NULL) {
6070Sstevel@tonic-gate 		/* Unable to reuse the T_CONN_REQ for the ack. */
6080Sstevel@tonic-gate 		freemsg(mp2);
6090Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
6100Sstevel@tonic-gate 		return;
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	icmp->icmp_state = TS_DATA_XFER;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* Hang onto the T_OK_ACK and T_CONN_CON for later. */
6160Sstevel@tonic-gate 	linkb(mp1, mp);
6170Sstevel@tonic-gate 	linkb(mp1, mp2);
6180Sstevel@tonic-gate 
6191676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
6200Sstevel@tonic-gate 	putnext(q, mp1);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate static int
6240Sstevel@tonic-gate icmp_close(queue_t *q)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
6270Sstevel@tonic-gate 	int	i1;
6280Sstevel@tonic-gate 
6291676Sjpk 	/* tell IP that if we're not here, he can't trust labels */
6301676Sjpk 	if (is_system_labeled())
6311676Sjpk 		putnext(WR(q), icmp->icmp_delabel);
6321676Sjpk 
6330Sstevel@tonic-gate 	qprocsoff(q);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/* If there are any options associated with the stream, free them. */
6360Sstevel@tonic-gate 	if (icmp->icmp_ip_snd_options)
6370Sstevel@tonic-gate 		mi_free((char *)icmp->icmp_ip_snd_options);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL)
6400Sstevel@tonic-gate 		kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/* Free memory associated with sticky options */
6430Sstevel@tonic-gate 	if (icmp->icmp_sticky_hdrs_len != 0) {
6440Sstevel@tonic-gate 		kmem_free(icmp->icmp_sticky_hdrs,
6450Sstevel@tonic-gate 		    icmp->icmp_sticky_hdrs_len);
6460Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = NULL;
6470Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = 0;
6480Sstevel@tonic-gate 	}
6491676Sjpk 
6501676Sjpk 	ip6_pkt_free(&icmp->icmp_sticky_ipp);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	crfree(icmp->icmp_credp);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/* Free the icmp structure and release the minor device number. */
6550Sstevel@tonic-gate 	i1 = mi_close_comm(&icmp_g_head, q);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	return (i1);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate  * This routine handles each T_DISCON_REQ message passed to icmp
6620Sstevel@tonic-gate  * as an indicating that ICMP is no longer connected. This results
6630Sstevel@tonic-gate  * in sending a T_BIND_REQ to IP to restore the binding to just
6640Sstevel@tonic-gate  * the local address.
6650Sstevel@tonic-gate  *
6660Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
6670Sstevel@tonic-gate  *	T_BIND_REQ	- specifying just the local address.
6680Sstevel@tonic-gate  *	T_OK_ACK	- for the T_DISCON_REQ
6690Sstevel@tonic-gate  *
6700Sstevel@tonic-gate  * The disconnect completes in icmp_rput.
6710Sstevel@tonic-gate  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
6720Sstevel@tonic-gate  * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
6730Sstevel@tonic-gate  * it to an error ack for the appropriate primitive.
6740Sstevel@tonic-gate  */
6750Sstevel@tonic-gate static void
6760Sstevel@tonic-gate icmp_disconnect(queue_t *q, mblk_t *mp)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	icmp_t	*icmp;
6790Sstevel@tonic-gate 	mblk_t	*mp1;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (icmp->icmp_state != TS_DATA_XFER) {
6840Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
6850Sstevel@tonic-gate 		    "icmp_disconnect: bad state, %d", icmp->icmp_state);
6860Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
6870Sstevel@tonic-gate 		return;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 	icmp->icmp_v6src = icmp->icmp_bound_v6src;
6900Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Send down bind to IP to remove the full binding and revert
6940Sstevel@tonic-gate 	 * to the local address binding.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
6970Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0);
6980Sstevel@tonic-gate 	} else {
6990Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
7000Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0);
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	if (mp1 == NULL) {
7030Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
7040Sstevel@tonic-gate 		return;
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
7070Sstevel@tonic-gate 	if (mp == NULL) {
7080Sstevel@tonic-gate 		/* Unable to reuse the T_DISCON_REQ for the ack. */
7090Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
7100Sstevel@tonic-gate 		return;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
7140Sstevel@tonic-gate 		int error;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		/* Rebuild the header template */
7170Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
7180Sstevel@tonic-gate 		if (error != 0) {
7190Sstevel@tonic-gate 			icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
7200Sstevel@tonic-gate 			freemsg(mp1);
7210Sstevel@tonic-gate 			return;
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 	icmp->icmp_discon_pending = 1;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/* Append the T_OK_ACK to the T_BIND_REQ for icmp_rput */
7270Sstevel@tonic-gate 	linkb(mp1, mp);
7280Sstevel@tonic-gate 	putnext(q, mp1);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate /* This routine creates a T_ERROR_ACK message and passes it upstream. */
7320Sstevel@tonic-gate static void
7330Sstevel@tonic-gate icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
7360Sstevel@tonic-gate 		qreply(q, mp);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /* Shorthand to generate and send TPI error acks to our client */
7400Sstevel@tonic-gate static void
7410Sstevel@tonic-gate icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
7420Sstevel@tonic-gate     t_scalar_t t_error, int sys_error)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	struct T_error_ack	*teackp;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
7470Sstevel@tonic-gate 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
7480Sstevel@tonic-gate 		teackp = (struct T_error_ack *)mp->b_rptr;
7490Sstevel@tonic-gate 		teackp->ERROR_prim = primitive;
7500Sstevel@tonic-gate 		teackp->TLI_error = t_error;
7510Sstevel@tonic-gate 		teackp->UNIX_error = sys_error;
7520Sstevel@tonic-gate 		qreply(q, mp);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate  * icmp_icmp_error is called by icmp_rput to process ICMP
7580Sstevel@tonic-gate  * messages passed up by IP.
7590Sstevel@tonic-gate  * Generates the appropriate T_UDERROR_IND for permanent
7600Sstevel@tonic-gate  * (non-transient) errors.
7610Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
7620Sstevel@tonic-gate  * the ICMP header.
7630Sstevel@tonic-gate  */
7640Sstevel@tonic-gate static void
7650Sstevel@tonic-gate icmp_icmp_error(queue_t *q, mblk_t *mp)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	icmph_t *icmph;
7680Sstevel@tonic-gate 	ipha_t	*ipha;
7690Sstevel@tonic-gate 	int	iph_hdr_length;
7700Sstevel@tonic-gate 	sin_t	sin;
7710Sstevel@tonic-gate 	sin6_t	sin6;
7720Sstevel@tonic-gate 	mblk_t	*mp1;
7730Sstevel@tonic-gate 	int	error = 0;
7740Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	/*
7770Sstevel@tonic-gate 	 * Deliver T_UDERROR_IND when the application has asked for it.
7780Sstevel@tonic-gate 	 * The socket layer enables this automatically when connected.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	if (!icmp->icmp_dgram_errind) {
7810Sstevel@tonic-gate 		freemsg(mp);
7820Sstevel@tonic-gate 		return;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7880Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
7890Sstevel@tonic-gate 		icmp_icmp_error_ipv6(q, mp);
7900Sstevel@tonic-gate 		return;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 	ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
7950Sstevel@tonic-gate 	icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]);
7960Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
7970Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	switch (icmph->icmph_type) {
8000Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
8010Sstevel@tonic-gate 		switch (icmph->icmph_code) {
8020Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
8030Sstevel@tonic-gate 			/*
8040Sstevel@tonic-gate 			 * IP has already adjusted the path MTU.
8050Sstevel@tonic-gate 			 * XXX Somehow pass MTU indication to application?
8060Sstevel@tonic-gate 			 */
8070Sstevel@tonic-gate 			break;
8080Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
8090Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
8100Sstevel@tonic-gate 			error = ECONNREFUSED;
8110Sstevel@tonic-gate 			break;
8120Sstevel@tonic-gate 		default:
8130Sstevel@tonic-gate 			/* Transient errors */
8140Sstevel@tonic-gate 			break;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		break;
8170Sstevel@tonic-gate 	default:
8180Sstevel@tonic-gate 		/* Transient errors */
8190Sstevel@tonic-gate 		break;
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 	if (error == 0) {
8220Sstevel@tonic-gate 		freemsg(mp);
8230Sstevel@tonic-gate 		return;
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	switch (icmp->icmp_family) {
8270Sstevel@tonic-gate 	case AF_INET:
8280Sstevel@tonic-gate 		sin = sin_null;
8290Sstevel@tonic-gate 		sin.sin_family = AF_INET;
8300Sstevel@tonic-gate 		sin.sin_addr.s_addr = ipha->ipha_dst;
8310Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
8320Sstevel@tonic-gate 		    error);
8330Sstevel@tonic-gate 		break;
8340Sstevel@tonic-gate 	case AF_INET6:
8350Sstevel@tonic-gate 		sin6 = sin6_null;
8360Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
8370Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
8400Sstevel@tonic-gate 		    NULL, 0, error);
8410Sstevel@tonic-gate 		break;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 	if (mp1)
8440Sstevel@tonic-gate 		putnext(q, mp1);
8450Sstevel@tonic-gate 	freemsg(mp);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate  * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6
8500Sstevel@tonic-gate  * for IPv6 packets.
8510Sstevel@tonic-gate  * Send permanent (non-transient) errors upstream.
8520Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
8530Sstevel@tonic-gate  * as the ICMPv6 header.
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate static void
8560Sstevel@tonic-gate icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	icmp6_t		*icmp6;
8590Sstevel@tonic-gate 	ip6_t		*ip6h, *outer_ip6h;
8600Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
8610Sstevel@tonic-gate 	uint8_t		*nexthdrp;
8620Sstevel@tonic-gate 	sin6_t		sin6;
8630Sstevel@tonic-gate 	mblk_t		*mp1;
8640Sstevel@tonic-gate 	int		error = 0;
8650Sstevel@tonic-gate 	icmp_t		*icmp = (icmp_t *)q->q_ptr;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	outer_ip6h = (ip6_t *)mp->b_rptr;
8680Sstevel@tonic-gate 	if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
8690Sstevel@tonic-gate 		iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
8700Sstevel@tonic-gate 	else
8710Sstevel@tonic-gate 		iph_hdr_length = IPV6_HDR_LEN;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
8740Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
8750Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
8760Sstevel@tonic-gate 		freemsg(mp);
8770Sstevel@tonic-gate 		return;
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	if (*nexthdrp != icmp->icmp_proto) {
8800Sstevel@tonic-gate 		/*
8810Sstevel@tonic-gate 		 * Could have switched icmp_proto after while ip did fanout of
8820Sstevel@tonic-gate 		 * this message
8830Sstevel@tonic-gate 		 */
8840Sstevel@tonic-gate 		freemsg(mp);
8850Sstevel@tonic-gate 		return;
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
8880Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
8890Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
8900Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
8910Sstevel@tonic-gate 			error = ECONNREFUSED;
8920Sstevel@tonic-gate 			break;
8930Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
8940Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
8950Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
8960Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
8970Sstevel@tonic-gate 			/* Transient errors */
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 		default:
9000Sstevel@tonic-gate 			break;
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 		break;
9030Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG: {
9040Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi;
9050Sstevel@tonic-gate 		struct T_opthdr		*toh;
9060Sstevel@tonic-gate 		size_t			udi_size;
9070Sstevel@tonic-gate 		mblk_t			*newmp;
9080Sstevel@tonic-gate 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
9090Sstevel@tonic-gate 		    sizeof (struct ip6_mtuinfo);
9100Sstevel@tonic-gate 		sin6_t			*sin6;
9110Sstevel@tonic-gate 		struct ip6_mtuinfo	*mtuinfo;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		/*
9140Sstevel@tonic-gate 		 * If the application has requested to receive path mtu
9150Sstevel@tonic-gate 		 * information, send up an empty message containing an
9160Sstevel@tonic-gate 		 * IPV6_PATHMTU ancillary data item.
9170Sstevel@tonic-gate 		 */
9180Sstevel@tonic-gate 		if (!icmp->icmp_ipv6_recvpathmtu)
9190Sstevel@tonic-gate 			break;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
9220Sstevel@tonic-gate 		    opt_length;
9230Sstevel@tonic-gate 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
9240Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipInErrors);
9250Sstevel@tonic-gate 			break;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 		/*
9290Sstevel@tonic-gate 		 * newmp->b_cont is left to NULL on purpose.  This is an
9300Sstevel@tonic-gate 		 * empty message containing only ancillary data.
9310Sstevel@tonic-gate 		 */
9320Sstevel@tonic-gate 		newmp->b_datap->db_type = M_PROTO;
9330Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
9340Sstevel@tonic-gate 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
9350Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
9360Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin6_t);
9370Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
9380Sstevel@tonic-gate 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
9390Sstevel@tonic-gate 		tudi->OPT_length = opt_length;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		sin6 = (sin6_t *)&tudi[1];
9420Sstevel@tonic-gate 		bzero(sin6, sizeof (sin6_t));
9430Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
9440Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_v6dst;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		toh = (struct T_opthdr *)&sin6[1];
9470Sstevel@tonic-gate 		toh->level = IPPROTO_IPV6;
9480Sstevel@tonic-gate 		toh->name = IPV6_PATHMTU;
9490Sstevel@tonic-gate 		toh->len = opt_length;
9500Sstevel@tonic-gate 		toh->status = 0;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
9530Sstevel@tonic-gate 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
9540Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
9550Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
9560Sstevel@tonic-gate 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
9570Sstevel@tonic-gate 		/*
9580Sstevel@tonic-gate 		 * We've consumed everything we need from the original
9590Sstevel@tonic-gate 		 * message.  Free it, then send our empty message.
9600Sstevel@tonic-gate 		 */
9610Sstevel@tonic-gate 		freemsg(mp);
9620Sstevel@tonic-gate 		putnext(q, newmp);
9630Sstevel@tonic-gate 		return;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
9660Sstevel@tonic-gate 		/* Transient errors */
9670Sstevel@tonic-gate 		break;
9680Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
9690Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
9700Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
9710Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
9720Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
9730Sstevel@tonic-gate 			error = ECONNREFUSED;
9740Sstevel@tonic-gate 			break;
9750Sstevel@tonic-gate 		}
9760Sstevel@tonic-gate 		break;
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	if (error == 0) {
9790Sstevel@tonic-gate 		freemsg(mp);
9800Sstevel@tonic-gate 		return;
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	sin6 = sin6_null;
9840Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
9850Sstevel@tonic-gate 	sin6.sin6_addr = ip6h->ip6_dst;
9860Sstevel@tonic-gate 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
9890Sstevel@tonic-gate 	    error);
9900Sstevel@tonic-gate 	if (mp1)
9910Sstevel@tonic-gate 		putnext(q, mp1);
9920Sstevel@tonic-gate 	freemsg(mp);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate /*
9960Sstevel@tonic-gate  * This routine responds to T_ADDR_REQ messages.  It is called by icmp_wput.
9970Sstevel@tonic-gate  * The local address is filled in if endpoint is bound. The remote address
9980Sstevel@tonic-gate  * is filled in if remote address has been precified ("connected endpoint")
9990Sstevel@tonic-gate  * (The concept of connected CLTS sockets is alien to published TPI
10000Sstevel@tonic-gate  *  but we support it anyway).
10010Sstevel@tonic-gate  */
10020Sstevel@tonic-gate static void
10030Sstevel@tonic-gate icmp_addr_req(queue_t *q, mblk_t *mp)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
10060Sstevel@tonic-gate 	mblk_t	*ackmp;
10070Sstevel@tonic-gate 	struct T_addr_ack *taa;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/* Make it large enough for worst case */
10100Sstevel@tonic-gate 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
10110Sstevel@tonic-gate 	    2 * sizeof (sin6_t), 1);
10120Sstevel@tonic-gate 	if (ackmp == NULL) {
10130Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
10140Sstevel@tonic-gate 		return;
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 	taa = (struct T_addr_ack *)ackmp->b_rptr;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	bzero(taa, sizeof (struct T_addr_ack));
10190Sstevel@tonic-gate 	ackmp->b_wptr = (uchar_t *)&taa[1];
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	taa->PRIM_type = T_ADDR_ACK;
10220Sstevel@tonic-gate 	ackmp->b_datap->db_type = M_PCPROTO;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	/*
10250Sstevel@tonic-gate 	 * Note: Following code assumes 32 bit alignment of basic
10260Sstevel@tonic-gate 	 * data structures like sin_t and struct T_addr_ack.
10270Sstevel@tonic-gate 	 */
10280Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
10290Sstevel@tonic-gate 		/*
10300Sstevel@tonic-gate 		 * Fill in local address
10310Sstevel@tonic-gate 		 */
10320Sstevel@tonic-gate 		taa->LOCADDR_offset = sizeof (*taa);
10330Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
10340Sstevel@tonic-gate 			sin_t	*sin;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin_t);
10370Sstevel@tonic-gate 			sin = (sin_t *)&taa[1];
10380Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
10390Sstevel@tonic-gate 			*sin = sin_null;
10400Sstevel@tonic-gate 			sin->sin_family = AF_INET;
10410Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
10420Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
10430Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src,
10440Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
10450Sstevel@tonic-gate 			} else {
10460Sstevel@tonic-gate 				/*
10470Sstevel@tonic-gate 				 * INADDR_ANY
10480Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
10490Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
10500Sstevel@tonic-gate 				 * local address instead (that could
10510Sstevel@tonic-gate 				 * also still be INADDR_ANY)
10520Sstevel@tonic-gate 				 */
10530Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src,
10540Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
10550Sstevel@tonic-gate 			}
10560Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin[1];
10570Sstevel@tonic-gate 		} else {
10580Sstevel@tonic-gate 			sin6_t	*sin6;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
10610Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin6_t);
10620Sstevel@tonic-gate 			sin6 = (sin6_t *)&taa[1];
10630Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
10640Sstevel@tonic-gate 			*sin6 = sin6_null;
10650Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
10660Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
10670Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_v6src;
10680Sstevel@tonic-gate 			} else {
10690Sstevel@tonic-gate 				/*
10700Sstevel@tonic-gate 				 * UNSPECIFIED
10710Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
10720Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
10730Sstevel@tonic-gate 				 * local address instead (that could
10740Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
10750Sstevel@tonic-gate 				 */
10760Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_bound_v6src;
10770Sstevel@tonic-gate 			}
10780Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin6[1];
10790Sstevel@tonic-gate 		}
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
10820Sstevel@tonic-gate 	qreply(q, ackmp);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate static void
10860Sstevel@tonic-gate icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate 	*tap = icmp_g_t_info_ack;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6)
10910Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin6_t);
10920Sstevel@tonic-gate 	else
10930Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin_t);
10940Sstevel@tonic-gate 	tap->CURRENT_state = icmp->icmp_state;
10950Sstevel@tonic-gate 	tap->OPT_size = icmp_max_optsize;
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*
10990Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
11000Sstevel@tonic-gate  * icmp_wput.  Much of the T_CAPABILITY_ACK information is copied from
11010Sstevel@tonic-gate  * icmp_g_t_info_ack.  The current state of the stream is copied from
11020Sstevel@tonic-gate  * icmp_state.
11030Sstevel@tonic-gate  */
11040Sstevel@tonic-gate static void
11050Sstevel@tonic-gate icmp_capability_req(queue_t *q, mblk_t *mp)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	icmp_t			*icmp = (icmp_t *)q->q_ptr;
11080Sstevel@tonic-gate 	t_uscalar_t		cap_bits1;
11090Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
11140Sstevel@tonic-gate 		mp->b_datap->db_type, T_CAPABILITY_ACK);
11150Sstevel@tonic-gate 	if (!mp)
11160Sstevel@tonic-gate 		return;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
11190Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
11220Sstevel@tonic-gate 		icmp_copy_info(&tcap->INFO_ack, icmp);
11230Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	qreply(q, mp);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate /*
11300Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages.  It is called by icmp_wput.
11310Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
11320Sstevel@tonic-gate  * The current state of the stream is copied from icmp_state.
11330Sstevel@tonic-gate  */
11340Sstevel@tonic-gate static void
11350Sstevel@tonic-gate icmp_info_req(queue_t *q, mblk_t *mp)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
11400Sstevel@tonic-gate 	    T_INFO_ACK);
11410Sstevel@tonic-gate 	if (!mp)
11420Sstevel@tonic-gate 		return;
11430Sstevel@tonic-gate 	icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
11440Sstevel@tonic-gate 	qreply(q, mp);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * IP recognizes seven kinds of bind requests:
11490Sstevel@tonic-gate  *
11500Sstevel@tonic-gate  * - A zero-length address binds only to the protocol number.
11510Sstevel@tonic-gate  *
11520Sstevel@tonic-gate  * - A 4-byte address is treated as a request to
11530Sstevel@tonic-gate  * validate that the address is a valid local IPv4
11540Sstevel@tonic-gate  * address, appropriate for an application to bind to.
11550Sstevel@tonic-gate  * IP does the verification, but does not make any note
11560Sstevel@tonic-gate  * of the address at this time.
11570Sstevel@tonic-gate  *
11580Sstevel@tonic-gate  * - A 16-byte address contains is treated as a request
11590Sstevel@tonic-gate  * to validate a local IPv6 address, as the 4-byte
11600Sstevel@tonic-gate  * address case above.
11610Sstevel@tonic-gate  *
11620Sstevel@tonic-gate  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
11630Sstevel@tonic-gate  * use it for the inbound fanout of packets.
11640Sstevel@tonic-gate  *
11650Sstevel@tonic-gate  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
11660Sstevel@tonic-gate  * use it for the inbound fanout of packets.
11670Sstevel@tonic-gate  *
11680Sstevel@tonic-gate  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
11690Sstevel@tonic-gate  * information consisting of local and remote addresses
11700Sstevel@tonic-gate  * and ports (unused for raw sockets).  In this case, the addresses are both
11710Sstevel@tonic-gate  * validated as appropriate for this operation, and, if
11720Sstevel@tonic-gate  * so, the information is retained for use in the
11730Sstevel@tonic-gate  * inbound fanout.
11740Sstevel@tonic-gate  *
11750Sstevel@tonic-gate  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
11760Sstevel@tonic-gate  * fanout information, like the 12-byte case above.
11770Sstevel@tonic-gate  *
11780Sstevel@tonic-gate  * IP will also fill in the IRE request mblk with information
11790Sstevel@tonic-gate  * regarding our peer.  In all cases, we notify IP of our protocol
11800Sstevel@tonic-gate  * type by appending a single protocol byte to the bind request.
11810Sstevel@tonic-gate  */
11820Sstevel@tonic-gate static mblk_t *
11830Sstevel@tonic-gate icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length,
11840Sstevel@tonic-gate     in_port_t fport)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate 	char	*cp;
11870Sstevel@tonic-gate 	mblk_t	*mp;
11880Sstevel@tonic-gate 	struct T_bind_req *tbr;
11890Sstevel@tonic-gate 	ipa_conn_t	*ac;
11900Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
11910Sstevel@tonic-gate 	sin_t		*sin;
11920Sstevel@tonic-gate 	sin6_t		*sin6;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
11970Sstevel@tonic-gate 	if (mp == NULL)
11980Sstevel@tonic-gate 		return (NULL);
11990Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
12000Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
12010Sstevel@tonic-gate 	tbr->PRIM_type = bind_prim;
12020Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (*tbr);
12030Sstevel@tonic-gate 	tbr->CONIND_number = 0;
12040Sstevel@tonic-gate 	tbr->ADDR_length = addr_length;
12050Sstevel@tonic-gate 	cp = (char *)&tbr[1];
12060Sstevel@tonic-gate 	switch (addr_length) {
12070Sstevel@tonic-gate 	case sizeof (ipa_conn_t):
12080Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
12090Sstevel@tonic-gate 		/* Append a request for an IRE */
12100Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12110Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
12120Sstevel@tonic-gate 			freemsg(mp);
12130Sstevel@tonic-gate 			return (NULL);
12140Sstevel@tonic-gate 		}
12150Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12160Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
12190Sstevel@tonic-gate 		ac = (ipa_conn_t *)cp;
12200Sstevel@tonic-gate 		ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src);
12210Sstevel@tonic-gate 		ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst);
12220Sstevel@tonic-gate 		ac->ac_fport = fport;
12230Sstevel@tonic-gate 		ac->ac_lport = 0;
12240Sstevel@tonic-gate 		break;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	case sizeof (ipa6_conn_t):
12270Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
12280Sstevel@tonic-gate 		/* Append a request for an IRE */
12290Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12300Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
12310Sstevel@tonic-gate 			freemsg(mp);
12320Sstevel@tonic-gate 			return (NULL);
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12350Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
12380Sstevel@tonic-gate 		ac6 = (ipa6_conn_t *)cp;
12390Sstevel@tonic-gate 		ac6->ac6_laddr = icmp->icmp_v6src;
12400Sstevel@tonic-gate 		ac6->ac6_faddr = icmp->icmp_v6dst;
12410Sstevel@tonic-gate 		ac6->ac6_fport = fport;
12420Sstevel@tonic-gate 		ac6->ac6_lport = 0;
12430Sstevel@tonic-gate 		break;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	case sizeof (sin_t):
12460Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
12470Sstevel@tonic-gate 		/* Append a request for an IRE */
12480Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12490Sstevel@tonic-gate 		if (!mp->b_cont) {
12500Sstevel@tonic-gate 			freemsg(mp);
12510Sstevel@tonic-gate 			return (NULL);
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12540Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 		sin = (sin_t *)cp;
12570Sstevel@tonic-gate 		*sin = sin_null;
12580Sstevel@tonic-gate 		sin->sin_family = AF_INET;
12590Sstevel@tonic-gate 		sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
12600Sstevel@tonic-gate 		break;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	case sizeof (sin6_t):
12630Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
12640Sstevel@tonic-gate 		/* Append a request for an IRE */
12650Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12660Sstevel@tonic-gate 		if (!mp->b_cont) {
12670Sstevel@tonic-gate 			freemsg(mp);
12680Sstevel@tonic-gate 			return (NULL);
12690Sstevel@tonic-gate 		}
12700Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12710Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
12740Sstevel@tonic-gate 		*sin6 = sin6_null;
12750Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
12760Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_bound_v6src;
12770Sstevel@tonic-gate 		break;
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 	/* Add protocol number to end */
12800Sstevel@tonic-gate 	cp[addr_length] = icmp->icmp_proto;
12810Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
12820Sstevel@tonic-gate 	return (mp);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate 
12851676Sjpk /* ARGSUSED */
12861676Sjpk static void
12871676Sjpk dummy_func(void *arg)
12881676Sjpk {
12891676Sjpk }
12901676Sjpk 
12911676Sjpk static mblk_t *
12921676Sjpk alloc_wait(queue_t *q, size_t len, int pri, int *errp)
12931676Sjpk {
12941676Sjpk 	mblk_t *mp;
12951676Sjpk 	bufcall_id_t id;
12961676Sjpk 	int retv;
12971676Sjpk 
12981676Sjpk 	while ((mp = allocb(len, pri)) == NULL) {
12991676Sjpk 		id = qbufcall(q, len, pri, dummy_func, NULL);
13001676Sjpk 		if (id == 0) {
13011676Sjpk 			*errp = ENOMEM;
13021676Sjpk 			break;
13031676Sjpk 		}
13041676Sjpk 		retv = qwait_sig(q);
13051676Sjpk 		qunbufcall(q, id);
13061676Sjpk 		if (retv == 0) {
13071676Sjpk 			*errp = EINTR;
13081676Sjpk 			break;
13091676Sjpk 		}
13101676Sjpk 	}
13111676Sjpk 	if (mp != NULL)
13121676Sjpk 		mp->b_wptr += len;
13131676Sjpk 	return (mp);
13141676Sjpk }
13151676Sjpk 
13160Sstevel@tonic-gate /*
13170Sstevel@tonic-gate  * This is the open routine for icmp.  It allocates a icmp_t structure for
13180Sstevel@tonic-gate  * the stream and, on the first open of the module, creates an ND table.
13190Sstevel@tonic-gate  */
13200Sstevel@tonic-gate static int
13210Sstevel@tonic-gate icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
13220Sstevel@tonic-gate {
13230Sstevel@tonic-gate 	int	err;
13240Sstevel@tonic-gate 	icmp_t	*icmp;
13251676Sjpk 	mblk_t	*mp;
13261676Sjpk 	out_labeled_t *olp;
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/* If the stream is already open, return immediately. */
13290Sstevel@tonic-gate 	if (q->q_ptr != NULL)
13300Sstevel@tonic-gate 		return (0);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/* If this is not a push of icmp as a module, fail. */
13330Sstevel@tonic-gate 	if (sflag != MODOPEN)
13340Sstevel@tonic-gate 		return (EINVAL);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * Defer the qprocson until everything is initialized since
13380Sstevel@tonic-gate 	 * we are D_MTPERQ and after qprocson the rput routine can
13390Sstevel@tonic-gate 	 * run. (Could do qprocson earlier since icmp currently
13400Sstevel@tonic-gate 	 * has an outer perimeter.)
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * Create a icmp_t structure for this stream and link into the
13450Sstevel@tonic-gate 	 * list of open streams.
13460Sstevel@tonic-gate 	 */
13470Sstevel@tonic-gate 	err = mi_open_comm(&icmp_g_head, sizeof (icmp_t), q, devp,
13480Sstevel@tonic-gate 	    flag, sflag, credp);
13491676Sjpk 	if (err != 0)
13500Sstevel@tonic-gate 		return (err);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	/*
13530Sstevel@tonic-gate 	 * The receive hiwat is only looked at on the stream head queue.
13540Sstevel@tonic-gate 	 * Store in q_hiwat in order to return on SO_RCVBUF getsockopts.
13550Sstevel@tonic-gate 	 */
13560Sstevel@tonic-gate 	q->q_hiwat = icmp_recv_hiwat;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	/* Set the initial state of the stream and the privilege status. */
13590Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
13600Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
13610Sstevel@tonic-gate 	icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13620Sstevel@tonic-gate 	icmp->icmp_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
13630Sstevel@tonic-gate 	icmp->icmp_filter = NULL;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	icmp->icmp_credp = credp;
13660Sstevel@tonic-gate 	crhold(credp);
13670Sstevel@tonic-gate 
13681676Sjpk 	/*
13691676Sjpk 	 * If the caller has the process-wide flag set, then default to MAC
13701676Sjpk 	 * exempt mode.  This allows read-down to unlabeled hosts.
13711676Sjpk 	 */
13721676Sjpk 	if (getpflags(NET_MAC_AWARE, credp) != 0)
13731676Sjpk 		icmp->icmp_mac_exempt = B_TRUE;
13741676Sjpk 
13750Sstevel@tonic-gate 	icmp->icmp_zoneid = getzoneid();
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	if (getmajor(*devp) == (major_t)ICMP6_MAJ) {
13780Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV6_VERSION;
13790Sstevel@tonic-gate 		icmp->icmp_family = AF_INET6;
13800Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
13810Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMPV6;
13820Sstevel@tonic-gate 		icmp->icmp_checksum_off = 2;	/* Offset for icmp6_cksum */
13830Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IPV6_HDR_LEN;
13840Sstevel@tonic-gate 		icmp->icmp_ttl = (uint8_t)icmp_ipv6_hoplimit;
13850Sstevel@tonic-gate 	} else {
13860Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV4_VERSION;
13870Sstevel@tonic-gate 		icmp->icmp_family = AF_INET;
13880Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
13890Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMP;
13900Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH;
13910Sstevel@tonic-gate 		icmp->icmp_ttl = (uint8_t)icmp_ipv4_ttl;
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 	qprocson(q);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	/*
13960Sstevel@tonic-gate 	 * Check if icmp is being I_PUSHed by a non-privileged user.
13970Sstevel@tonic-gate 	 * If so, we set icmp_restricted to indicate that only MIB
13980Sstevel@tonic-gate 	 * traffic may pass.
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	if (secpolicy_net_icmpaccess(credp) != 0) {
14010Sstevel@tonic-gate 		icmp->icmp_restricted = 1;
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	/*
14050Sstevel@tonic-gate 	 * The transmit hiwat is only looked at on IP's queue.
14060Sstevel@tonic-gate 	 * Store in q_hiwat in order to return on SO_SNDBUF
14070Sstevel@tonic-gate 	 * getsockopts.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	WR(q)->q_hiwat = icmp_xmit_hiwat;
14100Sstevel@tonic-gate 	WR(q)->q_next->q_hiwat = WR(q)->q_hiwat;
14110Sstevel@tonic-gate 	WR(q)->q_lowat = icmp_xmit_lowat;
14120Sstevel@tonic-gate 	WR(q)->q_next->q_lowat = WR(q)->q_lowat;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
14150Sstevel@tonic-gate 		/* Build initial header template for transmit */
14161676Sjpk 		err = icmp_build_hdrs(q, icmp);
14171676Sjpk 		if (err != 0)
14181676Sjpk 			goto open_error;
14190Sstevel@tonic-gate 	}
14200Sstevel@tonic-gate 	/* Set the Stream head write offset. */
14210Sstevel@tonic-gate 	(void) mi_set_sth_wroff(q, icmp->icmp_max_hdr_len + icmp_wroff_extra);
14220Sstevel@tonic-gate 	(void) mi_set_sth_hiwat(q, q->q_hiwat);
14230Sstevel@tonic-gate 
14241676Sjpk 	if (is_system_labeled()) {
14251676Sjpk 		/* notify IP that we know about labeling */
14261676Sjpk 		mp = alloc_wait(q, sizeof (*olp), BPRI_MED, &err);
14271676Sjpk 		if (mp == NULL)
14281676Sjpk 			goto open_error;
14291676Sjpk 		mp->b_datap->db_type = M_CTL;
14301676Sjpk 		olp = (out_labeled_t *)mp->b_rptr;
14311676Sjpk 		olp->out_labeled_type = IP_ULP_OUT_LABELED;
14321676Sjpk 		olp->out_qnext = WR(q)->q_next;
14331676Sjpk 		putnext(WR(q), mp);
14341676Sjpk 
14351676Sjpk 		/* save off a copy for closing */
14361676Sjpk 		mp = alloc_wait(q, sizeof (*olp), BPRI_MED, &err);
14371676Sjpk 		if (mp == NULL)
14381676Sjpk 			goto open_error;
14391676Sjpk 		mp->b_datap->db_type = M_CTL;
14401676Sjpk 		olp = (out_labeled_t *)mp->b_rptr;
14411676Sjpk 		olp->out_labeled_type = IP_ULP_OUT_LABELED;
14421676Sjpk 		olp->out_qnext = NULL;
14431676Sjpk 		icmp->icmp_delabel = mp;
14441676Sjpk 	}
14451676Sjpk 
14460Sstevel@tonic-gate 	return (0);
14471676Sjpk 
14481676Sjpk open_error:
14491676Sjpk 	qprocsoff(q);
14501676Sjpk 	crfree(credp);
14511676Sjpk 	(void) mi_close_comm(&icmp_g_head, q);
14521676Sjpk 	return (err);
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate /*
14560Sstevel@tonic-gate  * Which ICMP options OK to set through T_UNITDATA_REQ...
14570Sstevel@tonic-gate  */
14580Sstevel@tonic-gate /* ARGSUSED */
14590Sstevel@tonic-gate static boolean_t
14600Sstevel@tonic-gate icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
14610Sstevel@tonic-gate {
14620Sstevel@tonic-gate 	return (B_TRUE);
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate /*
14660Sstevel@tonic-gate  * This routine gets default values of certain options whose default
14670Sstevel@tonic-gate  * values are maintained by protcol specific code
14680Sstevel@tonic-gate  */
14690Sstevel@tonic-gate /* ARGSUSED */
14700Sstevel@tonic-gate int
14710Sstevel@tonic-gate icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr)
14720Sstevel@tonic-gate {
14730Sstevel@tonic-gate 	int *i1 = (int *)ptr;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	switch (level) {
14760Sstevel@tonic-gate 	case IPPROTO_IP:
14770Sstevel@tonic-gate 		switch (name) {
14780Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
14790Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
14800Sstevel@tonic-gate 			return (sizeof (uchar_t));
14810Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
14820Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
14830Sstevel@tonic-gate 			return (sizeof (uchar_t));
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 		break;
14860Sstevel@tonic-gate 	case IPPROTO_IPV6:
14870Sstevel@tonic-gate 		switch (name) {
14880Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
14890Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_TTL;
14900Sstevel@tonic-gate 			return (sizeof (int));
14910Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
14920Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
14930Sstevel@tonic-gate 			return (sizeof (int));
14940Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
14950Sstevel@tonic-gate 			*i1 = icmp_ipv6_hoplimit;
14960Sstevel@tonic-gate 			return (sizeof (int));
14970Sstevel@tonic-gate 		}
14980Sstevel@tonic-gate 		break;
14990Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
15000Sstevel@tonic-gate 		switch (name) {
15010Sstevel@tonic-gate 		case ICMP6_FILTER:
15020Sstevel@tonic-gate 			/* Make it look like "pass all" */
15030Sstevel@tonic-gate 			ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
15040Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
15050Sstevel@tonic-gate 		}
15060Sstevel@tonic-gate 		break;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 	return (-1);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate /*
15120Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
15130Sstevel@tonic-gate  * It returns the size of the option retrieved.
15140Sstevel@tonic-gate  */
15150Sstevel@tonic-gate int
15160Sstevel@tonic-gate icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
15190Sstevel@tonic-gate 	int	*i1 = (int *)ptr;
15200Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &icmp->icmp_sticky_ipp;
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	switch (level) {
15230Sstevel@tonic-gate 	case SOL_SOCKET:
15240Sstevel@tonic-gate 		switch (name) {
15250Sstevel@tonic-gate 		case SO_DEBUG:
15260Sstevel@tonic-gate 			*i1 = icmp->icmp_debug;
15270Sstevel@tonic-gate 			break;
15280Sstevel@tonic-gate 		case SO_TYPE:
15290Sstevel@tonic-gate 			*i1 = SOCK_RAW;
15300Sstevel@tonic-gate 			break;
15310Sstevel@tonic-gate 		case SO_PROTOTYPE:
15320Sstevel@tonic-gate 			*i1 = icmp->icmp_proto;
15330Sstevel@tonic-gate 			break;
15340Sstevel@tonic-gate 		case SO_REUSEADDR:
15350Sstevel@tonic-gate 			*i1 = icmp->icmp_reuseaddr;
15360Sstevel@tonic-gate 			break;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 		/*
15390Sstevel@tonic-gate 		 * The following three items are available here,
15400Sstevel@tonic-gate 		 * but are only meaningful to IP.
15410Sstevel@tonic-gate 		 */
15420Sstevel@tonic-gate 		case SO_DONTROUTE:
15430Sstevel@tonic-gate 			*i1 = icmp->icmp_dontroute;
15440Sstevel@tonic-gate 			break;
15450Sstevel@tonic-gate 		case SO_USELOOPBACK:
15460Sstevel@tonic-gate 			*i1 = icmp->icmp_useloopback;
15470Sstevel@tonic-gate 			break;
15480Sstevel@tonic-gate 		case SO_BROADCAST:
15490Sstevel@tonic-gate 			*i1 = icmp->icmp_broadcast;
15500Sstevel@tonic-gate 			break;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		case SO_SNDBUF:
15530Sstevel@tonic-gate 			ASSERT(q->q_hiwat <= INT_MAX);
15540Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
15550Sstevel@tonic-gate 			break;
15560Sstevel@tonic-gate 		case SO_RCVBUF:
15570Sstevel@tonic-gate 			ASSERT(RD(q)->q_hiwat <= INT_MAX);
15580Sstevel@tonic-gate 			*i1 = (int)RD(q)->q_hiwat;
15590Sstevel@tonic-gate 			break;
15600Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
15610Sstevel@tonic-gate 			*i1 = icmp->icmp_dgram_errind;
15620Sstevel@tonic-gate 			break;
15631673Sgt145670 		case SO_TIMESTAMP:
15641673Sgt145670 			*i1 = icmp->icmp_timestamp;
15651673Sgt145670 			break;
15661676Sjpk 		case SO_MAC_EXEMPT:
15671676Sjpk 			*i1 = icmp->icmp_mac_exempt;
15681676Sjpk 			break;
15690Sstevel@tonic-gate 		/*
1570*2263Ssommerfe 		 * Following four not meaningful for icmp
15710Sstevel@tonic-gate 		 * Action is same as "default" to which we fallthrough
15720Sstevel@tonic-gate 		 * so we keep them in comments.
15730Sstevel@tonic-gate 		 * case SO_LINGER:
15740Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
15750Sstevel@tonic-gate 		 * case SO_OOBINLINE:
1576*2263Ssommerfe 		 * case SO_ALLZONES:
15770Sstevel@tonic-gate 		 */
15780Sstevel@tonic-gate 		default:
15790Sstevel@tonic-gate 			return (-1);
15800Sstevel@tonic-gate 		}
15810Sstevel@tonic-gate 		break;
15820Sstevel@tonic-gate 	case IPPROTO_IP:
15830Sstevel@tonic-gate 		/*
15840Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
15850Sstevel@tonic-gate 		 */
15860Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET)
15870Sstevel@tonic-gate 			return (-1);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		switch (name) {
15900Sstevel@tonic-gate 		case IP_OPTIONS:
15910Sstevel@tonic-gate 		case T_IP_OPTIONS:
15920Sstevel@tonic-gate 			/* Options are passed up with each packet */
15930Sstevel@tonic-gate 			return (0);
15940Sstevel@tonic-gate 		case IP_HDRINCL:
15950Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_hdrincl;
15960Sstevel@tonic-gate 			break;
15970Sstevel@tonic-gate 		case IP_TOS:
15980Sstevel@tonic-gate 		case T_IP_TOS:
15990Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_type_of_service;
16000Sstevel@tonic-gate 			break;
16010Sstevel@tonic-gate 		case IP_TTL:
16020Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_ttl;
16030Sstevel@tonic-gate 			break;
16040Sstevel@tonic-gate 		case IP_MULTICAST_IF:
16050Sstevel@tonic-gate 			/* 0 address if not set */
16060Sstevel@tonic-gate 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
16070Sstevel@tonic-gate 			return (sizeof (ipaddr_t));
16080Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
16090Sstevel@tonic-gate 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
16100Sstevel@tonic-gate 			return (sizeof (uchar_t));
16110Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
16120Sstevel@tonic-gate 			*ptr = icmp->icmp_multicast_loop;
16130Sstevel@tonic-gate 			return (sizeof (uint8_t));
16140Sstevel@tonic-gate 		case IP_BOUND_IF:
16150Sstevel@tonic-gate 			/* Zero if not set */
16160Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
16170Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16180Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
16190Sstevel@tonic-gate 			*ptr = icmp->icmp_unspec_source;
16200Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16210Sstevel@tonic-gate 		case IP_XMIT_IF:
16220Sstevel@tonic-gate 			*i1 = icmp->icmp_xmit_if;
16230Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16240Sstevel@tonic-gate 		case IP_RECVIF:
16250Sstevel@tonic-gate 			*ptr = icmp->icmp_recvif;
16260Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16270Sstevel@tonic-gate 		/*
16280Sstevel@tonic-gate 		 * Cannot "get" the value of following options
16290Sstevel@tonic-gate 		 * at this level. Action is same as "default" to
16300Sstevel@tonic-gate 		 * which we fallthrough so we keep them in comments.
16310Sstevel@tonic-gate 		 *
16320Sstevel@tonic-gate 		 * case IP_ADD_MEMBERSHIP:
16330Sstevel@tonic-gate 		 * case IP_DROP_MEMBERSHIP:
16340Sstevel@tonic-gate 		 * case IP_BLOCK_SOURCE:
16350Sstevel@tonic-gate 		 * case IP_UNBLOCK_SOURCE:
16360Sstevel@tonic-gate 		 * case IP_ADD_SOURCE_MEMBERSHIP:
16370Sstevel@tonic-gate 		 * case IP_DROP_SOURCE_MEMBERSHIP:
16380Sstevel@tonic-gate 		 * case MCAST_JOIN_GROUP:
16390Sstevel@tonic-gate 		 * case MCAST_LEAVE_GROUP:
16400Sstevel@tonic-gate 		 * case MCAST_BLOCK_SOURCE:
16410Sstevel@tonic-gate 		 * case MCAST_UNBLOCK_SOURCE:
16420Sstevel@tonic-gate 		 * case MCAST_JOIN_SOURCE_GROUP:
16430Sstevel@tonic-gate 		 * case MCAST_LEAVE_SOURCE_GROUP:
16440Sstevel@tonic-gate 		 * case MRT_INIT:
16450Sstevel@tonic-gate 		 * case MRT_DONE:
16460Sstevel@tonic-gate 		 * case MRT_ADD_VIF:
16470Sstevel@tonic-gate 		 * case MRT_DEL_VIF:
16480Sstevel@tonic-gate 		 * case MRT_ADD_MFC:
16490Sstevel@tonic-gate 		 * case MRT_DEL_MFC:
16500Sstevel@tonic-gate 		 * case MRT_VERSION:
16510Sstevel@tonic-gate 		 * case MRT_ASSERT:
16520Sstevel@tonic-gate 		 * case IP_SEC_OPT:
16530Sstevel@tonic-gate 		 * case IP_DONTFAILOVER_IF:
16541663Spriyanka 		 * case IP_NEXTHOP:
16550Sstevel@tonic-gate 		 */
16560Sstevel@tonic-gate 		default:
16570Sstevel@tonic-gate 			return (-1);
16580Sstevel@tonic-gate 		}
16590Sstevel@tonic-gate 		break;
16600Sstevel@tonic-gate 	case IPPROTO_IPV6:
16610Sstevel@tonic-gate 		/*
16620Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
16630Sstevel@tonic-gate 		 */
16640Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
16650Sstevel@tonic-gate 			return (-1);
16660Sstevel@tonic-gate 		switch (name) {
16670Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
16680Sstevel@tonic-gate 			*i1 = (unsigned int)icmp->icmp_ttl;
16690Sstevel@tonic-gate 			break;
16700Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
16710Sstevel@tonic-gate 			/* 0 index if not set */
16720Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_if_index;
16730Sstevel@tonic-gate 			break;
16740Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
16750Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_ttl;
16760Sstevel@tonic-gate 			break;
16770Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
16780Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_loop;
16790Sstevel@tonic-gate 			break;
16800Sstevel@tonic-gate 		case IPV6_BOUND_IF:
16810Sstevel@tonic-gate 			/* Zero if not set */
16820Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
16830Sstevel@tonic-gate 			break;
16840Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
16850Sstevel@tonic-gate 			*i1 = icmp->icmp_unspec_source;
16860Sstevel@tonic-gate 			break;
16870Sstevel@tonic-gate 		case IPV6_CHECKSUM:
16880Sstevel@tonic-gate 			/*
16890Sstevel@tonic-gate 			 * Return offset or -1 if no checksum offset.
16900Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6
16910Sstevel@tonic-gate 			 */
16920Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
16930Sstevel@tonic-gate 				return (-1);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 			if (icmp->icmp_raw_checksum) {
16960Sstevel@tonic-gate 				*i1 = icmp->icmp_checksum_off;
16970Sstevel@tonic-gate 			} else {
16980Sstevel@tonic-gate 				*i1 = -1;
16990Sstevel@tonic-gate 			}
17000Sstevel@tonic-gate 			break;
17010Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
17020Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
17030Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
17040Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
17050Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
17060Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
17070Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
17080Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
17090Sstevel@tonic-gate 			/* cannot "get" the value for these */
17100Sstevel@tonic-gate 			return (-1);
17110Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
17120Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpktinfo;
17130Sstevel@tonic-gate 			break;
17140Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
17150Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvtclass;
17160Sstevel@tonic-gate 			break;
17170Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
17180Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpathmtu;
17190Sstevel@tonic-gate 			break;
17200Sstevel@tonic-gate 		case IPV6_V6ONLY:
17210Sstevel@tonic-gate 			*i1 = 1;
17220Sstevel@tonic-gate 			break;
17230Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
17240Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhoplimit;
17250Sstevel@tonic-gate 			break;
17260Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
17270Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhopopts;
17280Sstevel@tonic-gate 			break;
17290Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
17300Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvdstopts;
17310Sstevel@tonic-gate 			break;
17320Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
17330Sstevel@tonic-gate 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
17340Sstevel@tonic-gate 			break;
17350Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
17360Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
17370Sstevel@tonic-gate 			break;
17380Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
17390Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrthdr;
17400Sstevel@tonic-gate 			break;
17410Sstevel@tonic-gate 		case IPV6_PKTINFO: {
17420Sstevel@tonic-gate 			/* XXX assumes that caller has room for max size! */
17430Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
17460Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
17470Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
17480Sstevel@tonic-gate 			else
17490Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
17500Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
17510Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
17520Sstevel@tonic-gate 			else
17530Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
17540Sstevel@tonic-gate 			return (sizeof (struct in6_pktinfo));
17550Sstevel@tonic-gate 		}
17560Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
17570Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)ptr;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
17600Sstevel@tonic-gate 				return (0);
17610Sstevel@tonic-gate 			*sin6 = sin6_null;
17620Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
17630Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
17640Sstevel@tonic-gate 			return (sizeof (sin6_t));
17650Sstevel@tonic-gate 		}
17660Sstevel@tonic-gate 		case IPV6_HOPOPTS:
17670Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
17680Sstevel@tonic-gate 				return (0);
17691676Sjpk 			if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6)
17701676Sjpk 				return (0);
17711676Sjpk 			bcopy((char *)ipp->ipp_hopopts +
17721676Sjpk 			    icmp->icmp_label_len_v6, ptr,
17731676Sjpk 			    ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
17741676Sjpk 			if (icmp->icmp_label_len_v6 > 0) {
17751676Sjpk 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
17761676Sjpk 				ptr[1] = (ipp->ipp_hopoptslen -
17771676Sjpk 				    icmp->icmp_label_len_v6 + 7) / 8 - 1;
17781676Sjpk 			}
17791676Sjpk 			return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
17800Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
17810Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
17820Sstevel@tonic-gate 				return (0);
17830Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
17840Sstevel@tonic-gate 			return (ipp->ipp_rtdstoptslen);
17850Sstevel@tonic-gate 		case IPV6_RTHDR:
17860Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
17870Sstevel@tonic-gate 				return (0);
17880Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
17890Sstevel@tonic-gate 			return (ipp->ipp_rthdrlen);
17900Sstevel@tonic-gate 		case IPV6_DSTOPTS:
17910Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
17920Sstevel@tonic-gate 				return (0);
17930Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
17940Sstevel@tonic-gate 			return (ipp->ipp_dstoptslen);
17950Sstevel@tonic-gate 		case IPV6_PATHMTU:
17960Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
17970Sstevel@tonic-gate 				return (0);
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
18000Sstevel@tonic-gate 				(struct ip6_mtuinfo *)ptr));
18010Sstevel@tonic-gate 		case IPV6_TCLASS:
18020Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_TCLASS)
18030Sstevel@tonic-gate 				*i1 = ipp->ipp_tclass;
18040Sstevel@tonic-gate 			else
18050Sstevel@tonic-gate 				*i1 = IPV6_FLOW_TCLASS(
18060Sstevel@tonic-gate 				    IPV6_DEFAULT_VERS_AND_FLOW);
18070Sstevel@tonic-gate 			break;
18080Sstevel@tonic-gate 		default:
18090Sstevel@tonic-gate 			return (-1);
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 		break;
18120Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
18130Sstevel@tonic-gate 		/*
18140Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
18150Sstevel@tonic-gate 		 */
18160Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
18170Sstevel@tonic-gate 			return (-1);
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
18200Sstevel@tonic-gate 			return (-1);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 		switch (name) {
18230Sstevel@tonic-gate 		case ICMP6_FILTER:
18240Sstevel@tonic-gate 			if (icmp->icmp_filter == NULL) {
18250Sstevel@tonic-gate 				/* Make it look like "pass all" */
18260Sstevel@tonic-gate 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
18270Sstevel@tonic-gate 			} else {
18280Sstevel@tonic-gate 				(void) bcopy(icmp->icmp_filter, ptr,
18290Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
18300Sstevel@tonic-gate 			}
18310Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
18320Sstevel@tonic-gate 		default:
18330Sstevel@tonic-gate 			return (-1);
18340Sstevel@tonic-gate 		}
18350Sstevel@tonic-gate 	default:
18360Sstevel@tonic-gate 		return (-1);
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 	return (sizeof (int));
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate /* This routine sets socket options. */
18420Sstevel@tonic-gate /* ARGSUSED */
18430Sstevel@tonic-gate int
18440Sstevel@tonic-gate icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
18450Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
18460Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
18490Sstevel@tonic-gate 	int	*i1 = (int *)invalp;
18500Sstevel@tonic-gate 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
18510Sstevel@tonic-gate 	boolean_t checkonly;
18520Sstevel@tonic-gate 	int	error;
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	switch (optset_context) {
18550Sstevel@tonic-gate 	case SETFN_OPTCOM_CHECKONLY:
18560Sstevel@tonic-gate 		checkonly = B_TRUE;
18570Sstevel@tonic-gate 		/*
18580Sstevel@tonic-gate 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
18590Sstevel@tonic-gate 		 * inlen != 0 implies value supplied and
18600Sstevel@tonic-gate 		 * 	we have to "pretend" to set it.
18610Sstevel@tonic-gate 		 * inlen == 0 implies that there is no
18620Sstevel@tonic-gate 		 * 	value part in T_CHECK request and just validation
18630Sstevel@tonic-gate 		 * done elsewhere should be enough, we just return here.
18640Sstevel@tonic-gate 		 */
18650Sstevel@tonic-gate 		if (inlen == 0) {
18660Sstevel@tonic-gate 			*outlenp = 0;
18670Sstevel@tonic-gate 			return (0);
18680Sstevel@tonic-gate 		}
18690Sstevel@tonic-gate 		break;
18700Sstevel@tonic-gate 	case SETFN_OPTCOM_NEGOTIATE:
18710Sstevel@tonic-gate 		checkonly = B_FALSE;
18720Sstevel@tonic-gate 		break;
18730Sstevel@tonic-gate 	case SETFN_UD_NEGOTIATE:
18740Sstevel@tonic-gate 	case SETFN_CONN_NEGOTIATE:
18750Sstevel@tonic-gate 		checkonly = B_FALSE;
18760Sstevel@tonic-gate 		/*
18770Sstevel@tonic-gate 		 * Negotiating local and "association-related" options
18780Sstevel@tonic-gate 		 * through T_UNITDATA_REQ.
18790Sstevel@tonic-gate 		 *
18800Sstevel@tonic-gate 		 * Following routine can filter out ones we do not
18810Sstevel@tonic-gate 		 * want to be "set" this way.
18820Sstevel@tonic-gate 		 */
18830Sstevel@tonic-gate 		if (!icmp_opt_allow_udr_set(level, name)) {
18840Sstevel@tonic-gate 			*outlenp = 0;
18850Sstevel@tonic-gate 			return (EINVAL);
18860Sstevel@tonic-gate 		}
18870Sstevel@tonic-gate 		break;
18880Sstevel@tonic-gate 	default:
18890Sstevel@tonic-gate 		/*
18900Sstevel@tonic-gate 		 * We should never get here
18910Sstevel@tonic-gate 		 */
18920Sstevel@tonic-gate 		*outlenp = 0;
18930Sstevel@tonic-gate 		return (EINVAL);
18940Sstevel@tonic-gate 	}
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
18970Sstevel@tonic-gate 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	/*
19000Sstevel@tonic-gate 	 * For fixed length options, no sanity check
19010Sstevel@tonic-gate 	 * of passed in length is done. It is assumed *_optcom_req()
19020Sstevel@tonic-gate 	 * routines do the right thing.
19030Sstevel@tonic-gate 	 */
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	switch (level) {
19060Sstevel@tonic-gate 	case SOL_SOCKET:
19070Sstevel@tonic-gate 		switch (name) {
19080Sstevel@tonic-gate 		case SO_DEBUG:
19090Sstevel@tonic-gate 			if (!checkonly)
19100Sstevel@tonic-gate 				icmp->icmp_debug = onoff;
19110Sstevel@tonic-gate 			break;
19120Sstevel@tonic-gate 		case SO_PROTOTYPE:
19130Sstevel@tonic-gate 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
19140Sstevel@tonic-gate 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
19150Sstevel@tonic-gate 			    secpolicy_net_rawaccess(cr) != 0) {
19160Sstevel@tonic-gate 				*outlenp = 0;
19170Sstevel@tonic-gate 				return (EACCES);
19180Sstevel@tonic-gate 			}
19190Sstevel@tonic-gate 			/* Can't use IPPROTO_RAW with IPv6 */
19200Sstevel@tonic-gate 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
19210Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET6) {
19220Sstevel@tonic-gate 				*outlenp = 0;
19230Sstevel@tonic-gate 				return (EPROTONOSUPPORT);
19240Sstevel@tonic-gate 			}
19250Sstevel@tonic-gate 			if (checkonly) {
19260Sstevel@tonic-gate 				/* T_CHECK case */
19270Sstevel@tonic-gate 				*(int *)outvalp = (*i1 & 0xFF);
19280Sstevel@tonic-gate 				break;
19290Sstevel@tonic-gate 			}
19300Sstevel@tonic-gate 			icmp->icmp_proto = *i1 & 0xFF;
19310Sstevel@tonic-gate 			if ((icmp->icmp_proto == IPPROTO_RAW ||
19320Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_IGMP) &&
19330Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET)
19340Sstevel@tonic-gate 				icmp->icmp_hdrincl = 1;
19350Sstevel@tonic-gate 			else
19360Sstevel@tonic-gate 				icmp->icmp_hdrincl = 0;
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6 &&
19390Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
19400Sstevel@tonic-gate 				/* Set offset for icmp6_cksum */
19410Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
19420Sstevel@tonic-gate 				icmp->icmp_checksum_off = 2;
19430Sstevel@tonic-gate 			}
19440Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_UDP ||
19450Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_TCP ||
19460Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_SCTP) {
19470Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 1;
19480Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields |=
19490Sstevel@tonic-gate 				    IPPF_NO_CKSUM;
19500Sstevel@tonic-gate 			} else {
19510Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 0;
19520Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields &=
19530Sstevel@tonic-gate 				    ~IPPF_NO_CKSUM;
19540Sstevel@tonic-gate 			}
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 			if (icmp->icmp_filter != NULL &&
19570Sstevel@tonic-gate 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
19580Sstevel@tonic-gate 				kmem_free(icmp->icmp_filter,
19590Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
19600Sstevel@tonic-gate 				icmp->icmp_filter = NULL;
19610Sstevel@tonic-gate 			}
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 			/* Rebuild the header template */
19640Sstevel@tonic-gate 			error = icmp_build_hdrs(q, icmp);
19650Sstevel@tonic-gate 			if (error != 0) {
19660Sstevel@tonic-gate 				*outlenp = 0;
19670Sstevel@tonic-gate 				return (error);
19680Sstevel@tonic-gate 			}
19690Sstevel@tonic-gate 
1970409Skcpoon 			/*
1971409Skcpoon 			 * For SCTP, we don't use icmp_bind_proto() for
1972409Skcpoon 			 * raw socket binding.  Note that we do not need
1973409Skcpoon 			 * to set *outlenp.
1974409Skcpoon 			 */
1975409Skcpoon 			if (icmp->icmp_proto == IPPROTO_SCTP)
1976409Skcpoon 				return (0);
1977409Skcpoon 
19780Sstevel@tonic-gate 			icmp_bind_proto(q);
19790Sstevel@tonic-gate 			*outlenp = sizeof (int);
19800Sstevel@tonic-gate 			*(int *)outvalp = *i1 & 0xFF;
19810Sstevel@tonic-gate 			return (0);
19820Sstevel@tonic-gate 		case SO_REUSEADDR:
19830Sstevel@tonic-gate 			if (!checkonly)
19840Sstevel@tonic-gate 				icmp->icmp_reuseaddr = onoff;
19850Sstevel@tonic-gate 			break;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 		/*
19880Sstevel@tonic-gate 		 * The following three items are available here,
19890Sstevel@tonic-gate 		 * but are only meaningful to IP.
19900Sstevel@tonic-gate 		 */
19910Sstevel@tonic-gate 		case SO_DONTROUTE:
19920Sstevel@tonic-gate 			if (!checkonly)
19930Sstevel@tonic-gate 				icmp->icmp_dontroute = onoff;
19940Sstevel@tonic-gate 			break;
19950Sstevel@tonic-gate 		case SO_USELOOPBACK:
19960Sstevel@tonic-gate 			if (!checkonly)
19970Sstevel@tonic-gate 				icmp->icmp_useloopback = onoff;
19980Sstevel@tonic-gate 			break;
19990Sstevel@tonic-gate 		case SO_BROADCAST:
20000Sstevel@tonic-gate 			if (!checkonly)
20010Sstevel@tonic-gate 				icmp->icmp_broadcast = onoff;
20020Sstevel@tonic-gate 			break;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 		case SO_SNDBUF:
20050Sstevel@tonic-gate 			if (*i1 > icmp_max_buf) {
20060Sstevel@tonic-gate 				*outlenp = 0;
20070Sstevel@tonic-gate 				return (ENOBUFS);
20080Sstevel@tonic-gate 			}
20090Sstevel@tonic-gate 			if (!checkonly) {
20100Sstevel@tonic-gate 				q->q_hiwat = *i1;
20110Sstevel@tonic-gate 				q->q_next->q_hiwat = *i1;
20120Sstevel@tonic-gate 			}
20130Sstevel@tonic-gate 			break;
20140Sstevel@tonic-gate 		case SO_RCVBUF:
20150Sstevel@tonic-gate 			if (*i1 > icmp_max_buf) {
20160Sstevel@tonic-gate 				*outlenp = 0;
20170Sstevel@tonic-gate 				return (ENOBUFS);
20180Sstevel@tonic-gate 			}
20190Sstevel@tonic-gate 			if (!checkonly) {
20200Sstevel@tonic-gate 				RD(q)->q_hiwat = *i1;
20210Sstevel@tonic-gate 				(void) mi_set_sth_hiwat(RD(q), *i1);
20220Sstevel@tonic-gate 			}
20230Sstevel@tonic-gate 			break;
20240Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
20250Sstevel@tonic-gate 			if (!checkonly)
20260Sstevel@tonic-gate 				icmp->icmp_dgram_errind = onoff;
20270Sstevel@tonic-gate 			break;
2028*2263Ssommerfe 		case SO_ALLZONES:
2029*2263Ssommerfe 			/*
2030*2263Ssommerfe 			 * "soft" error (negative)
2031*2263Ssommerfe 			 * option not handled at this level
2032*2263Ssommerfe 			 * Note: Do not modify *outlenp
2033*2263Ssommerfe 			 */
2034*2263Ssommerfe 			return (-EINVAL);
20351673Sgt145670 		case SO_TIMESTAMP:
20361673Sgt145670 			if (!checkonly) {
20371673Sgt145670 				icmp->icmp_timestamp = onoff;
20381673Sgt145670 			}
20391673Sgt145670 			break;
20401676Sjpk 		case SO_MAC_EXEMPT:
20411676Sjpk 			if (secpolicy_net_mac_aware(cr) != 0 ||
20421676Sjpk 			    icmp->icmp_state != TS_UNBND)
20431676Sjpk 				return (EACCES);
20441676Sjpk 			if (!checkonly)
20451676Sjpk 				icmp->icmp_mac_exempt = onoff;
20461676Sjpk 			break;
20470Sstevel@tonic-gate 		/*
20480Sstevel@tonic-gate 		 * Following three not meaningful for icmp
20490Sstevel@tonic-gate 		 * Action is same as "default" so we keep them
20500Sstevel@tonic-gate 		 * in comments.
20510Sstevel@tonic-gate 		 * case SO_LINGER:
20520Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
20530Sstevel@tonic-gate 		 * case SO_OOBINLINE:
20540Sstevel@tonic-gate 		 */
20550Sstevel@tonic-gate 		default:
20560Sstevel@tonic-gate 			*outlenp = 0;
20570Sstevel@tonic-gate 			return (EINVAL);
20580Sstevel@tonic-gate 		}
20590Sstevel@tonic-gate 		break;
20600Sstevel@tonic-gate 	case IPPROTO_IP:
20610Sstevel@tonic-gate 		/*
20620Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
20630Sstevel@tonic-gate 		 */
20640Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET) {
20650Sstevel@tonic-gate 			*outlenp = 0;
20660Sstevel@tonic-gate 			return (ENOPROTOOPT);
20670Sstevel@tonic-gate 		}
20680Sstevel@tonic-gate 		switch (name) {
20690Sstevel@tonic-gate 		case IP_OPTIONS:
20700Sstevel@tonic-gate 		case T_IP_OPTIONS:
20710Sstevel@tonic-gate 			/* Save options for use by IP. */
20721676Sjpk 			if ((inlen & 0x3) ||
20731676Sjpk 			    inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) {
20740Sstevel@tonic-gate 				*outlenp = 0;
20750Sstevel@tonic-gate 				return (EINVAL);
20760Sstevel@tonic-gate 			}
20770Sstevel@tonic-gate 			if (checkonly)
20780Sstevel@tonic-gate 				break;
20790Sstevel@tonic-gate 
20801676Sjpk 			if (!tsol_option_set(&icmp->icmp_ip_snd_options,
20811676Sjpk 			    &icmp->icmp_ip_snd_options_len,
20821676Sjpk 			    icmp->icmp_label_len, invalp, inlen)) {
20831676Sjpk 				*outlenp = 0;
20841676Sjpk 				return (ENOMEM);
20850Sstevel@tonic-gate 			}
20861676Sjpk 
20870Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
20880Sstevel@tonic-gate 			    icmp->icmp_ip_snd_options_len;
20890Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
20900Sstevel@tonic-gate 						icmp_wroff_extra);
20910Sstevel@tonic-gate 			break;
20920Sstevel@tonic-gate 		case IP_HDRINCL:
20930Sstevel@tonic-gate 			if (!checkonly)
20940Sstevel@tonic-gate 				icmp->icmp_hdrincl = onoff;
20950Sstevel@tonic-gate 			break;
20960Sstevel@tonic-gate 		case IP_TOS:
20970Sstevel@tonic-gate 		case T_IP_TOS:
20980Sstevel@tonic-gate 			if (!checkonly) {
20990Sstevel@tonic-gate 				icmp->icmp_type_of_service = (uint8_t)*i1;
21000Sstevel@tonic-gate 			}
21010Sstevel@tonic-gate 			break;
21020Sstevel@tonic-gate 		case IP_TTL:
21030Sstevel@tonic-gate 			if (!checkonly) {
21040Sstevel@tonic-gate 				icmp->icmp_ttl = (uint8_t)*i1;
21050Sstevel@tonic-gate 			}
21060Sstevel@tonic-gate 			break;
21070Sstevel@tonic-gate 		case IP_MULTICAST_IF:
21080Sstevel@tonic-gate 			/*
21090Sstevel@tonic-gate 			 * TODO should check OPTMGMT reply and undo this if
21100Sstevel@tonic-gate 			 * there is an error.
21110Sstevel@tonic-gate 			 */
21120Sstevel@tonic-gate 			if (!checkonly)
21130Sstevel@tonic-gate 				icmp->icmp_multicast_if_addr = *i1;
21140Sstevel@tonic-gate 			break;
21150Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
21160Sstevel@tonic-gate 			if (!checkonly)
21170Sstevel@tonic-gate 				icmp->icmp_multicast_ttl = *invalp;
21180Sstevel@tonic-gate 			break;
21190Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
21200Sstevel@tonic-gate 			if (!checkonly) {
21210Sstevel@tonic-gate 				icmp->icmp_multicast_loop =
21220Sstevel@tonic-gate 				    (*invalp == 0) ? 0 : 1;
21230Sstevel@tonic-gate 			}
21240Sstevel@tonic-gate 			break;
21250Sstevel@tonic-gate 		case IP_BOUND_IF:
21260Sstevel@tonic-gate 			if (!checkonly)
21270Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
21280Sstevel@tonic-gate 			break;
21290Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
21300Sstevel@tonic-gate 			if (!checkonly)
21310Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
21320Sstevel@tonic-gate 			break;
21330Sstevel@tonic-gate 		case IP_XMIT_IF:
21340Sstevel@tonic-gate 			if (!checkonly)
21350Sstevel@tonic-gate 				icmp->icmp_xmit_if = *i1;
21360Sstevel@tonic-gate 			break;
21370Sstevel@tonic-gate 		case IP_RECVIF:
21380Sstevel@tonic-gate 			if (!checkonly)
21390Sstevel@tonic-gate 				icmp->icmp_recvif = onoff;
21400Sstevel@tonic-gate 			break;
21410Sstevel@tonic-gate 		case IP_ADD_MEMBERSHIP:
21420Sstevel@tonic-gate 		case IP_DROP_MEMBERSHIP:
21430Sstevel@tonic-gate 		case IP_BLOCK_SOURCE:
21440Sstevel@tonic-gate 		case IP_UNBLOCK_SOURCE:
21450Sstevel@tonic-gate 		case IP_ADD_SOURCE_MEMBERSHIP:
21460Sstevel@tonic-gate 		case IP_DROP_SOURCE_MEMBERSHIP:
21470Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
21480Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
21490Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
21500Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
21510Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
21520Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
21530Sstevel@tonic-gate 		case MRT_INIT:
21540Sstevel@tonic-gate 		case MRT_DONE:
21550Sstevel@tonic-gate 		case MRT_ADD_VIF:
21560Sstevel@tonic-gate 		case MRT_DEL_VIF:
21570Sstevel@tonic-gate 		case MRT_ADD_MFC:
21580Sstevel@tonic-gate 		case MRT_DEL_MFC:
21590Sstevel@tonic-gate 		case MRT_VERSION:
21600Sstevel@tonic-gate 		case MRT_ASSERT:
21610Sstevel@tonic-gate 		case IP_SEC_OPT:
21620Sstevel@tonic-gate 		case IP_DONTFAILOVER_IF:
21631663Spriyanka 		case IP_NEXTHOP:
21640Sstevel@tonic-gate 			/*
21650Sstevel@tonic-gate 			 * "soft" error (negative)
21660Sstevel@tonic-gate 			 * option not handled at this level
21670Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
21680Sstevel@tonic-gate 			 */
21690Sstevel@tonic-gate 			return (-EINVAL);
21700Sstevel@tonic-gate 		default:
21710Sstevel@tonic-gate 			*outlenp = 0;
21720Sstevel@tonic-gate 			return (EINVAL);
21730Sstevel@tonic-gate 		}
21740Sstevel@tonic-gate 		break;
21750Sstevel@tonic-gate 	case IPPROTO_IPV6: {
21760Sstevel@tonic-gate 		ip6_pkt_t		*ipp;
21770Sstevel@tonic-gate 		boolean_t		sticky;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
21800Sstevel@tonic-gate 			*outlenp = 0;
21810Sstevel@tonic-gate 			return (ENOPROTOOPT);
21820Sstevel@tonic-gate 		}
21830Sstevel@tonic-gate 		/*
21840Sstevel@tonic-gate 		 * Deal with both sticky options and ancillary data
21850Sstevel@tonic-gate 		 */
21860Sstevel@tonic-gate 		if (thisdg_attrs == NULL) {
21870Sstevel@tonic-gate 			/* sticky options, or none */
21880Sstevel@tonic-gate 			ipp = &icmp->icmp_sticky_ipp;
21890Sstevel@tonic-gate 			sticky = B_TRUE;
21900Sstevel@tonic-gate 		} else {
21910Sstevel@tonic-gate 			/* ancillary data */
21920Sstevel@tonic-gate 			ipp = (ip6_pkt_t *)thisdg_attrs;
21930Sstevel@tonic-gate 			sticky = B_FALSE;
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 		switch (name) {
21970Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
21980Sstevel@tonic-gate 			if (!checkonly)
21990Sstevel@tonic-gate 				icmp->icmp_multicast_if_index = *i1;
22000Sstevel@tonic-gate 			break;
22010Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
22020Sstevel@tonic-gate 			/* -1 means use default */
22030Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
22040Sstevel@tonic-gate 				*outlenp = 0;
22050Sstevel@tonic-gate 				return (EINVAL);
22060Sstevel@tonic-gate 			}
22070Sstevel@tonic-gate 			if (!checkonly) {
22080Sstevel@tonic-gate 				if (*i1 == -1) {
2209679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
22100Sstevel@tonic-gate 					    icmp_ipv6_hoplimit;
2211679Sseb 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
22120Sstevel@tonic-gate 					/* Pass modified value to IP. */
22130Sstevel@tonic-gate 					*i1 = ipp->ipp_hoplimit;
22140Sstevel@tonic-gate 				} else {
2215679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
22160Sstevel@tonic-gate 					    (uint8_t)*i1;
2217679Sseb 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
22180Sstevel@tonic-gate 				}
22190Sstevel@tonic-gate 				/* Rebuild the header template */
22200Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
22210Sstevel@tonic-gate 				if (error != 0) {
22220Sstevel@tonic-gate 					*outlenp = 0;
22230Sstevel@tonic-gate 					return (error);
22240Sstevel@tonic-gate 				}
22250Sstevel@tonic-gate 			}
22260Sstevel@tonic-gate 			break;
22270Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
22280Sstevel@tonic-gate 			/* -1 means use default */
22290Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
22300Sstevel@tonic-gate 				*outlenp = 0;
22310Sstevel@tonic-gate 				return (EINVAL);
22320Sstevel@tonic-gate 			}
22330Sstevel@tonic-gate 			if (!checkonly) {
22340Sstevel@tonic-gate 				if (*i1 == -1) {
22350Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2236679Sseb 					    ipp->ipp_multicast_hops =
22370Sstevel@tonic-gate 					    IP_DEFAULT_MULTICAST_TTL;
2238679Sseb 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
22390Sstevel@tonic-gate 					/* Pass modified value to IP. */
2240679Sseb 					*i1 = icmp->icmp_multicast_ttl;
22410Sstevel@tonic-gate 				} else {
22420Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2243679Sseb 					    ipp->ipp_multicast_hops =
22440Sstevel@tonic-gate 					    (uint8_t)*i1;
2245679Sseb 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
22460Sstevel@tonic-gate 				}
22470Sstevel@tonic-gate 			}
22480Sstevel@tonic-gate 			break;
22490Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
22500Sstevel@tonic-gate 			if (*i1 != 0 && *i1 != 1) {
22510Sstevel@tonic-gate 				*outlenp = 0;
22520Sstevel@tonic-gate 				return (EINVAL);
22530Sstevel@tonic-gate 			}
22540Sstevel@tonic-gate 			if (!checkonly)
22550Sstevel@tonic-gate 				icmp->icmp_multicast_loop = *i1;
22560Sstevel@tonic-gate 			break;
22570Sstevel@tonic-gate 		case IPV6_CHECKSUM:
22580Sstevel@tonic-gate 			/*
22590Sstevel@tonic-gate 			 * Integer offset into the user data of where the
22600Sstevel@tonic-gate 			 * checksum is located.
22610Sstevel@tonic-gate 			 * Offset of -1 disables option.
22620Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6.
22630Sstevel@tonic-gate 			 */
22640Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
22650Sstevel@tonic-gate 				*outlenp = 0;
22660Sstevel@tonic-gate 				return (EINVAL);
22670Sstevel@tonic-gate 			}
22680Sstevel@tonic-gate 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
22690Sstevel@tonic-gate 				/* Negative or not 16 bit aligned offset */
22700Sstevel@tonic-gate 				*outlenp = 0;
22710Sstevel@tonic-gate 				return (EINVAL);
22720Sstevel@tonic-gate 			}
22730Sstevel@tonic-gate 			if (checkonly)
22740Sstevel@tonic-gate 				break;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 			if (*i1 == -1) {
22770Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
22780Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
22790Sstevel@tonic-gate 			} else {
22800Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 1;
22810Sstevel@tonic-gate 				icmp->icmp_checksum_off = *i1;
22820Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
22830Sstevel@tonic-gate 			}
22840Sstevel@tonic-gate 			/* Rebuild the header template */
22850Sstevel@tonic-gate 			error = icmp_build_hdrs(q, icmp);
22860Sstevel@tonic-gate 			if (error != 0) {
22870Sstevel@tonic-gate 				*outlenp = 0;
22880Sstevel@tonic-gate 				return (error);
22890Sstevel@tonic-gate 			}
22900Sstevel@tonic-gate 			break;
22910Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
22920Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
22930Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
22940Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
22950Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
22960Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
22970Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
22980Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
22990Sstevel@tonic-gate 			/*
23000Sstevel@tonic-gate 			 * "soft" error (negative)
23010Sstevel@tonic-gate 			 * option not handled at this level
23020Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
23030Sstevel@tonic-gate 			 */
23040Sstevel@tonic-gate 			return (-EINVAL);
23050Sstevel@tonic-gate 		case IPV6_BOUND_IF:
23060Sstevel@tonic-gate 			if (!checkonly)
23070Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
23080Sstevel@tonic-gate 			break;
23090Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
23100Sstevel@tonic-gate 			if (!checkonly)
23110Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
23120Sstevel@tonic-gate 			break;
23130Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
23140Sstevel@tonic-gate 			if (!checkonly)
23150Sstevel@tonic-gate 				icmp->icmp_ipv6_recvtclass = onoff;
23160Sstevel@tonic-gate 			break;
23170Sstevel@tonic-gate 		/*
23180Sstevel@tonic-gate 		 * Set boolean switches for ancillary data delivery
23190Sstevel@tonic-gate 		 */
23200Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
23210Sstevel@tonic-gate 			if (!checkonly)
23220Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpktinfo = onoff;
23230Sstevel@tonic-gate 			break;
23240Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
23250Sstevel@tonic-gate 			if (!checkonly)
23260Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpathmtu = onoff;
23270Sstevel@tonic-gate 			break;
23280Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
23290Sstevel@tonic-gate 			if (!checkonly)
23300Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhoplimit = onoff;
23310Sstevel@tonic-gate 			break;
23320Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
23330Sstevel@tonic-gate 			if (!checkonly)
23340Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhopopts = onoff;
23350Sstevel@tonic-gate 			break;
23360Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
23370Sstevel@tonic-gate 			if (!checkonly)
23380Sstevel@tonic-gate 				icmp->icmp_ipv6_recvdstopts = onoff;
23390Sstevel@tonic-gate 			break;
23400Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
23410Sstevel@tonic-gate 			if (!checkonly)
23420Sstevel@tonic-gate 				icmp->icmp_old_ipv6_recvdstopts = onoff;
23430Sstevel@tonic-gate 			break;
23440Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
23450Sstevel@tonic-gate 			if (!checkonly)
23460Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrtdstopts = onoff;
23470Sstevel@tonic-gate 			break;
23480Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
23490Sstevel@tonic-gate 			if (!checkonly)
23500Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrthdr = onoff;
23510Sstevel@tonic-gate 			break;
23520Sstevel@tonic-gate 		/*
23530Sstevel@tonic-gate 		 * Set sticky options or ancillary data.
23540Sstevel@tonic-gate 		 * If sticky options, (re)build any extension headers
23550Sstevel@tonic-gate 		 * that might be needed as a result.
23560Sstevel@tonic-gate 		 */
23570Sstevel@tonic-gate 		case IPV6_PKTINFO:
23580Sstevel@tonic-gate 			/*
23590Sstevel@tonic-gate 			 * The source address and ifindex are verified
23600Sstevel@tonic-gate 			 * in ip_opt_set(). For ancillary data the
23610Sstevel@tonic-gate 			 * source address is checked in ip_wput_v6.
23620Sstevel@tonic-gate 			 */
23630Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
23640Sstevel@tonic-gate 				return (EINVAL);
23650Sstevel@tonic-gate 			if (checkonly)
23660Sstevel@tonic-gate 				break;
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 			if (inlen == 0) {
23690Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
23700Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |=
23710Sstevel@tonic-gate 				    (IPPF_IFINDEX|IPPF_ADDR);
23720Sstevel@tonic-gate 			} else {
23730Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
23760Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
23770Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
23780Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
23790Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
23800Sstevel@tonic-gate 				else
23810Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
23820Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
23830Sstevel@tonic-gate 				    &ipp->ipp_addr))
23840Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
23850Sstevel@tonic-gate 				else
23860Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
23870Sstevel@tonic-gate 			}
23880Sstevel@tonic-gate 			if (sticky) {
23890Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
23900Sstevel@tonic-gate 				if (error != 0)
23910Sstevel@tonic-gate 					return (error);
23920Sstevel@tonic-gate 			}
23930Sstevel@tonic-gate 			break;
23940Sstevel@tonic-gate 		case IPV6_HOPLIMIT:
2395679Sseb 			/* This option can only be used as ancillary data. */
2396679Sseb 			if (sticky)
2397679Sseb 				return (EINVAL);
23980Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
23990Sstevel@tonic-gate 				return (EINVAL);
24000Sstevel@tonic-gate 			if (checkonly)
24010Sstevel@tonic-gate 				break;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 			if (inlen == 0) {
24040Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
24050Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
24060Sstevel@tonic-gate 			} else {
24070Sstevel@tonic-gate 				if (*i1 > 255 || *i1 < -1)
24080Sstevel@tonic-gate 					return (EINVAL);
24090Sstevel@tonic-gate 				if (*i1 == -1)
24100Sstevel@tonic-gate 					ipp->ipp_hoplimit = icmp_ipv6_hoplimit;
24110Sstevel@tonic-gate 				else
24120Sstevel@tonic-gate 					ipp->ipp_hoplimit = *i1;
24130Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPLIMIT;
24140Sstevel@tonic-gate 			}
24150Sstevel@tonic-gate 			break;
24160Sstevel@tonic-gate 		case IPV6_TCLASS:
24170Sstevel@tonic-gate 			/*
24180Sstevel@tonic-gate 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
24190Sstevel@tonic-gate 			 * and [0, 255] as the actualy traffic class.
24200Sstevel@tonic-gate 			 */
24210Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
24220Sstevel@tonic-gate 				return (EINVAL);
24230Sstevel@tonic-gate 			if (checkonly)
24240Sstevel@tonic-gate 				break;
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 			if (inlen == 0) {
24270Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_TCLASS;
24280Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
24290Sstevel@tonic-gate 			} else {
24300Sstevel@tonic-gate 				if (*i1 >= 256 || *i1 < -1)
24310Sstevel@tonic-gate 					return (EINVAL);
24320Sstevel@tonic-gate 				if (*i1 == -1) {
24330Sstevel@tonic-gate 					ipp->ipp_tclass =
24340Sstevel@tonic-gate 					    IPV6_FLOW_TCLASS(
24350Sstevel@tonic-gate 					    IPV6_DEFAULT_VERS_AND_FLOW);
24360Sstevel@tonic-gate 				} else {
24370Sstevel@tonic-gate 					ipp->ipp_tclass = *i1;
24380Sstevel@tonic-gate 				}
24390Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_TCLASS;
24400Sstevel@tonic-gate 			}
24410Sstevel@tonic-gate 			if (sticky) {
24420Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
24430Sstevel@tonic-gate 				if (error != 0)
24440Sstevel@tonic-gate 					return (error);
24450Sstevel@tonic-gate 			}
24460Sstevel@tonic-gate 			break;
24470Sstevel@tonic-gate 		case IPV6_NEXTHOP:
24480Sstevel@tonic-gate 			/*
24490Sstevel@tonic-gate 			 * IP will verify that the nexthop is reachable
24500Sstevel@tonic-gate 			 * and fail for sticky options.
24510Sstevel@tonic-gate 			 */
24520Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t))
24530Sstevel@tonic-gate 				return (EINVAL);
24540Sstevel@tonic-gate 			if (checkonly)
24550Sstevel@tonic-gate 				break;
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 			if (inlen == 0) {
24580Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
24590Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
24600Sstevel@tonic-gate 			} else {
24610Sstevel@tonic-gate 				sin6_t *sin6 = (sin6_t *)invalp;
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6)
24640Sstevel@tonic-gate 					return (EAFNOSUPPORT);
24650Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
24660Sstevel@tonic-gate 					return (EADDRNOTAVAIL);
24670Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
24680Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
24690Sstevel@tonic-gate 				    &ipp->ipp_nexthop))
24700Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
24710Sstevel@tonic-gate 				else
24720Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
24730Sstevel@tonic-gate 			}
24740Sstevel@tonic-gate 			if (sticky) {
24750Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
24760Sstevel@tonic-gate 				if (error != 0)
24770Sstevel@tonic-gate 					return (error);
24780Sstevel@tonic-gate 			}
24790Sstevel@tonic-gate 			break;
24800Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
24810Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
24820Sstevel@tonic-gate 			/*
24830Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
24840Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
24850Sstevel@tonic-gate 			 */
24860Sstevel@tonic-gate 			if (inlen != 0 &&
24870Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1)))
24880Sstevel@tonic-gate 				return (EINVAL);
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 			if (checkonly)
24910Sstevel@tonic-gate 				break;
24921676Sjpk 			error = optcom_pkt_set(invalp, inlen, sticky,
24931676Sjpk 			    (uchar_t **)&ipp->ipp_hopopts,
24941676Sjpk 			    &ipp->ipp_hopoptslen,
24951676Sjpk 			    sticky ? icmp->icmp_label_len_v6 : 0);
24961676Sjpk 			if (error != 0)
24971676Sjpk 				return (error);
24981676Sjpk 			if (ipp->ipp_hopoptslen == 0) {
24990Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
25000Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
25010Sstevel@tonic-gate 			} else {
25020Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
25030Sstevel@tonic-gate 			}
25040Sstevel@tonic-gate 			if (sticky) {
25050Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25060Sstevel@tonic-gate 				if (error != 0)
25070Sstevel@tonic-gate 					return (error);
25080Sstevel@tonic-gate 			}
25090Sstevel@tonic-gate 			break;
25100Sstevel@tonic-gate 		}
25110Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
25120Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 			/*
25150Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25160Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25170Sstevel@tonic-gate 			 */
25180Sstevel@tonic-gate 			if (inlen != 0 &&
25190Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
25200Sstevel@tonic-gate 				return (EINVAL);
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 			if (checkonly)
25230Sstevel@tonic-gate 				break;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 			if (inlen == 0) {
25260Sstevel@tonic-gate 				if (sticky &&
25270Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
25280Sstevel@tonic-gate 					kmem_free(ipp->ipp_rtdstopts,
25290Sstevel@tonic-gate 					    ipp->ipp_rtdstoptslen);
25300Sstevel@tonic-gate 					ipp->ipp_rtdstopts = NULL;
25310Sstevel@tonic-gate 					ipp->ipp_rtdstoptslen = 0;
25320Sstevel@tonic-gate 				}
25330Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
25340Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
25350Sstevel@tonic-gate 			} else {
25361676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
25370Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rtdstopts,
25381676Sjpk 				    &ipp->ipp_rtdstoptslen, 0);
25390Sstevel@tonic-gate 				if (error != 0)
25400Sstevel@tonic-gate 					return (error);
25410Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
25420Sstevel@tonic-gate 			}
25430Sstevel@tonic-gate 			if (sticky) {
25440Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25450Sstevel@tonic-gate 				if (error != 0)
25460Sstevel@tonic-gate 					return (error);
25470Sstevel@tonic-gate 			}
25480Sstevel@tonic-gate 			break;
25490Sstevel@tonic-gate 		}
25500Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
25510Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 			/*
25540Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25550Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25560Sstevel@tonic-gate 			 */
25570Sstevel@tonic-gate 			if (inlen != 0 &&
25580Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
25590Sstevel@tonic-gate 				return (EINVAL);
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 			if (checkonly)
25620Sstevel@tonic-gate 				break;
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 			if (inlen == 0) {
25650Sstevel@tonic-gate 				if (sticky &&
25660Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
25670Sstevel@tonic-gate 					kmem_free(ipp->ipp_dstopts,
25680Sstevel@tonic-gate 					    ipp->ipp_dstoptslen);
25690Sstevel@tonic-gate 					ipp->ipp_dstopts = NULL;
25700Sstevel@tonic-gate 					ipp->ipp_dstoptslen = 0;
25710Sstevel@tonic-gate 				}
25720Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
25730Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
25740Sstevel@tonic-gate 			} else {
25751676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
25760Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_dstopts,
25771676Sjpk 				    &ipp->ipp_dstoptslen, 0);
25780Sstevel@tonic-gate 				if (error != 0)
25790Sstevel@tonic-gate 					return (error);
25800Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
25810Sstevel@tonic-gate 			}
25820Sstevel@tonic-gate 			if (sticky) {
25830Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25840Sstevel@tonic-gate 				if (error != 0)
25850Sstevel@tonic-gate 					return (error);
25860Sstevel@tonic-gate 			}
25870Sstevel@tonic-gate 			break;
25880Sstevel@tonic-gate 		}
25890Sstevel@tonic-gate 		case IPV6_RTHDR: {
25900Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 			/*
25930Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25940Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25950Sstevel@tonic-gate 			 */
25960Sstevel@tonic-gate 			if (inlen != 0 &&
25970Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1)))
25980Sstevel@tonic-gate 				return (EINVAL);
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 			if (checkonly)
26010Sstevel@tonic-gate 				break;
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 			if (inlen == 0) {
26040Sstevel@tonic-gate 				if (sticky &&
26050Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
26060Sstevel@tonic-gate 					kmem_free(ipp->ipp_rthdr,
26070Sstevel@tonic-gate 					    ipp->ipp_rthdrlen);
26080Sstevel@tonic-gate 					ipp->ipp_rthdr = NULL;
26090Sstevel@tonic-gate 					ipp->ipp_rthdrlen = 0;
26100Sstevel@tonic-gate 				}
26110Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
26120Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
26130Sstevel@tonic-gate 			} else {
26141676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
26150Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rthdr,
26161676Sjpk 				    &ipp->ipp_rthdrlen, 0);
26170Sstevel@tonic-gate 				if (error != 0)
26180Sstevel@tonic-gate 					return (error);
26190Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
26200Sstevel@tonic-gate 			}
26210Sstevel@tonic-gate 			if (sticky) {
26220Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
26230Sstevel@tonic-gate 				if (error != 0)
26240Sstevel@tonic-gate 					return (error);
26250Sstevel@tonic-gate 			}
26260Sstevel@tonic-gate 			break;
26270Sstevel@tonic-gate 		}
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 		case IPV6_DONTFRAG:
26300Sstevel@tonic-gate 			if (checkonly)
26310Sstevel@tonic-gate 				break;
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 			if (onoff) {
26340Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DONTFRAG;
26350Sstevel@tonic-gate 			} else {
26360Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
26370Sstevel@tonic-gate 			}
26380Sstevel@tonic-gate 			break;
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 		case IPV6_USE_MIN_MTU:
26410Sstevel@tonic-gate 			if (inlen != sizeof (int))
26420Sstevel@tonic-gate 				return (EINVAL);
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > 1)
26450Sstevel@tonic-gate 				return (EINVAL);
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 			if (checkonly)
26480Sstevel@tonic-gate 				break;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
26510Sstevel@tonic-gate 			ipp->ipp_use_min_mtu = *i1;
26520Sstevel@tonic-gate 			break;
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 		/*
26550Sstevel@tonic-gate 		 * This option can't be set.  Its only returned via
26560Sstevel@tonic-gate 		 * getsockopt() or ancillary data.
26570Sstevel@tonic-gate 		 */
26580Sstevel@tonic-gate 		case IPV6_PATHMTU:
26590Sstevel@tonic-gate 			return (EINVAL);
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 		case IPV6_BOUND_PIF:
26620Sstevel@tonic-gate 		case IPV6_SEC_OPT:
26630Sstevel@tonic-gate 		case IPV6_DONTFAILOVER_IF:
26640Sstevel@tonic-gate 		case IPV6_SRC_PREFERENCES:
26650Sstevel@tonic-gate 		case IPV6_V6ONLY:
26660Sstevel@tonic-gate 			/* Handled at IP level */
26670Sstevel@tonic-gate 			return (-EINVAL);
26680Sstevel@tonic-gate 		default:
26690Sstevel@tonic-gate 			*outlenp = 0;
26700Sstevel@tonic-gate 			return (EINVAL);
26710Sstevel@tonic-gate 		}
26720Sstevel@tonic-gate 		break;
26730Sstevel@tonic-gate 	}		/* end IPPROTO_IPV6 */
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
26760Sstevel@tonic-gate 		/*
26770Sstevel@tonic-gate 		 * Only allow IPv6 option processing on IPv6 sockets.
26780Sstevel@tonic-gate 		 */
26790Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
26800Sstevel@tonic-gate 			*outlenp = 0;
26810Sstevel@tonic-gate 			return (ENOPROTOOPT);
26820Sstevel@tonic-gate 		}
26830Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
26840Sstevel@tonic-gate 			*outlenp = 0;
26850Sstevel@tonic-gate 			return (ENOPROTOOPT);
26860Sstevel@tonic-gate 		}
26870Sstevel@tonic-gate 		switch (name) {
26880Sstevel@tonic-gate 		case ICMP6_FILTER:
26890Sstevel@tonic-gate 			if (!checkonly) {
26900Sstevel@tonic-gate 				if ((inlen != 0) &&
26910Sstevel@tonic-gate 				    (inlen != sizeof (icmp6_filter_t)))
26920Sstevel@tonic-gate 					return (EINVAL);
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 				if (inlen == 0) {
26950Sstevel@tonic-gate 					if (icmp->icmp_filter != NULL) {
26960Sstevel@tonic-gate 						kmem_free(icmp->icmp_filter,
26970Sstevel@tonic-gate 						    sizeof (icmp6_filter_t));
26980Sstevel@tonic-gate 						icmp->icmp_filter = NULL;
26990Sstevel@tonic-gate 					}
27000Sstevel@tonic-gate 				} else {
27010Sstevel@tonic-gate 					if (icmp->icmp_filter == NULL) {
27020Sstevel@tonic-gate 						icmp->icmp_filter = kmem_alloc(
27030Sstevel@tonic-gate 						    sizeof (icmp6_filter_t),
27040Sstevel@tonic-gate 						    KM_NOSLEEP);
27050Sstevel@tonic-gate 						if (icmp->icmp_filter == NULL) {
27060Sstevel@tonic-gate 							*outlenp = 0;
27070Sstevel@tonic-gate 							return (ENOBUFS);
27080Sstevel@tonic-gate 						}
27090Sstevel@tonic-gate 					}
27100Sstevel@tonic-gate 					(void) bcopy(invalp, icmp->icmp_filter,
27110Sstevel@tonic-gate 					    inlen);
27120Sstevel@tonic-gate 				}
27130Sstevel@tonic-gate 			}
27140Sstevel@tonic-gate 			break;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 		default:
27170Sstevel@tonic-gate 			*outlenp = 0;
27180Sstevel@tonic-gate 			return (EINVAL);
27190Sstevel@tonic-gate 		}
27200Sstevel@tonic-gate 		break;
27210Sstevel@tonic-gate 	default:
27220Sstevel@tonic-gate 		*outlenp = 0;
27230Sstevel@tonic-gate 		return (EINVAL);
27240Sstevel@tonic-gate 	}
27250Sstevel@tonic-gate 	/*
27260Sstevel@tonic-gate 	 * Common case of OK return with outval same as inval.
27270Sstevel@tonic-gate 	 */
27280Sstevel@tonic-gate 	if (invalp != outvalp) {
27290Sstevel@tonic-gate 		/* don't trust bcopy for identical src/dst */
27300Sstevel@tonic-gate 		(void) bcopy(invalp, outvalp, inlen);
27310Sstevel@tonic-gate 	}
27320Sstevel@tonic-gate 	*outlenp = inlen;
27330Sstevel@tonic-gate 	return (0);
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate /*
27370Sstevel@tonic-gate  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
27380Sstevel@tonic-gate  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
27390Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
27400Sstevel@tonic-gate  * headers.
27410Sstevel@tonic-gate  * Returns failure if can't allocate memory.
27420Sstevel@tonic-gate  */
27430Sstevel@tonic-gate static int
27440Sstevel@tonic-gate icmp_build_hdrs(queue_t *q, icmp_t *icmp)
27450Sstevel@tonic-gate {
27460Sstevel@tonic-gate 	uchar_t	*hdrs;
27470Sstevel@tonic-gate 	uint_t	hdrs_len;
27480Sstevel@tonic-gate 	ip6_t	*ip6h;
27490Sstevel@tonic-gate 	ip6i_t	*ip6i;
27500Sstevel@tonic-gate 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp);
27530Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
27540Sstevel@tonic-gate 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
27550Sstevel@tonic-gate 		/* Need to reallocate */
27560Sstevel@tonic-gate 		if (hdrs_len != 0) {
27570Sstevel@tonic-gate 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
27580Sstevel@tonic-gate 			if (hdrs == NULL)
27590Sstevel@tonic-gate 				return (ENOMEM);
27600Sstevel@tonic-gate 		} else {
27610Sstevel@tonic-gate 			hdrs = NULL;
27620Sstevel@tonic-gate 		}
27630Sstevel@tonic-gate 		if (icmp->icmp_sticky_hdrs_len != 0) {
27640Sstevel@tonic-gate 			kmem_free(icmp->icmp_sticky_hdrs,
27650Sstevel@tonic-gate 			    icmp->icmp_sticky_hdrs_len);
27660Sstevel@tonic-gate 		}
27670Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = hdrs;
27680Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = hdrs_len;
27690Sstevel@tonic-gate 	}
27700Sstevel@tonic-gate 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
27710Sstevel@tonic-gate 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate 	/* Set header fields not in ipp */
27740Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
27750Sstevel@tonic-gate 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
27760Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
27790Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
27800Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
27810Sstevel@tonic-gate 		}
27820Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
27830Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
27840Sstevel@tonic-gate 		}
27850Sstevel@tonic-gate 	} else {
27860Sstevel@tonic-gate 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
27870Sstevel@tonic-gate 	}
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_ADDR))
27900Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	/* Try to get everything in a single mblk */
27930Sstevel@tonic-gate 	if (hdrs_len > icmp->icmp_max_hdr_len) {
27940Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = hdrs_len;
27950Sstevel@tonic-gate 		(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
27960Sstevel@tonic-gate 		    icmp_wroff_extra);
27970Sstevel@tonic-gate 	}
27980Sstevel@tonic-gate 	return (0);
27990Sstevel@tonic-gate }
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate /*
28020Sstevel@tonic-gate  * This routine retrieves the value of an ND variable in a icmpparam_t
28030Sstevel@tonic-gate  * structure.  It is called through nd_getset when a user reads the
28040Sstevel@tonic-gate  * variable.
28050Sstevel@tonic-gate  */
28060Sstevel@tonic-gate /* ARGSUSED */
28070Sstevel@tonic-gate static int
28080Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
28090Sstevel@tonic-gate {
28100Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
28130Sstevel@tonic-gate 	return (0);
28140Sstevel@tonic-gate }
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate /*
28170Sstevel@tonic-gate  * Walk through the param array specified registering each element with the
28180Sstevel@tonic-gate  * named dispatch (ND) handler.
28190Sstevel@tonic-gate  */
28200Sstevel@tonic-gate static boolean_t
28210Sstevel@tonic-gate icmp_param_register(icmpparam_t *icmppa, int cnt)
28220Sstevel@tonic-gate {
28230Sstevel@tonic-gate 	for (; cnt-- > 0; icmppa++) {
28240Sstevel@tonic-gate 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
28250Sstevel@tonic-gate 			if (!nd_load(&icmp_g_nd, icmppa->icmp_param_name,
28260Sstevel@tonic-gate 			    icmp_param_get, icmp_param_set,
28270Sstevel@tonic-gate 			    (caddr_t)icmppa)) {
28280Sstevel@tonic-gate 				nd_free(&icmp_g_nd);
28290Sstevel@tonic-gate 				return (B_FALSE);
28300Sstevel@tonic-gate 			}
28310Sstevel@tonic-gate 		}
28320Sstevel@tonic-gate 	}
28330Sstevel@tonic-gate 	if (!nd_load(&icmp_g_nd, "icmp_status", icmp_status_report, NULL,
28340Sstevel@tonic-gate 	    NULL)) {
28350Sstevel@tonic-gate 		nd_free(&icmp_g_nd);
28360Sstevel@tonic-gate 		return (B_FALSE);
28370Sstevel@tonic-gate 	}
28380Sstevel@tonic-gate 	return (B_TRUE);
28390Sstevel@tonic-gate }
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */
28420Sstevel@tonic-gate /* ARGSUSED */
28430Sstevel@tonic-gate static int
28440Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
28450Sstevel@tonic-gate {
28460Sstevel@tonic-gate 	long		new_value;
28470Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 	/*
28500Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
28510Sstevel@tonic-gate 	 * required bounds.
28520Sstevel@tonic-gate 	 */
28530Sstevel@tonic-gate 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
28540Sstevel@tonic-gate 	    new_value < icmppa->icmp_param_min ||
28550Sstevel@tonic-gate 	    new_value > icmppa->icmp_param_max) {
28560Sstevel@tonic-gate 		return (EINVAL);
28570Sstevel@tonic-gate 	}
28580Sstevel@tonic-gate 	/* Set the new value */
28590Sstevel@tonic-gate 	icmppa->icmp_param_value = new_value;
28600Sstevel@tonic-gate 	return (0);
28610Sstevel@tonic-gate }
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate static void
28640Sstevel@tonic-gate icmp_rput(queue_t *q, mblk_t *mp)
28650Sstevel@tonic-gate {
28660Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
28670Sstevel@tonic-gate 	uchar_t			*rptr;
28680Sstevel@tonic-gate 	struct T_error_ack	*tea;
28690Sstevel@tonic-gate 	icmp_t			*icmp;
28700Sstevel@tonic-gate 	sin_t			*sin;
28710Sstevel@tonic-gate 	sin6_t			*sin6;
28720Sstevel@tonic-gate 	ip6_t			*ip6h;
28730Sstevel@tonic-gate 	ip6i_t			*ip6i;
28740Sstevel@tonic-gate 	mblk_t			*mp1;
28750Sstevel@tonic-gate 	int			hdr_len;
28760Sstevel@tonic-gate 	ipha_t			*ipha;
28770Sstevel@tonic-gate 	int			udi_size;	/* Size of T_unitdata_ind */
28780Sstevel@tonic-gate 	uint_t			ipvers;
28790Sstevel@tonic-gate 	ip6_pkt_t		ipp;
28800Sstevel@tonic-gate 	uint8_t			nexthdr;
28810Sstevel@tonic-gate 	boolean_t		recvif = B_FALSE;
28820Sstevel@tonic-gate 	in_pktinfo_t		*pinfo;
28830Sstevel@tonic-gate 	mblk_t			*options_mp = NULL;
28840Sstevel@tonic-gate 	uint_t			icmp_opt = 0;
28850Sstevel@tonic-gate 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
28861676Sjpk 	uint_t			hopstrip;
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
28890Sstevel@tonic-gate 	if (icmp->icmp_restricted) {
28900Sstevel@tonic-gate 		putnext(q, mp);
28910Sstevel@tonic-gate 		return;
28920Sstevel@tonic-gate 	}
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_CTL) {
28950Sstevel@tonic-gate 		/*
28960Sstevel@tonic-gate 		 * IP sends up the IPSEC_IN message for handling IPSEC
28970Sstevel@tonic-gate 		 * policy at the TCP level. We don't need it here.
28980Sstevel@tonic-gate 		 */
28990Sstevel@tonic-gate 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
29000Sstevel@tonic-gate 			mp1 = mp->b_cont;
29010Sstevel@tonic-gate 			freeb(mp);
29020Sstevel@tonic-gate 			mp = mp1;
29030Sstevel@tonic-gate 		} else {
29040Sstevel@tonic-gate 			pinfo = (in_pktinfo_t *)mp->b_rptr;
29050Sstevel@tonic-gate 			if ((icmp->icmp_recvif != 0) &&
29060Sstevel@tonic-gate 			    (pinfo->in_pkt_ulp_type == IN_PKTINFO)) {
29070Sstevel@tonic-gate 				/*
29080Sstevel@tonic-gate 				 * IP has passed the options in mp and the
29090Sstevel@tonic-gate 				 * actual data is in b_cont.
29100Sstevel@tonic-gate 				 */
29110Sstevel@tonic-gate 				recvif = B_TRUE;
29120Sstevel@tonic-gate 				/*
29130Sstevel@tonic-gate 				 * We are here bcos IP_RECVIF is set so we need
29140Sstevel@tonic-gate 				 * to extract the options mblk and adjust the
29150Sstevel@tonic-gate 				 * rptr
29160Sstevel@tonic-gate 				 */
29170Sstevel@tonic-gate 				options_mp = mp;
29180Sstevel@tonic-gate 				mp = mp->b_cont;
29190Sstevel@tonic-gate 			}
29200Sstevel@tonic-gate 		}
29210Sstevel@tonic-gate 	}
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	rptr = mp->b_rptr;
29240Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
29250Sstevel@tonic-gate 	case M_DATA:
29260Sstevel@tonic-gate 		/*
29270Sstevel@tonic-gate 		 * M_DATA messages contain IP packets.  They are handled
29280Sstevel@tonic-gate 		 * following the switch.
29290Sstevel@tonic-gate 		 */
29300Sstevel@tonic-gate 		break;
29310Sstevel@tonic-gate 	case M_PROTO:
29320Sstevel@tonic-gate 	case M_PCPROTO:
29330Sstevel@tonic-gate 		/* M_PROTO messages contain some type of TPI message. */
29340Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
29350Sstevel@tonic-gate 			freemsg(mp);
29360Sstevel@tonic-gate 			return;
29370Sstevel@tonic-gate 		}
29380Sstevel@tonic-gate 		tea = (struct T_error_ack *)rptr;
29390Sstevel@tonic-gate 		switch (tea->PRIM_type) {
29400Sstevel@tonic-gate 		case T_ERROR_ACK:
29410Sstevel@tonic-gate 			switch (tea->ERROR_prim) {
29420Sstevel@tonic-gate 			case O_T_BIND_REQ:
29430Sstevel@tonic-gate 			case T_BIND_REQ:
29440Sstevel@tonic-gate 				/*
29450Sstevel@tonic-gate 				 * If our O_T_BIND_REQ/T_BIND_REQ fails,
29460Sstevel@tonic-gate 				 * clear out the source address before
29470Sstevel@tonic-gate 				 * passing the message upstream.
29480Sstevel@tonic-gate 				 * If this was caused by a T_CONN_REQ
29490Sstevel@tonic-gate 				 * revert back to bound state.
29500Sstevel@tonic-gate 				 */
29510Sstevel@tonic-gate 				if (icmp->icmp_state == TS_UNBND) {
29520Sstevel@tonic-gate 					/*
29530Sstevel@tonic-gate 					 * TPI has not yet bound - bind sent by
29540Sstevel@tonic-gate 					 * icmp_bind_proto.
29550Sstevel@tonic-gate 					 */
29560Sstevel@tonic-gate 					freemsg(mp);
29570Sstevel@tonic-gate 					return;
29580Sstevel@tonic-gate 				}
29590Sstevel@tonic-gate 				if (icmp->icmp_state == TS_DATA_XFER) {
29600Sstevel@tonic-gate 					/* Connect failed */
29610Sstevel@tonic-gate 					tea->ERROR_prim = T_CONN_REQ;
29620Sstevel@tonic-gate 					icmp->icmp_v6src =
29630Sstevel@tonic-gate 					    icmp->icmp_bound_v6src;
29640Sstevel@tonic-gate 					icmp->icmp_state = TS_IDLE;
29650Sstevel@tonic-gate 					if (icmp->icmp_family == AF_INET6)
29660Sstevel@tonic-gate 						(void) icmp_build_hdrs(q, icmp);
29670Sstevel@tonic-gate 					break;
29680Sstevel@tonic-gate 				}
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 				if (icmp->icmp_discon_pending) {
29710Sstevel@tonic-gate 					tea->ERROR_prim = T_DISCON_REQ;
29720Sstevel@tonic-gate 					icmp->icmp_discon_pending = 0;
29730Sstevel@tonic-gate 				}
29740Sstevel@tonic-gate 				V6_SET_ZERO(icmp->icmp_v6src);
29750Sstevel@tonic-gate 				V6_SET_ZERO(icmp->icmp_bound_v6src);
29760Sstevel@tonic-gate 				icmp->icmp_state = TS_UNBND;
29770Sstevel@tonic-gate 				if (icmp->icmp_family == AF_INET6)
29780Sstevel@tonic-gate 					(void) icmp_build_hdrs(q, icmp);
29790Sstevel@tonic-gate 				break;
29800Sstevel@tonic-gate 			default:
29810Sstevel@tonic-gate 				break;
29820Sstevel@tonic-gate 			}
29830Sstevel@tonic-gate 			break;
29840Sstevel@tonic-gate 		case T_BIND_ACK:
29850Sstevel@tonic-gate 			icmp_rput_bind_ack(q, mp);
29860Sstevel@tonic-gate 			return;
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 		case T_OPTMGMT_ACK:
29890Sstevel@tonic-gate 		case T_OK_ACK:
29900Sstevel@tonic-gate 			if (tea->PRIM_type == T_OK_ACK) {
29910Sstevel@tonic-gate 				struct T_ok_ack *toa;
29920Sstevel@tonic-gate 				toa = (struct T_ok_ack *)rptr;
29930Sstevel@tonic-gate 				if (toa->CORRECT_prim == T_UNBIND_REQ) {
29940Sstevel@tonic-gate 					/*
29950Sstevel@tonic-gate 					 * If somebody sets IPSEC options, IP
29960Sstevel@tonic-gate 					 * sends some IPSEC info which is used
29970Sstevel@tonic-gate 					 * by the TCP for detached connections.
29980Sstevel@tonic-gate 					 * We don't need it here.
29990Sstevel@tonic-gate 					 */
30000Sstevel@tonic-gate 					if ((mp1 = mp->b_cont) != NULL) {
30010Sstevel@tonic-gate 						freemsg(mp1);
30020Sstevel@tonic-gate 						mp->b_cont = NULL;
30030Sstevel@tonic-gate 					}
30040Sstevel@tonic-gate 				}
30050Sstevel@tonic-gate 			}
30060Sstevel@tonic-gate 			break;
30070Sstevel@tonic-gate 		default:
30080Sstevel@tonic-gate 			freemsg(mp);
30090Sstevel@tonic-gate 			return;
30100Sstevel@tonic-gate 		}
30110Sstevel@tonic-gate 		putnext(q, mp);
30120Sstevel@tonic-gate 		return;
30130Sstevel@tonic-gate 	case M_CTL:
30140Sstevel@tonic-gate 		if (recvif) {
30150Sstevel@tonic-gate 			/*
30160Sstevel@tonic-gate 			 * IP has passed the options in mp and the actual data
30170Sstevel@tonic-gate 			 * is in b_cont. Jump to normal data processing.
30180Sstevel@tonic-gate 			 */
30190Sstevel@tonic-gate 			break;
30200Sstevel@tonic-gate 		}
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 		/* Contains ICMP packet from IP */
30230Sstevel@tonic-gate 		icmp_icmp_error(q, mp);
30240Sstevel@tonic-gate 		return;
30250Sstevel@tonic-gate 	default:
30260Sstevel@tonic-gate 		putnext(q, mp);
30270Sstevel@tonic-gate 		return;
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	/*
30310Sstevel@tonic-gate 	 * Discard message if it is misaligned or smaller than the IP header.
30320Sstevel@tonic-gate 	 */
30330Sstevel@tonic-gate 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
30340Sstevel@tonic-gate 		freemsg(mp);
30350Sstevel@tonic-gate 		if (options_mp != NULL)
30360Sstevel@tonic-gate 			freeb(options_mp);
30370Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
30380Sstevel@tonic-gate 		return;
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	/* Handle M_DATA messages containing IP packets messages */
30430Sstevel@tonic-gate 	if (ipvers == IPV4_VERSION) {
30440Sstevel@tonic-gate 		/*
30450Sstevel@tonic-gate 		 * Special case where IP attaches
30460Sstevel@tonic-gate 		 * the IRE needs to be handled so that we don't send up
30470Sstevel@tonic-gate 		 * IRE to the user land.
30480Sstevel@tonic-gate 		 */
30490Sstevel@tonic-gate 		ipha = (ipha_t *)rptr;
30500Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate 		if (ipha->ipha_protocol == IPPROTO_TCP) {
30530Sstevel@tonic-gate 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
30560Sstevel@tonic-gate 			    TH_SYN) && mp->b_cont != NULL) {
30570Sstevel@tonic-gate 				mp1 = mp->b_cont;
30580Sstevel@tonic-gate 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
30590Sstevel@tonic-gate 					freeb(mp1);
30600Sstevel@tonic-gate 					mp->b_cont = NULL;
30610Sstevel@tonic-gate 				}
30620Sstevel@tonic-gate 			}
30630Sstevel@tonic-gate 		}
30640Sstevel@tonic-gate 		if (icmp_bsd_compat) {
30650Sstevel@tonic-gate 			ushort_t len;
30660Sstevel@tonic-gate 			len = ntohs(ipha->ipha_length);
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 			if (mp->b_datap->db_ref > 1) {
30690Sstevel@tonic-gate 				/*
30700Sstevel@tonic-gate 				 * Allocate a new IP header so that we can
30710Sstevel@tonic-gate 				 * modify ipha_length.
30720Sstevel@tonic-gate 				 */
30730Sstevel@tonic-gate 				mblk_t	*mp1;
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 				mp1 = allocb(hdr_len, BPRI_MED);
30760Sstevel@tonic-gate 				if (!mp1) {
30770Sstevel@tonic-gate 					freemsg(mp);
30780Sstevel@tonic-gate 					if (options_mp != NULL)
30790Sstevel@tonic-gate 						freeb(options_mp);
30800Sstevel@tonic-gate 					BUMP_MIB(&rawip_mib, rawipInErrors);
30810Sstevel@tonic-gate 					return;
30820Sstevel@tonic-gate 				}
30830Sstevel@tonic-gate 				bcopy(rptr, mp1->b_rptr, hdr_len);
30840Sstevel@tonic-gate 				mp->b_rptr = rptr + hdr_len;
30850Sstevel@tonic-gate 				rptr = mp1->b_rptr;
30860Sstevel@tonic-gate 				ipha = (ipha_t *)rptr;
30870Sstevel@tonic-gate 				mp1->b_cont = mp;
30880Sstevel@tonic-gate 				mp1->b_wptr = rptr + hdr_len;
30890Sstevel@tonic-gate 				mp = mp1;
30900Sstevel@tonic-gate 			}
30910Sstevel@tonic-gate 			len -= hdr_len;
30920Sstevel@tonic-gate 			ipha->ipha_length = htons(len);
30930Sstevel@tonic-gate 		}
30940Sstevel@tonic-gate 	}
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	/*
30970Sstevel@tonic-gate 	 * This is the inbound data path.  Packets are passed upstream as
30980Sstevel@tonic-gate 	 * T_UNITDATA_IND messages with full IP headers still attached.
30990Sstevel@tonic-gate 	 */
31000Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
31010Sstevel@tonic-gate 		ASSERT(ipvers == IPV4_VERSION);
31020Sstevel@tonic-gate 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
31030Sstevel@tonic-gate 		if (recvif) {
31040Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
31050Sstevel@tonic-gate 			    sizeof (uint_t);
31060Sstevel@tonic-gate 		}
31071673Sgt145670 		/*
31081673Sgt145670 		 * If SO_TIMESTAMP is set allocate the appropriate sized
31091673Sgt145670 		 * buffer. Since gethrestime() expects a pointer aligned
31101673Sgt145670 		 * argument, we allocate space necessary for extra
31111673Sgt145670 		 * alignment (even though it might not be used).
31121673Sgt145670 		 */
31131673Sgt145670 		if (icmp->icmp_timestamp) {
31141673Sgt145670 			udi_size += sizeof (struct T_opthdr) +
31151673Sgt145670 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
31161673Sgt145670 		}
31170Sstevel@tonic-gate 		mp1 = allocb(udi_size, BPRI_MED);
31180Sstevel@tonic-gate 		if (mp1 == NULL) {
31190Sstevel@tonic-gate 			freemsg(mp);
31200Sstevel@tonic-gate 			if (options_mp != NULL)
31210Sstevel@tonic-gate 				freeb(options_mp);
31220Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipInErrors);
31230Sstevel@tonic-gate 			return;
31240Sstevel@tonic-gate 		}
31250Sstevel@tonic-gate 		mp1->b_cont = mp;
31260Sstevel@tonic-gate 		mp = mp1;
31270Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
31280Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
31290Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)tudi + udi_size;
31300Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
31310Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin_t);
31320Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
31330Sstevel@tonic-gate 		sin = (sin_t *)&tudi[1];
31340Sstevel@tonic-gate 		*sin = sin_null;
31350Sstevel@tonic-gate 		sin->sin_family = AF_INET;
31360Sstevel@tonic-gate 		sin->sin_addr.s_addr = ipha->ipha_src;
31370Sstevel@tonic-gate 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
31380Sstevel@tonic-gate 		    sizeof (sin_t);
31390Sstevel@tonic-gate 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
31400Sstevel@tonic-gate 		tudi->OPT_length = udi_size;
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate 		/*
31430Sstevel@tonic-gate 		 * Add options if IP_RECVIF is set
31440Sstevel@tonic-gate 		 */
31450Sstevel@tonic-gate 		if (udi_size != 0) {
31460Sstevel@tonic-gate 			char *dstopt;
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 			dstopt = (char *)&sin[1];
31490Sstevel@tonic-gate 			if (recvif) {
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 				struct T_opthdr *toh;
31520Sstevel@tonic-gate 				uint_t		*dstptr;
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 				toh = (struct T_opthdr *)dstopt;
31550Sstevel@tonic-gate 				toh->level = IPPROTO_IP;
31560Sstevel@tonic-gate 				toh->name = IP_RECVIF;
31570Sstevel@tonic-gate 				toh->len = sizeof (struct T_opthdr) +
31580Sstevel@tonic-gate 					sizeof (uint_t);
31590Sstevel@tonic-gate 				toh->status = 0;
31600Sstevel@tonic-gate 				dstopt += sizeof (struct T_opthdr);
31610Sstevel@tonic-gate 				dstptr = (uint_t *)dstopt;
31620Sstevel@tonic-gate 				*dstptr = pinfo->in_pkt_ifindex;
31630Sstevel@tonic-gate 				dstopt += sizeof (uint_t);
31640Sstevel@tonic-gate 				freeb(options_mp);
31650Sstevel@tonic-gate 				udi_size -= toh->len;
31660Sstevel@tonic-gate 			}
31671673Sgt145670 			if (icmp->icmp_timestamp) {
31681673Sgt145670 				struct	T_opthdr *toh;
31691673Sgt145670 
31701673Sgt145670 				toh = (struct T_opthdr *)dstopt;
31711673Sgt145670 				toh->level = SOL_SOCKET;
31721673Sgt145670 				toh->name = SCM_TIMESTAMP;
31731673Sgt145670 				toh->len = sizeof (struct T_opthdr) +
31741673Sgt145670 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
31751673Sgt145670 				toh->status = 0;
31761673Sgt145670 				dstopt += sizeof (struct T_opthdr);
31771673Sgt145670 				/* Align for gethrestime() */
31781673Sgt145670 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
31791673Sgt145670 				    sizeof (intptr_t));
31801673Sgt145670 				gethrestime((timestruc_t *)dstopt);
31811673Sgt145670 				dstopt += sizeof (timestruc_t);
31821673Sgt145670 				udi_size -= toh->len;
31831673Sgt145670 			}
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 			/* Consumed all of allocated space */
31860Sstevel@tonic-gate 			ASSERT(udi_size == 0);
31870Sstevel@tonic-gate 		}
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInDatagrams);
31900Sstevel@tonic-gate 		putnext(q, mp);
31910Sstevel@tonic-gate 		return;
31920Sstevel@tonic-gate 	}
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	/*
31950Sstevel@tonic-gate 	 * We don't need options_mp in the IPv6 path.
31960Sstevel@tonic-gate 	 */
31970Sstevel@tonic-gate 	if (options_mp != NULL) {
31980Sstevel@tonic-gate 		freeb(options_mp);
31990Sstevel@tonic-gate 		options_mp = NULL;
32000Sstevel@tonic-gate 	}
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 	/*
32030Sstevel@tonic-gate 	 * Discard message if it is smaller than the IPv6 header
32040Sstevel@tonic-gate 	 * or if the header is malformed.
32050Sstevel@tonic-gate 	 */
32060Sstevel@tonic-gate 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
32070Sstevel@tonic-gate 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
32080Sstevel@tonic-gate 	    icmp->icmp_family != AF_INET6) {
32090Sstevel@tonic-gate 		freemsg(mp);
32100Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
32110Sstevel@tonic-gate 		return;
32120Sstevel@tonic-gate 	}
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	/* Initialize */
32150Sstevel@tonic-gate 	ipp.ipp_fields = 0;
32161676Sjpk 	hopstrip = 0;
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate 	ip6h = (ip6_t *)rptr;
32190Sstevel@tonic-gate 	/*
32200Sstevel@tonic-gate 	 * Call on ip_find_hdr_v6 which gets the total hdr len
32210Sstevel@tonic-gate 	 * as well as individual lenghts of ext hdrs (and ptrs to
32220Sstevel@tonic-gate 	 * them).
32230Sstevel@tonic-gate 	 */
32240Sstevel@tonic-gate 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
32250Sstevel@tonic-gate 		/* Look for ifindex information */
32260Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
32270Sstevel@tonic-gate 			ip6i = (ip6i_t *)ip6h;
32280Sstevel@tonic-gate 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
32290Sstevel@tonic-gate 				ASSERT(ip6i->ip6i_ifindex != 0);
32300Sstevel@tonic-gate 				ipp.ipp_fields |= IPPF_IFINDEX;
32310Sstevel@tonic-gate 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
32320Sstevel@tonic-gate 			}
32330Sstevel@tonic-gate 			rptr = (uchar_t *)&ip6i[1];
32340Sstevel@tonic-gate 			mp->b_rptr = rptr;
32350Sstevel@tonic-gate 			if (rptr == mp->b_wptr) {
32360Sstevel@tonic-gate 				mp1 = mp->b_cont;
32370Sstevel@tonic-gate 				freeb(mp);
32380Sstevel@tonic-gate 				mp = mp1;
32390Sstevel@tonic-gate 				rptr = mp->b_rptr;
32400Sstevel@tonic-gate 			}
32410Sstevel@tonic-gate 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
32420Sstevel@tonic-gate 			ip6h = (ip6_t *)rptr;
32430Sstevel@tonic-gate 		}
32440Sstevel@tonic-gate 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
32451676Sjpk 
32461676Sjpk 		/*
32471676Sjpk 		 * We need to lie a bit to the user because users inside
32481676Sjpk 		 * labeled compartments should not see their own labels.  We
32491676Sjpk 		 * assume that in all other respects IP has checked the label,
32501676Sjpk 		 * and that the label is always first among the options.  (If
32511676Sjpk 		 * it's not first, then this code won't see it, and the option
32521676Sjpk 		 * will be passed along to the user.)
32531676Sjpk 		 *
32541676Sjpk 		 * If we had multilevel ICMP sockets, then the following code
32551676Sjpk 		 * should be skipped for them to allow the user to see the
32561676Sjpk 		 * label.
32571676Sjpk 		 *
32581676Sjpk 		 * Alignment restrictions in the definition of IP options
32591676Sjpk 		 * (namely, the requirement that the 4-octet DOI goes on a
32601676Sjpk 		 * 4-octet boundary) mean that we know exactly where the option
32611676Sjpk 		 * should start, but we're lenient for other hosts.
32621676Sjpk 		 *
32631676Sjpk 		 * Note that there are no multilevel ICMP or raw IP sockets
32641676Sjpk 		 * yet, thus nobody ever sees the IP6OPT_LS option.
32651676Sjpk 		 */
32661676Sjpk 		if ((ipp.ipp_fields & IPPF_HOPOPTS) &&
32671676Sjpk 		    ipp.ipp_hopoptslen > 5 && is_system_labeled()) {
32681676Sjpk 			const uchar_t *ucp =
32691676Sjpk 			    (const uchar_t *)ipp.ipp_hopopts + 2;
32701676Sjpk 			int remlen = ipp.ipp_hopoptslen - 2;
32711676Sjpk 
32721676Sjpk 			while (remlen > 0) {
32731676Sjpk 				if (*ucp == IP6OPT_PAD1) {
32741676Sjpk 					remlen--;
32751676Sjpk 					ucp++;
32761676Sjpk 				} else if (*ucp == IP6OPT_PADN) {
32771676Sjpk 					remlen -= ucp[1] + 2;
32781676Sjpk 					ucp += ucp[1] + 2;
32791676Sjpk 				} else if (*ucp == ip6opt_ls) {
32801676Sjpk 					hopstrip = (ucp -
32811676Sjpk 					    (const uchar_t *)ipp.ipp_hopopts) +
32821676Sjpk 					    ucp[1] + 2;
32831676Sjpk 					hopstrip = (hopstrip + 7) & ~7;
32841676Sjpk 					break;
32851676Sjpk 				} else {
32861676Sjpk 					/* label option must be first */
32871676Sjpk 					break;
32881676Sjpk 				}
32891676Sjpk 			}
32901676Sjpk 		}
32910Sstevel@tonic-gate 	} else {
32920Sstevel@tonic-gate 		hdr_len = IPV6_HDR_LEN;
32930Sstevel@tonic-gate 		ip6i = NULL;
32940Sstevel@tonic-gate 		nexthdr = ip6h->ip6_nxt;
32950Sstevel@tonic-gate 	}
32960Sstevel@tonic-gate 	/*
32970Sstevel@tonic-gate 	 * One special case where IP attaches the IRE needs to
32980Sstevel@tonic-gate 	 * be handled so that we don't send up IRE to the user land.
32990Sstevel@tonic-gate 	 */
33000Sstevel@tonic-gate 	if (nexthdr == IPPROTO_TCP) {
33010Sstevel@tonic-gate 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
33040Sstevel@tonic-gate 		    mp->b_cont != NULL) {
33050Sstevel@tonic-gate 			mp1 = mp->b_cont;
33060Sstevel@tonic-gate 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
33070Sstevel@tonic-gate 				freeb(mp1);
33080Sstevel@tonic-gate 				mp->b_cont = NULL;
33090Sstevel@tonic-gate 			}
33100Sstevel@tonic-gate 		}
33110Sstevel@tonic-gate 	}
33120Sstevel@tonic-gate 	/*
33130Sstevel@tonic-gate 	 * Check a filter for ICMPv6 types if needed.
33140Sstevel@tonic-gate 	 * Verify raw checksums if needed.
33150Sstevel@tonic-gate 	 */
33160Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
33170Sstevel@tonic-gate 		if (icmp->icmp_filter != NULL) {
33180Sstevel@tonic-gate 			int type;
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 			/* Assumes that IP has done the pullupmsg */
33210Sstevel@tonic-gate 			type = mp->b_rptr[hdr_len];
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
33240Sstevel@tonic-gate 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
33250Sstevel@tonic-gate 				freemsg(mp);
33260Sstevel@tonic-gate 				return;
33270Sstevel@tonic-gate 			}
33280Sstevel@tonic-gate 		} else {
33290Sstevel@tonic-gate 			/* Checksum */
33300Sstevel@tonic-gate 			uint16_t	*up;
33310Sstevel@tonic-gate 			uint32_t	sum;
33320Sstevel@tonic-gate 			int		remlen;
33330Sstevel@tonic-gate 
33340Sstevel@tonic-gate 			up = (uint16_t *)&ip6h->ip6_src;
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 			remlen = msgdsize(mp) - hdr_len;
33370Sstevel@tonic-gate 			sum = htons(icmp->icmp_proto + remlen)
33380Sstevel@tonic-gate 			    + up[0] + up[1] + up[2] + up[3]
33390Sstevel@tonic-gate 			    + up[4] + up[5] + up[6] + up[7]
33400Sstevel@tonic-gate 			    + up[8] + up[9] + up[10] + up[11]
33410Sstevel@tonic-gate 			    + up[12] + up[13] + up[14] + up[15];
33420Sstevel@tonic-gate 			sum = (sum & 0xffff) + (sum >> 16);
33430Sstevel@tonic-gate 			sum = IP_CSUM(mp, hdr_len, sum);
33440Sstevel@tonic-gate 			if (sum != 0) {
33450Sstevel@tonic-gate 				/* IPv6 RAW checksum failed */
33460Sstevel@tonic-gate 				ip0dbg(("icmp_rput: RAW checksum "
33470Sstevel@tonic-gate 				    "failed %x\n", sum));
33480Sstevel@tonic-gate 				freemsg(mp);
33490Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipInCksumErrs);
33500Sstevel@tonic-gate 				return;
33510Sstevel@tonic-gate 			}
33520Sstevel@tonic-gate 		}
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 	/* Skip all the IPv6 headers per API */
33550Sstevel@tonic-gate 	mp->b_rptr += hdr_len;
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
33580Sstevel@tonic-gate 
33590Sstevel@tonic-gate 	/*
33600Sstevel@tonic-gate 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
33610Sstevel@tonic-gate 	 * maintain state information, instead of relying on icmp_t
33620Sstevel@tonic-gate 	 * structure, since there arent any locks protecting these members
33630Sstevel@tonic-gate 	 * and there is a window where there might be a race between a
33640Sstevel@tonic-gate 	 * thread setting options on the write side and a thread reading
33650Sstevel@tonic-gate 	 * these options on the read size.
33660Sstevel@tonic-gate 	 */
33670Sstevel@tonic-gate 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
33680Sstevel@tonic-gate 	    IPPF_RTHDR|IPPF_IFINDEX)) {
33690Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvhopopts &&
33701676Sjpk 		    (ipp.ipp_fields & IPPF_HOPOPTS) &&
33711676Sjpk 		    ipp.ipp_hopoptslen > hopstrip) {
33720Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33731676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
33740Sstevel@tonic-gate 			icmp_opt |= IPPF_HOPOPTS;
33750Sstevel@tonic-gate 		}
33760Sstevel@tonic-gate 		if ((icmp->icmp_ipv6_recvdstopts ||
33770Sstevel@tonic-gate 			icmp->icmp_old_ipv6_recvdstopts) &&
33780Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
33790Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33800Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
33810Sstevel@tonic-gate 			icmp_opt |= IPPF_DSTOPTS;
33820Sstevel@tonic-gate 		}
33830Sstevel@tonic-gate 		if (((icmp->icmp_ipv6_recvdstopts &&
33840Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrthdr &&
33850Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
33860Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrtdstopts) &&
33870Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
33880Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33890Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
33900Sstevel@tonic-gate 			icmp_opt |= IPPF_RTDSTOPTS;
33910Sstevel@tonic-gate 		}
33920Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvrthdr &&
33930Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) {
33940Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33950Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
33960Sstevel@tonic-gate 			icmp_opt |= IPPF_RTHDR;
33970Sstevel@tonic-gate 		}
33980Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvpktinfo &&
33990Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
34000Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
34010Sstevel@tonic-gate 			    sizeof (struct in6_pktinfo);
34020Sstevel@tonic-gate 			icmp_opt |= IPPF_IFINDEX;
34030Sstevel@tonic-gate 		}
34040Sstevel@tonic-gate 	}
34050Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvhoplimit) {
34060Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
34070Sstevel@tonic-gate 		icmp_ipv6_recvhoplimit = B_TRUE;
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvtclass)
34110Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate 	mp1 = allocb(udi_size, BPRI_MED);
34140Sstevel@tonic-gate 	if (mp1 == NULL) {
34150Sstevel@tonic-gate 		freemsg(mp);
34160Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
34170Sstevel@tonic-gate 		return;
34180Sstevel@tonic-gate 	}
34190Sstevel@tonic-gate 	mp1->b_cont = mp;
34200Sstevel@tonic-gate 	mp = mp1;
34210Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
34220Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
34230Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)tudi + udi_size;
34240Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
34250Sstevel@tonic-gate 	tudi->SRC_length = sizeof (sin6_t);
34260Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
34270Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
34280Sstevel@tonic-gate 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
34290Sstevel@tonic-gate 	tudi->OPT_length = udi_size;
34300Sstevel@tonic-gate 	sin6 = (sin6_t *)&tudi[1];
34310Sstevel@tonic-gate 	sin6->sin6_port = 0;
34320Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	sin6->sin6_addr = ip6h->ip6_src;
34350Sstevel@tonic-gate 	/* No sin6_flowinfo per API */
34360Sstevel@tonic-gate 	sin6->sin6_flowinfo = 0;
34370Sstevel@tonic-gate 	/* For link-scope source pass up scope id */
34380Sstevel@tonic-gate 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
34390Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
34400Sstevel@tonic-gate 		sin6->sin6_scope_id = ipp.ipp_ifindex;
34410Sstevel@tonic-gate 	else
34420Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
34430Sstevel@tonic-gate 
34440Sstevel@tonic-gate 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
34450Sstevel@tonic-gate 	    icmp->icmp_zoneid);
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 	if (udi_size != 0) {
34480Sstevel@tonic-gate 		uchar_t *dstopt;
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate 		dstopt = (uchar_t *)&sin6[1];
34510Sstevel@tonic-gate 		if (icmp_opt & IPPF_IFINDEX) {
34520Sstevel@tonic-gate 			struct T_opthdr *toh;
34530Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34560Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34570Sstevel@tonic-gate 			toh->name = IPV6_PKTINFO;
34580Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
34590Sstevel@tonic-gate 			    sizeof (*pkti);
34600Sstevel@tonic-gate 			toh->status = 0;
34610Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
34620Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)dstopt;
34630Sstevel@tonic-gate 			pkti->ipi6_addr = ip6h->ip6_dst;
34640Sstevel@tonic-gate 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
34650Sstevel@tonic-gate 			dstopt += sizeof (*pkti);
34660Sstevel@tonic-gate 			udi_size -= toh->len;
34670Sstevel@tonic-gate 		}
34680Sstevel@tonic-gate 		if (icmp_ipv6_recvhoplimit) {
34690Sstevel@tonic-gate 			struct T_opthdr *toh;
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34720Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34730Sstevel@tonic-gate 			toh->name = IPV6_HOPLIMIT;
34740Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
34750Sstevel@tonic-gate 			    sizeof (uint_t);
34760Sstevel@tonic-gate 			toh->status = 0;
34770Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
34780Sstevel@tonic-gate 			*(uint_t *)dstopt = ip6h->ip6_hops;
34790Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
34800Sstevel@tonic-gate 			udi_size -= toh->len;
34810Sstevel@tonic-gate 		}
34820Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvtclass) {
34830Sstevel@tonic-gate 			struct T_opthdr *toh;
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34860Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34870Sstevel@tonic-gate 			toh->name = IPV6_TCLASS;
34880Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
34890Sstevel@tonic-gate 			    sizeof (uint_t);
34900Sstevel@tonic-gate 			toh->status = 0;
34910Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
34920Sstevel@tonic-gate 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
34930Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
34940Sstevel@tonic-gate 			udi_size -= toh->len;
34950Sstevel@tonic-gate 		}
34960Sstevel@tonic-gate 		if (icmp_opt & IPPF_HOPOPTS) {
34970Sstevel@tonic-gate 			struct T_opthdr *toh;
34980Sstevel@tonic-gate 
34990Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35000Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35010Sstevel@tonic-gate 			toh->name = IPV6_HOPOPTS;
35020Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35031676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
35040Sstevel@tonic-gate 			toh->status = 0;
35050Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35061676Sjpk 			bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt,
35071676Sjpk 			    ipp.ipp_hopoptslen - hopstrip);
35081676Sjpk 			if (hopstrip > 0) {
35091676Sjpk 				/* copy next header value and fake length */
35101676Sjpk 				dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0];
35111676Sjpk 				dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] -
35121676Sjpk 				    hopstrip / 8;
35131676Sjpk 			}
35141676Sjpk 			dstopt += ipp.ipp_hopoptslen - hopstrip;
35150Sstevel@tonic-gate 			udi_size -= toh->len;
35160Sstevel@tonic-gate 		}
35170Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTDSTOPTS) {
35180Sstevel@tonic-gate 			struct T_opthdr *toh;
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35210Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35220Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
35230Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35240Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
35250Sstevel@tonic-gate 			toh->status = 0;
35260Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35270Sstevel@tonic-gate 			bcopy(ipp.ipp_rtdstopts, dstopt,
35280Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen);
35290Sstevel@tonic-gate 			dstopt += ipp.ipp_rtdstoptslen;
35300Sstevel@tonic-gate 			udi_size -= toh->len;
35310Sstevel@tonic-gate 		}
35320Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTHDR) {
35330Sstevel@tonic-gate 			struct T_opthdr *toh;
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35360Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35370Sstevel@tonic-gate 			toh->name = IPV6_RTHDR;
35380Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35390Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
35400Sstevel@tonic-gate 			toh->status = 0;
35410Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35420Sstevel@tonic-gate 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
35430Sstevel@tonic-gate 			dstopt += ipp.ipp_rthdrlen;
35440Sstevel@tonic-gate 			udi_size -= toh->len;
35450Sstevel@tonic-gate 		}
35460Sstevel@tonic-gate 		if (icmp_opt & IPPF_DSTOPTS) {
35470Sstevel@tonic-gate 			struct T_opthdr *toh;
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35500Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35510Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
35520Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35530Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
35540Sstevel@tonic-gate 			toh->status = 0;
35550Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35560Sstevel@tonic-gate 			bcopy(ipp.ipp_dstopts, dstopt,
35570Sstevel@tonic-gate 			    ipp.ipp_dstoptslen);
35580Sstevel@tonic-gate 			dstopt += ipp.ipp_dstoptslen;
35590Sstevel@tonic-gate 			udi_size -= toh->len;
35600Sstevel@tonic-gate 		}
35610Sstevel@tonic-gate 		/* Consumed all of allocated space */
35620Sstevel@tonic-gate 		ASSERT(udi_size == 0);
35630Sstevel@tonic-gate 	}
35640Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipInDatagrams);
35650Sstevel@tonic-gate 	putnext(q, mp);
35660Sstevel@tonic-gate }
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate /*
35690Sstevel@tonic-gate  * Process a T_BIND_ACK
35700Sstevel@tonic-gate  */
35710Sstevel@tonic-gate static void
35720Sstevel@tonic-gate icmp_rput_bind_ack(queue_t *q, mblk_t *mp)
35730Sstevel@tonic-gate {
35740Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
35750Sstevel@tonic-gate 	mblk_t	*mp1;
35760Sstevel@tonic-gate 	ire_t	*ire;
35770Sstevel@tonic-gate 	struct T_bind_ack *tba;
35780Sstevel@tonic-gate 	uchar_t *addrp;
35790Sstevel@tonic-gate 	ipa_conn_t	*ac;
35800Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	/*
35830Sstevel@tonic-gate 	 * We know if headers are included or not so we can
35840Sstevel@tonic-gate 	 * safely do this.
35850Sstevel@tonic-gate 	 */
35860Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
35870Sstevel@tonic-gate 		/*
35880Sstevel@tonic-gate 		 * TPI has not yet bound - bind sent by
35890Sstevel@tonic-gate 		 * icmp_bind_proto.
35900Sstevel@tonic-gate 		 */
35910Sstevel@tonic-gate 		freemsg(mp);
35920Sstevel@tonic-gate 		return;
35930Sstevel@tonic-gate 	}
35940Sstevel@tonic-gate 	if (icmp->icmp_discon_pending)
35950Sstevel@tonic-gate 		icmp->icmp_discon_pending = 0;
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate 	/*
35980Sstevel@tonic-gate 	 * If a broadcast/multicast address was bound set
35990Sstevel@tonic-gate 	 * the source address to 0.
36000Sstevel@tonic-gate 	 * This ensures no datagrams with broadcast address
36010Sstevel@tonic-gate 	 * as source address are emitted (which would violate
36020Sstevel@tonic-gate 	 * RFC1122 - Hosts requirements)
36030Sstevel@tonic-gate 	 *
36040Sstevel@tonic-gate 	 * Note that when connecting the returned IRE is
36050Sstevel@tonic-gate 	 * for the destination address and we only perform
36060Sstevel@tonic-gate 	 * the broadcast check for the source address (it
36070Sstevel@tonic-gate 	 * is OK to connect to a broadcast/multicast address.)
36080Sstevel@tonic-gate 	 */
36090Sstevel@tonic-gate 	mp1 = mp->b_cont;
36100Sstevel@tonic-gate 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
36110Sstevel@tonic-gate 		ire = (ire_t *)mp1->b_rptr;
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 		/*
36140Sstevel@tonic-gate 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
36150Sstevel@tonic-gate 		 * local address.
36160Sstevel@tonic-gate 		 */
36170Sstevel@tonic-gate 		if (ire->ire_type == IRE_BROADCAST &&
36180Sstevel@tonic-gate 		    icmp->icmp_state != TS_DATA_XFER) {
36190Sstevel@tonic-gate 			/* This was just a local bind to a MC/broadcast addr */
36200Sstevel@tonic-gate 			V6_SET_ZERO(icmp->icmp_v6src);
36210Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6)
36220Sstevel@tonic-gate 				(void) icmp_build_hdrs(q, icmp);
36230Sstevel@tonic-gate 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
36240Sstevel@tonic-gate 			/*
36250Sstevel@tonic-gate 			 * Local address not yet set - pick it from the
36260Sstevel@tonic-gate 			 * T_bind_ack
36270Sstevel@tonic-gate 			 */
36280Sstevel@tonic-gate 			tba = (struct T_bind_ack *)mp->b_rptr;
36290Sstevel@tonic-gate 			addrp = &mp->b_rptr[tba->ADDR_offset];
36300Sstevel@tonic-gate 			switch (icmp->icmp_family) {
36310Sstevel@tonic-gate 			case AF_INET:
36320Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
36330Sstevel@tonic-gate 					ac = (ipa_conn_t *)addrp;
36340Sstevel@tonic-gate 				} else {
36350Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
36360Sstevel@tonic-gate 					    sizeof (ipa_conn_x_t));
36370Sstevel@tonic-gate 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
36380Sstevel@tonic-gate 				}
36390Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
36400Sstevel@tonic-gate 				    &icmp->icmp_v6src);
36410Sstevel@tonic-gate 				break;
36420Sstevel@tonic-gate 			case AF_INET6:
36430Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
36440Sstevel@tonic-gate 					ac6 = (ipa6_conn_t *)addrp;
36450Sstevel@tonic-gate 				} else {
36460Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
36470Sstevel@tonic-gate 					    sizeof (ipa6_conn_x_t));
36480Sstevel@tonic-gate 					ac6 = &((ipa6_conn_x_t *)
36490Sstevel@tonic-gate 					    addrp)->ac6x_conn;
36500Sstevel@tonic-gate 				}
36510Sstevel@tonic-gate 				icmp->icmp_v6src = ac6->ac6_laddr;
36520Sstevel@tonic-gate 				(void) icmp_build_hdrs(q, icmp);
36530Sstevel@tonic-gate 			}
36540Sstevel@tonic-gate 		}
36550Sstevel@tonic-gate 		mp1 = mp1->b_cont;
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 	/*
36580Sstevel@tonic-gate 	 * Look for one or more appended ACK message added by
36590Sstevel@tonic-gate 	 * icmp_connect or icmp_disconnect.
36600Sstevel@tonic-gate 	 * If none found just send up the T_BIND_ACK.
36610Sstevel@tonic-gate 	 * icmp_connect has appended a T_OK_ACK and a
36620Sstevel@tonic-gate 	 * T_CONN_CON.
36630Sstevel@tonic-gate 	 * icmp_disconnect has appended a T_OK_ACK.
36640Sstevel@tonic-gate 	 */
36650Sstevel@tonic-gate 	if (mp1 != NULL) {
36660Sstevel@tonic-gate 		if (mp->b_cont == mp1)
36670Sstevel@tonic-gate 			mp->b_cont = NULL;
36680Sstevel@tonic-gate 		else {
36690Sstevel@tonic-gate 			ASSERT(mp->b_cont->b_cont == mp1);
36700Sstevel@tonic-gate 			mp->b_cont->b_cont = NULL;
36710Sstevel@tonic-gate 		}
36720Sstevel@tonic-gate 		freemsg(mp);
36730Sstevel@tonic-gate 		mp = mp1;
36740Sstevel@tonic-gate 		while (mp != NULL) {
36750Sstevel@tonic-gate 			mp1 = mp->b_cont;
36760Sstevel@tonic-gate 			mp->b_cont = NULL;
36770Sstevel@tonic-gate 			putnext(q, mp);
36780Sstevel@tonic-gate 			mp = mp1;
36790Sstevel@tonic-gate 		}
36800Sstevel@tonic-gate 		return;
36810Sstevel@tonic-gate 	}
36820Sstevel@tonic-gate 	freemsg(mp->b_cont);
36830Sstevel@tonic-gate 	mp->b_cont = NULL;
36840Sstevel@tonic-gate 	putnext(q, mp);
36850Sstevel@tonic-gate }
36860Sstevel@tonic-gate 
36870Sstevel@tonic-gate /*
36880Sstevel@tonic-gate  * return SNMP stuff in buffer in mpdata
36890Sstevel@tonic-gate  */
36900Sstevel@tonic-gate static int
36910Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl)
36920Sstevel@tonic-gate {
36930Sstevel@tonic-gate 	mblk_t			*mpdata;
36940Sstevel@tonic-gate 	struct opthdr		*optp;
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 	if (mpctl == NULL ||
36970Sstevel@tonic-gate 	    (mpdata = mpctl->b_cont) == NULL) {
36980Sstevel@tonic-gate 		return (0);
36990Sstevel@tonic-gate 	}
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 	/* fixed length structure for IPv4 and IPv6 counters */
37020Sstevel@tonic-gate 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
37030Sstevel@tonic-gate 	optp->level = EXPER_RAWIP;
37040Sstevel@tonic-gate 	optp->name = 0;
37050Sstevel@tonic-gate 	(void) snmp_append_data(mpdata, (char *)&rawip_mib, sizeof (rawip_mib));
37060Sstevel@tonic-gate 	optp->len = msgdsize(mpdata);
37070Sstevel@tonic-gate 	qreply(q, mpctl);
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 	return (1);
37100Sstevel@tonic-gate }
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate /*
37130Sstevel@tonic-gate  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
37140Sstevel@tonic-gate  * TODO:  If this ever actually tries to set anything, it needs to be
37150Sstevel@tonic-gate  * to do the appropriate locking.
37160Sstevel@tonic-gate  */
37170Sstevel@tonic-gate /* ARGSUSED */
37180Sstevel@tonic-gate static int
37190Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
37200Sstevel@tonic-gate     uchar_t *ptr, int len)
37210Sstevel@tonic-gate {
37220Sstevel@tonic-gate 	switch (level) {
37230Sstevel@tonic-gate 	case EXPER_RAWIP:
37240Sstevel@tonic-gate 		return (0);
37250Sstevel@tonic-gate 	default:
37260Sstevel@tonic-gate 		return (1);
37270Sstevel@tonic-gate 	}
37280Sstevel@tonic-gate }
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate /* Report for ndd "icmp_status" */
37310Sstevel@tonic-gate /* ARGSUSED */
37320Sstevel@tonic-gate static int
37330Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
37340Sstevel@tonic-gate {
37350Sstevel@tonic-gate 	IDP	idp;
37360Sstevel@tonic-gate 	icmp_t	*icmp;
37370Sstevel@tonic-gate 	char	*state;
37380Sstevel@tonic-gate 	char	laddrbuf[INET6_ADDRSTRLEN];
37390Sstevel@tonic-gate 	char	faddrbuf[INET6_ADDRSTRLEN];
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
37420Sstevel@tonic-gate 	    "RAWIP    " MI_COL_HDRPAD_STR
37430Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
37440Sstevel@tonic-gate 	    "  src addr        dest addr       state");
37450Sstevel@tonic-gate 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
37460Sstevel@tonic-gate 
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 	for (idp = mi_first_ptr(&icmp_g_head);
37490Sstevel@tonic-gate 	    (icmp = (icmp_t *)idp) != NULL;
37500Sstevel@tonic-gate 	    idp = mi_next_ptr(&icmp_g_head, idp)) {
37510Sstevel@tonic-gate 		if (icmp->icmp_state == TS_UNBND)
37520Sstevel@tonic-gate 			state = "UNBOUND";
37530Sstevel@tonic-gate 		else if (icmp->icmp_state == TS_IDLE)
37540Sstevel@tonic-gate 			state = "IDLE";
37550Sstevel@tonic-gate 		else if (icmp->icmp_state == TS_DATA_XFER)
37560Sstevel@tonic-gate 			state = "CONNECTED";
37570Sstevel@tonic-gate 		else
37580Sstevel@tonic-gate 			state = "UnkState";
37590Sstevel@tonic-gate 
37600Sstevel@tonic-gate 		(void) mi_mpprintf(mp,
37610Sstevel@tonic-gate 		    MI_COL_PTRFMT_STR "%s %s %s",
37620Sstevel@tonic-gate 		    (void *)icmp,
37630Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
37640Sstevel@tonic-gate 		    sizeof (faddrbuf)),
37650Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
37660Sstevel@tonic-gate 		    sizeof (laddrbuf)),
37670Sstevel@tonic-gate 		    state);
37680Sstevel@tonic-gate 	}
37690Sstevel@tonic-gate 	return (0);
37700Sstevel@tonic-gate }
37710Sstevel@tonic-gate 
37720Sstevel@tonic-gate /*
37730Sstevel@tonic-gate  * This routine creates a T_UDERROR_IND message and passes it upstream.
37740Sstevel@tonic-gate  * The address and options are copied from the T_UNITDATA_REQ message
37750Sstevel@tonic-gate  * passed in mp.  This message is freed.
37760Sstevel@tonic-gate  */
37770Sstevel@tonic-gate static void
37780Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
37790Sstevel@tonic-gate {
37800Sstevel@tonic-gate 	mblk_t	*mp1;
37810Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
37820Sstevel@tonic-gate 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
37850Sstevel@tonic-gate 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
37860Sstevel@tonic-gate 	    tudr->OPT_length, err);
37870Sstevel@tonic-gate 	if (mp1)
37880Sstevel@tonic-gate 		qreply(q, mp1);
37890Sstevel@tonic-gate 	freemsg(mp);
37900Sstevel@tonic-gate }
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate /*
37930Sstevel@tonic-gate  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
37940Sstevel@tonic-gate  * After some error checking, the message is passed downstream to ip.
37950Sstevel@tonic-gate  */
37960Sstevel@tonic-gate static void
37970Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp)
37980Sstevel@tonic-gate {
37990Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 	/* If a bind has not been done, we can't unbind. */
38020Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
38030Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
38040Sstevel@tonic-gate 		return;
38050Sstevel@tonic-gate 	}
38060Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_v6src);
38070Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_bound_v6src);
38080Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
38110Sstevel@tonic-gate 		int error;
38120Sstevel@tonic-gate 
38130Sstevel@tonic-gate 		/* Rebuild the header template */
38140Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
38150Sstevel@tonic-gate 		if (error != 0) {
38160Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
38170Sstevel@tonic-gate 			return;
38180Sstevel@tonic-gate 		}
38190Sstevel@tonic-gate 	}
38200Sstevel@tonic-gate 	/* Pass the unbind to IP. */
38210Sstevel@tonic-gate 	putnext(q, mp);
38220Sstevel@tonic-gate }
38230Sstevel@tonic-gate 
38240Sstevel@tonic-gate /*
38250Sstevel@tonic-gate  * Process IPv4 packets that already include an IP header.
38260Sstevel@tonic-gate  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
38270Sstevel@tonic-gate  * IPPROTO_IGMP).
38280Sstevel@tonic-gate  */
38290Sstevel@tonic-gate static void
38300Sstevel@tonic-gate icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp)
38310Sstevel@tonic-gate {
38320Sstevel@tonic-gate 	ipha_t	*ipha;
38330Sstevel@tonic-gate 	int	ip_hdr_length;
38340Sstevel@tonic-gate 	int	tp_hdr_len;
38350Sstevel@tonic-gate 	mblk_t	*mp1;
38360Sstevel@tonic-gate 	uint_t	pkt_len;
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
38390Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
38400Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
38410Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
38420Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
38430Sstevel@tonic-gate 			freemsg(mp);
38440Sstevel@tonic-gate 			return;
38450Sstevel@tonic-gate 		}
38460Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
38470Sstevel@tonic-gate 	}
38480Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length =
38490Sstevel@tonic-gate 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	/*
38520Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
38530Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
38540Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
38550Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
38560Sstevel@tonic-gate 	 */
38570Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
38580Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
38590Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
38600Sstevel@tonic-gate 	/* Insert options if any */
38610Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
38620Sstevel@tonic-gate 		/*
38630Sstevel@tonic-gate 		 * Put the IP header plus any transport header that is
38640Sstevel@tonic-gate 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
38650Sstevel@tonic-gate 		 * that at least the checksum field is in the first mblk.)
38660Sstevel@tonic-gate 		 */
38670Sstevel@tonic-gate 		switch (ipha->ipha_protocol) {
38680Sstevel@tonic-gate 		case IPPROTO_UDP:
38690Sstevel@tonic-gate 			tp_hdr_len = 8;
38700Sstevel@tonic-gate 			break;
38710Sstevel@tonic-gate 		case IPPROTO_TCP:
38720Sstevel@tonic-gate 			tp_hdr_len = 20;
38730Sstevel@tonic-gate 			break;
38740Sstevel@tonic-gate 		default:
38750Sstevel@tonic-gate 			tp_hdr_len = 0;
38760Sstevel@tonic-gate 			break;
38770Sstevel@tonic-gate 		}
38780Sstevel@tonic-gate 		/*
38790Sstevel@tonic-gate 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
38800Sstevel@tonic-gate 		 * tp_hdr_len bytes will be in a single mblk.
38810Sstevel@tonic-gate 		 */
38820Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
38830Sstevel@tonic-gate 		    tp_hdr_len)) {
38840Sstevel@tonic-gate 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
38850Sstevel@tonic-gate 			    tp_hdr_len)) {
38860Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
38870Sstevel@tonic-gate 				freemsg(mp);
38880Sstevel@tonic-gate 				return;
38890Sstevel@tonic-gate 			}
38900Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
38910Sstevel@tonic-gate 		}
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate 		/*
38940Sstevel@tonic-gate 		 * if the length is larger then the max allowed IP packet,
38950Sstevel@tonic-gate 		 * then send an error and abort the processing.
38960Sstevel@tonic-gate 		 */
38970Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length)
38980Sstevel@tonic-gate 		    + icmp->icmp_ip_snd_options_len;
38990Sstevel@tonic-gate 		if (pkt_len > IP_MAXPACKET) {
39000Sstevel@tonic-gate 			icmp_ud_err(q, mp, EMSGSIZE);
39010Sstevel@tonic-gate 			return;
39020Sstevel@tonic-gate 		}
39030Sstevel@tonic-gate 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra +
39040Sstevel@tonic-gate 		    tp_hdr_len, BPRI_LO))) {
39050Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
39060Sstevel@tonic-gate 			return;
39070Sstevel@tonic-gate 		}
39080Sstevel@tonic-gate 		mp1->b_rptr += icmp_wroff_extra;
39090Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
39100Sstevel@tonic-gate 
39110Sstevel@tonic-gate 		ipha->ipha_length = htons((uint16_t)pkt_len);
39120Sstevel@tonic-gate 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate 		/* Copy transport header if any */
39150Sstevel@tonic-gate 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
39160Sstevel@tonic-gate 		mp1->b_wptr += tp_hdr_len;
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate 		/* Add options */
39190Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_rptr;
39200Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
39210Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len);
39220Sstevel@tonic-gate 
39230Sstevel@tonic-gate 		/* Drop IP header and transport header from original */
39240Sstevel@tonic-gate 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 		mp1->b_cont = mp;
39270Sstevel@tonic-gate 		mp = mp1;
39280Sstevel@tonic-gate 		/*
39290Sstevel@tonic-gate 		 * Massage source route putting first source
39300Sstevel@tonic-gate 		 * route in ipha_dst.
39310Sstevel@tonic-gate 		 */
39320Sstevel@tonic-gate 		(void) ip_massage_options(ipha);
39330Sstevel@tonic-gate 	}
39341676Sjpk 	mblk_setcred(mp, icmp->icmp_credp);
39350Sstevel@tonic-gate 	putnext(q, mp);
39360Sstevel@tonic-gate }
39370Sstevel@tonic-gate 
39381676Sjpk static boolean_t
39391676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst)
39401676Sjpk {
39411676Sjpk 	int err;
39421676Sjpk 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
39431676Sjpk 
39441676Sjpk 	err = tsol_compute_label(DB_CREDDEF(mp, icmp->icmp_credp), dst,
39451676Sjpk 	    opt_storage, icmp->icmp_mac_exempt);
39461676Sjpk 	if (err == 0) {
39471676Sjpk 		err = tsol_update_options(&icmp->icmp_ip_snd_options,
39481676Sjpk 		    &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len,
39491676Sjpk 		    opt_storage);
39501676Sjpk 	}
39511676Sjpk 	if (err != 0) {
39521676Sjpk 		BUMP_MIB(&rawip_mib, rawipOutErrors);
39531676Sjpk 		DTRACE_PROBE4(
39541676Sjpk 		    tx__ip__log__drop__updatelabel__icmp,
39551676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
39561676Sjpk 		    queue_t *, q, char *, opt_storage, mblk_t *, mp);
39571676Sjpk 		icmp_ud_err(q, mp, err);
39581676Sjpk 		return (B_FALSE);
39591676Sjpk 	}
39601676Sjpk 	IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst);
39611676Sjpk 	return (B_TRUE);
39621676Sjpk }
39631676Sjpk 
39640Sstevel@tonic-gate /*
39650Sstevel@tonic-gate  * This routine handles all messages passed downstream.  It either
39660Sstevel@tonic-gate  * consumes the message or passes it downstream; it never queues a
39670Sstevel@tonic-gate  * a message.
39680Sstevel@tonic-gate  */
39690Sstevel@tonic-gate static void
39700Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp)
39710Sstevel@tonic-gate {
39720Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
39730Sstevel@tonic-gate 	ipha_t	*ipha;
39740Sstevel@tonic-gate 	mblk_t	*mp1;
39750Sstevel@tonic-gate 	int	ip_hdr_length;
39760Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
39770Sstevel@tonic-gate 	size_t	ip_len;
39780Sstevel@tonic-gate 	icmp_t	*icmp;
39790Sstevel@tonic-gate 	sin6_t	*sin6;
39800Sstevel@tonic-gate 	sin_t	*sin;
39810Sstevel@tonic-gate 	ipaddr_t	v4dst;
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
39840Sstevel@tonic-gate 	if (icmp->icmp_restricted) {
39850Sstevel@tonic-gate 		icmp_wput_restricted(q, mp);
39860Sstevel@tonic-gate 		return;
39870Sstevel@tonic-gate 	}
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
39900Sstevel@tonic-gate 	case M_DATA:
39910Sstevel@tonic-gate 		if (icmp->icmp_hdrincl) {
39920Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
39931676Sjpk 			ipha = (ipha_t *)mp->b_rptr;
39941676Sjpk 			if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
39951676Sjpk 				if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
39961676Sjpk 					BUMP_MIB(&rawip_mib, rawipOutErrors);
39971676Sjpk 					freemsg(mp);
39981676Sjpk 					return;
39991676Sjpk 				}
40001676Sjpk 				ipha = (ipha_t *)mp->b_rptr;
40011676Sjpk 			}
40021676Sjpk 			/*
40031676Sjpk 			 * If this connection was used for v6 (inconceivable!)
40041676Sjpk 			 * or if we have a new destination, then it's time to
40051676Sjpk 			 * figure a new label.
40061676Sjpk 			 */
40071676Sjpk 			if (is_system_labeled() &&
40081676Sjpk 			    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
40091676Sjpk 			    V4_PART_OF_V6(icmp->icmp_v6lastdst) !=
40101676Sjpk 			    ipha->ipha_dst) &&
40111676Sjpk 			    !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) {
40121676Sjpk 				return;
40131676Sjpk 			}
40140Sstevel@tonic-gate 			icmp_wput_hdrincl(q, mp, icmp);
40150Sstevel@tonic-gate 			return;
40160Sstevel@tonic-gate 		}
40170Sstevel@tonic-gate 		freemsg(mp);
40180Sstevel@tonic-gate 		return;
40190Sstevel@tonic-gate 	case M_PROTO:
40200Sstevel@tonic-gate 	case M_PCPROTO:
40210Sstevel@tonic-gate 		ip_len = mp->b_wptr - rptr;
40220Sstevel@tonic-gate 		if (ip_len >= sizeof (struct T_unitdata_req)) {
40230Sstevel@tonic-gate 			/* Expedite valid T_UNITDATA_REQ to below the switch */
40240Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type
40250Sstevel@tonic-gate 			    == T_UNITDATA_REQ)
40260Sstevel@tonic-gate 				break;
40270Sstevel@tonic-gate 		}
40280Sstevel@tonic-gate 		/* FALLTHRU */
40290Sstevel@tonic-gate 	default:
40300Sstevel@tonic-gate 		icmp_wput_other(q, mp);
40310Sstevel@tonic-gate 		return;
40320Sstevel@tonic-gate 	}
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	/* Handle T_UNITDATA_REQ messages here. */
40350Sstevel@tonic-gate 
40360Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
40370Sstevel@tonic-gate 		/* If a port has not been bound to the stream, fail. */
40380Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40390Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
40400Sstevel@tonic-gate 		return;
40410Sstevel@tonic-gate 	}
40420Sstevel@tonic-gate 	mp1 = mp->b_cont;
40430Sstevel@tonic-gate 	if (mp1 == NULL) {
40440Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40450Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
40460Sstevel@tonic-gate 		return;
40470Sstevel@tonic-gate 	}
40480Sstevel@tonic-gate 
40490Sstevel@tonic-gate 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
40500Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40510Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
40520Sstevel@tonic-gate 		return;
40530Sstevel@tonic-gate 	}
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate 	switch (icmp->icmp_family) {
40560Sstevel@tonic-gate 	case AF_INET6:
40570Sstevel@tonic-gate 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
40580Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin6) ||
40590Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin6_t) ||
40600Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
40610Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40620Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
40630Sstevel@tonic-gate 			return;
40640Sstevel@tonic-gate 		}
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
40670Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
40680Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40690Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
40700Sstevel@tonic-gate 			return;
40710Sstevel@tonic-gate 		}
40720Sstevel@tonic-gate 
40730Sstevel@tonic-gate 		/*
40740Sstevel@tonic-gate 		 * Destination is a native IPv6 address.
40750Sstevel@tonic-gate 		 * Send out an IPv6 format packet.
40760Sstevel@tonic-gate 		 */
40770Sstevel@tonic-gate 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
40780Sstevel@tonic-gate 		return;
40790Sstevel@tonic-gate 
40800Sstevel@tonic-gate 	case AF_INET:
40810Sstevel@tonic-gate 		sin = (sin_t *)&rptr[tudr->DEST_offset];
40820Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin) ||
40830Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin_t) ||
40840Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
40850Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40860Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
40870Sstevel@tonic-gate 			return;
40880Sstevel@tonic-gate 		}
40890Sstevel@tonic-gate 		/* Extract and ipaddr */
40900Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
40910Sstevel@tonic-gate 		break;
40921676Sjpk 
40931676Sjpk 	default:
40941676Sjpk 		ASSERT(0);
40950Sstevel@tonic-gate 	}
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate 	/*
40980Sstevel@tonic-gate 	 * If options passed in, feed it for verification and handling
40990Sstevel@tonic-gate 	 */
41000Sstevel@tonic-gate 	if (tudr->OPT_length != 0) {
41010Sstevel@tonic-gate 		int error;
41020Sstevel@tonic-gate 
41030Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
41040Sstevel@tonic-gate 		    (uchar_t *)0) < 0) {
41050Sstevel@tonic-gate 			/* failure */
41060Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
41070Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
41080Sstevel@tonic-gate 			return;
41090Sstevel@tonic-gate 		}
41100Sstevel@tonic-gate 		/*
41110Sstevel@tonic-gate 		 * Note: Success in processing options.
41120Sstevel@tonic-gate 		 * mp option buffer represented by
41130Sstevel@tonic-gate 		 * OPT_length/offset now potentially modified
41140Sstevel@tonic-gate 		 * and contain option setting results
41150Sstevel@tonic-gate 		 */
41160Sstevel@tonic-gate 	}
41170Sstevel@tonic-gate 
41181676Sjpk 	if (v4dst == INADDR_ANY)
41191676Sjpk 		v4dst = htonl(INADDR_LOOPBACK);
41201676Sjpk 
41211676Sjpk 	/* Check if our saved options are valid; update if not */
41221676Sjpk 	if (is_system_labeled() &&
41231676Sjpk 	    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
41241676Sjpk 	    V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) &&
41251676Sjpk 	    !icmp_update_label(q, icmp, mp, v4dst)) {
41261676Sjpk 		return;
41271676Sjpk 	}
41281676Sjpk 
41290Sstevel@tonic-gate 	/* Protocol 255 contains full IP headers */
41300Sstevel@tonic-gate 	if (icmp->icmp_hdrincl) {
41310Sstevel@tonic-gate 		freeb(mp);
41320Sstevel@tonic-gate 		icmp_wput_hdrincl(q, mp1, icmp);
41330Sstevel@tonic-gate 		return;
41340Sstevel@tonic-gate 	}
41351676Sjpk 
41360Sstevel@tonic-gate 	/* Add an IP header */
41370Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
41380Sstevel@tonic-gate 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
41390Sstevel@tonic-gate 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
41400Sstevel@tonic-gate 	    mp1->b_datap->db_ref != 1 ||
41410Sstevel@tonic-gate 	    !OK_32PTR(ipha)) {
41420Sstevel@tonic-gate 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra,
41430Sstevel@tonic-gate 		    BPRI_LO))) {
41440Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
41451289Sja97890 			icmp_ud_err(q, mp, ENOMEM);
41460Sstevel@tonic-gate 			return;
41470Sstevel@tonic-gate 		}
41480Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
41490Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_datap->db_lim;
41500Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)ipha;
41510Sstevel@tonic-gate 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
41520Sstevel@tonic-gate 	}
41530Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
41540Sstevel@tonic-gate 	/* Set version, header length, and tos */
41550Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
41560Sstevel@tonic-gate 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
41570Sstevel@tonic-gate 		icmp->icmp_type_of_service);
41580Sstevel@tonic-gate 	/* Set ttl and protocol */
41590Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
41600Sstevel@tonic-gate #else
41610Sstevel@tonic-gate 	/* Set version, header length, and tos */
41620Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
41630Sstevel@tonic-gate 	    ((icmp->icmp_type_of_service << 8) |
41640Sstevel@tonic-gate 		((IP_VERSION << 4) | (ip_hdr_length>>2)));
41650Sstevel@tonic-gate 	/* Set ttl and protocol */
41660Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
41670Sstevel@tonic-gate #endif
41680Sstevel@tonic-gate 	/*
41690Sstevel@tonic-gate 	 * Copy our address into the packet.  If this is zero,
41700Sstevel@tonic-gate 	 * ip will fill in the real source address.
41710Sstevel@tonic-gate 	 */
41720Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
41730Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
41740Sstevel@tonic-gate 
41750Sstevel@tonic-gate 	/*
41760Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
41770Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
41780Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
41790Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
41800Sstevel@tonic-gate 	 */
41810Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate 	/* Finish common formatting of the packet. */
41840Sstevel@tonic-gate 	mp1->b_rptr = (uchar_t *)ipha;
41850Sstevel@tonic-gate 
41860Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
41870Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
41880Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
41890Sstevel@tonic-gate 
41900Sstevel@tonic-gate 	/*
41910Sstevel@tonic-gate 	 * Set the length into the IP header.
41920Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
41930Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
41940Sstevel@tonic-gate 	 * as this can cause problems in layers below.
41950Sstevel@tonic-gate 	 */
41960Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
41970Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
41980Sstevel@tonic-gate 		icmp_ud_err(q, mp, EMSGSIZE);
41990Sstevel@tonic-gate 		return;
42000Sstevel@tonic-gate 	}
42010Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)ip_len);
42020Sstevel@tonic-gate 	/*
42030Sstevel@tonic-gate 	 * Copy in the destination address from the T_UNITDATA
42040Sstevel@tonic-gate 	 * request
42050Sstevel@tonic-gate 	 */
42061676Sjpk 	ipha->ipha_dst = v4dst;
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate 	/*
42090Sstevel@tonic-gate 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
42100Sstevel@tonic-gate 	 */
42110Sstevel@tonic-gate 	if (CLASSD(v4dst))
42120Sstevel@tonic-gate 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
42130Sstevel@tonic-gate 
42140Sstevel@tonic-gate 	/* Copy in options if any */
42150Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
42160Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options,
42170Sstevel@tonic-gate 		    &ipha[1], icmp->icmp_ip_snd_options_len);
42180Sstevel@tonic-gate 		/*
42190Sstevel@tonic-gate 		 * Massage source route putting first source route in ipha_dst.
42200Sstevel@tonic-gate 		 * Ignore the destination in the T_unitdata_req.
42210Sstevel@tonic-gate 		 */
42220Sstevel@tonic-gate 		(void) ip_massage_options(ipha);
42230Sstevel@tonic-gate 	}
42240Sstevel@tonic-gate 	freeb(mp);
42250Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
42261676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
42270Sstevel@tonic-gate 	putnext(q, mp1);
42280Sstevel@tonic-gate #undef	ipha
42290Sstevel@tonic-gate #undef tudr
42300Sstevel@tonic-gate }
42310Sstevel@tonic-gate 
42321676Sjpk static boolean_t
42331676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst)
42341676Sjpk {
42351676Sjpk 	int err;
42361676Sjpk 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
42371676Sjpk 
42381676Sjpk 	err = tsol_compute_label_v6(DB_CREDDEF(mp, icmp->icmp_credp), dst,
42391676Sjpk 	    opt_storage, icmp->icmp_mac_exempt);
42401676Sjpk 	if (err == 0) {
42411676Sjpk 		err = tsol_update_sticky(&icmp->icmp_sticky_ipp,
42421676Sjpk 		    &icmp->icmp_label_len_v6, opt_storage);
42431676Sjpk 	}
42441676Sjpk 	if (err != 0) {
42451676Sjpk 		BUMP_MIB(&rawip_mib, rawipOutErrors);
42461676Sjpk 		DTRACE_PROBE4(
42471676Sjpk 		    tx__ip__log__drop__updatelabel__icmp6,
42481676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
42491676Sjpk 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
42501676Sjpk 		icmp_ud_err(wq, mp, err);
42511676Sjpk 		return (B_FALSE);
42521676Sjpk 	}
42531676Sjpk 
42541676Sjpk 	icmp->icmp_v6lastdst = *dst;
42551676Sjpk 	return (B_TRUE);
42561676Sjpk }
42571676Sjpk 
42580Sstevel@tonic-gate /*
42590Sstevel@tonic-gate  * icmp_wput_ipv6():
42600Sstevel@tonic-gate  * Assumes that icmp_wput did some sanity checking on the destination
42611676Sjpk  * address, but that the label may not yet be correct.
42620Sstevel@tonic-gate  */
42630Sstevel@tonic-gate void
42640Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
42650Sstevel@tonic-gate {
42660Sstevel@tonic-gate 	ip6_t			*ip6h;
42670Sstevel@tonic-gate 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
42680Sstevel@tonic-gate 	mblk_t			*mp1;
42690Sstevel@tonic-gate 	int			ip_hdr_len = IPV6_HDR_LEN;
42700Sstevel@tonic-gate 	size_t			ip_len;
42710Sstevel@tonic-gate 	icmp_t			*icmp;
42720Sstevel@tonic-gate 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
42730Sstevel@tonic-gate 	ip6_pkt_t		*ipp = &ipp_s;
42740Sstevel@tonic-gate 	ip6_pkt_t		*tipp;
42750Sstevel@tonic-gate 	uint32_t		csum = 0;
42760Sstevel@tonic-gate 	uint_t			ignore = 0;
42770Sstevel@tonic-gate 	uint_t			option_exists = 0, is_sticky = 0;
42780Sstevel@tonic-gate 	uint8_t			*cp;
42790Sstevel@tonic-gate 	uint8_t			*nxthdr_ptr;
42801676Sjpk 	in6_addr_t		ip6_dst;
42810Sstevel@tonic-gate 
42820Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
42830Sstevel@tonic-gate 
42840Sstevel@tonic-gate 	/*
42850Sstevel@tonic-gate 	 * If the local address is a mapped address return
42860Sstevel@tonic-gate 	 * an error.
42870Sstevel@tonic-gate 	 * It would be possible to send an IPv6 packet but the
42880Sstevel@tonic-gate 	 * response would never make it back to the application
42890Sstevel@tonic-gate 	 * since it is bound to a mapped address.
42900Sstevel@tonic-gate 	 */
42910Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
42920Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
42930Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
42940Sstevel@tonic-gate 		return;
42950Sstevel@tonic-gate 	}
42960Sstevel@tonic-gate 
42970Sstevel@tonic-gate 	ipp->ipp_fields = 0;
42980Sstevel@tonic-gate 	ipp->ipp_sticky_ignored = 0;
42990Sstevel@tonic-gate 
43000Sstevel@tonic-gate 	/*
43010Sstevel@tonic-gate 	 * If TPI options passed in, feed it for verification and handling
43020Sstevel@tonic-gate 	 */
43030Sstevel@tonic-gate 	if (tudr_optlen != 0) {
43040Sstevel@tonic-gate 		int error;
43050Sstevel@tonic-gate 
43060Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
43070Sstevel@tonic-gate 		    (void *)ipp) < 0) {
43080Sstevel@tonic-gate 			/* failure */
43090Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
43100Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
43110Sstevel@tonic-gate 			return;
43120Sstevel@tonic-gate 		}
43130Sstevel@tonic-gate 		ignore = ipp->ipp_sticky_ignored;
43140Sstevel@tonic-gate 		ASSERT(error == 0);
43150Sstevel@tonic-gate 	}
43160Sstevel@tonic-gate 
43170Sstevel@tonic-gate 	if (sin6->sin6_scope_id != 0 &&
43180Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
43190Sstevel@tonic-gate 		/*
43200Sstevel@tonic-gate 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
43210Sstevel@tonic-gate 		 * option nor ancillary data.  It needs to be
43220Sstevel@tonic-gate 		 * explicitly set in options_exists.
43230Sstevel@tonic-gate 		 */
43240Sstevel@tonic-gate 		option_exists |= IPPF_SCOPE_ID;
43250Sstevel@tonic-gate 	}
43260Sstevel@tonic-gate 
43271676Sjpk 	/*
43281676Sjpk 	 * Compute the destination address
43291676Sjpk 	 */
43301676Sjpk 	ip6_dst = sin6->sin6_addr;
43311676Sjpk 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
43321676Sjpk 		ip6_dst = ipv6_loopback;
43331676Sjpk 
43341676Sjpk 	/*
43351676Sjpk 	 * If we're not going to the same destination as last time, then
43361676Sjpk 	 * recompute the label required.  This is done in a separate routine to
43371676Sjpk 	 * avoid blowing up our stack here.
43381676Sjpk 	 */
43391676Sjpk 	if (is_system_labeled() &&
43401676Sjpk 	    !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) &&
43411676Sjpk 	    !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) {
43421676Sjpk 		return;
43431676Sjpk 	}
43441676Sjpk 
43451676Sjpk 	/*
43461676Sjpk 	 * If there's a security label here, then we ignore any options the
43471676Sjpk 	 * user may try to set.  We keep the peer's label as a hidden sticky
43481676Sjpk 	 * option.
43491676Sjpk 	 */
43501676Sjpk 	if (icmp->icmp_label_len_v6 > 0) {
43511676Sjpk 		ignore &= ~IPPF_HOPOPTS;
43521676Sjpk 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
43531676Sjpk 	}
43541676Sjpk 
43550Sstevel@tonic-gate 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
43560Sstevel@tonic-gate 	    (ipp->ipp_fields == 0)) {
43570Sstevel@tonic-gate 		/* No sticky options nor ancillary data. */
43580Sstevel@tonic-gate 		goto no_options;
43590Sstevel@tonic-gate 	}
43600Sstevel@tonic-gate 
43610Sstevel@tonic-gate 	/*
43620Sstevel@tonic-gate 	 * Go through the options figuring out where each is going to
43630Sstevel@tonic-gate 	 * come from and build two masks.  The first mask indicates if
43640Sstevel@tonic-gate 	 * the option exists at all.  The second mask indicates if the
43650Sstevel@tonic-gate 	 * option is sticky or ancillary.
43660Sstevel@tonic-gate 	 */
43670Sstevel@tonic-gate 	if (!(ignore & IPPF_HOPOPTS)) {
43680Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
43690Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
43700Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_hopoptslen;
43710Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
43720Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
43730Sstevel@tonic-gate 			is_sticky |= IPPF_HOPOPTS;
43740Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
43750Sstevel@tonic-gate 		}
43760Sstevel@tonic-gate 	}
43770Sstevel@tonic-gate 
43780Sstevel@tonic-gate 	if (!(ignore & IPPF_RTHDR)) {
43790Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTHDR) {
43800Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
43810Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rthdrlen;
43820Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
43830Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
43840Sstevel@tonic-gate 			is_sticky |= IPPF_RTHDR;
43850Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
43860Sstevel@tonic-gate 		}
43870Sstevel@tonic-gate 	}
43880Sstevel@tonic-gate 
43890Sstevel@tonic-gate 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
43900Sstevel@tonic-gate 		/*
43910Sstevel@tonic-gate 		 * Need to have a router header to use these.
43920Sstevel@tonic-gate 		 */
43930Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
43940Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
43950Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rtdstoptslen;
43960Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
43970Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
43980Sstevel@tonic-gate 			is_sticky |= IPPF_RTDSTOPTS;
43990Sstevel@tonic-gate 			ip_hdr_len +=
44000Sstevel@tonic-gate 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
44010Sstevel@tonic-gate 		}
44020Sstevel@tonic-gate 	}
44030Sstevel@tonic-gate 
44040Sstevel@tonic-gate 	if (!(ignore & IPPF_DSTOPTS)) {
44050Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
44060Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
44070Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_dstoptslen;
44080Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
44090Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
44100Sstevel@tonic-gate 			is_sticky |= IPPF_DSTOPTS;
44110Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
44120Sstevel@tonic-gate 		}
44130Sstevel@tonic-gate 	}
44140Sstevel@tonic-gate 
44150Sstevel@tonic-gate 	if (!(ignore & IPPF_IFINDEX)) {
44160Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_IFINDEX) {
44170Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
44180Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
44190Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
44200Sstevel@tonic-gate 			is_sticky |= IPPF_IFINDEX;
44210Sstevel@tonic-gate 		}
44220Sstevel@tonic-gate 	}
44230Sstevel@tonic-gate 
44240Sstevel@tonic-gate 	if (!(ignore & IPPF_ADDR)) {
44250Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_ADDR) {
44260Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
44270Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
44280Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
44290Sstevel@tonic-gate 			is_sticky |= IPPF_ADDR;
44300Sstevel@tonic-gate 		}
44310Sstevel@tonic-gate 	}
44320Sstevel@tonic-gate 
44330Sstevel@tonic-gate 	if (!(ignore & IPPF_DONTFRAG)) {
44340Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
44350Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
44360Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
44370Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
44380Sstevel@tonic-gate 			is_sticky |= IPPF_DONTFRAG;
44390Sstevel@tonic-gate 		}
44400Sstevel@tonic-gate 	}
44410Sstevel@tonic-gate 
44420Sstevel@tonic-gate 	if (!(ignore & IPPF_USE_MIN_MTU)) {
44430Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
44440Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
44450Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
44460Sstevel@tonic-gate 		    IPPF_USE_MIN_MTU) {
44470Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
44480Sstevel@tonic-gate 			is_sticky |= IPPF_USE_MIN_MTU;
44490Sstevel@tonic-gate 		}
44500Sstevel@tonic-gate 	}
44510Sstevel@tonic-gate 
44520Sstevel@tonic-gate 	if (!(ignore & IPPF_NEXTHOP)) {
44530Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
44540Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
44550Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
44560Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
44570Sstevel@tonic-gate 			is_sticky |= IPPF_NEXTHOP;
44580Sstevel@tonic-gate 		}
44590Sstevel@tonic-gate 	}
44600Sstevel@tonic-gate 
4461679Sseb 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4462679Sseb 		option_exists |= IPPF_HOPLIMIT;
4463679Sseb 	/* IPV6_HOPLIMIT can never be sticky */
4464679Sseb 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4465679Sseb 
4466679Sseb 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4467679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4468679Sseb 		option_exists |= IPPF_UNICAST_HOPS;
4469679Sseb 		is_sticky |= IPPF_UNICAST_HOPS;
4470679Sseb 	}
4471679Sseb 
4472679Sseb 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4473679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4474679Sseb 		option_exists |= IPPF_MULTICAST_HOPS;
4475679Sseb 		is_sticky |= IPPF_MULTICAST_HOPS;
44760Sstevel@tonic-gate 	}
44770Sstevel@tonic-gate 
44780Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
44790Sstevel@tonic-gate 		/* This is a sticky socket option only */
44800Sstevel@tonic-gate 		option_exists |= IPPF_NO_CKSUM;
44810Sstevel@tonic-gate 		is_sticky |= IPPF_NO_CKSUM;
44820Sstevel@tonic-gate 	}
44830Sstevel@tonic-gate 
44840Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
44850Sstevel@tonic-gate 		/* This is a sticky socket option only */
44860Sstevel@tonic-gate 		option_exists |= IPPF_RAW_CKSUM;
44870Sstevel@tonic-gate 		is_sticky |= IPPF_RAW_CKSUM;
44880Sstevel@tonic-gate 	}
44890Sstevel@tonic-gate 
44900Sstevel@tonic-gate 	if (!(ignore & IPPF_TCLASS)) {
44910Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_TCLASS) {
44920Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
44930Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
44940Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
44950Sstevel@tonic-gate 			is_sticky |= IPPF_TCLASS;
44960Sstevel@tonic-gate 		}
44970Sstevel@tonic-gate 	}
44980Sstevel@tonic-gate 
44990Sstevel@tonic-gate no_options:
45000Sstevel@tonic-gate 
45010Sstevel@tonic-gate 	/*
45020Sstevel@tonic-gate 	 * If any options carried in the ip6i_t were specified, we
45030Sstevel@tonic-gate 	 * need to account for the ip6i_t in the data we'll be sending
45040Sstevel@tonic-gate 	 * down.
45050Sstevel@tonic-gate 	 */
45060Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I)
45070Sstevel@tonic-gate 		ip_hdr_len += sizeof (ip6i_t);
45080Sstevel@tonic-gate 
45090Sstevel@tonic-gate 	/* check/fix buffer config, setup pointers into it */
45100Sstevel@tonic-gate 	mp1 = mp->b_cont;
45110Sstevel@tonic-gate 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
45120Sstevel@tonic-gate 	if ((mp1->b_datap->db_ref != 1) ||
45130Sstevel@tonic-gate 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
45140Sstevel@tonic-gate 	    !OK_32PTR(ip6h)) {
45150Sstevel@tonic-gate 		/* Try to get everything in a single mblk next time */
45160Sstevel@tonic-gate 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
45170Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = ip_hdr_len;
45180Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q),
45190Sstevel@tonic-gate 			    icmp->icmp_max_hdr_len + icmp_wroff_extra);
45200Sstevel@tonic-gate 		}
45210Sstevel@tonic-gate 		mp1 = allocb(ip_hdr_len + icmp_wroff_extra, BPRI_LO);
45220Sstevel@tonic-gate 		if (!mp1) {
45230Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
45240Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
45250Sstevel@tonic-gate 			return;
45260Sstevel@tonic-gate 		}
45270Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
45280Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_datap->db_lim;
45290Sstevel@tonic-gate 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
45300Sstevel@tonic-gate 	}
45310Sstevel@tonic-gate 	mp1->b_rptr = (unsigned char *)ip6h;
45320Sstevel@tonic-gate 	ip6i = (ip6i_t *)ip6h;
45330Sstevel@tonic-gate 
45340Sstevel@tonic-gate #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
45350Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I) {
45360Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
45370Sstevel@tonic-gate 		ip6i->ip6i_flags = 0;
45380Sstevel@tonic-gate 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
45390Sstevel@tonic-gate 
45400Sstevel@tonic-gate 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
45410Sstevel@tonic-gate 		if (option_exists & IPPF_SCOPE_ID) {
45420Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
45430Sstevel@tonic-gate 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
45440Sstevel@tonic-gate 		} else if (option_exists & IPPF_IFINDEX) {
45450Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
45460Sstevel@tonic-gate 			ASSERT(tipp->ipp_ifindex != 0);
45470Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
45480Sstevel@tonic-gate 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
45490Sstevel@tonic-gate 		}
45500Sstevel@tonic-gate 
45510Sstevel@tonic-gate 		if (option_exists & IPPF_RAW_CKSUM) {
45520Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
45530Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
45540Sstevel@tonic-gate 		}
45550Sstevel@tonic-gate 
45560Sstevel@tonic-gate 		if (option_exists & IPPF_NO_CKSUM) {
45570Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
45580Sstevel@tonic-gate 		}
45590Sstevel@tonic-gate 
45600Sstevel@tonic-gate 		if (option_exists & IPPF_ADDR) {
45610Sstevel@tonic-gate 			/*
45620Sstevel@tonic-gate 			 * Enable per-packet source address verification if
45630Sstevel@tonic-gate 			 * IPV6_PKTINFO specified the source address.
45640Sstevel@tonic-gate 			 * ip6_src is set in the transport's _wput function.
45650Sstevel@tonic-gate 			 */
45660Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
45670Sstevel@tonic-gate 		}
45680Sstevel@tonic-gate 
45690Sstevel@tonic-gate 		if (option_exists & IPPF_DONTFRAG) {
45700Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
45710Sstevel@tonic-gate 		}
45720Sstevel@tonic-gate 
45730Sstevel@tonic-gate 		if (option_exists & IPPF_USE_MIN_MTU) {
45740Sstevel@tonic-gate 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
45750Sstevel@tonic-gate 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
45760Sstevel@tonic-gate 		}
45770Sstevel@tonic-gate 
45780Sstevel@tonic-gate 		if (option_exists & IPPF_NEXTHOP) {
45790Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
45800Sstevel@tonic-gate 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
45810Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
45820Sstevel@tonic-gate 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
45830Sstevel@tonic-gate 		}
45840Sstevel@tonic-gate 
45850Sstevel@tonic-gate 		/*
45860Sstevel@tonic-gate 		 * tell IP this is an ip6i_t private header
45870Sstevel@tonic-gate 		 */
45880Sstevel@tonic-gate 		ip6i->ip6i_nxt = IPPROTO_RAW;
45890Sstevel@tonic-gate 	}
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	/* Initialize IPv6 header */
45920Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
45930Sstevel@tonic-gate 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
45940Sstevel@tonic-gate 
4595679Sseb 	/* Set the hoplimit of the outgoing packet. */
45960Sstevel@tonic-gate 	if (option_exists & IPPF_HOPLIMIT) {
4597679Sseb 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4598679Sseb 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4599679Sseb 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4600679Sseb 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
46010Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4602679Sseb 		if (option_exists & IPPF_MULTICAST_HOPS)
4603679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
46040Sstevel@tonic-gate 	} else {
46050Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_ttl;
4606679Sseb 		if (option_exists & IPPF_UNICAST_HOPS)
4607679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
46080Sstevel@tonic-gate 	}
46090Sstevel@tonic-gate 
46100Sstevel@tonic-gate 	if (option_exists & IPPF_ADDR) {
46110Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
46120Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
46130Sstevel@tonic-gate 		ip6h->ip6_src = tipp->ipp_addr;
46140Sstevel@tonic-gate 	} else {
46150Sstevel@tonic-gate 		/*
46160Sstevel@tonic-gate 		 * The source address was not set using IPV6_PKTINFO.
46170Sstevel@tonic-gate 		 * First look at the bound source.
46180Sstevel@tonic-gate 		 * If unspecified fallback to __sin6_src_id.
46190Sstevel@tonic-gate 		 */
46200Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
46210Sstevel@tonic-gate 		if (sin6->__sin6_src_id != 0 &&
46220Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
46230Sstevel@tonic-gate 			ip_srcid_find_id(sin6->__sin6_src_id,
46240Sstevel@tonic-gate 			    &ip6h->ip6_src, icmp->icmp_zoneid);
46250Sstevel@tonic-gate 		}
46260Sstevel@tonic-gate 	}
46270Sstevel@tonic-gate 
46280Sstevel@tonic-gate 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
46290Sstevel@tonic-gate 	cp = (uint8_t *)&ip6h[1];
46300Sstevel@tonic-gate 
46310Sstevel@tonic-gate 	/*
46320Sstevel@tonic-gate 	 * Here's where we have to start stringing together
46330Sstevel@tonic-gate 	 * any extension headers in the right order:
46340Sstevel@tonic-gate 	 * Hop-by-hop, destination, routing, and final destination opts.
46350Sstevel@tonic-gate 	 */
46360Sstevel@tonic-gate 	if (option_exists & IPPF_HOPOPTS) {
46370Sstevel@tonic-gate 		/* Hop-by-hop options */
46380Sstevel@tonic-gate 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
46390Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
46400Sstevel@tonic-gate 
46410Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_HOPOPTS;
46420Sstevel@tonic-gate 		nxthdr_ptr = &hbh->ip6h_nxt;
46430Sstevel@tonic-gate 
46440Sstevel@tonic-gate 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
46450Sstevel@tonic-gate 		cp += tipp->ipp_hopoptslen;
46460Sstevel@tonic-gate 	}
46470Sstevel@tonic-gate 	/*
46480Sstevel@tonic-gate 	 * En-route destination options
46490Sstevel@tonic-gate 	 * Only do them if there's a routing header as well
46500Sstevel@tonic-gate 	 */
46510Sstevel@tonic-gate 	if (option_exists & IPPF_RTDSTOPTS) {
46520Sstevel@tonic-gate 		ip6_dest_t *dst = (ip6_dest_t *)cp;
46530Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
46540Sstevel@tonic-gate 
46550Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
46560Sstevel@tonic-gate 		nxthdr_ptr = &dst->ip6d_nxt;
46570Sstevel@tonic-gate 
46580Sstevel@tonic-gate 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
46590Sstevel@tonic-gate 		cp += tipp->ipp_rtdstoptslen;
46600Sstevel@tonic-gate 	}
46610Sstevel@tonic-gate 	/*
46620Sstevel@tonic-gate 	 * Routing header next
46630Sstevel@tonic-gate 	 */
46640Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
46650Sstevel@tonic-gate 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
46660Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_ROUTING;
46690Sstevel@tonic-gate 		nxthdr_ptr = &rt->ip6r_nxt;
46700Sstevel@tonic-gate 
46710Sstevel@tonic-gate 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
46720Sstevel@tonic-gate 		cp += tipp->ipp_rthdrlen;
46730Sstevel@tonic-gate 	}
46740Sstevel@tonic-gate 	/*
46750Sstevel@tonic-gate 	 * Do ultimate destination options
46760Sstevel@tonic-gate 	 */
46770Sstevel@tonic-gate 	if (option_exists & IPPF_DSTOPTS) {
46780Sstevel@tonic-gate 		ip6_dest_t *dest = (ip6_dest_t *)cp;
46790Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
46800Sstevel@tonic-gate 
46810Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
46820Sstevel@tonic-gate 		nxthdr_ptr = &dest->ip6d_nxt;
46830Sstevel@tonic-gate 
46840Sstevel@tonic-gate 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
46850Sstevel@tonic-gate 		cp += tipp->ipp_dstoptslen;
46860Sstevel@tonic-gate 	}
46870Sstevel@tonic-gate 
46880Sstevel@tonic-gate 	/*
46890Sstevel@tonic-gate 	 * Now set the last header pointer to the proto passed in
46900Sstevel@tonic-gate 	 */
46910Sstevel@tonic-gate 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
46920Sstevel@tonic-gate 	*nxthdr_ptr = icmp->icmp_proto;
46930Sstevel@tonic-gate 
46940Sstevel@tonic-gate 	/*
46950Sstevel@tonic-gate 	 * Copy in the destination address
46960Sstevel@tonic-gate 	 */
46971676Sjpk 	ip6h->ip6_dst = ip6_dst;
46980Sstevel@tonic-gate 
46990Sstevel@tonic-gate 	ip6h->ip6_vcf =
47000Sstevel@tonic-gate 		(IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
47010Sstevel@tonic-gate 		(sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
47020Sstevel@tonic-gate 
47030Sstevel@tonic-gate 	if (option_exists & IPPF_TCLASS) {
47040Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
47050Sstevel@tonic-gate 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
47060Sstevel@tonic-gate 		    tipp->ipp_tclass);
47070Sstevel@tonic-gate 	}
47080Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
47090Sstevel@tonic-gate 		ip6_rthdr_t	*rth;
47100Sstevel@tonic-gate 
47110Sstevel@tonic-gate 		/*
47120Sstevel@tonic-gate 		 * Perform any processing needed for source routing.
47130Sstevel@tonic-gate 		 * We know that all extension headers will be in the same mblk
47140Sstevel@tonic-gate 		 * as the IPv6 header.
47150Sstevel@tonic-gate 		 */
47160Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
47170Sstevel@tonic-gate 		if (rth != NULL && rth->ip6r_segleft != 0) {
47180Sstevel@tonic-gate 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
47190Sstevel@tonic-gate 				/*
47200Sstevel@tonic-gate 				 * Drop packet - only support Type 0 routing.
47210Sstevel@tonic-gate 				 * Notify the application as well.
47220Sstevel@tonic-gate 				 */
47230Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
47240Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47250Sstevel@tonic-gate 				return;
47260Sstevel@tonic-gate 			}
47270Sstevel@tonic-gate 			/*
47280Sstevel@tonic-gate 			 * rth->ip6r_len is twice the number of
47290Sstevel@tonic-gate 			 * addresses in the header
47300Sstevel@tonic-gate 			 */
47310Sstevel@tonic-gate 			if (rth->ip6r_len & 0x1) {
47320Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
47330Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47340Sstevel@tonic-gate 				return;
47350Sstevel@tonic-gate 			}
47360Sstevel@tonic-gate 			/*
47370Sstevel@tonic-gate 			 * Shuffle the routing header and ip6_dst
47380Sstevel@tonic-gate 			 * addresses, and get the checksum difference
47390Sstevel@tonic-gate 			 * between the first hop (in ip6_dst) and
47400Sstevel@tonic-gate 			 * the destination (in the last routing hdr entry).
47410Sstevel@tonic-gate 			 */
47420Sstevel@tonic-gate 			csum = ip_massage_options_v6(ip6h, rth);
47430Sstevel@tonic-gate 			/*
47440Sstevel@tonic-gate 			 * Verify that the first hop isn't a mapped address.
47450Sstevel@tonic-gate 			 * Routers along the path need to do this verification
47460Sstevel@tonic-gate 			 * for subsequent hops.
47470Sstevel@tonic-gate 			 */
47480Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
47490Sstevel@tonic-gate 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
47500Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47510Sstevel@tonic-gate 				return;
47520Sstevel@tonic-gate 			}
47530Sstevel@tonic-gate 		}
47540Sstevel@tonic-gate 	}
47550Sstevel@tonic-gate 
47560Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
47570Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
47580Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
47590Sstevel@tonic-gate 
47600Sstevel@tonic-gate 	/*
47610Sstevel@tonic-gate 	 * Set the length into the IP header.
47620Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
47630Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
47640Sstevel@tonic-gate 	 * as this can cause problems in layers below.
47650Sstevel@tonic-gate 	 */
47660Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
47670Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
47681289Sja97890 		icmp_ud_err(q, mp, EMSGSIZE);
47690Sstevel@tonic-gate 		return;
47700Sstevel@tonic-gate 	}
47710Sstevel@tonic-gate 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
47720Sstevel@tonic-gate 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
47730Sstevel@tonic-gate 		uint16_t *cksum_ptr;
47740Sstevel@tonic-gate 		uint_t	ext_hdrs_len;
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
47770Sstevel@tonic-gate 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
47780Sstevel@tonic-gate 		    icmp->icmp_checksum_off == 2);
47790Sstevel@tonic-gate 
47800Sstevel@tonic-gate 		/*
47810Sstevel@tonic-gate 		 * We make it easy for IP to include our pseudo header
47820Sstevel@tonic-gate 		 * by putting our length in uh_checksum, modified (if
47830Sstevel@tonic-gate 		 * we have a routing header) by the checksum difference
47840Sstevel@tonic-gate 		 * between the ultimate destination and first hop addresses.
47850Sstevel@tonic-gate 		 * Note: ICMPv6 must always checksum the packet.
47860Sstevel@tonic-gate 		 */
47870Sstevel@tonic-gate 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
47880Sstevel@tonic-gate 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
47890Sstevel@tonic-gate 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
47900Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47910Sstevel@tonic-gate 				freemsg(mp);
47920Sstevel@tonic-gate 				return;
47930Sstevel@tonic-gate 			}
47940Sstevel@tonic-gate 			ip6i = (ip6i_t *)mp1->b_rptr;
47950Sstevel@tonic-gate 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
47960Sstevel@tonic-gate 				ip6h = (ip6_t *)&ip6i[1];
47970Sstevel@tonic-gate 			else
47980Sstevel@tonic-gate 				ip6h = (ip6_t *)ip6i;
47990Sstevel@tonic-gate 		}
48000Sstevel@tonic-gate 		/* Add payload length to checksum */
48010Sstevel@tonic-gate 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
48020Sstevel@tonic-gate 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
48030Sstevel@tonic-gate 		csum += htons(ip_len - ext_hdrs_len);
48040Sstevel@tonic-gate 
48050Sstevel@tonic-gate 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
48060Sstevel@tonic-gate 		csum = (csum & 0xFFFF) + (csum >> 16);
48070Sstevel@tonic-gate 		*cksum_ptr = (uint16_t)csum;
48080Sstevel@tonic-gate 	}
48090Sstevel@tonic-gate 
48100Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
48110Sstevel@tonic-gate 	ip_len = htons(ip_len);
48120Sstevel@tonic-gate #endif
48130Sstevel@tonic-gate 	ip6h->ip6_plen = (uint16_t)ip_len;
48140Sstevel@tonic-gate 
48150Sstevel@tonic-gate 	freeb(mp);
48160Sstevel@tonic-gate 
48170Sstevel@tonic-gate 	/* We're done. Pass the packet to IP */
48180Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
48191676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
48200Sstevel@tonic-gate 	putnext(q, mp1);
48210Sstevel@tonic-gate }
48220Sstevel@tonic-gate 
48230Sstevel@tonic-gate static void
48240Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp)
48250Sstevel@tonic-gate {
48260Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
48270Sstevel@tonic-gate 	struct iocblk *iocp;
48280Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
48290Sstevel@tonic-gate 	icmp_t	*icmp;
48300Sstevel@tonic-gate 	cred_t *cr;
48310Sstevel@tonic-gate 
48320Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
48330Sstevel@tonic-gate 
48340Sstevel@tonic-gate 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
48350Sstevel@tonic-gate 
48360Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
48370Sstevel@tonic-gate 	case M_PROTO:
48380Sstevel@tonic-gate 	case M_PCPROTO:
48390Sstevel@tonic-gate 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
48400Sstevel@tonic-gate 			/*
48410Sstevel@tonic-gate 			 * If the message does not contain a PRIM_type,
48420Sstevel@tonic-gate 			 * throw it away.
48430Sstevel@tonic-gate 			 */
48440Sstevel@tonic-gate 			freemsg(mp);
48450Sstevel@tonic-gate 			return;
48460Sstevel@tonic-gate 		}
48470Sstevel@tonic-gate 		switch (((union T_primitives *)rptr)->type) {
48480Sstevel@tonic-gate 		case T_ADDR_REQ:
48490Sstevel@tonic-gate 			icmp_addr_req(q, mp);
48500Sstevel@tonic-gate 			return;
48510Sstevel@tonic-gate 		case O_T_BIND_REQ:
48520Sstevel@tonic-gate 		case T_BIND_REQ:
48530Sstevel@tonic-gate 			qwriter(q, mp, icmp_bind, PERIM_OUTER);
48540Sstevel@tonic-gate 			return;
48550Sstevel@tonic-gate 		case T_CONN_REQ:
48560Sstevel@tonic-gate 			icmp_connect(q, mp);
48570Sstevel@tonic-gate 			return;
48580Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
48590Sstevel@tonic-gate 			icmp_capability_req(q, mp);
48600Sstevel@tonic-gate 			return;
48610Sstevel@tonic-gate 		case T_INFO_REQ:
48620Sstevel@tonic-gate 			icmp_info_req(q, mp);
48630Sstevel@tonic-gate 			return;
48640Sstevel@tonic-gate 		case T_UNITDATA_REQ:
48650Sstevel@tonic-gate 			/*
48660Sstevel@tonic-gate 			 * If a T_UNITDATA_REQ gets here, the address must
48670Sstevel@tonic-gate 			 * be bad.  Valid T_UNITDATA_REQs are found above
48680Sstevel@tonic-gate 			 * and break to below this switch.
48690Sstevel@tonic-gate 			 */
48700Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
48710Sstevel@tonic-gate 			return;
48720Sstevel@tonic-gate 		case T_UNBIND_REQ:
48730Sstevel@tonic-gate 			icmp_unbind(q, mp);
48740Sstevel@tonic-gate 			return;
48750Sstevel@tonic-gate 
48760Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
48770Sstevel@tonic-gate 			if (!snmpcom_req(q, mp, icmp_snmp_set, icmp_snmp_get,
48780Sstevel@tonic-gate 			    cr))
48790Sstevel@tonic-gate 				/* Only IP can return anything meaningful */
48800Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
48810Sstevel@tonic-gate 				    &icmp_opt_obj);
48820Sstevel@tonic-gate 			return;
48830Sstevel@tonic-gate 
48840Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
48850Sstevel@tonic-gate 			/* Only IP can return anything meaningful */
48860Sstevel@tonic-gate 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
48870Sstevel@tonic-gate 			return;
48880Sstevel@tonic-gate 
48890Sstevel@tonic-gate 		case T_DISCON_REQ:
48900Sstevel@tonic-gate 			icmp_disconnect(q, mp);
48910Sstevel@tonic-gate 			return;
48920Sstevel@tonic-gate 
48930Sstevel@tonic-gate 		/* The following TPI message is not supported by icmp. */
48940Sstevel@tonic-gate 		case O_T_CONN_RES:
48950Sstevel@tonic-gate 		case T_CONN_RES:
48960Sstevel@tonic-gate 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
48970Sstevel@tonic-gate 			return;
48980Sstevel@tonic-gate 
48990Sstevel@tonic-gate 		/* The following 3 TPI requests are illegal for icmp. */
49000Sstevel@tonic-gate 		case T_DATA_REQ:
49010Sstevel@tonic-gate 		case T_EXDATA_REQ:
49020Sstevel@tonic-gate 		case T_ORDREL_REQ:
49030Sstevel@tonic-gate 			freemsg(mp);
49040Sstevel@tonic-gate 			(void) putctl1(RD(q), M_ERROR, EPROTO);
49050Sstevel@tonic-gate 			return;
49060Sstevel@tonic-gate 		default:
49070Sstevel@tonic-gate 			break;
49080Sstevel@tonic-gate 		}
49090Sstevel@tonic-gate 		break;
49100Sstevel@tonic-gate 	case M_IOCTL:
49110Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
49120Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
49130Sstevel@tonic-gate 		case TI_GETPEERNAME:
49140Sstevel@tonic-gate 			if (icmp->icmp_state != TS_DATA_XFER) {
49150Sstevel@tonic-gate 				/*
49160Sstevel@tonic-gate 				 * If a default destination address has not
49170Sstevel@tonic-gate 				 * been associated with the stream, then we
49180Sstevel@tonic-gate 				 * don't know the peer's name.
49190Sstevel@tonic-gate 				 */
49200Sstevel@tonic-gate 				iocp->ioc_error = ENOTCONN;
49210Sstevel@tonic-gate 			    err_ret:;
49220Sstevel@tonic-gate 				iocp->ioc_count = 0;
49230Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
49240Sstevel@tonic-gate 				qreply(q, mp);
49250Sstevel@tonic-gate 				return;
49260Sstevel@tonic-gate 			}
49270Sstevel@tonic-gate 			/* FALLTHRU */
49280Sstevel@tonic-gate 		case TI_GETMYNAME:
49290Sstevel@tonic-gate 			/*
49300Sstevel@tonic-gate 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
49310Sstevel@tonic-gate 			 * need to copyin the user's strbuf structure.
49320Sstevel@tonic-gate 			 * Processing will continue in the M_IOCDATA case
49330Sstevel@tonic-gate 			 * below.
49340Sstevel@tonic-gate 			 */
49350Sstevel@tonic-gate 			mi_copyin(q, mp, NULL,
49360Sstevel@tonic-gate 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
49370Sstevel@tonic-gate 			return;
49380Sstevel@tonic-gate 		case ND_SET:
49390Sstevel@tonic-gate 			/* nd_getset performs the necessary error checking */
49400Sstevel@tonic-gate 		case ND_GET:
49410Sstevel@tonic-gate 			if (nd_getset(q, icmp_g_nd, mp)) {
49420Sstevel@tonic-gate 				qreply(q, mp);
49430Sstevel@tonic-gate 				return;
49440Sstevel@tonic-gate 			}
49450Sstevel@tonic-gate 			break;
49460Sstevel@tonic-gate 		default:
49470Sstevel@tonic-gate 			break;
49480Sstevel@tonic-gate 		}
49490Sstevel@tonic-gate 		break;
49500Sstevel@tonic-gate 	case M_IOCDATA:
49510Sstevel@tonic-gate 		icmp_wput_iocdata(q, mp);
49520Sstevel@tonic-gate 		return;
49530Sstevel@tonic-gate 	default:
49540Sstevel@tonic-gate 		break;
49550Sstevel@tonic-gate 	}
49560Sstevel@tonic-gate 	putnext(q, mp);
49570Sstevel@tonic-gate }
49580Sstevel@tonic-gate 
49590Sstevel@tonic-gate /*
49600Sstevel@tonic-gate  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
49610Sstevel@tonic-gate  * messages.
49620Sstevel@tonic-gate  */
49630Sstevel@tonic-gate static void
49640Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp)
49650Sstevel@tonic-gate {
49660Sstevel@tonic-gate 	mblk_t	*mp1;
49670Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
49680Sstevel@tonic-gate 	icmp_t	*icmp;
49690Sstevel@tonic-gate 	in6_addr_t	v6addr;
49700Sstevel@tonic-gate 	ipaddr_t	v4addr;
49710Sstevel@tonic-gate 	uint32_t	flowinfo = 0;
49720Sstevel@tonic-gate 	int		addrlen;
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate 	/* Make sure it is one of ours. */
49750Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
49760Sstevel@tonic-gate 	case TI_GETMYNAME:
49770Sstevel@tonic-gate 	case TI_GETPEERNAME:
49780Sstevel@tonic-gate 		break;
49790Sstevel@tonic-gate 	default:
49800Sstevel@tonic-gate 		putnext(q, mp);
49810Sstevel@tonic-gate 		return;
49820Sstevel@tonic-gate 	}
49830Sstevel@tonic-gate 	switch (mi_copy_state(q, mp, &mp1)) {
49840Sstevel@tonic-gate 	case -1:
49850Sstevel@tonic-gate 		return;
49860Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_IN, 1):
49870Sstevel@tonic-gate 		break;
49880Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 1):
49890Sstevel@tonic-gate 		/*
49900Sstevel@tonic-gate 		 * The address has been copied out, so now
49910Sstevel@tonic-gate 		 * copyout the strbuf.
49920Sstevel@tonic-gate 		 */
49930Sstevel@tonic-gate 		mi_copyout(q, mp);
49940Sstevel@tonic-gate 		return;
49950Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 2):
49960Sstevel@tonic-gate 		/*
49970Sstevel@tonic-gate 		 * The address and strbuf have been copied out.
49980Sstevel@tonic-gate 		 * We're done, so just acknowledge the original
49990Sstevel@tonic-gate 		 * M_IOCTL.
50000Sstevel@tonic-gate 		 */
50010Sstevel@tonic-gate 		mi_copy_done(q, mp, 0);
50020Sstevel@tonic-gate 		return;
50030Sstevel@tonic-gate 	default:
50040Sstevel@tonic-gate 		/*
50050Sstevel@tonic-gate 		 * Something strange has happened, so acknowledge
50060Sstevel@tonic-gate 		 * the original M_IOCTL with an EPROTO error.
50070Sstevel@tonic-gate 		 */
50080Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
50090Sstevel@tonic-gate 		return;
50100Sstevel@tonic-gate 	}
50110Sstevel@tonic-gate 	/*
50120Sstevel@tonic-gate 	 * Now we have the strbuf structure for TI_GETMYNAME
50130Sstevel@tonic-gate 	 * and TI_GETPEERNAME.  Next we copyout the requested
50140Sstevel@tonic-gate 	 * address and then we'll copyout the strbuf.
50150Sstevel@tonic-gate 	 */
50160Sstevel@tonic-gate 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
50170Sstevel@tonic-gate 	    (void *)mp1->b_rptr);
50180Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
50190Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET)
50200Sstevel@tonic-gate 		addrlen = sizeof (sin_t);
50210Sstevel@tonic-gate 	else
50220Sstevel@tonic-gate 		addrlen = sizeof (sin6_t);
50230Sstevel@tonic-gate 
50240Sstevel@tonic-gate 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
50250Sstevel@tonic-gate 		mi_copy_done(q, mp, EINVAL);
50260Sstevel@tonic-gate 		return;
50270Sstevel@tonic-gate 	}
50280Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
50290Sstevel@tonic-gate 	case TI_GETMYNAME:
50300Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
50310Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
50320Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
50330Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
50340Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
50350Sstevel@tonic-gate 			} else {
50360Sstevel@tonic-gate 				/*
50370Sstevel@tonic-gate 				 * INADDR_ANY
50380Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
50390Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
50400Sstevel@tonic-gate 				 * local address instead (that could
50410Sstevel@tonic-gate 				 * also still be INADDR_ANY)
50420Sstevel@tonic-gate 				 */
50430Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
50440Sstevel@tonic-gate 			}
50450Sstevel@tonic-gate 		} else {
50460Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6 */
50470Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
50480Sstevel@tonic-gate 				v6addr = icmp->icmp_v6src;
50490Sstevel@tonic-gate 			} else {
50500Sstevel@tonic-gate 				/*
50510Sstevel@tonic-gate 				 * UNSPECIFIED
50520Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
50530Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
50540Sstevel@tonic-gate 				 * local address instead (that could
50550Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
50560Sstevel@tonic-gate 				 */
50570Sstevel@tonic-gate 				v6addr = icmp->icmp_bound_v6src;
50580Sstevel@tonic-gate 			}
50590Sstevel@tonic-gate 		}
50600Sstevel@tonic-gate 		break;
50610Sstevel@tonic-gate 	case TI_GETPEERNAME:
50620Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
50630Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
50640Sstevel@tonic-gate 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
50650Sstevel@tonic-gate 		} else {
50660Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6) */
50670Sstevel@tonic-gate 			v6addr = icmp->icmp_v6dst;
50680Sstevel@tonic-gate 			flowinfo = icmp->icmp_flowinfo;
50690Sstevel@tonic-gate 		}
50700Sstevel@tonic-gate 		break;
50710Sstevel@tonic-gate 	default:
50720Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
50730Sstevel@tonic-gate 		return;
50740Sstevel@tonic-gate 	}
50750Sstevel@tonic-gate 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
50760Sstevel@tonic-gate 	if (!mp1)
50770Sstevel@tonic-gate 		return;
50780Sstevel@tonic-gate 
50790Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
50800Sstevel@tonic-gate 		sin_t *sin;
50810Sstevel@tonic-gate 
50820Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
50830Sstevel@tonic-gate 		sin = (sin_t *)mp1->b_rptr;
50840Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin[1];
50850Sstevel@tonic-gate 		*sin = sin_null;
50860Sstevel@tonic-gate 		sin->sin_family = AF_INET;
50870Sstevel@tonic-gate 		sin->sin_addr.s_addr = v4addr;
50880Sstevel@tonic-gate 	} else {
50890Sstevel@tonic-gate 		/* icmp->icmp_family == AF_INET6 */
50900Sstevel@tonic-gate 		sin6_t *sin6;
50910Sstevel@tonic-gate 
50920Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
50930Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
50940Sstevel@tonic-gate 		sin6 = (sin6_t *)mp1->b_rptr;
50950Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin6[1];
50960Sstevel@tonic-gate 		*sin6 = sin6_null;
50970Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
50980Sstevel@tonic-gate 		sin6->sin6_flowinfo = flowinfo;
50990Sstevel@tonic-gate 		sin6->sin6_addr = v6addr;
51000Sstevel@tonic-gate 	}
51010Sstevel@tonic-gate 	/* Copy out the address */
51020Sstevel@tonic-gate 	mi_copyout(q, mp);
51030Sstevel@tonic-gate }
51040Sstevel@tonic-gate 
51050Sstevel@tonic-gate /*
51060Sstevel@tonic-gate  * Only allow MIB requests and M_FLUSHes to pass.
51070Sstevel@tonic-gate  * All other messages are nacked or dropped.
51080Sstevel@tonic-gate  */
51090Sstevel@tonic-gate static void
51100Sstevel@tonic-gate icmp_wput_restricted(queue_t *q, mblk_t *mp)
51110Sstevel@tonic-gate {
51120Sstevel@tonic-gate 	cred_t	*cr;
51130Sstevel@tonic-gate 	icmp_t	*icmp;
51140Sstevel@tonic-gate 
51150Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
51160Sstevel@tonic-gate 	case M_PROTO:
51170Sstevel@tonic-gate 	case M_PCPROTO:
51180Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
51190Sstevel@tonic-gate 			freemsg(mp);
51200Sstevel@tonic-gate 			return;
51210Sstevel@tonic-gate 		}
51220Sstevel@tonic-gate 		icmp = (icmp_t *)q->q_ptr;
51230Sstevel@tonic-gate 		cr = DB_CREDDEF(mp, icmp->icmp_credp);
51240Sstevel@tonic-gate 
51250Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
51260Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
51270Sstevel@tonic-gate 			if (!snmpcom_req(q, mp,
51280Sstevel@tonic-gate 			    icmp_snmp_set, icmp_snmp_get, cr))
51290Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
51300Sstevel@tonic-gate 				    &icmp_opt_obj);
51310Sstevel@tonic-gate 			return;
51320Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
51330Sstevel@tonic-gate 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
51340Sstevel@tonic-gate 			return;
51350Sstevel@tonic-gate 		default:
51360Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOTSUP);
51370Sstevel@tonic-gate 			return;
51380Sstevel@tonic-gate 		}
51390Sstevel@tonic-gate 		/* NOTREACHED */
51400Sstevel@tonic-gate 	case M_IOCTL:
51410Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOTSUP);
51420Sstevel@tonic-gate 		break;
51430Sstevel@tonic-gate 	case M_FLUSH:
51440Sstevel@tonic-gate 		putnext(q, mp);
51450Sstevel@tonic-gate 		break;
51460Sstevel@tonic-gate 	default:
51470Sstevel@tonic-gate 		freemsg(mp);
51480Sstevel@tonic-gate 		break;
51490Sstevel@tonic-gate 	}
51500Sstevel@tonic-gate }
51510Sstevel@tonic-gate 
51520Sstevel@tonic-gate static int
51530Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
51540Sstevel@tonic-gate     void *thisdg_attrs)
51550Sstevel@tonic-gate {
51560Sstevel@tonic-gate 	icmp_t	*icmp;
51570Sstevel@tonic-gate 	struct T_unitdata_req *udreqp;
51580Sstevel@tonic-gate 	int is_absreq_failure;
51590Sstevel@tonic-gate 	cred_t *cr;
51600Sstevel@tonic-gate 
51610Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
51620Sstevel@tonic-gate 
51630Sstevel@tonic-gate 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
51640Sstevel@tonic-gate 	*errorp = 0;
51650Sstevel@tonic-gate 
51660Sstevel@tonic-gate 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
51670Sstevel@tonic-gate 
51680Sstevel@tonic-gate 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
51690Sstevel@tonic-gate 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
51700Sstevel@tonic-gate 	    thisdg_attrs, &is_absreq_failure);
51710Sstevel@tonic-gate 
51720Sstevel@tonic-gate 	if (*errorp != 0) {
51730Sstevel@tonic-gate 		/*
51740Sstevel@tonic-gate 		 * Note: No special action needed in this
51750Sstevel@tonic-gate 		 * module for "is_absreq_failure"
51760Sstevel@tonic-gate 		 */
51770Sstevel@tonic-gate 		return (-1);		/* failure */
51780Sstevel@tonic-gate 	}
51790Sstevel@tonic-gate 	ASSERT(is_absreq_failure == 0);
51800Sstevel@tonic-gate 	return (0);	/* success */
51810Sstevel@tonic-gate }
51820Sstevel@tonic-gate 
51830Sstevel@tonic-gate void
51840Sstevel@tonic-gate icmp_ddi_init(void)
51850Sstevel@tonic-gate {
51860Sstevel@tonic-gate 	ICMP6_MAJ = ddi_name_to_major(ICMP6);
51870Sstevel@tonic-gate 	icmp_max_optsize =
51880Sstevel@tonic-gate 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
51890Sstevel@tonic-gate 		icmp_opt_obj.odb_opt_arr_cnt);
51900Sstevel@tonic-gate 
51910Sstevel@tonic-gate 	(void) icmp_param_register(icmp_param_arr, A_CNT(icmp_param_arr));
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate 	rawip_kstat_init();
51940Sstevel@tonic-gate }
51950Sstevel@tonic-gate 
51960Sstevel@tonic-gate void
51970Sstevel@tonic-gate icmp_ddi_destroy(void)
51980Sstevel@tonic-gate {
51990Sstevel@tonic-gate 	nd_free(&icmp_g_nd);
52000Sstevel@tonic-gate 
52010Sstevel@tonic-gate 	rawip_kstat_fini();
52020Sstevel@tonic-gate }
52030Sstevel@tonic-gate 
52040Sstevel@tonic-gate static void
52050Sstevel@tonic-gate rawip_kstat_init(void) {
52060Sstevel@tonic-gate 
52070Sstevel@tonic-gate 	rawip_named_kstat_t template = {
52080Sstevel@tonic-gate 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
52090Sstevel@tonic-gate 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
52100Sstevel@tonic-gate 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
52110Sstevel@tonic-gate 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
52120Sstevel@tonic-gate 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
52130Sstevel@tonic-gate 	};
52140Sstevel@tonic-gate 
52150Sstevel@tonic-gate 	rawip_mibkp = kstat_create("icmp", 0, "rawip", "mib2",
52160Sstevel@tonic-gate 					KSTAT_TYPE_NAMED,
52170Sstevel@tonic-gate 					NUM_OF_FIELDS(rawip_named_kstat_t),
52180Sstevel@tonic-gate 					0);
52190Sstevel@tonic-gate 	if (rawip_mibkp == NULL)
52200Sstevel@tonic-gate 		return;
52210Sstevel@tonic-gate 
52220Sstevel@tonic-gate 	bcopy(&template, rawip_mibkp->ks_data, sizeof (template));
52230Sstevel@tonic-gate 
52240Sstevel@tonic-gate 	rawip_mibkp->ks_update = rawip_kstat_update;
52250Sstevel@tonic-gate 
52260Sstevel@tonic-gate 	kstat_install(rawip_mibkp);
52270Sstevel@tonic-gate }
52280Sstevel@tonic-gate 
52290Sstevel@tonic-gate static void
52300Sstevel@tonic-gate rawip_kstat_fini(void) {
52310Sstevel@tonic-gate 	if (rawip_mibkp) {
52320Sstevel@tonic-gate 		kstat_delete(rawip_mibkp);
52330Sstevel@tonic-gate 		rawip_mibkp = NULL;
52340Sstevel@tonic-gate 	}
52350Sstevel@tonic-gate }
52360Sstevel@tonic-gate 
52370Sstevel@tonic-gate static int
52380Sstevel@tonic-gate rawip_kstat_update(kstat_t *kp, int rw) {
52390Sstevel@tonic-gate 	rawip_named_kstat_t *rawipkp;
52400Sstevel@tonic-gate 
52410Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
52420Sstevel@tonic-gate 		return (EIO);
52430Sstevel@tonic-gate 
52440Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
52450Sstevel@tonic-gate 		return (EACCES);
52460Sstevel@tonic-gate 
52470Sstevel@tonic-gate 	rawipkp = (rawip_named_kstat_t *)kp->ks_data;
52480Sstevel@tonic-gate 
52490Sstevel@tonic-gate 	rawipkp->inDatagrams.value.ui32 =	rawip_mib.rawipInDatagrams;
52500Sstevel@tonic-gate 	rawipkp->inCksumErrs.value.ui32 =	rawip_mib.rawipInCksumErrs;
52510Sstevel@tonic-gate 	rawipkp->inErrors.value.ui32 =		rawip_mib.rawipInErrors;
52520Sstevel@tonic-gate 	rawipkp->outDatagrams.value.ui32 =	rawip_mib.rawipOutDatagrams;
52530Sstevel@tonic-gate 	rawipkp->outErrors.value.ui32 =		rawip_mib.rawipOutErrors;
52540Sstevel@tonic-gate 
52550Sstevel@tonic-gate 	return (0);
52560Sstevel@tonic-gate }
5257