xref: /onnv-gate/usr/src/uts/common/inet/ip/icmp.c (revision 1676:37f4a3e2bd99)
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>
40*1676Sjpk #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>
45*1676Sjpk #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 
76*1676Sjpk #include <sys/tsol/label.h>
77*1676Sjpk #include <sys/tsol/tnet.h>
78*1676Sjpk 
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 
619*1676Sjpk 	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 
629*1676Sjpk 	/* tell IP that if we're not here, he can't trust labels */
630*1676Sjpk 	if (is_system_labeled())
631*1676Sjpk 		putnext(WR(q), icmp->icmp_delabel);
632*1676Sjpk 
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 	}
649*1676Sjpk 
650*1676Sjpk 	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 
1285*1676Sjpk /* ARGSUSED */
1286*1676Sjpk static void
1287*1676Sjpk dummy_func(void *arg)
1288*1676Sjpk {
1289*1676Sjpk }
1290*1676Sjpk 
1291*1676Sjpk static mblk_t *
1292*1676Sjpk alloc_wait(queue_t *q, size_t len, int pri, int *errp)
1293*1676Sjpk {
1294*1676Sjpk 	mblk_t *mp;
1295*1676Sjpk 	bufcall_id_t id;
1296*1676Sjpk 	int retv;
1297*1676Sjpk 
1298*1676Sjpk 	while ((mp = allocb(len, pri)) == NULL) {
1299*1676Sjpk 		id = qbufcall(q, len, pri, dummy_func, NULL);
1300*1676Sjpk 		if (id == 0) {
1301*1676Sjpk 			*errp = ENOMEM;
1302*1676Sjpk 			break;
1303*1676Sjpk 		}
1304*1676Sjpk 		retv = qwait_sig(q);
1305*1676Sjpk 		qunbufcall(q, id);
1306*1676Sjpk 		if (retv == 0) {
1307*1676Sjpk 			*errp = EINTR;
1308*1676Sjpk 			break;
1309*1676Sjpk 		}
1310*1676Sjpk 	}
1311*1676Sjpk 	if (mp != NULL)
1312*1676Sjpk 		mp->b_wptr += len;
1313*1676Sjpk 	return (mp);
1314*1676Sjpk }
1315*1676Sjpk 
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;
1325*1676Sjpk 	mblk_t	*mp;
1326*1676Sjpk 	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);
1349*1676Sjpk 	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 
1368*1676Sjpk 	/*
1369*1676Sjpk 	 * If the caller has the process-wide flag set, then default to MAC
1370*1676Sjpk 	 * exempt mode.  This allows read-down to unlabeled hosts.
1371*1676Sjpk 	 */
1372*1676Sjpk 	if (getpflags(NET_MAC_AWARE, credp) != 0)
1373*1676Sjpk 		icmp->icmp_mac_exempt = B_TRUE;
1374*1676Sjpk 
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 */
1416*1676Sjpk 		err = icmp_build_hdrs(q, icmp);
1417*1676Sjpk 		if (err != 0)
1418*1676Sjpk 			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 
1424*1676Sjpk 	if (is_system_labeled()) {
1425*1676Sjpk 		/* notify IP that we know about labeling */
1426*1676Sjpk 		mp = alloc_wait(q, sizeof (*olp), BPRI_MED, &err);
1427*1676Sjpk 		if (mp == NULL)
1428*1676Sjpk 			goto open_error;
1429*1676Sjpk 		mp->b_datap->db_type = M_CTL;
1430*1676Sjpk 		olp = (out_labeled_t *)mp->b_rptr;
1431*1676Sjpk 		olp->out_labeled_type = IP_ULP_OUT_LABELED;
1432*1676Sjpk 		olp->out_qnext = WR(q)->q_next;
1433*1676Sjpk 		putnext(WR(q), mp);
1434*1676Sjpk 
1435*1676Sjpk 		/* save off a copy for closing */
1436*1676Sjpk 		mp = alloc_wait(q, sizeof (*olp), BPRI_MED, &err);
1437*1676Sjpk 		if (mp == NULL)
1438*1676Sjpk 			goto open_error;
1439*1676Sjpk 		mp->b_datap->db_type = M_CTL;
1440*1676Sjpk 		olp = (out_labeled_t *)mp->b_rptr;
1441*1676Sjpk 		olp->out_labeled_type = IP_ULP_OUT_LABELED;
1442*1676Sjpk 		olp->out_qnext = NULL;
1443*1676Sjpk 		icmp->icmp_delabel = mp;
1444*1676Sjpk 	}
1445*1676Sjpk 
14460Sstevel@tonic-gate 	return (0);
1447*1676Sjpk 
1448*1676Sjpk open_error:
1449*1676Sjpk 	qprocsoff(q);
1450*1676Sjpk 	crfree(credp);
1451*1676Sjpk 	(void) mi_close_comm(&icmp_g_head, q);
1452*1676Sjpk 	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;
1566*1676Sjpk 		case SO_MAC_EXEMPT:
1567*1676Sjpk 			*i1 = icmp->icmp_mac_exempt;
1568*1676Sjpk 			break;
15690Sstevel@tonic-gate 		/*
15700Sstevel@tonic-gate 		 * Following three 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:
15760Sstevel@tonic-gate 		 */
15770Sstevel@tonic-gate 		default:
15780Sstevel@tonic-gate 			return (-1);
15790Sstevel@tonic-gate 		}
15800Sstevel@tonic-gate 		break;
15810Sstevel@tonic-gate 	case IPPROTO_IP:
15820Sstevel@tonic-gate 		/*
15830Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
15840Sstevel@tonic-gate 		 */
15850Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET)
15860Sstevel@tonic-gate 			return (-1);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 		switch (name) {
15890Sstevel@tonic-gate 		case IP_OPTIONS:
15900Sstevel@tonic-gate 		case T_IP_OPTIONS:
15910Sstevel@tonic-gate 			/* Options are passed up with each packet */
15920Sstevel@tonic-gate 			return (0);
15930Sstevel@tonic-gate 		case IP_HDRINCL:
15940Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_hdrincl;
15950Sstevel@tonic-gate 			break;
15960Sstevel@tonic-gate 		case IP_TOS:
15970Sstevel@tonic-gate 		case T_IP_TOS:
15980Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_type_of_service;
15990Sstevel@tonic-gate 			break;
16000Sstevel@tonic-gate 		case IP_TTL:
16010Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_ttl;
16020Sstevel@tonic-gate 			break;
16030Sstevel@tonic-gate 		case IP_MULTICAST_IF:
16040Sstevel@tonic-gate 			/* 0 address if not set */
16050Sstevel@tonic-gate 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
16060Sstevel@tonic-gate 			return (sizeof (ipaddr_t));
16070Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
16080Sstevel@tonic-gate 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
16090Sstevel@tonic-gate 			return (sizeof (uchar_t));
16100Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
16110Sstevel@tonic-gate 			*ptr = icmp->icmp_multicast_loop;
16120Sstevel@tonic-gate 			return (sizeof (uint8_t));
16130Sstevel@tonic-gate 		case IP_BOUND_IF:
16140Sstevel@tonic-gate 			/* Zero if not set */
16150Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
16160Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16170Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
16180Sstevel@tonic-gate 			*ptr = icmp->icmp_unspec_source;
16190Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16200Sstevel@tonic-gate 		case IP_XMIT_IF:
16210Sstevel@tonic-gate 			*i1 = icmp->icmp_xmit_if;
16220Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16230Sstevel@tonic-gate 		case IP_RECVIF:
16240Sstevel@tonic-gate 			*ptr = icmp->icmp_recvif;
16250Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16260Sstevel@tonic-gate 		/*
16270Sstevel@tonic-gate 		 * Cannot "get" the value of following options
16280Sstevel@tonic-gate 		 * at this level. Action is same as "default" to
16290Sstevel@tonic-gate 		 * which we fallthrough so we keep them in comments.
16300Sstevel@tonic-gate 		 *
16310Sstevel@tonic-gate 		 * case IP_ADD_MEMBERSHIP:
16320Sstevel@tonic-gate 		 * case IP_DROP_MEMBERSHIP:
16330Sstevel@tonic-gate 		 * case IP_BLOCK_SOURCE:
16340Sstevel@tonic-gate 		 * case IP_UNBLOCK_SOURCE:
16350Sstevel@tonic-gate 		 * case IP_ADD_SOURCE_MEMBERSHIP:
16360Sstevel@tonic-gate 		 * case IP_DROP_SOURCE_MEMBERSHIP:
16370Sstevel@tonic-gate 		 * case MCAST_JOIN_GROUP:
16380Sstevel@tonic-gate 		 * case MCAST_LEAVE_GROUP:
16390Sstevel@tonic-gate 		 * case MCAST_BLOCK_SOURCE:
16400Sstevel@tonic-gate 		 * case MCAST_UNBLOCK_SOURCE:
16410Sstevel@tonic-gate 		 * case MCAST_JOIN_SOURCE_GROUP:
16420Sstevel@tonic-gate 		 * case MCAST_LEAVE_SOURCE_GROUP:
16430Sstevel@tonic-gate 		 * case MRT_INIT:
16440Sstevel@tonic-gate 		 * case MRT_DONE:
16450Sstevel@tonic-gate 		 * case MRT_ADD_VIF:
16460Sstevel@tonic-gate 		 * case MRT_DEL_VIF:
16470Sstevel@tonic-gate 		 * case MRT_ADD_MFC:
16480Sstevel@tonic-gate 		 * case MRT_DEL_MFC:
16490Sstevel@tonic-gate 		 * case MRT_VERSION:
16500Sstevel@tonic-gate 		 * case MRT_ASSERT:
16510Sstevel@tonic-gate 		 * case IP_SEC_OPT:
16520Sstevel@tonic-gate 		 * case IP_DONTFAILOVER_IF:
16531663Spriyanka 		 * case IP_NEXTHOP:
16540Sstevel@tonic-gate 		 */
16550Sstevel@tonic-gate 		default:
16560Sstevel@tonic-gate 			return (-1);
16570Sstevel@tonic-gate 		}
16580Sstevel@tonic-gate 		break;
16590Sstevel@tonic-gate 	case IPPROTO_IPV6:
16600Sstevel@tonic-gate 		/*
16610Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
16620Sstevel@tonic-gate 		 */
16630Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
16640Sstevel@tonic-gate 			return (-1);
16650Sstevel@tonic-gate 		switch (name) {
16660Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
16670Sstevel@tonic-gate 			*i1 = (unsigned int)icmp->icmp_ttl;
16680Sstevel@tonic-gate 			break;
16690Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
16700Sstevel@tonic-gate 			/* 0 index if not set */
16710Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_if_index;
16720Sstevel@tonic-gate 			break;
16730Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
16740Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_ttl;
16750Sstevel@tonic-gate 			break;
16760Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
16770Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_loop;
16780Sstevel@tonic-gate 			break;
16790Sstevel@tonic-gate 		case IPV6_BOUND_IF:
16800Sstevel@tonic-gate 			/* Zero if not set */
16810Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
16820Sstevel@tonic-gate 			break;
16830Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
16840Sstevel@tonic-gate 			*i1 = icmp->icmp_unspec_source;
16850Sstevel@tonic-gate 			break;
16860Sstevel@tonic-gate 		case IPV6_CHECKSUM:
16870Sstevel@tonic-gate 			/*
16880Sstevel@tonic-gate 			 * Return offset or -1 if no checksum offset.
16890Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6
16900Sstevel@tonic-gate 			 */
16910Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
16920Sstevel@tonic-gate 				return (-1);
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 			if (icmp->icmp_raw_checksum) {
16950Sstevel@tonic-gate 				*i1 = icmp->icmp_checksum_off;
16960Sstevel@tonic-gate 			} else {
16970Sstevel@tonic-gate 				*i1 = -1;
16980Sstevel@tonic-gate 			}
16990Sstevel@tonic-gate 			break;
17000Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
17010Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
17020Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
17030Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
17040Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
17050Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
17060Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
17070Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
17080Sstevel@tonic-gate 			/* cannot "get" the value for these */
17090Sstevel@tonic-gate 			return (-1);
17100Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
17110Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpktinfo;
17120Sstevel@tonic-gate 			break;
17130Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
17140Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvtclass;
17150Sstevel@tonic-gate 			break;
17160Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
17170Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpathmtu;
17180Sstevel@tonic-gate 			break;
17190Sstevel@tonic-gate 		case IPV6_V6ONLY:
17200Sstevel@tonic-gate 			*i1 = 1;
17210Sstevel@tonic-gate 			break;
17220Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
17230Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhoplimit;
17240Sstevel@tonic-gate 			break;
17250Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
17260Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhopopts;
17270Sstevel@tonic-gate 			break;
17280Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
17290Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvdstopts;
17300Sstevel@tonic-gate 			break;
17310Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
17320Sstevel@tonic-gate 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
17330Sstevel@tonic-gate 			break;
17340Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
17350Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
17360Sstevel@tonic-gate 			break;
17370Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
17380Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrthdr;
17390Sstevel@tonic-gate 			break;
17400Sstevel@tonic-gate 		case IPV6_PKTINFO: {
17410Sstevel@tonic-gate 			/* XXX assumes that caller has room for max size! */
17420Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
17450Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
17460Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
17470Sstevel@tonic-gate 			else
17480Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
17490Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
17500Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
17510Sstevel@tonic-gate 			else
17520Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
17530Sstevel@tonic-gate 			return (sizeof (struct in6_pktinfo));
17540Sstevel@tonic-gate 		}
17550Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
17560Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)ptr;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
17590Sstevel@tonic-gate 				return (0);
17600Sstevel@tonic-gate 			*sin6 = sin6_null;
17610Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
17620Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
17630Sstevel@tonic-gate 			return (sizeof (sin6_t));
17640Sstevel@tonic-gate 		}
17650Sstevel@tonic-gate 		case IPV6_HOPOPTS:
17660Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
17670Sstevel@tonic-gate 				return (0);
1768*1676Sjpk 			if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6)
1769*1676Sjpk 				return (0);
1770*1676Sjpk 			bcopy((char *)ipp->ipp_hopopts +
1771*1676Sjpk 			    icmp->icmp_label_len_v6, ptr,
1772*1676Sjpk 			    ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
1773*1676Sjpk 			if (icmp->icmp_label_len_v6 > 0) {
1774*1676Sjpk 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
1775*1676Sjpk 				ptr[1] = (ipp->ipp_hopoptslen -
1776*1676Sjpk 				    icmp->icmp_label_len_v6 + 7) / 8 - 1;
1777*1676Sjpk 			}
1778*1676Sjpk 			return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
17790Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
17800Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
17810Sstevel@tonic-gate 				return (0);
17820Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
17830Sstevel@tonic-gate 			return (ipp->ipp_rtdstoptslen);
17840Sstevel@tonic-gate 		case IPV6_RTHDR:
17850Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
17860Sstevel@tonic-gate 				return (0);
17870Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
17880Sstevel@tonic-gate 			return (ipp->ipp_rthdrlen);
17890Sstevel@tonic-gate 		case IPV6_DSTOPTS:
17900Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
17910Sstevel@tonic-gate 				return (0);
17920Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
17930Sstevel@tonic-gate 			return (ipp->ipp_dstoptslen);
17940Sstevel@tonic-gate 		case IPV6_PATHMTU:
17950Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
17960Sstevel@tonic-gate 				return (0);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
17990Sstevel@tonic-gate 				(struct ip6_mtuinfo *)ptr));
18000Sstevel@tonic-gate 		case IPV6_TCLASS:
18010Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_TCLASS)
18020Sstevel@tonic-gate 				*i1 = ipp->ipp_tclass;
18030Sstevel@tonic-gate 			else
18040Sstevel@tonic-gate 				*i1 = IPV6_FLOW_TCLASS(
18050Sstevel@tonic-gate 				    IPV6_DEFAULT_VERS_AND_FLOW);
18060Sstevel@tonic-gate 			break;
18070Sstevel@tonic-gate 		default:
18080Sstevel@tonic-gate 			return (-1);
18090Sstevel@tonic-gate 		}
18100Sstevel@tonic-gate 		break;
18110Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
18120Sstevel@tonic-gate 		/*
18130Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
18140Sstevel@tonic-gate 		 */
18150Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
18160Sstevel@tonic-gate 			return (-1);
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
18190Sstevel@tonic-gate 			return (-1);
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 		switch (name) {
18220Sstevel@tonic-gate 		case ICMP6_FILTER:
18230Sstevel@tonic-gate 			if (icmp->icmp_filter == NULL) {
18240Sstevel@tonic-gate 				/* Make it look like "pass all" */
18250Sstevel@tonic-gate 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
18260Sstevel@tonic-gate 			} else {
18270Sstevel@tonic-gate 				(void) bcopy(icmp->icmp_filter, ptr,
18280Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
18290Sstevel@tonic-gate 			}
18300Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
18310Sstevel@tonic-gate 		default:
18320Sstevel@tonic-gate 			return (-1);
18330Sstevel@tonic-gate 		}
18340Sstevel@tonic-gate 	default:
18350Sstevel@tonic-gate 		return (-1);
18360Sstevel@tonic-gate 	}
18370Sstevel@tonic-gate 	return (sizeof (int));
18380Sstevel@tonic-gate }
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate /* This routine sets socket options. */
18410Sstevel@tonic-gate /* ARGSUSED */
18420Sstevel@tonic-gate int
18430Sstevel@tonic-gate icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
18440Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
18450Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
18460Sstevel@tonic-gate {
18470Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
18480Sstevel@tonic-gate 	int	*i1 = (int *)invalp;
18490Sstevel@tonic-gate 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
18500Sstevel@tonic-gate 	boolean_t checkonly;
18510Sstevel@tonic-gate 	int	error;
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	switch (optset_context) {
18540Sstevel@tonic-gate 	case SETFN_OPTCOM_CHECKONLY:
18550Sstevel@tonic-gate 		checkonly = B_TRUE;
18560Sstevel@tonic-gate 		/*
18570Sstevel@tonic-gate 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
18580Sstevel@tonic-gate 		 * inlen != 0 implies value supplied and
18590Sstevel@tonic-gate 		 * 	we have to "pretend" to set it.
18600Sstevel@tonic-gate 		 * inlen == 0 implies that there is no
18610Sstevel@tonic-gate 		 * 	value part in T_CHECK request and just validation
18620Sstevel@tonic-gate 		 * done elsewhere should be enough, we just return here.
18630Sstevel@tonic-gate 		 */
18640Sstevel@tonic-gate 		if (inlen == 0) {
18650Sstevel@tonic-gate 			*outlenp = 0;
18660Sstevel@tonic-gate 			return (0);
18670Sstevel@tonic-gate 		}
18680Sstevel@tonic-gate 		break;
18690Sstevel@tonic-gate 	case SETFN_OPTCOM_NEGOTIATE:
18700Sstevel@tonic-gate 		checkonly = B_FALSE;
18710Sstevel@tonic-gate 		break;
18720Sstevel@tonic-gate 	case SETFN_UD_NEGOTIATE:
18730Sstevel@tonic-gate 	case SETFN_CONN_NEGOTIATE:
18740Sstevel@tonic-gate 		checkonly = B_FALSE;
18750Sstevel@tonic-gate 		/*
18760Sstevel@tonic-gate 		 * Negotiating local and "association-related" options
18770Sstevel@tonic-gate 		 * through T_UNITDATA_REQ.
18780Sstevel@tonic-gate 		 *
18790Sstevel@tonic-gate 		 * Following routine can filter out ones we do not
18800Sstevel@tonic-gate 		 * want to be "set" this way.
18810Sstevel@tonic-gate 		 */
18820Sstevel@tonic-gate 		if (!icmp_opt_allow_udr_set(level, name)) {
18830Sstevel@tonic-gate 			*outlenp = 0;
18840Sstevel@tonic-gate 			return (EINVAL);
18850Sstevel@tonic-gate 		}
18860Sstevel@tonic-gate 		break;
18870Sstevel@tonic-gate 	default:
18880Sstevel@tonic-gate 		/*
18890Sstevel@tonic-gate 		 * We should never get here
18900Sstevel@tonic-gate 		 */
18910Sstevel@tonic-gate 		*outlenp = 0;
18920Sstevel@tonic-gate 		return (EINVAL);
18930Sstevel@tonic-gate 	}
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
18960Sstevel@tonic-gate 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	/*
18990Sstevel@tonic-gate 	 * For fixed length options, no sanity check
19000Sstevel@tonic-gate 	 * of passed in length is done. It is assumed *_optcom_req()
19010Sstevel@tonic-gate 	 * routines do the right thing.
19020Sstevel@tonic-gate 	 */
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	switch (level) {
19050Sstevel@tonic-gate 	case SOL_SOCKET:
19060Sstevel@tonic-gate 		switch (name) {
19070Sstevel@tonic-gate 		case SO_DEBUG:
19080Sstevel@tonic-gate 			if (!checkonly)
19090Sstevel@tonic-gate 				icmp->icmp_debug = onoff;
19100Sstevel@tonic-gate 			break;
19110Sstevel@tonic-gate 		case SO_PROTOTYPE:
19120Sstevel@tonic-gate 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
19130Sstevel@tonic-gate 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
19140Sstevel@tonic-gate 			    secpolicy_net_rawaccess(cr) != 0) {
19150Sstevel@tonic-gate 				*outlenp = 0;
19160Sstevel@tonic-gate 				return (EACCES);
19170Sstevel@tonic-gate 			}
19180Sstevel@tonic-gate 			/* Can't use IPPROTO_RAW with IPv6 */
19190Sstevel@tonic-gate 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
19200Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET6) {
19210Sstevel@tonic-gate 				*outlenp = 0;
19220Sstevel@tonic-gate 				return (EPROTONOSUPPORT);
19230Sstevel@tonic-gate 			}
19240Sstevel@tonic-gate 			if (checkonly) {
19250Sstevel@tonic-gate 				/* T_CHECK case */
19260Sstevel@tonic-gate 				*(int *)outvalp = (*i1 & 0xFF);
19270Sstevel@tonic-gate 				break;
19280Sstevel@tonic-gate 			}
19290Sstevel@tonic-gate 			icmp->icmp_proto = *i1 & 0xFF;
19300Sstevel@tonic-gate 			if ((icmp->icmp_proto == IPPROTO_RAW ||
19310Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_IGMP) &&
19320Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET)
19330Sstevel@tonic-gate 				icmp->icmp_hdrincl = 1;
19340Sstevel@tonic-gate 			else
19350Sstevel@tonic-gate 				icmp->icmp_hdrincl = 0;
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6 &&
19380Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
19390Sstevel@tonic-gate 				/* Set offset for icmp6_cksum */
19400Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
19410Sstevel@tonic-gate 				icmp->icmp_checksum_off = 2;
19420Sstevel@tonic-gate 			}
19430Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_UDP ||
19440Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_TCP ||
19450Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_SCTP) {
19460Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 1;
19470Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields |=
19480Sstevel@tonic-gate 				    IPPF_NO_CKSUM;
19490Sstevel@tonic-gate 			} else {
19500Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 0;
19510Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields &=
19520Sstevel@tonic-gate 				    ~IPPF_NO_CKSUM;
19530Sstevel@tonic-gate 			}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 			if (icmp->icmp_filter != NULL &&
19560Sstevel@tonic-gate 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
19570Sstevel@tonic-gate 				kmem_free(icmp->icmp_filter,
19580Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
19590Sstevel@tonic-gate 				icmp->icmp_filter = NULL;
19600Sstevel@tonic-gate 			}
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 			/* Rebuild the header template */
19630Sstevel@tonic-gate 			error = icmp_build_hdrs(q, icmp);
19640Sstevel@tonic-gate 			if (error != 0) {
19650Sstevel@tonic-gate 				*outlenp = 0;
19660Sstevel@tonic-gate 				return (error);
19670Sstevel@tonic-gate 			}
19680Sstevel@tonic-gate 
1969409Skcpoon 			/*
1970409Skcpoon 			 * For SCTP, we don't use icmp_bind_proto() for
1971409Skcpoon 			 * raw socket binding.  Note that we do not need
1972409Skcpoon 			 * to set *outlenp.
1973409Skcpoon 			 */
1974409Skcpoon 			if (icmp->icmp_proto == IPPROTO_SCTP)
1975409Skcpoon 				return (0);
1976409Skcpoon 
19770Sstevel@tonic-gate 			icmp_bind_proto(q);
19780Sstevel@tonic-gate 			*outlenp = sizeof (int);
19790Sstevel@tonic-gate 			*(int *)outvalp = *i1 & 0xFF;
19800Sstevel@tonic-gate 			return (0);
19810Sstevel@tonic-gate 		case SO_REUSEADDR:
19820Sstevel@tonic-gate 			if (!checkonly)
19830Sstevel@tonic-gate 				icmp->icmp_reuseaddr = onoff;
19840Sstevel@tonic-gate 			break;
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 		/*
19870Sstevel@tonic-gate 		 * The following three items are available here,
19880Sstevel@tonic-gate 		 * but are only meaningful to IP.
19890Sstevel@tonic-gate 		 */
19900Sstevel@tonic-gate 		case SO_DONTROUTE:
19910Sstevel@tonic-gate 			if (!checkonly)
19920Sstevel@tonic-gate 				icmp->icmp_dontroute = onoff;
19930Sstevel@tonic-gate 			break;
19940Sstevel@tonic-gate 		case SO_USELOOPBACK:
19950Sstevel@tonic-gate 			if (!checkonly)
19960Sstevel@tonic-gate 				icmp->icmp_useloopback = onoff;
19970Sstevel@tonic-gate 			break;
19980Sstevel@tonic-gate 		case SO_BROADCAST:
19990Sstevel@tonic-gate 			if (!checkonly)
20000Sstevel@tonic-gate 				icmp->icmp_broadcast = onoff;
20010Sstevel@tonic-gate 			break;
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 		case SO_SNDBUF:
20040Sstevel@tonic-gate 			if (*i1 > icmp_max_buf) {
20050Sstevel@tonic-gate 				*outlenp = 0;
20060Sstevel@tonic-gate 				return (ENOBUFS);
20070Sstevel@tonic-gate 			}
20080Sstevel@tonic-gate 			if (!checkonly) {
20090Sstevel@tonic-gate 				q->q_hiwat = *i1;
20100Sstevel@tonic-gate 				q->q_next->q_hiwat = *i1;
20110Sstevel@tonic-gate 			}
20120Sstevel@tonic-gate 			break;
20130Sstevel@tonic-gate 		case SO_RCVBUF:
20140Sstevel@tonic-gate 			if (*i1 > icmp_max_buf) {
20150Sstevel@tonic-gate 				*outlenp = 0;
20160Sstevel@tonic-gate 				return (ENOBUFS);
20170Sstevel@tonic-gate 			}
20180Sstevel@tonic-gate 			if (!checkonly) {
20190Sstevel@tonic-gate 				RD(q)->q_hiwat = *i1;
20200Sstevel@tonic-gate 				(void) mi_set_sth_hiwat(RD(q), *i1);
20210Sstevel@tonic-gate 			}
20220Sstevel@tonic-gate 			break;
20230Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
20240Sstevel@tonic-gate 			if (!checkonly)
20250Sstevel@tonic-gate 				icmp->icmp_dgram_errind = onoff;
20260Sstevel@tonic-gate 			break;
20271673Sgt145670 		case SO_TIMESTAMP:
20281673Sgt145670 			if (!checkonly) {
20291673Sgt145670 				icmp->icmp_timestamp = onoff;
20301673Sgt145670 			}
20311673Sgt145670 			break;
2032*1676Sjpk 		case SO_MAC_EXEMPT:
2033*1676Sjpk 			if (secpolicy_net_mac_aware(cr) != 0 ||
2034*1676Sjpk 			    icmp->icmp_state != TS_UNBND)
2035*1676Sjpk 				return (EACCES);
2036*1676Sjpk 			if (!checkonly)
2037*1676Sjpk 				icmp->icmp_mac_exempt = onoff;
2038*1676Sjpk 			break;
20390Sstevel@tonic-gate 		/*
20400Sstevel@tonic-gate 		 * Following three not meaningful for icmp
20410Sstevel@tonic-gate 		 * Action is same as "default" so we keep them
20420Sstevel@tonic-gate 		 * in comments.
20430Sstevel@tonic-gate 		 * case SO_LINGER:
20440Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
20450Sstevel@tonic-gate 		 * case SO_OOBINLINE:
20460Sstevel@tonic-gate 		 */
20470Sstevel@tonic-gate 		default:
20480Sstevel@tonic-gate 			*outlenp = 0;
20490Sstevel@tonic-gate 			return (EINVAL);
20500Sstevel@tonic-gate 		}
20510Sstevel@tonic-gate 		break;
20520Sstevel@tonic-gate 	case IPPROTO_IP:
20530Sstevel@tonic-gate 		/*
20540Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
20550Sstevel@tonic-gate 		 */
20560Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET) {
20570Sstevel@tonic-gate 			*outlenp = 0;
20580Sstevel@tonic-gate 			return (ENOPROTOOPT);
20590Sstevel@tonic-gate 		}
20600Sstevel@tonic-gate 		switch (name) {
20610Sstevel@tonic-gate 		case IP_OPTIONS:
20620Sstevel@tonic-gate 		case T_IP_OPTIONS:
20630Sstevel@tonic-gate 			/* Save options for use by IP. */
2064*1676Sjpk 			if ((inlen & 0x3) ||
2065*1676Sjpk 			    inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) {
20660Sstevel@tonic-gate 				*outlenp = 0;
20670Sstevel@tonic-gate 				return (EINVAL);
20680Sstevel@tonic-gate 			}
20690Sstevel@tonic-gate 			if (checkonly)
20700Sstevel@tonic-gate 				break;
20710Sstevel@tonic-gate 
2072*1676Sjpk 			if (!tsol_option_set(&icmp->icmp_ip_snd_options,
2073*1676Sjpk 			    &icmp->icmp_ip_snd_options_len,
2074*1676Sjpk 			    icmp->icmp_label_len, invalp, inlen)) {
2075*1676Sjpk 				*outlenp = 0;
2076*1676Sjpk 				return (ENOMEM);
20770Sstevel@tonic-gate 			}
2078*1676Sjpk 
20790Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
20800Sstevel@tonic-gate 			    icmp->icmp_ip_snd_options_len;
20810Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
20820Sstevel@tonic-gate 						icmp_wroff_extra);
20830Sstevel@tonic-gate 			break;
20840Sstevel@tonic-gate 		case IP_HDRINCL:
20850Sstevel@tonic-gate 			if (!checkonly)
20860Sstevel@tonic-gate 				icmp->icmp_hdrincl = onoff;
20870Sstevel@tonic-gate 			break;
20880Sstevel@tonic-gate 		case IP_TOS:
20890Sstevel@tonic-gate 		case T_IP_TOS:
20900Sstevel@tonic-gate 			if (!checkonly) {
20910Sstevel@tonic-gate 				icmp->icmp_type_of_service = (uint8_t)*i1;
20920Sstevel@tonic-gate 			}
20930Sstevel@tonic-gate 			break;
20940Sstevel@tonic-gate 		case IP_TTL:
20950Sstevel@tonic-gate 			if (!checkonly) {
20960Sstevel@tonic-gate 				icmp->icmp_ttl = (uint8_t)*i1;
20970Sstevel@tonic-gate 			}
20980Sstevel@tonic-gate 			break;
20990Sstevel@tonic-gate 		case IP_MULTICAST_IF:
21000Sstevel@tonic-gate 			/*
21010Sstevel@tonic-gate 			 * TODO should check OPTMGMT reply and undo this if
21020Sstevel@tonic-gate 			 * there is an error.
21030Sstevel@tonic-gate 			 */
21040Sstevel@tonic-gate 			if (!checkonly)
21050Sstevel@tonic-gate 				icmp->icmp_multicast_if_addr = *i1;
21060Sstevel@tonic-gate 			break;
21070Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
21080Sstevel@tonic-gate 			if (!checkonly)
21090Sstevel@tonic-gate 				icmp->icmp_multicast_ttl = *invalp;
21100Sstevel@tonic-gate 			break;
21110Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
21120Sstevel@tonic-gate 			if (!checkonly) {
21130Sstevel@tonic-gate 				icmp->icmp_multicast_loop =
21140Sstevel@tonic-gate 				    (*invalp == 0) ? 0 : 1;
21150Sstevel@tonic-gate 			}
21160Sstevel@tonic-gate 			break;
21170Sstevel@tonic-gate 		case IP_BOUND_IF:
21180Sstevel@tonic-gate 			if (!checkonly)
21190Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
21200Sstevel@tonic-gate 			break;
21210Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
21220Sstevel@tonic-gate 			if (!checkonly)
21230Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
21240Sstevel@tonic-gate 			break;
21250Sstevel@tonic-gate 		case IP_XMIT_IF:
21260Sstevel@tonic-gate 			if (!checkonly)
21270Sstevel@tonic-gate 				icmp->icmp_xmit_if = *i1;
21280Sstevel@tonic-gate 			break;
21290Sstevel@tonic-gate 		case IP_RECVIF:
21300Sstevel@tonic-gate 			if (!checkonly)
21310Sstevel@tonic-gate 				icmp->icmp_recvif = onoff;
21320Sstevel@tonic-gate 			break;
21330Sstevel@tonic-gate 		case IP_ADD_MEMBERSHIP:
21340Sstevel@tonic-gate 		case IP_DROP_MEMBERSHIP:
21350Sstevel@tonic-gate 		case IP_BLOCK_SOURCE:
21360Sstevel@tonic-gate 		case IP_UNBLOCK_SOURCE:
21370Sstevel@tonic-gate 		case IP_ADD_SOURCE_MEMBERSHIP:
21380Sstevel@tonic-gate 		case IP_DROP_SOURCE_MEMBERSHIP:
21390Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
21400Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
21410Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
21420Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
21430Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
21440Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
21450Sstevel@tonic-gate 		case MRT_INIT:
21460Sstevel@tonic-gate 		case MRT_DONE:
21470Sstevel@tonic-gate 		case MRT_ADD_VIF:
21480Sstevel@tonic-gate 		case MRT_DEL_VIF:
21490Sstevel@tonic-gate 		case MRT_ADD_MFC:
21500Sstevel@tonic-gate 		case MRT_DEL_MFC:
21510Sstevel@tonic-gate 		case MRT_VERSION:
21520Sstevel@tonic-gate 		case MRT_ASSERT:
21530Sstevel@tonic-gate 		case IP_SEC_OPT:
21540Sstevel@tonic-gate 		case IP_DONTFAILOVER_IF:
21551663Spriyanka 		case IP_NEXTHOP:
21560Sstevel@tonic-gate 			/*
21570Sstevel@tonic-gate 			 * "soft" error (negative)
21580Sstevel@tonic-gate 			 * option not handled at this level
21590Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
21600Sstevel@tonic-gate 			 */
21610Sstevel@tonic-gate 			return (-EINVAL);
21620Sstevel@tonic-gate 		default:
21630Sstevel@tonic-gate 			*outlenp = 0;
21640Sstevel@tonic-gate 			return (EINVAL);
21650Sstevel@tonic-gate 		}
21660Sstevel@tonic-gate 		break;
21670Sstevel@tonic-gate 	case IPPROTO_IPV6: {
21680Sstevel@tonic-gate 		ip6_pkt_t		*ipp;
21690Sstevel@tonic-gate 		boolean_t		sticky;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
21720Sstevel@tonic-gate 			*outlenp = 0;
21730Sstevel@tonic-gate 			return (ENOPROTOOPT);
21740Sstevel@tonic-gate 		}
21750Sstevel@tonic-gate 		/*
21760Sstevel@tonic-gate 		 * Deal with both sticky options and ancillary data
21770Sstevel@tonic-gate 		 */
21780Sstevel@tonic-gate 		if (thisdg_attrs == NULL) {
21790Sstevel@tonic-gate 			/* sticky options, or none */
21800Sstevel@tonic-gate 			ipp = &icmp->icmp_sticky_ipp;
21810Sstevel@tonic-gate 			sticky = B_TRUE;
21820Sstevel@tonic-gate 		} else {
21830Sstevel@tonic-gate 			/* ancillary data */
21840Sstevel@tonic-gate 			ipp = (ip6_pkt_t *)thisdg_attrs;
21850Sstevel@tonic-gate 			sticky = B_FALSE;
21860Sstevel@tonic-gate 		}
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 		switch (name) {
21890Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
21900Sstevel@tonic-gate 			if (!checkonly)
21910Sstevel@tonic-gate 				icmp->icmp_multicast_if_index = *i1;
21920Sstevel@tonic-gate 			break;
21930Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
21940Sstevel@tonic-gate 			/* -1 means use default */
21950Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
21960Sstevel@tonic-gate 				*outlenp = 0;
21970Sstevel@tonic-gate 				return (EINVAL);
21980Sstevel@tonic-gate 			}
21990Sstevel@tonic-gate 			if (!checkonly) {
22000Sstevel@tonic-gate 				if (*i1 == -1) {
2201679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
22020Sstevel@tonic-gate 					    icmp_ipv6_hoplimit;
2203679Sseb 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
22040Sstevel@tonic-gate 					/* Pass modified value to IP. */
22050Sstevel@tonic-gate 					*i1 = ipp->ipp_hoplimit;
22060Sstevel@tonic-gate 				} else {
2207679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
22080Sstevel@tonic-gate 					    (uint8_t)*i1;
2209679Sseb 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
22100Sstevel@tonic-gate 				}
22110Sstevel@tonic-gate 				/* Rebuild the header template */
22120Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
22130Sstevel@tonic-gate 				if (error != 0) {
22140Sstevel@tonic-gate 					*outlenp = 0;
22150Sstevel@tonic-gate 					return (error);
22160Sstevel@tonic-gate 				}
22170Sstevel@tonic-gate 			}
22180Sstevel@tonic-gate 			break;
22190Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
22200Sstevel@tonic-gate 			/* -1 means use default */
22210Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
22220Sstevel@tonic-gate 				*outlenp = 0;
22230Sstevel@tonic-gate 				return (EINVAL);
22240Sstevel@tonic-gate 			}
22250Sstevel@tonic-gate 			if (!checkonly) {
22260Sstevel@tonic-gate 				if (*i1 == -1) {
22270Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2228679Sseb 					    ipp->ipp_multicast_hops =
22290Sstevel@tonic-gate 					    IP_DEFAULT_MULTICAST_TTL;
2230679Sseb 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
22310Sstevel@tonic-gate 					/* Pass modified value to IP. */
2232679Sseb 					*i1 = icmp->icmp_multicast_ttl;
22330Sstevel@tonic-gate 				} else {
22340Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2235679Sseb 					    ipp->ipp_multicast_hops =
22360Sstevel@tonic-gate 					    (uint8_t)*i1;
2237679Sseb 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
22380Sstevel@tonic-gate 				}
22390Sstevel@tonic-gate 			}
22400Sstevel@tonic-gate 			break;
22410Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
22420Sstevel@tonic-gate 			if (*i1 != 0 && *i1 != 1) {
22430Sstevel@tonic-gate 				*outlenp = 0;
22440Sstevel@tonic-gate 				return (EINVAL);
22450Sstevel@tonic-gate 			}
22460Sstevel@tonic-gate 			if (!checkonly)
22470Sstevel@tonic-gate 				icmp->icmp_multicast_loop = *i1;
22480Sstevel@tonic-gate 			break;
22490Sstevel@tonic-gate 		case IPV6_CHECKSUM:
22500Sstevel@tonic-gate 			/*
22510Sstevel@tonic-gate 			 * Integer offset into the user data of where the
22520Sstevel@tonic-gate 			 * checksum is located.
22530Sstevel@tonic-gate 			 * Offset of -1 disables option.
22540Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6.
22550Sstevel@tonic-gate 			 */
22560Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
22570Sstevel@tonic-gate 				*outlenp = 0;
22580Sstevel@tonic-gate 				return (EINVAL);
22590Sstevel@tonic-gate 			}
22600Sstevel@tonic-gate 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
22610Sstevel@tonic-gate 				/* Negative or not 16 bit aligned offset */
22620Sstevel@tonic-gate 				*outlenp = 0;
22630Sstevel@tonic-gate 				return (EINVAL);
22640Sstevel@tonic-gate 			}
22650Sstevel@tonic-gate 			if (checkonly)
22660Sstevel@tonic-gate 				break;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 			if (*i1 == -1) {
22690Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
22700Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
22710Sstevel@tonic-gate 			} else {
22720Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 1;
22730Sstevel@tonic-gate 				icmp->icmp_checksum_off = *i1;
22740Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
22750Sstevel@tonic-gate 			}
22760Sstevel@tonic-gate 			/* Rebuild the header template */
22770Sstevel@tonic-gate 			error = icmp_build_hdrs(q, icmp);
22780Sstevel@tonic-gate 			if (error != 0) {
22790Sstevel@tonic-gate 				*outlenp = 0;
22800Sstevel@tonic-gate 				return (error);
22810Sstevel@tonic-gate 			}
22820Sstevel@tonic-gate 			break;
22830Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
22840Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
22850Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
22860Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
22870Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
22880Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
22890Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
22900Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
22910Sstevel@tonic-gate 			/*
22920Sstevel@tonic-gate 			 * "soft" error (negative)
22930Sstevel@tonic-gate 			 * option not handled at this level
22940Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
22950Sstevel@tonic-gate 			 */
22960Sstevel@tonic-gate 			return (-EINVAL);
22970Sstevel@tonic-gate 		case IPV6_BOUND_IF:
22980Sstevel@tonic-gate 			if (!checkonly)
22990Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
23000Sstevel@tonic-gate 			break;
23010Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
23020Sstevel@tonic-gate 			if (!checkonly)
23030Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
23040Sstevel@tonic-gate 			break;
23050Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
23060Sstevel@tonic-gate 			if (!checkonly)
23070Sstevel@tonic-gate 				icmp->icmp_ipv6_recvtclass = onoff;
23080Sstevel@tonic-gate 			break;
23090Sstevel@tonic-gate 		/*
23100Sstevel@tonic-gate 		 * Set boolean switches for ancillary data delivery
23110Sstevel@tonic-gate 		 */
23120Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
23130Sstevel@tonic-gate 			if (!checkonly)
23140Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpktinfo = onoff;
23150Sstevel@tonic-gate 			break;
23160Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
23170Sstevel@tonic-gate 			if (!checkonly)
23180Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpathmtu = onoff;
23190Sstevel@tonic-gate 			break;
23200Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
23210Sstevel@tonic-gate 			if (!checkonly)
23220Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhoplimit = onoff;
23230Sstevel@tonic-gate 			break;
23240Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
23250Sstevel@tonic-gate 			if (!checkonly)
23260Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhopopts = onoff;
23270Sstevel@tonic-gate 			break;
23280Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
23290Sstevel@tonic-gate 			if (!checkonly)
23300Sstevel@tonic-gate 				icmp->icmp_ipv6_recvdstopts = onoff;
23310Sstevel@tonic-gate 			break;
23320Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
23330Sstevel@tonic-gate 			if (!checkonly)
23340Sstevel@tonic-gate 				icmp->icmp_old_ipv6_recvdstopts = onoff;
23350Sstevel@tonic-gate 			break;
23360Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
23370Sstevel@tonic-gate 			if (!checkonly)
23380Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrtdstopts = onoff;
23390Sstevel@tonic-gate 			break;
23400Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
23410Sstevel@tonic-gate 			if (!checkonly)
23420Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrthdr = onoff;
23430Sstevel@tonic-gate 			break;
23440Sstevel@tonic-gate 		/*
23450Sstevel@tonic-gate 		 * Set sticky options or ancillary data.
23460Sstevel@tonic-gate 		 * If sticky options, (re)build any extension headers
23470Sstevel@tonic-gate 		 * that might be needed as a result.
23480Sstevel@tonic-gate 		 */
23490Sstevel@tonic-gate 		case IPV6_PKTINFO:
23500Sstevel@tonic-gate 			/*
23510Sstevel@tonic-gate 			 * The source address and ifindex are verified
23520Sstevel@tonic-gate 			 * in ip_opt_set(). For ancillary data the
23530Sstevel@tonic-gate 			 * source address is checked in ip_wput_v6.
23540Sstevel@tonic-gate 			 */
23550Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
23560Sstevel@tonic-gate 				return (EINVAL);
23570Sstevel@tonic-gate 			if (checkonly)
23580Sstevel@tonic-gate 				break;
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 			if (inlen == 0) {
23610Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
23620Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |=
23630Sstevel@tonic-gate 				    (IPPF_IFINDEX|IPPF_ADDR);
23640Sstevel@tonic-gate 			} else {
23650Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
23680Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
23690Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
23700Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
23710Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
23720Sstevel@tonic-gate 				else
23730Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
23740Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
23750Sstevel@tonic-gate 				    &ipp->ipp_addr))
23760Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
23770Sstevel@tonic-gate 				else
23780Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
23790Sstevel@tonic-gate 			}
23800Sstevel@tonic-gate 			if (sticky) {
23810Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
23820Sstevel@tonic-gate 				if (error != 0)
23830Sstevel@tonic-gate 					return (error);
23840Sstevel@tonic-gate 			}
23850Sstevel@tonic-gate 			break;
23860Sstevel@tonic-gate 		case IPV6_HOPLIMIT:
2387679Sseb 			/* This option can only be used as ancillary data. */
2388679Sseb 			if (sticky)
2389679Sseb 				return (EINVAL);
23900Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
23910Sstevel@tonic-gate 				return (EINVAL);
23920Sstevel@tonic-gate 			if (checkonly)
23930Sstevel@tonic-gate 				break;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 			if (inlen == 0) {
23960Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
23970Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
23980Sstevel@tonic-gate 			} else {
23990Sstevel@tonic-gate 				if (*i1 > 255 || *i1 < -1)
24000Sstevel@tonic-gate 					return (EINVAL);
24010Sstevel@tonic-gate 				if (*i1 == -1)
24020Sstevel@tonic-gate 					ipp->ipp_hoplimit = icmp_ipv6_hoplimit;
24030Sstevel@tonic-gate 				else
24040Sstevel@tonic-gate 					ipp->ipp_hoplimit = *i1;
24050Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPLIMIT;
24060Sstevel@tonic-gate 			}
24070Sstevel@tonic-gate 			break;
24080Sstevel@tonic-gate 		case IPV6_TCLASS:
24090Sstevel@tonic-gate 			/*
24100Sstevel@tonic-gate 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
24110Sstevel@tonic-gate 			 * and [0, 255] as the actualy traffic class.
24120Sstevel@tonic-gate 			 */
24130Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
24140Sstevel@tonic-gate 				return (EINVAL);
24150Sstevel@tonic-gate 			if (checkonly)
24160Sstevel@tonic-gate 				break;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 			if (inlen == 0) {
24190Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_TCLASS;
24200Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
24210Sstevel@tonic-gate 			} else {
24220Sstevel@tonic-gate 				if (*i1 >= 256 || *i1 < -1)
24230Sstevel@tonic-gate 					return (EINVAL);
24240Sstevel@tonic-gate 				if (*i1 == -1) {
24250Sstevel@tonic-gate 					ipp->ipp_tclass =
24260Sstevel@tonic-gate 					    IPV6_FLOW_TCLASS(
24270Sstevel@tonic-gate 					    IPV6_DEFAULT_VERS_AND_FLOW);
24280Sstevel@tonic-gate 				} else {
24290Sstevel@tonic-gate 					ipp->ipp_tclass = *i1;
24300Sstevel@tonic-gate 				}
24310Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_TCLASS;
24320Sstevel@tonic-gate 			}
24330Sstevel@tonic-gate 			if (sticky) {
24340Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
24350Sstevel@tonic-gate 				if (error != 0)
24360Sstevel@tonic-gate 					return (error);
24370Sstevel@tonic-gate 			}
24380Sstevel@tonic-gate 			break;
24390Sstevel@tonic-gate 		case IPV6_NEXTHOP:
24400Sstevel@tonic-gate 			/*
24410Sstevel@tonic-gate 			 * IP will verify that the nexthop is reachable
24420Sstevel@tonic-gate 			 * and fail for sticky options.
24430Sstevel@tonic-gate 			 */
24440Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t))
24450Sstevel@tonic-gate 				return (EINVAL);
24460Sstevel@tonic-gate 			if (checkonly)
24470Sstevel@tonic-gate 				break;
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate 			if (inlen == 0) {
24500Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
24510Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
24520Sstevel@tonic-gate 			} else {
24530Sstevel@tonic-gate 				sin6_t *sin6 = (sin6_t *)invalp;
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6)
24560Sstevel@tonic-gate 					return (EAFNOSUPPORT);
24570Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
24580Sstevel@tonic-gate 					return (EADDRNOTAVAIL);
24590Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
24600Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
24610Sstevel@tonic-gate 				    &ipp->ipp_nexthop))
24620Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
24630Sstevel@tonic-gate 				else
24640Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
24650Sstevel@tonic-gate 			}
24660Sstevel@tonic-gate 			if (sticky) {
24670Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
24680Sstevel@tonic-gate 				if (error != 0)
24690Sstevel@tonic-gate 					return (error);
24700Sstevel@tonic-gate 			}
24710Sstevel@tonic-gate 			break;
24720Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
24730Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
24740Sstevel@tonic-gate 			/*
24750Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
24760Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
24770Sstevel@tonic-gate 			 */
24780Sstevel@tonic-gate 			if (inlen != 0 &&
24790Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1)))
24800Sstevel@tonic-gate 				return (EINVAL);
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 			if (checkonly)
24830Sstevel@tonic-gate 				break;
2484*1676Sjpk 			error = optcom_pkt_set(invalp, inlen, sticky,
2485*1676Sjpk 			    (uchar_t **)&ipp->ipp_hopopts,
2486*1676Sjpk 			    &ipp->ipp_hopoptslen,
2487*1676Sjpk 			    sticky ? icmp->icmp_label_len_v6 : 0);
2488*1676Sjpk 			if (error != 0)
2489*1676Sjpk 				return (error);
2490*1676Sjpk 			if (ipp->ipp_hopoptslen == 0) {
24910Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
24920Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
24930Sstevel@tonic-gate 			} else {
24940Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
24950Sstevel@tonic-gate 			}
24960Sstevel@tonic-gate 			if (sticky) {
24970Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
24980Sstevel@tonic-gate 				if (error != 0)
24990Sstevel@tonic-gate 					return (error);
25000Sstevel@tonic-gate 			}
25010Sstevel@tonic-gate 			break;
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
25040Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 			/*
25070Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25080Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25090Sstevel@tonic-gate 			 */
25100Sstevel@tonic-gate 			if (inlen != 0 &&
25110Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
25120Sstevel@tonic-gate 				return (EINVAL);
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 			if (checkonly)
25150Sstevel@tonic-gate 				break;
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 			if (inlen == 0) {
25180Sstevel@tonic-gate 				if (sticky &&
25190Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
25200Sstevel@tonic-gate 					kmem_free(ipp->ipp_rtdstopts,
25210Sstevel@tonic-gate 					    ipp->ipp_rtdstoptslen);
25220Sstevel@tonic-gate 					ipp->ipp_rtdstopts = NULL;
25230Sstevel@tonic-gate 					ipp->ipp_rtdstoptslen = 0;
25240Sstevel@tonic-gate 				}
25250Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
25260Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
25270Sstevel@tonic-gate 			} else {
2528*1676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
25290Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rtdstopts,
2530*1676Sjpk 				    &ipp->ipp_rtdstoptslen, 0);
25310Sstevel@tonic-gate 				if (error != 0)
25320Sstevel@tonic-gate 					return (error);
25330Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
25340Sstevel@tonic-gate 			}
25350Sstevel@tonic-gate 			if (sticky) {
25360Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25370Sstevel@tonic-gate 				if (error != 0)
25380Sstevel@tonic-gate 					return (error);
25390Sstevel@tonic-gate 			}
25400Sstevel@tonic-gate 			break;
25410Sstevel@tonic-gate 		}
25420Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
25430Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 			/*
25460Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25470Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25480Sstevel@tonic-gate 			 */
25490Sstevel@tonic-gate 			if (inlen != 0 &&
25500Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
25510Sstevel@tonic-gate 				return (EINVAL);
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 			if (checkonly)
25540Sstevel@tonic-gate 				break;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 			if (inlen == 0) {
25570Sstevel@tonic-gate 				if (sticky &&
25580Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
25590Sstevel@tonic-gate 					kmem_free(ipp->ipp_dstopts,
25600Sstevel@tonic-gate 					    ipp->ipp_dstoptslen);
25610Sstevel@tonic-gate 					ipp->ipp_dstopts = NULL;
25620Sstevel@tonic-gate 					ipp->ipp_dstoptslen = 0;
25630Sstevel@tonic-gate 				}
25640Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
25650Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
25660Sstevel@tonic-gate 			} else {
2567*1676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
25680Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_dstopts,
2569*1676Sjpk 				    &ipp->ipp_dstoptslen, 0);
25700Sstevel@tonic-gate 				if (error != 0)
25710Sstevel@tonic-gate 					return (error);
25720Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
25730Sstevel@tonic-gate 			}
25740Sstevel@tonic-gate 			if (sticky) {
25750Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25760Sstevel@tonic-gate 				if (error != 0)
25770Sstevel@tonic-gate 					return (error);
25780Sstevel@tonic-gate 			}
25790Sstevel@tonic-gate 			break;
25800Sstevel@tonic-gate 		}
25810Sstevel@tonic-gate 		case IPV6_RTHDR: {
25820Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 			/*
25850Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25860Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25870Sstevel@tonic-gate 			 */
25880Sstevel@tonic-gate 			if (inlen != 0 &&
25890Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1)))
25900Sstevel@tonic-gate 				return (EINVAL);
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 			if (checkonly)
25930Sstevel@tonic-gate 				break;
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 			if (inlen == 0) {
25960Sstevel@tonic-gate 				if (sticky &&
25970Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
25980Sstevel@tonic-gate 					kmem_free(ipp->ipp_rthdr,
25990Sstevel@tonic-gate 					    ipp->ipp_rthdrlen);
26000Sstevel@tonic-gate 					ipp->ipp_rthdr = NULL;
26010Sstevel@tonic-gate 					ipp->ipp_rthdrlen = 0;
26020Sstevel@tonic-gate 				}
26030Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
26040Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
26050Sstevel@tonic-gate 			} else {
2606*1676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
26070Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rthdr,
2608*1676Sjpk 				    &ipp->ipp_rthdrlen, 0);
26090Sstevel@tonic-gate 				if (error != 0)
26100Sstevel@tonic-gate 					return (error);
26110Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
26120Sstevel@tonic-gate 			}
26130Sstevel@tonic-gate 			if (sticky) {
26140Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
26150Sstevel@tonic-gate 				if (error != 0)
26160Sstevel@tonic-gate 					return (error);
26170Sstevel@tonic-gate 			}
26180Sstevel@tonic-gate 			break;
26190Sstevel@tonic-gate 		}
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 		case IPV6_DONTFRAG:
26220Sstevel@tonic-gate 			if (checkonly)
26230Sstevel@tonic-gate 				break;
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 			if (onoff) {
26260Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DONTFRAG;
26270Sstevel@tonic-gate 			} else {
26280Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
26290Sstevel@tonic-gate 			}
26300Sstevel@tonic-gate 			break;
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 		case IPV6_USE_MIN_MTU:
26330Sstevel@tonic-gate 			if (inlen != sizeof (int))
26340Sstevel@tonic-gate 				return (EINVAL);
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > 1)
26370Sstevel@tonic-gate 				return (EINVAL);
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 			if (checkonly)
26400Sstevel@tonic-gate 				break;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
26430Sstevel@tonic-gate 			ipp->ipp_use_min_mtu = *i1;
26440Sstevel@tonic-gate 			break;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 		/*
26470Sstevel@tonic-gate 		 * This option can't be set.  Its only returned via
26480Sstevel@tonic-gate 		 * getsockopt() or ancillary data.
26490Sstevel@tonic-gate 		 */
26500Sstevel@tonic-gate 		case IPV6_PATHMTU:
26510Sstevel@tonic-gate 			return (EINVAL);
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 		case IPV6_BOUND_PIF:
26540Sstevel@tonic-gate 		case IPV6_SEC_OPT:
26550Sstevel@tonic-gate 		case IPV6_DONTFAILOVER_IF:
26560Sstevel@tonic-gate 		case IPV6_SRC_PREFERENCES:
26570Sstevel@tonic-gate 		case IPV6_V6ONLY:
26580Sstevel@tonic-gate 			/* Handled at IP level */
26590Sstevel@tonic-gate 			return (-EINVAL);
26600Sstevel@tonic-gate 		default:
26610Sstevel@tonic-gate 			*outlenp = 0;
26620Sstevel@tonic-gate 			return (EINVAL);
26630Sstevel@tonic-gate 		}
26640Sstevel@tonic-gate 		break;
26650Sstevel@tonic-gate 	}		/* end IPPROTO_IPV6 */
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
26680Sstevel@tonic-gate 		/*
26690Sstevel@tonic-gate 		 * Only allow IPv6 option processing on IPv6 sockets.
26700Sstevel@tonic-gate 		 */
26710Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
26720Sstevel@tonic-gate 			*outlenp = 0;
26730Sstevel@tonic-gate 			return (ENOPROTOOPT);
26740Sstevel@tonic-gate 		}
26750Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
26760Sstevel@tonic-gate 			*outlenp = 0;
26770Sstevel@tonic-gate 			return (ENOPROTOOPT);
26780Sstevel@tonic-gate 		}
26790Sstevel@tonic-gate 		switch (name) {
26800Sstevel@tonic-gate 		case ICMP6_FILTER:
26810Sstevel@tonic-gate 			if (!checkonly) {
26820Sstevel@tonic-gate 				if ((inlen != 0) &&
26830Sstevel@tonic-gate 				    (inlen != sizeof (icmp6_filter_t)))
26840Sstevel@tonic-gate 					return (EINVAL);
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 				if (inlen == 0) {
26870Sstevel@tonic-gate 					if (icmp->icmp_filter != NULL) {
26880Sstevel@tonic-gate 						kmem_free(icmp->icmp_filter,
26890Sstevel@tonic-gate 						    sizeof (icmp6_filter_t));
26900Sstevel@tonic-gate 						icmp->icmp_filter = NULL;
26910Sstevel@tonic-gate 					}
26920Sstevel@tonic-gate 				} else {
26930Sstevel@tonic-gate 					if (icmp->icmp_filter == NULL) {
26940Sstevel@tonic-gate 						icmp->icmp_filter = kmem_alloc(
26950Sstevel@tonic-gate 						    sizeof (icmp6_filter_t),
26960Sstevel@tonic-gate 						    KM_NOSLEEP);
26970Sstevel@tonic-gate 						if (icmp->icmp_filter == NULL) {
26980Sstevel@tonic-gate 							*outlenp = 0;
26990Sstevel@tonic-gate 							return (ENOBUFS);
27000Sstevel@tonic-gate 						}
27010Sstevel@tonic-gate 					}
27020Sstevel@tonic-gate 					(void) bcopy(invalp, icmp->icmp_filter,
27030Sstevel@tonic-gate 					    inlen);
27040Sstevel@tonic-gate 				}
27050Sstevel@tonic-gate 			}
27060Sstevel@tonic-gate 			break;
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 		default:
27090Sstevel@tonic-gate 			*outlenp = 0;
27100Sstevel@tonic-gate 			return (EINVAL);
27110Sstevel@tonic-gate 		}
27120Sstevel@tonic-gate 		break;
27130Sstevel@tonic-gate 	default:
27140Sstevel@tonic-gate 		*outlenp = 0;
27150Sstevel@tonic-gate 		return (EINVAL);
27160Sstevel@tonic-gate 	}
27170Sstevel@tonic-gate 	/*
27180Sstevel@tonic-gate 	 * Common case of OK return with outval same as inval.
27190Sstevel@tonic-gate 	 */
27200Sstevel@tonic-gate 	if (invalp != outvalp) {
27210Sstevel@tonic-gate 		/* don't trust bcopy for identical src/dst */
27220Sstevel@tonic-gate 		(void) bcopy(invalp, outvalp, inlen);
27230Sstevel@tonic-gate 	}
27240Sstevel@tonic-gate 	*outlenp = inlen;
27250Sstevel@tonic-gate 	return (0);
27260Sstevel@tonic-gate }
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate /*
27290Sstevel@tonic-gate  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
27300Sstevel@tonic-gate  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
27310Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
27320Sstevel@tonic-gate  * headers.
27330Sstevel@tonic-gate  * Returns failure if can't allocate memory.
27340Sstevel@tonic-gate  */
27350Sstevel@tonic-gate static int
27360Sstevel@tonic-gate icmp_build_hdrs(queue_t *q, icmp_t *icmp)
27370Sstevel@tonic-gate {
27380Sstevel@tonic-gate 	uchar_t	*hdrs;
27390Sstevel@tonic-gate 	uint_t	hdrs_len;
27400Sstevel@tonic-gate 	ip6_t	*ip6h;
27410Sstevel@tonic-gate 	ip6i_t	*ip6i;
27420Sstevel@tonic-gate 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp);
27450Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
27460Sstevel@tonic-gate 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
27470Sstevel@tonic-gate 		/* Need to reallocate */
27480Sstevel@tonic-gate 		if (hdrs_len != 0) {
27490Sstevel@tonic-gate 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
27500Sstevel@tonic-gate 			if (hdrs == NULL)
27510Sstevel@tonic-gate 				return (ENOMEM);
27520Sstevel@tonic-gate 		} else {
27530Sstevel@tonic-gate 			hdrs = NULL;
27540Sstevel@tonic-gate 		}
27550Sstevel@tonic-gate 		if (icmp->icmp_sticky_hdrs_len != 0) {
27560Sstevel@tonic-gate 			kmem_free(icmp->icmp_sticky_hdrs,
27570Sstevel@tonic-gate 			    icmp->icmp_sticky_hdrs_len);
27580Sstevel@tonic-gate 		}
27590Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = hdrs;
27600Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = hdrs_len;
27610Sstevel@tonic-gate 	}
27620Sstevel@tonic-gate 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
27630Sstevel@tonic-gate 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	/* Set header fields not in ipp */
27660Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
27670Sstevel@tonic-gate 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
27680Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
27710Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
27720Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
27730Sstevel@tonic-gate 		}
27740Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
27750Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
27760Sstevel@tonic-gate 		}
27770Sstevel@tonic-gate 	} else {
27780Sstevel@tonic-gate 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
27790Sstevel@tonic-gate 	}
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_ADDR))
27820Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	/* Try to get everything in a single mblk */
27850Sstevel@tonic-gate 	if (hdrs_len > icmp->icmp_max_hdr_len) {
27860Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = hdrs_len;
27870Sstevel@tonic-gate 		(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
27880Sstevel@tonic-gate 		    icmp_wroff_extra);
27890Sstevel@tonic-gate 	}
27900Sstevel@tonic-gate 	return (0);
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate /*
27940Sstevel@tonic-gate  * This routine retrieves the value of an ND variable in a icmpparam_t
27950Sstevel@tonic-gate  * structure.  It is called through nd_getset when a user reads the
27960Sstevel@tonic-gate  * variable.
27970Sstevel@tonic-gate  */
27980Sstevel@tonic-gate /* ARGSUSED */
27990Sstevel@tonic-gate static int
28000Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
28010Sstevel@tonic-gate {
28020Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
28050Sstevel@tonic-gate 	return (0);
28060Sstevel@tonic-gate }
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate /*
28090Sstevel@tonic-gate  * Walk through the param array specified registering each element with the
28100Sstevel@tonic-gate  * named dispatch (ND) handler.
28110Sstevel@tonic-gate  */
28120Sstevel@tonic-gate static boolean_t
28130Sstevel@tonic-gate icmp_param_register(icmpparam_t *icmppa, int cnt)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate 	for (; cnt-- > 0; icmppa++) {
28160Sstevel@tonic-gate 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
28170Sstevel@tonic-gate 			if (!nd_load(&icmp_g_nd, icmppa->icmp_param_name,
28180Sstevel@tonic-gate 			    icmp_param_get, icmp_param_set,
28190Sstevel@tonic-gate 			    (caddr_t)icmppa)) {
28200Sstevel@tonic-gate 				nd_free(&icmp_g_nd);
28210Sstevel@tonic-gate 				return (B_FALSE);
28220Sstevel@tonic-gate 			}
28230Sstevel@tonic-gate 		}
28240Sstevel@tonic-gate 	}
28250Sstevel@tonic-gate 	if (!nd_load(&icmp_g_nd, "icmp_status", icmp_status_report, NULL,
28260Sstevel@tonic-gate 	    NULL)) {
28270Sstevel@tonic-gate 		nd_free(&icmp_g_nd);
28280Sstevel@tonic-gate 		return (B_FALSE);
28290Sstevel@tonic-gate 	}
28300Sstevel@tonic-gate 	return (B_TRUE);
28310Sstevel@tonic-gate }
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */
28340Sstevel@tonic-gate /* ARGSUSED */
28350Sstevel@tonic-gate static int
28360Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
28370Sstevel@tonic-gate {
28380Sstevel@tonic-gate 	long		new_value;
28390Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	/*
28420Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
28430Sstevel@tonic-gate 	 * required bounds.
28440Sstevel@tonic-gate 	 */
28450Sstevel@tonic-gate 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
28460Sstevel@tonic-gate 	    new_value < icmppa->icmp_param_min ||
28470Sstevel@tonic-gate 	    new_value > icmppa->icmp_param_max) {
28480Sstevel@tonic-gate 		return (EINVAL);
28490Sstevel@tonic-gate 	}
28500Sstevel@tonic-gate 	/* Set the new value */
28510Sstevel@tonic-gate 	icmppa->icmp_param_value = new_value;
28520Sstevel@tonic-gate 	return (0);
28530Sstevel@tonic-gate }
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate static void
28560Sstevel@tonic-gate icmp_rput(queue_t *q, mblk_t *mp)
28570Sstevel@tonic-gate {
28580Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
28590Sstevel@tonic-gate 	uchar_t			*rptr;
28600Sstevel@tonic-gate 	struct T_error_ack	*tea;
28610Sstevel@tonic-gate 	icmp_t			*icmp;
28620Sstevel@tonic-gate 	sin_t			*sin;
28630Sstevel@tonic-gate 	sin6_t			*sin6;
28640Sstevel@tonic-gate 	ip6_t			*ip6h;
28650Sstevel@tonic-gate 	ip6i_t			*ip6i;
28660Sstevel@tonic-gate 	mblk_t			*mp1;
28670Sstevel@tonic-gate 	int			hdr_len;
28680Sstevel@tonic-gate 	ipha_t			*ipha;
28690Sstevel@tonic-gate 	int			udi_size;	/* Size of T_unitdata_ind */
28700Sstevel@tonic-gate 	uint_t			ipvers;
28710Sstevel@tonic-gate 	ip6_pkt_t		ipp;
28720Sstevel@tonic-gate 	uint8_t			nexthdr;
28730Sstevel@tonic-gate 	boolean_t		recvif = B_FALSE;
28740Sstevel@tonic-gate 	in_pktinfo_t		*pinfo;
28750Sstevel@tonic-gate 	mblk_t			*options_mp = NULL;
28760Sstevel@tonic-gate 	uint_t			icmp_opt = 0;
28770Sstevel@tonic-gate 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
2878*1676Sjpk 	uint_t			hopstrip;
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
28810Sstevel@tonic-gate 	if (icmp->icmp_restricted) {
28820Sstevel@tonic-gate 		putnext(q, mp);
28830Sstevel@tonic-gate 		return;
28840Sstevel@tonic-gate 	}
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_CTL) {
28870Sstevel@tonic-gate 		/*
28880Sstevel@tonic-gate 		 * IP sends up the IPSEC_IN message for handling IPSEC
28890Sstevel@tonic-gate 		 * policy at the TCP level. We don't need it here.
28900Sstevel@tonic-gate 		 */
28910Sstevel@tonic-gate 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
28920Sstevel@tonic-gate 			mp1 = mp->b_cont;
28930Sstevel@tonic-gate 			freeb(mp);
28940Sstevel@tonic-gate 			mp = mp1;
28950Sstevel@tonic-gate 		} else {
28960Sstevel@tonic-gate 			pinfo = (in_pktinfo_t *)mp->b_rptr;
28970Sstevel@tonic-gate 			if ((icmp->icmp_recvif != 0) &&
28980Sstevel@tonic-gate 			    (pinfo->in_pkt_ulp_type == IN_PKTINFO)) {
28990Sstevel@tonic-gate 				/*
29000Sstevel@tonic-gate 				 * IP has passed the options in mp and the
29010Sstevel@tonic-gate 				 * actual data is in b_cont.
29020Sstevel@tonic-gate 				 */
29030Sstevel@tonic-gate 				recvif = B_TRUE;
29040Sstevel@tonic-gate 				/*
29050Sstevel@tonic-gate 				 * We are here bcos IP_RECVIF is set so we need
29060Sstevel@tonic-gate 				 * to extract the options mblk and adjust the
29070Sstevel@tonic-gate 				 * rptr
29080Sstevel@tonic-gate 				 */
29090Sstevel@tonic-gate 				options_mp = mp;
29100Sstevel@tonic-gate 				mp = mp->b_cont;
29110Sstevel@tonic-gate 			}
29120Sstevel@tonic-gate 		}
29130Sstevel@tonic-gate 	}
29140Sstevel@tonic-gate 
29150Sstevel@tonic-gate 	rptr = mp->b_rptr;
29160Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
29170Sstevel@tonic-gate 	case M_DATA:
29180Sstevel@tonic-gate 		/*
29190Sstevel@tonic-gate 		 * M_DATA messages contain IP packets.  They are handled
29200Sstevel@tonic-gate 		 * following the switch.
29210Sstevel@tonic-gate 		 */
29220Sstevel@tonic-gate 		break;
29230Sstevel@tonic-gate 	case M_PROTO:
29240Sstevel@tonic-gate 	case M_PCPROTO:
29250Sstevel@tonic-gate 		/* M_PROTO messages contain some type of TPI message. */
29260Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
29270Sstevel@tonic-gate 			freemsg(mp);
29280Sstevel@tonic-gate 			return;
29290Sstevel@tonic-gate 		}
29300Sstevel@tonic-gate 		tea = (struct T_error_ack *)rptr;
29310Sstevel@tonic-gate 		switch (tea->PRIM_type) {
29320Sstevel@tonic-gate 		case T_ERROR_ACK:
29330Sstevel@tonic-gate 			switch (tea->ERROR_prim) {
29340Sstevel@tonic-gate 			case O_T_BIND_REQ:
29350Sstevel@tonic-gate 			case T_BIND_REQ:
29360Sstevel@tonic-gate 				/*
29370Sstevel@tonic-gate 				 * If our O_T_BIND_REQ/T_BIND_REQ fails,
29380Sstevel@tonic-gate 				 * clear out the source address before
29390Sstevel@tonic-gate 				 * passing the message upstream.
29400Sstevel@tonic-gate 				 * If this was caused by a T_CONN_REQ
29410Sstevel@tonic-gate 				 * revert back to bound state.
29420Sstevel@tonic-gate 				 */
29430Sstevel@tonic-gate 				if (icmp->icmp_state == TS_UNBND) {
29440Sstevel@tonic-gate 					/*
29450Sstevel@tonic-gate 					 * TPI has not yet bound - bind sent by
29460Sstevel@tonic-gate 					 * icmp_bind_proto.
29470Sstevel@tonic-gate 					 */
29480Sstevel@tonic-gate 					freemsg(mp);
29490Sstevel@tonic-gate 					return;
29500Sstevel@tonic-gate 				}
29510Sstevel@tonic-gate 				if (icmp->icmp_state == TS_DATA_XFER) {
29520Sstevel@tonic-gate 					/* Connect failed */
29530Sstevel@tonic-gate 					tea->ERROR_prim = T_CONN_REQ;
29540Sstevel@tonic-gate 					icmp->icmp_v6src =
29550Sstevel@tonic-gate 					    icmp->icmp_bound_v6src;
29560Sstevel@tonic-gate 					icmp->icmp_state = TS_IDLE;
29570Sstevel@tonic-gate 					if (icmp->icmp_family == AF_INET6)
29580Sstevel@tonic-gate 						(void) icmp_build_hdrs(q, icmp);
29590Sstevel@tonic-gate 					break;
29600Sstevel@tonic-gate 				}
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 				if (icmp->icmp_discon_pending) {
29630Sstevel@tonic-gate 					tea->ERROR_prim = T_DISCON_REQ;
29640Sstevel@tonic-gate 					icmp->icmp_discon_pending = 0;
29650Sstevel@tonic-gate 				}
29660Sstevel@tonic-gate 				V6_SET_ZERO(icmp->icmp_v6src);
29670Sstevel@tonic-gate 				V6_SET_ZERO(icmp->icmp_bound_v6src);
29680Sstevel@tonic-gate 				icmp->icmp_state = TS_UNBND;
29690Sstevel@tonic-gate 				if (icmp->icmp_family == AF_INET6)
29700Sstevel@tonic-gate 					(void) icmp_build_hdrs(q, icmp);
29710Sstevel@tonic-gate 				break;
29720Sstevel@tonic-gate 			default:
29730Sstevel@tonic-gate 				break;
29740Sstevel@tonic-gate 			}
29750Sstevel@tonic-gate 			break;
29760Sstevel@tonic-gate 		case T_BIND_ACK:
29770Sstevel@tonic-gate 			icmp_rput_bind_ack(q, mp);
29780Sstevel@tonic-gate 			return;
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 		case T_OPTMGMT_ACK:
29810Sstevel@tonic-gate 		case T_OK_ACK:
29820Sstevel@tonic-gate 			if (tea->PRIM_type == T_OK_ACK) {
29830Sstevel@tonic-gate 				struct T_ok_ack *toa;
29840Sstevel@tonic-gate 				toa = (struct T_ok_ack *)rptr;
29850Sstevel@tonic-gate 				if (toa->CORRECT_prim == T_UNBIND_REQ) {
29860Sstevel@tonic-gate 					/*
29870Sstevel@tonic-gate 					 * If somebody sets IPSEC options, IP
29880Sstevel@tonic-gate 					 * sends some IPSEC info which is used
29890Sstevel@tonic-gate 					 * by the TCP for detached connections.
29900Sstevel@tonic-gate 					 * We don't need it here.
29910Sstevel@tonic-gate 					 */
29920Sstevel@tonic-gate 					if ((mp1 = mp->b_cont) != NULL) {
29930Sstevel@tonic-gate 						freemsg(mp1);
29940Sstevel@tonic-gate 						mp->b_cont = NULL;
29950Sstevel@tonic-gate 					}
29960Sstevel@tonic-gate 				}
29970Sstevel@tonic-gate 			}
29980Sstevel@tonic-gate 			break;
29990Sstevel@tonic-gate 		default:
30000Sstevel@tonic-gate 			freemsg(mp);
30010Sstevel@tonic-gate 			return;
30020Sstevel@tonic-gate 		}
30030Sstevel@tonic-gate 		putnext(q, mp);
30040Sstevel@tonic-gate 		return;
30050Sstevel@tonic-gate 	case M_CTL:
30060Sstevel@tonic-gate 		if (recvif) {
30070Sstevel@tonic-gate 			/*
30080Sstevel@tonic-gate 			 * IP has passed the options in mp and the actual data
30090Sstevel@tonic-gate 			 * is in b_cont. Jump to normal data processing.
30100Sstevel@tonic-gate 			 */
30110Sstevel@tonic-gate 			break;
30120Sstevel@tonic-gate 		}
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 		/* Contains ICMP packet from IP */
30150Sstevel@tonic-gate 		icmp_icmp_error(q, mp);
30160Sstevel@tonic-gate 		return;
30170Sstevel@tonic-gate 	default:
30180Sstevel@tonic-gate 		putnext(q, mp);
30190Sstevel@tonic-gate 		return;
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 	/*
30230Sstevel@tonic-gate 	 * Discard message if it is misaligned or smaller than the IP header.
30240Sstevel@tonic-gate 	 */
30250Sstevel@tonic-gate 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
30260Sstevel@tonic-gate 		freemsg(mp);
30270Sstevel@tonic-gate 		if (options_mp != NULL)
30280Sstevel@tonic-gate 			freeb(options_mp);
30290Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
30300Sstevel@tonic-gate 		return;
30310Sstevel@tonic-gate 	}
30320Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	/* Handle M_DATA messages containing IP packets messages */
30350Sstevel@tonic-gate 	if (ipvers == IPV4_VERSION) {
30360Sstevel@tonic-gate 		/*
30370Sstevel@tonic-gate 		 * Special case where IP attaches
30380Sstevel@tonic-gate 		 * the IRE needs to be handled so that we don't send up
30390Sstevel@tonic-gate 		 * IRE to the user land.
30400Sstevel@tonic-gate 		 */
30410Sstevel@tonic-gate 		ipha = (ipha_t *)rptr;
30420Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 		if (ipha->ipha_protocol == IPPROTO_TCP) {
30450Sstevel@tonic-gate 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
30480Sstevel@tonic-gate 			    TH_SYN) && mp->b_cont != NULL) {
30490Sstevel@tonic-gate 				mp1 = mp->b_cont;
30500Sstevel@tonic-gate 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
30510Sstevel@tonic-gate 					freeb(mp1);
30520Sstevel@tonic-gate 					mp->b_cont = NULL;
30530Sstevel@tonic-gate 				}
30540Sstevel@tonic-gate 			}
30550Sstevel@tonic-gate 		}
30560Sstevel@tonic-gate 		if (icmp_bsd_compat) {
30570Sstevel@tonic-gate 			ushort_t len;
30580Sstevel@tonic-gate 			len = ntohs(ipha->ipha_length);
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 			if (mp->b_datap->db_ref > 1) {
30610Sstevel@tonic-gate 				/*
30620Sstevel@tonic-gate 				 * Allocate a new IP header so that we can
30630Sstevel@tonic-gate 				 * modify ipha_length.
30640Sstevel@tonic-gate 				 */
30650Sstevel@tonic-gate 				mblk_t	*mp1;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 				mp1 = allocb(hdr_len, BPRI_MED);
30680Sstevel@tonic-gate 				if (!mp1) {
30690Sstevel@tonic-gate 					freemsg(mp);
30700Sstevel@tonic-gate 					if (options_mp != NULL)
30710Sstevel@tonic-gate 						freeb(options_mp);
30720Sstevel@tonic-gate 					BUMP_MIB(&rawip_mib, rawipInErrors);
30730Sstevel@tonic-gate 					return;
30740Sstevel@tonic-gate 				}
30750Sstevel@tonic-gate 				bcopy(rptr, mp1->b_rptr, hdr_len);
30760Sstevel@tonic-gate 				mp->b_rptr = rptr + hdr_len;
30770Sstevel@tonic-gate 				rptr = mp1->b_rptr;
30780Sstevel@tonic-gate 				ipha = (ipha_t *)rptr;
30790Sstevel@tonic-gate 				mp1->b_cont = mp;
30800Sstevel@tonic-gate 				mp1->b_wptr = rptr + hdr_len;
30810Sstevel@tonic-gate 				mp = mp1;
30820Sstevel@tonic-gate 			}
30830Sstevel@tonic-gate 			len -= hdr_len;
30840Sstevel@tonic-gate 			ipha->ipha_length = htons(len);
30850Sstevel@tonic-gate 		}
30860Sstevel@tonic-gate 	}
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate 	/*
30890Sstevel@tonic-gate 	 * This is the inbound data path.  Packets are passed upstream as
30900Sstevel@tonic-gate 	 * T_UNITDATA_IND messages with full IP headers still attached.
30910Sstevel@tonic-gate 	 */
30920Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
30930Sstevel@tonic-gate 		ASSERT(ipvers == IPV4_VERSION);
30940Sstevel@tonic-gate 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
30950Sstevel@tonic-gate 		if (recvif) {
30960Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
30970Sstevel@tonic-gate 			    sizeof (uint_t);
30980Sstevel@tonic-gate 		}
30991673Sgt145670 		/*
31001673Sgt145670 		 * If SO_TIMESTAMP is set allocate the appropriate sized
31011673Sgt145670 		 * buffer. Since gethrestime() expects a pointer aligned
31021673Sgt145670 		 * argument, we allocate space necessary for extra
31031673Sgt145670 		 * alignment (even though it might not be used).
31041673Sgt145670 		 */
31051673Sgt145670 		if (icmp->icmp_timestamp) {
31061673Sgt145670 			udi_size += sizeof (struct T_opthdr) +
31071673Sgt145670 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
31081673Sgt145670 		}
31090Sstevel@tonic-gate 		mp1 = allocb(udi_size, BPRI_MED);
31100Sstevel@tonic-gate 		if (mp1 == NULL) {
31110Sstevel@tonic-gate 			freemsg(mp);
31120Sstevel@tonic-gate 			if (options_mp != NULL)
31130Sstevel@tonic-gate 				freeb(options_mp);
31140Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipInErrors);
31150Sstevel@tonic-gate 			return;
31160Sstevel@tonic-gate 		}
31170Sstevel@tonic-gate 		mp1->b_cont = mp;
31180Sstevel@tonic-gate 		mp = mp1;
31190Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
31200Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
31210Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)tudi + udi_size;
31220Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
31230Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin_t);
31240Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
31250Sstevel@tonic-gate 		sin = (sin_t *)&tudi[1];
31260Sstevel@tonic-gate 		*sin = sin_null;
31270Sstevel@tonic-gate 		sin->sin_family = AF_INET;
31280Sstevel@tonic-gate 		sin->sin_addr.s_addr = ipha->ipha_src;
31290Sstevel@tonic-gate 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
31300Sstevel@tonic-gate 		    sizeof (sin_t);
31310Sstevel@tonic-gate 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
31320Sstevel@tonic-gate 		tudi->OPT_length = udi_size;
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 		/*
31350Sstevel@tonic-gate 		 * Add options if IP_RECVIF is set
31360Sstevel@tonic-gate 		 */
31370Sstevel@tonic-gate 		if (udi_size != 0) {
31380Sstevel@tonic-gate 			char *dstopt;
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 			dstopt = (char *)&sin[1];
31410Sstevel@tonic-gate 			if (recvif) {
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 				struct T_opthdr *toh;
31440Sstevel@tonic-gate 				uint_t		*dstptr;
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 				toh = (struct T_opthdr *)dstopt;
31470Sstevel@tonic-gate 				toh->level = IPPROTO_IP;
31480Sstevel@tonic-gate 				toh->name = IP_RECVIF;
31490Sstevel@tonic-gate 				toh->len = sizeof (struct T_opthdr) +
31500Sstevel@tonic-gate 					sizeof (uint_t);
31510Sstevel@tonic-gate 				toh->status = 0;
31520Sstevel@tonic-gate 				dstopt += sizeof (struct T_opthdr);
31530Sstevel@tonic-gate 				dstptr = (uint_t *)dstopt;
31540Sstevel@tonic-gate 				*dstptr = pinfo->in_pkt_ifindex;
31550Sstevel@tonic-gate 				dstopt += sizeof (uint_t);
31560Sstevel@tonic-gate 				freeb(options_mp);
31570Sstevel@tonic-gate 				udi_size -= toh->len;
31580Sstevel@tonic-gate 			}
31591673Sgt145670 			if (icmp->icmp_timestamp) {
31601673Sgt145670 				struct	T_opthdr *toh;
31611673Sgt145670 
31621673Sgt145670 				toh = (struct T_opthdr *)dstopt;
31631673Sgt145670 				toh->level = SOL_SOCKET;
31641673Sgt145670 				toh->name = SCM_TIMESTAMP;
31651673Sgt145670 				toh->len = sizeof (struct T_opthdr) +
31661673Sgt145670 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
31671673Sgt145670 				toh->status = 0;
31681673Sgt145670 				dstopt += sizeof (struct T_opthdr);
31691673Sgt145670 				/* Align for gethrestime() */
31701673Sgt145670 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
31711673Sgt145670 				    sizeof (intptr_t));
31721673Sgt145670 				gethrestime((timestruc_t *)dstopt);
31731673Sgt145670 				dstopt += sizeof (timestruc_t);
31741673Sgt145670 				udi_size -= toh->len;
31751673Sgt145670 			}
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 			/* Consumed all of allocated space */
31780Sstevel@tonic-gate 			ASSERT(udi_size == 0);
31790Sstevel@tonic-gate 		}
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInDatagrams);
31820Sstevel@tonic-gate 		putnext(q, mp);
31830Sstevel@tonic-gate 		return;
31840Sstevel@tonic-gate 	}
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate 	/*
31870Sstevel@tonic-gate 	 * We don't need options_mp in the IPv6 path.
31880Sstevel@tonic-gate 	 */
31890Sstevel@tonic-gate 	if (options_mp != NULL) {
31900Sstevel@tonic-gate 		freeb(options_mp);
31910Sstevel@tonic-gate 		options_mp = NULL;
31920Sstevel@tonic-gate 	}
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	/*
31950Sstevel@tonic-gate 	 * Discard message if it is smaller than the IPv6 header
31960Sstevel@tonic-gate 	 * or if the header is malformed.
31970Sstevel@tonic-gate 	 */
31980Sstevel@tonic-gate 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
31990Sstevel@tonic-gate 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
32000Sstevel@tonic-gate 	    icmp->icmp_family != AF_INET6) {
32010Sstevel@tonic-gate 		freemsg(mp);
32020Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
32030Sstevel@tonic-gate 		return;
32040Sstevel@tonic-gate 	}
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	/* Initialize */
32070Sstevel@tonic-gate 	ipp.ipp_fields = 0;
3208*1676Sjpk 	hopstrip = 0;
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	ip6h = (ip6_t *)rptr;
32110Sstevel@tonic-gate 	/*
32120Sstevel@tonic-gate 	 * Call on ip_find_hdr_v6 which gets the total hdr len
32130Sstevel@tonic-gate 	 * as well as individual lenghts of ext hdrs (and ptrs to
32140Sstevel@tonic-gate 	 * them).
32150Sstevel@tonic-gate 	 */
32160Sstevel@tonic-gate 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
32170Sstevel@tonic-gate 		/* Look for ifindex information */
32180Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
32190Sstevel@tonic-gate 			ip6i = (ip6i_t *)ip6h;
32200Sstevel@tonic-gate 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
32210Sstevel@tonic-gate 				ASSERT(ip6i->ip6i_ifindex != 0);
32220Sstevel@tonic-gate 				ipp.ipp_fields |= IPPF_IFINDEX;
32230Sstevel@tonic-gate 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
32240Sstevel@tonic-gate 			}
32250Sstevel@tonic-gate 			rptr = (uchar_t *)&ip6i[1];
32260Sstevel@tonic-gate 			mp->b_rptr = rptr;
32270Sstevel@tonic-gate 			if (rptr == mp->b_wptr) {
32280Sstevel@tonic-gate 				mp1 = mp->b_cont;
32290Sstevel@tonic-gate 				freeb(mp);
32300Sstevel@tonic-gate 				mp = mp1;
32310Sstevel@tonic-gate 				rptr = mp->b_rptr;
32320Sstevel@tonic-gate 			}
32330Sstevel@tonic-gate 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
32340Sstevel@tonic-gate 			ip6h = (ip6_t *)rptr;
32350Sstevel@tonic-gate 		}
32360Sstevel@tonic-gate 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
3237*1676Sjpk 
3238*1676Sjpk 		/*
3239*1676Sjpk 		 * We need to lie a bit to the user because users inside
3240*1676Sjpk 		 * labeled compartments should not see their own labels.  We
3241*1676Sjpk 		 * assume that in all other respects IP has checked the label,
3242*1676Sjpk 		 * and that the label is always first among the options.  (If
3243*1676Sjpk 		 * it's not first, then this code won't see it, and the option
3244*1676Sjpk 		 * will be passed along to the user.)
3245*1676Sjpk 		 *
3246*1676Sjpk 		 * If we had multilevel ICMP sockets, then the following code
3247*1676Sjpk 		 * should be skipped for them to allow the user to see the
3248*1676Sjpk 		 * label.
3249*1676Sjpk 		 *
3250*1676Sjpk 		 * Alignment restrictions in the definition of IP options
3251*1676Sjpk 		 * (namely, the requirement that the 4-octet DOI goes on a
3252*1676Sjpk 		 * 4-octet boundary) mean that we know exactly where the option
3253*1676Sjpk 		 * should start, but we're lenient for other hosts.
3254*1676Sjpk 		 *
3255*1676Sjpk 		 * Note that there are no multilevel ICMP or raw IP sockets
3256*1676Sjpk 		 * yet, thus nobody ever sees the IP6OPT_LS option.
3257*1676Sjpk 		 */
3258*1676Sjpk 		if ((ipp.ipp_fields & IPPF_HOPOPTS) &&
3259*1676Sjpk 		    ipp.ipp_hopoptslen > 5 && is_system_labeled()) {
3260*1676Sjpk 			const uchar_t *ucp =
3261*1676Sjpk 			    (const uchar_t *)ipp.ipp_hopopts + 2;
3262*1676Sjpk 			int remlen = ipp.ipp_hopoptslen - 2;
3263*1676Sjpk 
3264*1676Sjpk 			while (remlen > 0) {
3265*1676Sjpk 				if (*ucp == IP6OPT_PAD1) {
3266*1676Sjpk 					remlen--;
3267*1676Sjpk 					ucp++;
3268*1676Sjpk 				} else if (*ucp == IP6OPT_PADN) {
3269*1676Sjpk 					remlen -= ucp[1] + 2;
3270*1676Sjpk 					ucp += ucp[1] + 2;
3271*1676Sjpk 				} else if (*ucp == ip6opt_ls) {
3272*1676Sjpk 					hopstrip = (ucp -
3273*1676Sjpk 					    (const uchar_t *)ipp.ipp_hopopts) +
3274*1676Sjpk 					    ucp[1] + 2;
3275*1676Sjpk 					hopstrip = (hopstrip + 7) & ~7;
3276*1676Sjpk 					break;
3277*1676Sjpk 				} else {
3278*1676Sjpk 					/* label option must be first */
3279*1676Sjpk 					break;
3280*1676Sjpk 				}
3281*1676Sjpk 			}
3282*1676Sjpk 		}
32830Sstevel@tonic-gate 	} else {
32840Sstevel@tonic-gate 		hdr_len = IPV6_HDR_LEN;
32850Sstevel@tonic-gate 		ip6i = NULL;
32860Sstevel@tonic-gate 		nexthdr = ip6h->ip6_nxt;
32870Sstevel@tonic-gate 	}
32880Sstevel@tonic-gate 	/*
32890Sstevel@tonic-gate 	 * One special case where IP attaches the IRE needs to
32900Sstevel@tonic-gate 	 * be handled so that we don't send up IRE to the user land.
32910Sstevel@tonic-gate 	 */
32920Sstevel@tonic-gate 	if (nexthdr == IPPROTO_TCP) {
32930Sstevel@tonic-gate 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
32960Sstevel@tonic-gate 		    mp->b_cont != NULL) {
32970Sstevel@tonic-gate 			mp1 = mp->b_cont;
32980Sstevel@tonic-gate 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
32990Sstevel@tonic-gate 				freeb(mp1);
33000Sstevel@tonic-gate 				mp->b_cont = NULL;
33010Sstevel@tonic-gate 			}
33020Sstevel@tonic-gate 		}
33030Sstevel@tonic-gate 	}
33040Sstevel@tonic-gate 	/*
33050Sstevel@tonic-gate 	 * Check a filter for ICMPv6 types if needed.
33060Sstevel@tonic-gate 	 * Verify raw checksums if needed.
33070Sstevel@tonic-gate 	 */
33080Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
33090Sstevel@tonic-gate 		if (icmp->icmp_filter != NULL) {
33100Sstevel@tonic-gate 			int type;
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 			/* Assumes that IP has done the pullupmsg */
33130Sstevel@tonic-gate 			type = mp->b_rptr[hdr_len];
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
33160Sstevel@tonic-gate 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
33170Sstevel@tonic-gate 				freemsg(mp);
33180Sstevel@tonic-gate 				return;
33190Sstevel@tonic-gate 			}
33200Sstevel@tonic-gate 		} else {
33210Sstevel@tonic-gate 			/* Checksum */
33220Sstevel@tonic-gate 			uint16_t	*up;
33230Sstevel@tonic-gate 			uint32_t	sum;
33240Sstevel@tonic-gate 			int		remlen;
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 			up = (uint16_t *)&ip6h->ip6_src;
33270Sstevel@tonic-gate 
33280Sstevel@tonic-gate 			remlen = msgdsize(mp) - hdr_len;
33290Sstevel@tonic-gate 			sum = htons(icmp->icmp_proto + remlen)
33300Sstevel@tonic-gate 			    + up[0] + up[1] + up[2] + up[3]
33310Sstevel@tonic-gate 			    + up[4] + up[5] + up[6] + up[7]
33320Sstevel@tonic-gate 			    + up[8] + up[9] + up[10] + up[11]
33330Sstevel@tonic-gate 			    + up[12] + up[13] + up[14] + up[15];
33340Sstevel@tonic-gate 			sum = (sum & 0xffff) + (sum >> 16);
33350Sstevel@tonic-gate 			sum = IP_CSUM(mp, hdr_len, sum);
33360Sstevel@tonic-gate 			if (sum != 0) {
33370Sstevel@tonic-gate 				/* IPv6 RAW checksum failed */
33380Sstevel@tonic-gate 				ip0dbg(("icmp_rput: RAW checksum "
33390Sstevel@tonic-gate 				    "failed %x\n", sum));
33400Sstevel@tonic-gate 				freemsg(mp);
33410Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipInCksumErrs);
33420Sstevel@tonic-gate 				return;
33430Sstevel@tonic-gate 			}
33440Sstevel@tonic-gate 		}
33450Sstevel@tonic-gate 	}
33460Sstevel@tonic-gate 	/* Skip all the IPv6 headers per API */
33470Sstevel@tonic-gate 	mp->b_rptr += hdr_len;
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	/*
33520Sstevel@tonic-gate 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
33530Sstevel@tonic-gate 	 * maintain state information, instead of relying on icmp_t
33540Sstevel@tonic-gate 	 * structure, since there arent any locks protecting these members
33550Sstevel@tonic-gate 	 * and there is a window where there might be a race between a
33560Sstevel@tonic-gate 	 * thread setting options on the write side and a thread reading
33570Sstevel@tonic-gate 	 * these options on the read size.
33580Sstevel@tonic-gate 	 */
33590Sstevel@tonic-gate 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
33600Sstevel@tonic-gate 	    IPPF_RTHDR|IPPF_IFINDEX)) {
33610Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvhopopts &&
3362*1676Sjpk 		    (ipp.ipp_fields & IPPF_HOPOPTS) &&
3363*1676Sjpk 		    ipp.ipp_hopoptslen > hopstrip) {
33640Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
3365*1676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
33660Sstevel@tonic-gate 			icmp_opt |= IPPF_HOPOPTS;
33670Sstevel@tonic-gate 		}
33680Sstevel@tonic-gate 		if ((icmp->icmp_ipv6_recvdstopts ||
33690Sstevel@tonic-gate 			icmp->icmp_old_ipv6_recvdstopts) &&
33700Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
33710Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33720Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
33730Sstevel@tonic-gate 			icmp_opt |= IPPF_DSTOPTS;
33740Sstevel@tonic-gate 		}
33750Sstevel@tonic-gate 		if (((icmp->icmp_ipv6_recvdstopts &&
33760Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrthdr &&
33770Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
33780Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrtdstopts) &&
33790Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
33800Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33810Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
33820Sstevel@tonic-gate 			icmp_opt |= IPPF_RTDSTOPTS;
33830Sstevel@tonic-gate 		}
33840Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvrthdr &&
33850Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) {
33860Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33870Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
33880Sstevel@tonic-gate 			icmp_opt |= IPPF_RTHDR;
33890Sstevel@tonic-gate 		}
33900Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvpktinfo &&
33910Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
33920Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
33930Sstevel@tonic-gate 			    sizeof (struct in6_pktinfo);
33940Sstevel@tonic-gate 			icmp_opt |= IPPF_IFINDEX;
33950Sstevel@tonic-gate 		}
33960Sstevel@tonic-gate 	}
33970Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvhoplimit) {
33980Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
33990Sstevel@tonic-gate 		icmp_ipv6_recvhoplimit = B_TRUE;
34000Sstevel@tonic-gate 	}
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvtclass)
34030Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
34040Sstevel@tonic-gate 
34050Sstevel@tonic-gate 	mp1 = allocb(udi_size, BPRI_MED);
34060Sstevel@tonic-gate 	if (mp1 == NULL) {
34070Sstevel@tonic-gate 		freemsg(mp);
34080Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
34090Sstevel@tonic-gate 		return;
34100Sstevel@tonic-gate 	}
34110Sstevel@tonic-gate 	mp1->b_cont = mp;
34120Sstevel@tonic-gate 	mp = mp1;
34130Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
34140Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
34150Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)tudi + udi_size;
34160Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
34170Sstevel@tonic-gate 	tudi->SRC_length = sizeof (sin6_t);
34180Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
34190Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
34200Sstevel@tonic-gate 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
34210Sstevel@tonic-gate 	tudi->OPT_length = udi_size;
34220Sstevel@tonic-gate 	sin6 = (sin6_t *)&tudi[1];
34230Sstevel@tonic-gate 	sin6->sin6_port = 0;
34240Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 	sin6->sin6_addr = ip6h->ip6_src;
34270Sstevel@tonic-gate 	/* No sin6_flowinfo per API */
34280Sstevel@tonic-gate 	sin6->sin6_flowinfo = 0;
34290Sstevel@tonic-gate 	/* For link-scope source pass up scope id */
34300Sstevel@tonic-gate 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
34310Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
34320Sstevel@tonic-gate 		sin6->sin6_scope_id = ipp.ipp_ifindex;
34330Sstevel@tonic-gate 	else
34340Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
34370Sstevel@tonic-gate 	    icmp->icmp_zoneid);
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 	if (udi_size != 0) {
34400Sstevel@tonic-gate 		uchar_t *dstopt;
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 		dstopt = (uchar_t *)&sin6[1];
34430Sstevel@tonic-gate 		if (icmp_opt & IPPF_IFINDEX) {
34440Sstevel@tonic-gate 			struct T_opthdr *toh;
34450Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34480Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34490Sstevel@tonic-gate 			toh->name = IPV6_PKTINFO;
34500Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
34510Sstevel@tonic-gate 			    sizeof (*pkti);
34520Sstevel@tonic-gate 			toh->status = 0;
34530Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
34540Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)dstopt;
34550Sstevel@tonic-gate 			pkti->ipi6_addr = ip6h->ip6_dst;
34560Sstevel@tonic-gate 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
34570Sstevel@tonic-gate 			dstopt += sizeof (*pkti);
34580Sstevel@tonic-gate 			udi_size -= toh->len;
34590Sstevel@tonic-gate 		}
34600Sstevel@tonic-gate 		if (icmp_ipv6_recvhoplimit) {
34610Sstevel@tonic-gate 			struct T_opthdr *toh;
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34640Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34650Sstevel@tonic-gate 			toh->name = IPV6_HOPLIMIT;
34660Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
34670Sstevel@tonic-gate 			    sizeof (uint_t);
34680Sstevel@tonic-gate 			toh->status = 0;
34690Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
34700Sstevel@tonic-gate 			*(uint_t *)dstopt = ip6h->ip6_hops;
34710Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
34720Sstevel@tonic-gate 			udi_size -= toh->len;
34730Sstevel@tonic-gate 		}
34740Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvtclass) {
34750Sstevel@tonic-gate 			struct T_opthdr *toh;
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34780Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34790Sstevel@tonic-gate 			toh->name = IPV6_TCLASS;
34800Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
34810Sstevel@tonic-gate 			    sizeof (uint_t);
34820Sstevel@tonic-gate 			toh->status = 0;
34830Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
34840Sstevel@tonic-gate 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
34850Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
34860Sstevel@tonic-gate 			udi_size -= toh->len;
34870Sstevel@tonic-gate 		}
34880Sstevel@tonic-gate 		if (icmp_opt & IPPF_HOPOPTS) {
34890Sstevel@tonic-gate 			struct T_opthdr *toh;
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
34920Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
34930Sstevel@tonic-gate 			toh->name = IPV6_HOPOPTS;
34940Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
3495*1676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
34960Sstevel@tonic-gate 			toh->status = 0;
34970Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
3498*1676Sjpk 			bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt,
3499*1676Sjpk 			    ipp.ipp_hopoptslen - hopstrip);
3500*1676Sjpk 			if (hopstrip > 0) {
3501*1676Sjpk 				/* copy next header value and fake length */
3502*1676Sjpk 				dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0];
3503*1676Sjpk 				dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] -
3504*1676Sjpk 				    hopstrip / 8;
3505*1676Sjpk 			}
3506*1676Sjpk 			dstopt += ipp.ipp_hopoptslen - hopstrip;
35070Sstevel@tonic-gate 			udi_size -= toh->len;
35080Sstevel@tonic-gate 		}
35090Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTDSTOPTS) {
35100Sstevel@tonic-gate 			struct T_opthdr *toh;
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35130Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35140Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
35150Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35160Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
35170Sstevel@tonic-gate 			toh->status = 0;
35180Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35190Sstevel@tonic-gate 			bcopy(ipp.ipp_rtdstopts, dstopt,
35200Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen);
35210Sstevel@tonic-gate 			dstopt += ipp.ipp_rtdstoptslen;
35220Sstevel@tonic-gate 			udi_size -= toh->len;
35230Sstevel@tonic-gate 		}
35240Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTHDR) {
35250Sstevel@tonic-gate 			struct T_opthdr *toh;
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35280Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35290Sstevel@tonic-gate 			toh->name = IPV6_RTHDR;
35300Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35310Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
35320Sstevel@tonic-gate 			toh->status = 0;
35330Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35340Sstevel@tonic-gate 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
35350Sstevel@tonic-gate 			dstopt += ipp.ipp_rthdrlen;
35360Sstevel@tonic-gate 			udi_size -= toh->len;
35370Sstevel@tonic-gate 		}
35380Sstevel@tonic-gate 		if (icmp_opt & IPPF_DSTOPTS) {
35390Sstevel@tonic-gate 			struct T_opthdr *toh;
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35420Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35430Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
35440Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35450Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
35460Sstevel@tonic-gate 			toh->status = 0;
35470Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35480Sstevel@tonic-gate 			bcopy(ipp.ipp_dstopts, dstopt,
35490Sstevel@tonic-gate 			    ipp.ipp_dstoptslen);
35500Sstevel@tonic-gate 			dstopt += ipp.ipp_dstoptslen;
35510Sstevel@tonic-gate 			udi_size -= toh->len;
35520Sstevel@tonic-gate 		}
35530Sstevel@tonic-gate 		/* Consumed all of allocated space */
35540Sstevel@tonic-gate 		ASSERT(udi_size == 0);
35550Sstevel@tonic-gate 	}
35560Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipInDatagrams);
35570Sstevel@tonic-gate 	putnext(q, mp);
35580Sstevel@tonic-gate }
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate /*
35610Sstevel@tonic-gate  * Process a T_BIND_ACK
35620Sstevel@tonic-gate  */
35630Sstevel@tonic-gate static void
35640Sstevel@tonic-gate icmp_rput_bind_ack(queue_t *q, mblk_t *mp)
35650Sstevel@tonic-gate {
35660Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
35670Sstevel@tonic-gate 	mblk_t	*mp1;
35680Sstevel@tonic-gate 	ire_t	*ire;
35690Sstevel@tonic-gate 	struct T_bind_ack *tba;
35700Sstevel@tonic-gate 	uchar_t *addrp;
35710Sstevel@tonic-gate 	ipa_conn_t	*ac;
35720Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 	/*
35750Sstevel@tonic-gate 	 * We know if headers are included or not so we can
35760Sstevel@tonic-gate 	 * safely do this.
35770Sstevel@tonic-gate 	 */
35780Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
35790Sstevel@tonic-gate 		/*
35800Sstevel@tonic-gate 		 * TPI has not yet bound - bind sent by
35810Sstevel@tonic-gate 		 * icmp_bind_proto.
35820Sstevel@tonic-gate 		 */
35830Sstevel@tonic-gate 		freemsg(mp);
35840Sstevel@tonic-gate 		return;
35850Sstevel@tonic-gate 	}
35860Sstevel@tonic-gate 	if (icmp->icmp_discon_pending)
35870Sstevel@tonic-gate 		icmp->icmp_discon_pending = 0;
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 	/*
35900Sstevel@tonic-gate 	 * If a broadcast/multicast address was bound set
35910Sstevel@tonic-gate 	 * the source address to 0.
35920Sstevel@tonic-gate 	 * This ensures no datagrams with broadcast address
35930Sstevel@tonic-gate 	 * as source address are emitted (which would violate
35940Sstevel@tonic-gate 	 * RFC1122 - Hosts requirements)
35950Sstevel@tonic-gate 	 *
35960Sstevel@tonic-gate 	 * Note that when connecting the returned IRE is
35970Sstevel@tonic-gate 	 * for the destination address and we only perform
35980Sstevel@tonic-gate 	 * the broadcast check for the source address (it
35990Sstevel@tonic-gate 	 * is OK to connect to a broadcast/multicast address.)
36000Sstevel@tonic-gate 	 */
36010Sstevel@tonic-gate 	mp1 = mp->b_cont;
36020Sstevel@tonic-gate 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
36030Sstevel@tonic-gate 		ire = (ire_t *)mp1->b_rptr;
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 		/*
36060Sstevel@tonic-gate 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
36070Sstevel@tonic-gate 		 * local address.
36080Sstevel@tonic-gate 		 */
36090Sstevel@tonic-gate 		if (ire->ire_type == IRE_BROADCAST &&
36100Sstevel@tonic-gate 		    icmp->icmp_state != TS_DATA_XFER) {
36110Sstevel@tonic-gate 			/* This was just a local bind to a MC/broadcast addr */
36120Sstevel@tonic-gate 			V6_SET_ZERO(icmp->icmp_v6src);
36130Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6)
36140Sstevel@tonic-gate 				(void) icmp_build_hdrs(q, icmp);
36150Sstevel@tonic-gate 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
36160Sstevel@tonic-gate 			/*
36170Sstevel@tonic-gate 			 * Local address not yet set - pick it from the
36180Sstevel@tonic-gate 			 * T_bind_ack
36190Sstevel@tonic-gate 			 */
36200Sstevel@tonic-gate 			tba = (struct T_bind_ack *)mp->b_rptr;
36210Sstevel@tonic-gate 			addrp = &mp->b_rptr[tba->ADDR_offset];
36220Sstevel@tonic-gate 			switch (icmp->icmp_family) {
36230Sstevel@tonic-gate 			case AF_INET:
36240Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
36250Sstevel@tonic-gate 					ac = (ipa_conn_t *)addrp;
36260Sstevel@tonic-gate 				} else {
36270Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
36280Sstevel@tonic-gate 					    sizeof (ipa_conn_x_t));
36290Sstevel@tonic-gate 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
36300Sstevel@tonic-gate 				}
36310Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
36320Sstevel@tonic-gate 				    &icmp->icmp_v6src);
36330Sstevel@tonic-gate 				break;
36340Sstevel@tonic-gate 			case AF_INET6:
36350Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
36360Sstevel@tonic-gate 					ac6 = (ipa6_conn_t *)addrp;
36370Sstevel@tonic-gate 				} else {
36380Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
36390Sstevel@tonic-gate 					    sizeof (ipa6_conn_x_t));
36400Sstevel@tonic-gate 					ac6 = &((ipa6_conn_x_t *)
36410Sstevel@tonic-gate 					    addrp)->ac6x_conn;
36420Sstevel@tonic-gate 				}
36430Sstevel@tonic-gate 				icmp->icmp_v6src = ac6->ac6_laddr;
36440Sstevel@tonic-gate 				(void) icmp_build_hdrs(q, icmp);
36450Sstevel@tonic-gate 			}
36460Sstevel@tonic-gate 		}
36470Sstevel@tonic-gate 		mp1 = mp1->b_cont;
36480Sstevel@tonic-gate 	}
36490Sstevel@tonic-gate 	/*
36500Sstevel@tonic-gate 	 * Look for one or more appended ACK message added by
36510Sstevel@tonic-gate 	 * icmp_connect or icmp_disconnect.
36520Sstevel@tonic-gate 	 * If none found just send up the T_BIND_ACK.
36530Sstevel@tonic-gate 	 * icmp_connect has appended a T_OK_ACK and a
36540Sstevel@tonic-gate 	 * T_CONN_CON.
36550Sstevel@tonic-gate 	 * icmp_disconnect has appended a T_OK_ACK.
36560Sstevel@tonic-gate 	 */
36570Sstevel@tonic-gate 	if (mp1 != NULL) {
36580Sstevel@tonic-gate 		if (mp->b_cont == mp1)
36590Sstevel@tonic-gate 			mp->b_cont = NULL;
36600Sstevel@tonic-gate 		else {
36610Sstevel@tonic-gate 			ASSERT(mp->b_cont->b_cont == mp1);
36620Sstevel@tonic-gate 			mp->b_cont->b_cont = NULL;
36630Sstevel@tonic-gate 		}
36640Sstevel@tonic-gate 		freemsg(mp);
36650Sstevel@tonic-gate 		mp = mp1;
36660Sstevel@tonic-gate 		while (mp != NULL) {
36670Sstevel@tonic-gate 			mp1 = mp->b_cont;
36680Sstevel@tonic-gate 			mp->b_cont = NULL;
36690Sstevel@tonic-gate 			putnext(q, mp);
36700Sstevel@tonic-gate 			mp = mp1;
36710Sstevel@tonic-gate 		}
36720Sstevel@tonic-gate 		return;
36730Sstevel@tonic-gate 	}
36740Sstevel@tonic-gate 	freemsg(mp->b_cont);
36750Sstevel@tonic-gate 	mp->b_cont = NULL;
36760Sstevel@tonic-gate 	putnext(q, mp);
36770Sstevel@tonic-gate }
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate /*
36800Sstevel@tonic-gate  * return SNMP stuff in buffer in mpdata
36810Sstevel@tonic-gate  */
36820Sstevel@tonic-gate static int
36830Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl)
36840Sstevel@tonic-gate {
36850Sstevel@tonic-gate 	mblk_t			*mpdata;
36860Sstevel@tonic-gate 	struct opthdr		*optp;
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	if (mpctl == NULL ||
36890Sstevel@tonic-gate 	    (mpdata = mpctl->b_cont) == NULL) {
36900Sstevel@tonic-gate 		return (0);
36910Sstevel@tonic-gate 	}
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate 	/* fixed length structure for IPv4 and IPv6 counters */
36940Sstevel@tonic-gate 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
36950Sstevel@tonic-gate 	optp->level = EXPER_RAWIP;
36960Sstevel@tonic-gate 	optp->name = 0;
36970Sstevel@tonic-gate 	(void) snmp_append_data(mpdata, (char *)&rawip_mib, sizeof (rawip_mib));
36980Sstevel@tonic-gate 	optp->len = msgdsize(mpdata);
36990Sstevel@tonic-gate 	qreply(q, mpctl);
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 	return (1);
37020Sstevel@tonic-gate }
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate /*
37050Sstevel@tonic-gate  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
37060Sstevel@tonic-gate  * TODO:  If this ever actually tries to set anything, it needs to be
37070Sstevel@tonic-gate  * to do the appropriate locking.
37080Sstevel@tonic-gate  */
37090Sstevel@tonic-gate /* ARGSUSED */
37100Sstevel@tonic-gate static int
37110Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
37120Sstevel@tonic-gate     uchar_t *ptr, int len)
37130Sstevel@tonic-gate {
37140Sstevel@tonic-gate 	switch (level) {
37150Sstevel@tonic-gate 	case EXPER_RAWIP:
37160Sstevel@tonic-gate 		return (0);
37170Sstevel@tonic-gate 	default:
37180Sstevel@tonic-gate 		return (1);
37190Sstevel@tonic-gate 	}
37200Sstevel@tonic-gate }
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate /* Report for ndd "icmp_status" */
37230Sstevel@tonic-gate /* ARGSUSED */
37240Sstevel@tonic-gate static int
37250Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
37260Sstevel@tonic-gate {
37270Sstevel@tonic-gate 	IDP	idp;
37280Sstevel@tonic-gate 	icmp_t	*icmp;
37290Sstevel@tonic-gate 	char	*state;
37300Sstevel@tonic-gate 	char	laddrbuf[INET6_ADDRSTRLEN];
37310Sstevel@tonic-gate 	char	faddrbuf[INET6_ADDRSTRLEN];
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
37340Sstevel@tonic-gate 	    "RAWIP    " MI_COL_HDRPAD_STR
37350Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
37360Sstevel@tonic-gate 	    "  src addr        dest addr       state");
37370Sstevel@tonic-gate 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate 	for (idp = mi_first_ptr(&icmp_g_head);
37410Sstevel@tonic-gate 	    (icmp = (icmp_t *)idp) != NULL;
37420Sstevel@tonic-gate 	    idp = mi_next_ptr(&icmp_g_head, idp)) {
37430Sstevel@tonic-gate 		if (icmp->icmp_state == TS_UNBND)
37440Sstevel@tonic-gate 			state = "UNBOUND";
37450Sstevel@tonic-gate 		else if (icmp->icmp_state == TS_IDLE)
37460Sstevel@tonic-gate 			state = "IDLE";
37470Sstevel@tonic-gate 		else if (icmp->icmp_state == TS_DATA_XFER)
37480Sstevel@tonic-gate 			state = "CONNECTED";
37490Sstevel@tonic-gate 		else
37500Sstevel@tonic-gate 			state = "UnkState";
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate 		(void) mi_mpprintf(mp,
37530Sstevel@tonic-gate 		    MI_COL_PTRFMT_STR "%s %s %s",
37540Sstevel@tonic-gate 		    (void *)icmp,
37550Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
37560Sstevel@tonic-gate 		    sizeof (faddrbuf)),
37570Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
37580Sstevel@tonic-gate 		    sizeof (laddrbuf)),
37590Sstevel@tonic-gate 		    state);
37600Sstevel@tonic-gate 	}
37610Sstevel@tonic-gate 	return (0);
37620Sstevel@tonic-gate }
37630Sstevel@tonic-gate 
37640Sstevel@tonic-gate /*
37650Sstevel@tonic-gate  * This routine creates a T_UDERROR_IND message and passes it upstream.
37660Sstevel@tonic-gate  * The address and options are copied from the T_UNITDATA_REQ message
37670Sstevel@tonic-gate  * passed in mp.  This message is freed.
37680Sstevel@tonic-gate  */
37690Sstevel@tonic-gate static void
37700Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
37710Sstevel@tonic-gate {
37720Sstevel@tonic-gate 	mblk_t	*mp1;
37730Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
37740Sstevel@tonic-gate 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
37750Sstevel@tonic-gate 
37760Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
37770Sstevel@tonic-gate 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
37780Sstevel@tonic-gate 	    tudr->OPT_length, err);
37790Sstevel@tonic-gate 	if (mp1)
37800Sstevel@tonic-gate 		qreply(q, mp1);
37810Sstevel@tonic-gate 	freemsg(mp);
37820Sstevel@tonic-gate }
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate /*
37850Sstevel@tonic-gate  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
37860Sstevel@tonic-gate  * After some error checking, the message is passed downstream to ip.
37870Sstevel@tonic-gate  */
37880Sstevel@tonic-gate static void
37890Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp)
37900Sstevel@tonic-gate {
37910Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 	/* If a bind has not been done, we can't unbind. */
37940Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
37950Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
37960Sstevel@tonic-gate 		return;
37970Sstevel@tonic-gate 	}
37980Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_v6src);
37990Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_bound_v6src);
38000Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
38010Sstevel@tonic-gate 
38020Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
38030Sstevel@tonic-gate 		int error;
38040Sstevel@tonic-gate 
38050Sstevel@tonic-gate 		/* Rebuild the header template */
38060Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
38070Sstevel@tonic-gate 		if (error != 0) {
38080Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
38090Sstevel@tonic-gate 			return;
38100Sstevel@tonic-gate 		}
38110Sstevel@tonic-gate 	}
38120Sstevel@tonic-gate 	/* Pass the unbind to IP. */
38130Sstevel@tonic-gate 	putnext(q, mp);
38140Sstevel@tonic-gate }
38150Sstevel@tonic-gate 
38160Sstevel@tonic-gate /*
38170Sstevel@tonic-gate  * Process IPv4 packets that already include an IP header.
38180Sstevel@tonic-gate  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
38190Sstevel@tonic-gate  * IPPROTO_IGMP).
38200Sstevel@tonic-gate  */
38210Sstevel@tonic-gate static void
38220Sstevel@tonic-gate icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp)
38230Sstevel@tonic-gate {
38240Sstevel@tonic-gate 	ipha_t	*ipha;
38250Sstevel@tonic-gate 	int	ip_hdr_length;
38260Sstevel@tonic-gate 	int	tp_hdr_len;
38270Sstevel@tonic-gate 	mblk_t	*mp1;
38280Sstevel@tonic-gate 	uint_t	pkt_len;
38290Sstevel@tonic-gate 
38300Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
38310Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
38320Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
38330Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
38340Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
38350Sstevel@tonic-gate 			freemsg(mp);
38360Sstevel@tonic-gate 			return;
38370Sstevel@tonic-gate 		}
38380Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
38390Sstevel@tonic-gate 	}
38400Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length =
38410Sstevel@tonic-gate 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
38420Sstevel@tonic-gate 
38430Sstevel@tonic-gate 	/*
38440Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
38450Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
38460Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
38470Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
38480Sstevel@tonic-gate 	 */
38490Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
38500Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
38510Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
38520Sstevel@tonic-gate 	/* Insert options if any */
38530Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
38540Sstevel@tonic-gate 		/*
38550Sstevel@tonic-gate 		 * Put the IP header plus any transport header that is
38560Sstevel@tonic-gate 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
38570Sstevel@tonic-gate 		 * that at least the checksum field is in the first mblk.)
38580Sstevel@tonic-gate 		 */
38590Sstevel@tonic-gate 		switch (ipha->ipha_protocol) {
38600Sstevel@tonic-gate 		case IPPROTO_UDP:
38610Sstevel@tonic-gate 			tp_hdr_len = 8;
38620Sstevel@tonic-gate 			break;
38630Sstevel@tonic-gate 		case IPPROTO_TCP:
38640Sstevel@tonic-gate 			tp_hdr_len = 20;
38650Sstevel@tonic-gate 			break;
38660Sstevel@tonic-gate 		default:
38670Sstevel@tonic-gate 			tp_hdr_len = 0;
38680Sstevel@tonic-gate 			break;
38690Sstevel@tonic-gate 		}
38700Sstevel@tonic-gate 		/*
38710Sstevel@tonic-gate 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
38720Sstevel@tonic-gate 		 * tp_hdr_len bytes will be in a single mblk.
38730Sstevel@tonic-gate 		 */
38740Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
38750Sstevel@tonic-gate 		    tp_hdr_len)) {
38760Sstevel@tonic-gate 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
38770Sstevel@tonic-gate 			    tp_hdr_len)) {
38780Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
38790Sstevel@tonic-gate 				freemsg(mp);
38800Sstevel@tonic-gate 				return;
38810Sstevel@tonic-gate 			}
38820Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
38830Sstevel@tonic-gate 		}
38840Sstevel@tonic-gate 
38850Sstevel@tonic-gate 		/*
38860Sstevel@tonic-gate 		 * if the length is larger then the max allowed IP packet,
38870Sstevel@tonic-gate 		 * then send an error and abort the processing.
38880Sstevel@tonic-gate 		 */
38890Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length)
38900Sstevel@tonic-gate 		    + icmp->icmp_ip_snd_options_len;
38910Sstevel@tonic-gate 		if (pkt_len > IP_MAXPACKET) {
38920Sstevel@tonic-gate 			icmp_ud_err(q, mp, EMSGSIZE);
38930Sstevel@tonic-gate 			return;
38940Sstevel@tonic-gate 		}
38950Sstevel@tonic-gate 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra +
38960Sstevel@tonic-gate 		    tp_hdr_len, BPRI_LO))) {
38970Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
38980Sstevel@tonic-gate 			return;
38990Sstevel@tonic-gate 		}
39000Sstevel@tonic-gate 		mp1->b_rptr += icmp_wroff_extra;
39010Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
39020Sstevel@tonic-gate 
39030Sstevel@tonic-gate 		ipha->ipha_length = htons((uint16_t)pkt_len);
39040Sstevel@tonic-gate 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 		/* Copy transport header if any */
39070Sstevel@tonic-gate 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
39080Sstevel@tonic-gate 		mp1->b_wptr += tp_hdr_len;
39090Sstevel@tonic-gate 
39100Sstevel@tonic-gate 		/* Add options */
39110Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_rptr;
39120Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
39130Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len);
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 		/* Drop IP header and transport header from original */
39160Sstevel@tonic-gate 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate 		mp1->b_cont = mp;
39190Sstevel@tonic-gate 		mp = mp1;
39200Sstevel@tonic-gate 		/*
39210Sstevel@tonic-gate 		 * Massage source route putting first source
39220Sstevel@tonic-gate 		 * route in ipha_dst.
39230Sstevel@tonic-gate 		 */
39240Sstevel@tonic-gate 		(void) ip_massage_options(ipha);
39250Sstevel@tonic-gate 	}
3926*1676Sjpk 	mblk_setcred(mp, icmp->icmp_credp);
39270Sstevel@tonic-gate 	putnext(q, mp);
39280Sstevel@tonic-gate }
39290Sstevel@tonic-gate 
3930*1676Sjpk static boolean_t
3931*1676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst)
3932*1676Sjpk {
3933*1676Sjpk 	int err;
3934*1676Sjpk 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
3935*1676Sjpk 
3936*1676Sjpk 	err = tsol_compute_label(DB_CREDDEF(mp, icmp->icmp_credp), dst,
3937*1676Sjpk 	    opt_storage, icmp->icmp_mac_exempt);
3938*1676Sjpk 	if (err == 0) {
3939*1676Sjpk 		err = tsol_update_options(&icmp->icmp_ip_snd_options,
3940*1676Sjpk 		    &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len,
3941*1676Sjpk 		    opt_storage);
3942*1676Sjpk 	}
3943*1676Sjpk 	if (err != 0) {
3944*1676Sjpk 		BUMP_MIB(&rawip_mib, rawipOutErrors);
3945*1676Sjpk 		DTRACE_PROBE4(
3946*1676Sjpk 		    tx__ip__log__drop__updatelabel__icmp,
3947*1676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
3948*1676Sjpk 		    queue_t *, q, char *, opt_storage, mblk_t *, mp);
3949*1676Sjpk 		icmp_ud_err(q, mp, err);
3950*1676Sjpk 		return (B_FALSE);
3951*1676Sjpk 	}
3952*1676Sjpk 	IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst);
3953*1676Sjpk 	return (B_TRUE);
3954*1676Sjpk }
3955*1676Sjpk 
39560Sstevel@tonic-gate /*
39570Sstevel@tonic-gate  * This routine handles all messages passed downstream.  It either
39580Sstevel@tonic-gate  * consumes the message or passes it downstream; it never queues a
39590Sstevel@tonic-gate  * a message.
39600Sstevel@tonic-gate  */
39610Sstevel@tonic-gate static void
39620Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp)
39630Sstevel@tonic-gate {
39640Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
39650Sstevel@tonic-gate 	ipha_t	*ipha;
39660Sstevel@tonic-gate 	mblk_t	*mp1;
39670Sstevel@tonic-gate 	int	ip_hdr_length;
39680Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
39690Sstevel@tonic-gate 	size_t	ip_len;
39700Sstevel@tonic-gate 	icmp_t	*icmp;
39710Sstevel@tonic-gate 	sin6_t	*sin6;
39720Sstevel@tonic-gate 	sin_t	*sin;
39730Sstevel@tonic-gate 	ipaddr_t	v4dst;
39740Sstevel@tonic-gate 
39750Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
39760Sstevel@tonic-gate 	if (icmp->icmp_restricted) {
39770Sstevel@tonic-gate 		icmp_wput_restricted(q, mp);
39780Sstevel@tonic-gate 		return;
39790Sstevel@tonic-gate 	}
39800Sstevel@tonic-gate 
39810Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
39820Sstevel@tonic-gate 	case M_DATA:
39830Sstevel@tonic-gate 		if (icmp->icmp_hdrincl) {
39840Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3985*1676Sjpk 			ipha = (ipha_t *)mp->b_rptr;
3986*1676Sjpk 			if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
3987*1676Sjpk 				if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
3988*1676Sjpk 					BUMP_MIB(&rawip_mib, rawipOutErrors);
3989*1676Sjpk 					freemsg(mp);
3990*1676Sjpk 					return;
3991*1676Sjpk 				}
3992*1676Sjpk 				ipha = (ipha_t *)mp->b_rptr;
3993*1676Sjpk 			}
3994*1676Sjpk 			/*
3995*1676Sjpk 			 * If this connection was used for v6 (inconceivable!)
3996*1676Sjpk 			 * or if we have a new destination, then it's time to
3997*1676Sjpk 			 * figure a new label.
3998*1676Sjpk 			 */
3999*1676Sjpk 			if (is_system_labeled() &&
4000*1676Sjpk 			    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
4001*1676Sjpk 			    V4_PART_OF_V6(icmp->icmp_v6lastdst) !=
4002*1676Sjpk 			    ipha->ipha_dst) &&
4003*1676Sjpk 			    !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) {
4004*1676Sjpk 				return;
4005*1676Sjpk 			}
40060Sstevel@tonic-gate 			icmp_wput_hdrincl(q, mp, icmp);
40070Sstevel@tonic-gate 			return;
40080Sstevel@tonic-gate 		}
40090Sstevel@tonic-gate 		freemsg(mp);
40100Sstevel@tonic-gate 		return;
40110Sstevel@tonic-gate 	case M_PROTO:
40120Sstevel@tonic-gate 	case M_PCPROTO:
40130Sstevel@tonic-gate 		ip_len = mp->b_wptr - rptr;
40140Sstevel@tonic-gate 		if (ip_len >= sizeof (struct T_unitdata_req)) {
40150Sstevel@tonic-gate 			/* Expedite valid T_UNITDATA_REQ to below the switch */
40160Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type
40170Sstevel@tonic-gate 			    == T_UNITDATA_REQ)
40180Sstevel@tonic-gate 				break;
40190Sstevel@tonic-gate 		}
40200Sstevel@tonic-gate 		/* FALLTHRU */
40210Sstevel@tonic-gate 	default:
40220Sstevel@tonic-gate 		icmp_wput_other(q, mp);
40230Sstevel@tonic-gate 		return;
40240Sstevel@tonic-gate 	}
40250Sstevel@tonic-gate 
40260Sstevel@tonic-gate 	/* Handle T_UNITDATA_REQ messages here. */
40270Sstevel@tonic-gate 
40280Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
40290Sstevel@tonic-gate 		/* If a port has not been bound to the stream, fail. */
40300Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40310Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
40320Sstevel@tonic-gate 		return;
40330Sstevel@tonic-gate 	}
40340Sstevel@tonic-gate 	mp1 = mp->b_cont;
40350Sstevel@tonic-gate 	if (mp1 == NULL) {
40360Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40370Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
40380Sstevel@tonic-gate 		return;
40390Sstevel@tonic-gate 	}
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
40420Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40430Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
40440Sstevel@tonic-gate 		return;
40450Sstevel@tonic-gate 	}
40460Sstevel@tonic-gate 
40470Sstevel@tonic-gate 	switch (icmp->icmp_family) {
40480Sstevel@tonic-gate 	case AF_INET6:
40490Sstevel@tonic-gate 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
40500Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin6) ||
40510Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin6_t) ||
40520Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
40530Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40540Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
40550Sstevel@tonic-gate 			return;
40560Sstevel@tonic-gate 		}
40570Sstevel@tonic-gate 
40580Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
40590Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
40600Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40610Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
40620Sstevel@tonic-gate 			return;
40630Sstevel@tonic-gate 		}
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 		/*
40660Sstevel@tonic-gate 		 * Destination is a native IPv6 address.
40670Sstevel@tonic-gate 		 * Send out an IPv6 format packet.
40680Sstevel@tonic-gate 		 */
40690Sstevel@tonic-gate 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
40700Sstevel@tonic-gate 		return;
40710Sstevel@tonic-gate 
40720Sstevel@tonic-gate 	case AF_INET:
40730Sstevel@tonic-gate 		sin = (sin_t *)&rptr[tudr->DEST_offset];
40740Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin) ||
40750Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin_t) ||
40760Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
40770Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40780Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
40790Sstevel@tonic-gate 			return;
40800Sstevel@tonic-gate 		}
40810Sstevel@tonic-gate 		/* Extract and ipaddr */
40820Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
40830Sstevel@tonic-gate 		break;
4084*1676Sjpk 
4085*1676Sjpk 	default:
4086*1676Sjpk 		ASSERT(0);
40870Sstevel@tonic-gate 	}
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate 	/*
40900Sstevel@tonic-gate 	 * If options passed in, feed it for verification and handling
40910Sstevel@tonic-gate 	 */
40920Sstevel@tonic-gate 	if (tudr->OPT_length != 0) {
40930Sstevel@tonic-gate 		int error;
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
40960Sstevel@tonic-gate 		    (uchar_t *)0) < 0) {
40970Sstevel@tonic-gate 			/* failure */
40980Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
40990Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
41000Sstevel@tonic-gate 			return;
41010Sstevel@tonic-gate 		}
41020Sstevel@tonic-gate 		/*
41030Sstevel@tonic-gate 		 * Note: Success in processing options.
41040Sstevel@tonic-gate 		 * mp option buffer represented by
41050Sstevel@tonic-gate 		 * OPT_length/offset now potentially modified
41060Sstevel@tonic-gate 		 * and contain option setting results
41070Sstevel@tonic-gate 		 */
41080Sstevel@tonic-gate 	}
41090Sstevel@tonic-gate 
4110*1676Sjpk 	if (v4dst == INADDR_ANY)
4111*1676Sjpk 		v4dst = htonl(INADDR_LOOPBACK);
4112*1676Sjpk 
4113*1676Sjpk 	/* Check if our saved options are valid; update if not */
4114*1676Sjpk 	if (is_system_labeled() &&
4115*1676Sjpk 	    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
4116*1676Sjpk 	    V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) &&
4117*1676Sjpk 	    !icmp_update_label(q, icmp, mp, v4dst)) {
4118*1676Sjpk 		return;
4119*1676Sjpk 	}
4120*1676Sjpk 
41210Sstevel@tonic-gate 	/* Protocol 255 contains full IP headers */
41220Sstevel@tonic-gate 	if (icmp->icmp_hdrincl) {
41230Sstevel@tonic-gate 		freeb(mp);
41240Sstevel@tonic-gate 		icmp_wput_hdrincl(q, mp1, icmp);
41250Sstevel@tonic-gate 		return;
41260Sstevel@tonic-gate 	}
4127*1676Sjpk 
41280Sstevel@tonic-gate 	/* Add an IP header */
41290Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
41300Sstevel@tonic-gate 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
41310Sstevel@tonic-gate 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
41320Sstevel@tonic-gate 	    mp1->b_datap->db_ref != 1 ||
41330Sstevel@tonic-gate 	    !OK_32PTR(ipha)) {
41340Sstevel@tonic-gate 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra,
41350Sstevel@tonic-gate 		    BPRI_LO))) {
41360Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
41371289Sja97890 			icmp_ud_err(q, mp, ENOMEM);
41380Sstevel@tonic-gate 			return;
41390Sstevel@tonic-gate 		}
41400Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
41410Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_datap->db_lim;
41420Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)ipha;
41430Sstevel@tonic-gate 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
41440Sstevel@tonic-gate 	}
41450Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
41460Sstevel@tonic-gate 	/* Set version, header length, and tos */
41470Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
41480Sstevel@tonic-gate 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
41490Sstevel@tonic-gate 		icmp->icmp_type_of_service);
41500Sstevel@tonic-gate 	/* Set ttl and protocol */
41510Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
41520Sstevel@tonic-gate #else
41530Sstevel@tonic-gate 	/* Set version, header length, and tos */
41540Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
41550Sstevel@tonic-gate 	    ((icmp->icmp_type_of_service << 8) |
41560Sstevel@tonic-gate 		((IP_VERSION << 4) | (ip_hdr_length>>2)));
41570Sstevel@tonic-gate 	/* Set ttl and protocol */
41580Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
41590Sstevel@tonic-gate #endif
41600Sstevel@tonic-gate 	/*
41610Sstevel@tonic-gate 	 * Copy our address into the packet.  If this is zero,
41620Sstevel@tonic-gate 	 * ip will fill in the real source address.
41630Sstevel@tonic-gate 	 */
41640Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
41650Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
41660Sstevel@tonic-gate 
41670Sstevel@tonic-gate 	/*
41680Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
41690Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
41700Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
41710Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
41720Sstevel@tonic-gate 	 */
41730Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
41740Sstevel@tonic-gate 
41750Sstevel@tonic-gate 	/* Finish common formatting of the packet. */
41760Sstevel@tonic-gate 	mp1->b_rptr = (uchar_t *)ipha;
41770Sstevel@tonic-gate 
41780Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
41790Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
41800Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
41810Sstevel@tonic-gate 
41820Sstevel@tonic-gate 	/*
41830Sstevel@tonic-gate 	 * Set the length into the IP header.
41840Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
41850Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
41860Sstevel@tonic-gate 	 * as this can cause problems in layers below.
41870Sstevel@tonic-gate 	 */
41880Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
41890Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
41900Sstevel@tonic-gate 		icmp_ud_err(q, mp, EMSGSIZE);
41910Sstevel@tonic-gate 		return;
41920Sstevel@tonic-gate 	}
41930Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)ip_len);
41940Sstevel@tonic-gate 	/*
41950Sstevel@tonic-gate 	 * Copy in the destination address from the T_UNITDATA
41960Sstevel@tonic-gate 	 * request
41970Sstevel@tonic-gate 	 */
4198*1676Sjpk 	ipha->ipha_dst = v4dst;
41990Sstevel@tonic-gate 
42000Sstevel@tonic-gate 	/*
42010Sstevel@tonic-gate 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
42020Sstevel@tonic-gate 	 */
42030Sstevel@tonic-gate 	if (CLASSD(v4dst))
42040Sstevel@tonic-gate 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
42050Sstevel@tonic-gate 
42060Sstevel@tonic-gate 	/* Copy in options if any */
42070Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
42080Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options,
42090Sstevel@tonic-gate 		    &ipha[1], icmp->icmp_ip_snd_options_len);
42100Sstevel@tonic-gate 		/*
42110Sstevel@tonic-gate 		 * Massage source route putting first source route in ipha_dst.
42120Sstevel@tonic-gate 		 * Ignore the destination in the T_unitdata_req.
42130Sstevel@tonic-gate 		 */
42140Sstevel@tonic-gate 		(void) ip_massage_options(ipha);
42150Sstevel@tonic-gate 	}
42160Sstevel@tonic-gate 	freeb(mp);
42170Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
4218*1676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
42190Sstevel@tonic-gate 	putnext(q, mp1);
42200Sstevel@tonic-gate #undef	ipha
42210Sstevel@tonic-gate #undef tudr
42220Sstevel@tonic-gate }
42230Sstevel@tonic-gate 
4224*1676Sjpk static boolean_t
4225*1676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst)
4226*1676Sjpk {
4227*1676Sjpk 	int err;
4228*1676Sjpk 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
4229*1676Sjpk 
4230*1676Sjpk 	err = tsol_compute_label_v6(DB_CREDDEF(mp, icmp->icmp_credp), dst,
4231*1676Sjpk 	    opt_storage, icmp->icmp_mac_exempt);
4232*1676Sjpk 	if (err == 0) {
4233*1676Sjpk 		err = tsol_update_sticky(&icmp->icmp_sticky_ipp,
4234*1676Sjpk 		    &icmp->icmp_label_len_v6, opt_storage);
4235*1676Sjpk 	}
4236*1676Sjpk 	if (err != 0) {
4237*1676Sjpk 		BUMP_MIB(&rawip_mib, rawipOutErrors);
4238*1676Sjpk 		DTRACE_PROBE4(
4239*1676Sjpk 		    tx__ip__log__drop__updatelabel__icmp6,
4240*1676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
4241*1676Sjpk 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
4242*1676Sjpk 		icmp_ud_err(wq, mp, err);
4243*1676Sjpk 		return (B_FALSE);
4244*1676Sjpk 	}
4245*1676Sjpk 
4246*1676Sjpk 	icmp->icmp_v6lastdst = *dst;
4247*1676Sjpk 	return (B_TRUE);
4248*1676Sjpk }
4249*1676Sjpk 
42500Sstevel@tonic-gate /*
42510Sstevel@tonic-gate  * icmp_wput_ipv6():
42520Sstevel@tonic-gate  * Assumes that icmp_wput did some sanity checking on the destination
4253*1676Sjpk  * address, but that the label may not yet be correct.
42540Sstevel@tonic-gate  */
42550Sstevel@tonic-gate void
42560Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
42570Sstevel@tonic-gate {
42580Sstevel@tonic-gate 	ip6_t			*ip6h;
42590Sstevel@tonic-gate 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
42600Sstevel@tonic-gate 	mblk_t			*mp1;
42610Sstevel@tonic-gate 	int			ip_hdr_len = IPV6_HDR_LEN;
42620Sstevel@tonic-gate 	size_t			ip_len;
42630Sstevel@tonic-gate 	icmp_t			*icmp;
42640Sstevel@tonic-gate 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
42650Sstevel@tonic-gate 	ip6_pkt_t		*ipp = &ipp_s;
42660Sstevel@tonic-gate 	ip6_pkt_t		*tipp;
42670Sstevel@tonic-gate 	uint32_t		csum = 0;
42680Sstevel@tonic-gate 	uint_t			ignore = 0;
42690Sstevel@tonic-gate 	uint_t			option_exists = 0, is_sticky = 0;
42700Sstevel@tonic-gate 	uint8_t			*cp;
42710Sstevel@tonic-gate 	uint8_t			*nxthdr_ptr;
4272*1676Sjpk 	in6_addr_t		ip6_dst;
42730Sstevel@tonic-gate 
42740Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 	/*
42770Sstevel@tonic-gate 	 * If the local address is a mapped address return
42780Sstevel@tonic-gate 	 * an error.
42790Sstevel@tonic-gate 	 * It would be possible to send an IPv6 packet but the
42800Sstevel@tonic-gate 	 * response would never make it back to the application
42810Sstevel@tonic-gate 	 * since it is bound to a mapped address.
42820Sstevel@tonic-gate 	 */
42830Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
42840Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
42850Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
42860Sstevel@tonic-gate 		return;
42870Sstevel@tonic-gate 	}
42880Sstevel@tonic-gate 
42890Sstevel@tonic-gate 	ipp->ipp_fields = 0;
42900Sstevel@tonic-gate 	ipp->ipp_sticky_ignored = 0;
42910Sstevel@tonic-gate 
42920Sstevel@tonic-gate 	/*
42930Sstevel@tonic-gate 	 * If TPI options passed in, feed it for verification and handling
42940Sstevel@tonic-gate 	 */
42950Sstevel@tonic-gate 	if (tudr_optlen != 0) {
42960Sstevel@tonic-gate 		int error;
42970Sstevel@tonic-gate 
42980Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
42990Sstevel@tonic-gate 		    (void *)ipp) < 0) {
43000Sstevel@tonic-gate 			/* failure */
43010Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
43020Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
43030Sstevel@tonic-gate 			return;
43040Sstevel@tonic-gate 		}
43050Sstevel@tonic-gate 		ignore = ipp->ipp_sticky_ignored;
43060Sstevel@tonic-gate 		ASSERT(error == 0);
43070Sstevel@tonic-gate 	}
43080Sstevel@tonic-gate 
43090Sstevel@tonic-gate 	if (sin6->sin6_scope_id != 0 &&
43100Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
43110Sstevel@tonic-gate 		/*
43120Sstevel@tonic-gate 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
43130Sstevel@tonic-gate 		 * option nor ancillary data.  It needs to be
43140Sstevel@tonic-gate 		 * explicitly set in options_exists.
43150Sstevel@tonic-gate 		 */
43160Sstevel@tonic-gate 		option_exists |= IPPF_SCOPE_ID;
43170Sstevel@tonic-gate 	}
43180Sstevel@tonic-gate 
4319*1676Sjpk 	/*
4320*1676Sjpk 	 * Compute the destination address
4321*1676Sjpk 	 */
4322*1676Sjpk 	ip6_dst = sin6->sin6_addr;
4323*1676Sjpk 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
4324*1676Sjpk 		ip6_dst = ipv6_loopback;
4325*1676Sjpk 
4326*1676Sjpk 	/*
4327*1676Sjpk 	 * If we're not going to the same destination as last time, then
4328*1676Sjpk 	 * recompute the label required.  This is done in a separate routine to
4329*1676Sjpk 	 * avoid blowing up our stack here.
4330*1676Sjpk 	 */
4331*1676Sjpk 	if (is_system_labeled() &&
4332*1676Sjpk 	    !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) &&
4333*1676Sjpk 	    !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) {
4334*1676Sjpk 		return;
4335*1676Sjpk 	}
4336*1676Sjpk 
4337*1676Sjpk 	/*
4338*1676Sjpk 	 * If there's a security label here, then we ignore any options the
4339*1676Sjpk 	 * user may try to set.  We keep the peer's label as a hidden sticky
4340*1676Sjpk 	 * option.
4341*1676Sjpk 	 */
4342*1676Sjpk 	if (icmp->icmp_label_len_v6 > 0) {
4343*1676Sjpk 		ignore &= ~IPPF_HOPOPTS;
4344*1676Sjpk 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
4345*1676Sjpk 	}
4346*1676Sjpk 
43470Sstevel@tonic-gate 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
43480Sstevel@tonic-gate 	    (ipp->ipp_fields == 0)) {
43490Sstevel@tonic-gate 		/* No sticky options nor ancillary data. */
43500Sstevel@tonic-gate 		goto no_options;
43510Sstevel@tonic-gate 	}
43520Sstevel@tonic-gate 
43530Sstevel@tonic-gate 	/*
43540Sstevel@tonic-gate 	 * Go through the options figuring out where each is going to
43550Sstevel@tonic-gate 	 * come from and build two masks.  The first mask indicates if
43560Sstevel@tonic-gate 	 * the option exists at all.  The second mask indicates if the
43570Sstevel@tonic-gate 	 * option is sticky or ancillary.
43580Sstevel@tonic-gate 	 */
43590Sstevel@tonic-gate 	if (!(ignore & IPPF_HOPOPTS)) {
43600Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
43610Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
43620Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_hopoptslen;
43630Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
43640Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
43650Sstevel@tonic-gate 			is_sticky |= IPPF_HOPOPTS;
43660Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
43670Sstevel@tonic-gate 		}
43680Sstevel@tonic-gate 	}
43690Sstevel@tonic-gate 
43700Sstevel@tonic-gate 	if (!(ignore & IPPF_RTHDR)) {
43710Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTHDR) {
43720Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
43730Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rthdrlen;
43740Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
43750Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
43760Sstevel@tonic-gate 			is_sticky |= IPPF_RTHDR;
43770Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
43780Sstevel@tonic-gate 		}
43790Sstevel@tonic-gate 	}
43800Sstevel@tonic-gate 
43810Sstevel@tonic-gate 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
43820Sstevel@tonic-gate 		/*
43830Sstevel@tonic-gate 		 * Need to have a router header to use these.
43840Sstevel@tonic-gate 		 */
43850Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
43860Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
43870Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rtdstoptslen;
43880Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
43890Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
43900Sstevel@tonic-gate 			is_sticky |= IPPF_RTDSTOPTS;
43910Sstevel@tonic-gate 			ip_hdr_len +=
43920Sstevel@tonic-gate 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
43930Sstevel@tonic-gate 		}
43940Sstevel@tonic-gate 	}
43950Sstevel@tonic-gate 
43960Sstevel@tonic-gate 	if (!(ignore & IPPF_DSTOPTS)) {
43970Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
43980Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
43990Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_dstoptslen;
44000Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
44010Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
44020Sstevel@tonic-gate 			is_sticky |= IPPF_DSTOPTS;
44030Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
44040Sstevel@tonic-gate 		}
44050Sstevel@tonic-gate 	}
44060Sstevel@tonic-gate 
44070Sstevel@tonic-gate 	if (!(ignore & IPPF_IFINDEX)) {
44080Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_IFINDEX) {
44090Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
44100Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
44110Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
44120Sstevel@tonic-gate 			is_sticky |= IPPF_IFINDEX;
44130Sstevel@tonic-gate 		}
44140Sstevel@tonic-gate 	}
44150Sstevel@tonic-gate 
44160Sstevel@tonic-gate 	if (!(ignore & IPPF_ADDR)) {
44170Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_ADDR) {
44180Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
44190Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
44200Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
44210Sstevel@tonic-gate 			is_sticky |= IPPF_ADDR;
44220Sstevel@tonic-gate 		}
44230Sstevel@tonic-gate 	}
44240Sstevel@tonic-gate 
44250Sstevel@tonic-gate 	if (!(ignore & IPPF_DONTFRAG)) {
44260Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
44270Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
44280Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
44290Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
44300Sstevel@tonic-gate 			is_sticky |= IPPF_DONTFRAG;
44310Sstevel@tonic-gate 		}
44320Sstevel@tonic-gate 	}
44330Sstevel@tonic-gate 
44340Sstevel@tonic-gate 	if (!(ignore & IPPF_USE_MIN_MTU)) {
44350Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
44360Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
44370Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
44380Sstevel@tonic-gate 		    IPPF_USE_MIN_MTU) {
44390Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
44400Sstevel@tonic-gate 			is_sticky |= IPPF_USE_MIN_MTU;
44410Sstevel@tonic-gate 		}
44420Sstevel@tonic-gate 	}
44430Sstevel@tonic-gate 
44440Sstevel@tonic-gate 	if (!(ignore & IPPF_NEXTHOP)) {
44450Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
44460Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
44470Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
44480Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
44490Sstevel@tonic-gate 			is_sticky |= IPPF_NEXTHOP;
44500Sstevel@tonic-gate 		}
44510Sstevel@tonic-gate 	}
44520Sstevel@tonic-gate 
4453679Sseb 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4454679Sseb 		option_exists |= IPPF_HOPLIMIT;
4455679Sseb 	/* IPV6_HOPLIMIT can never be sticky */
4456679Sseb 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4457679Sseb 
4458679Sseb 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4459679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4460679Sseb 		option_exists |= IPPF_UNICAST_HOPS;
4461679Sseb 		is_sticky |= IPPF_UNICAST_HOPS;
4462679Sseb 	}
4463679Sseb 
4464679Sseb 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4465679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4466679Sseb 		option_exists |= IPPF_MULTICAST_HOPS;
4467679Sseb 		is_sticky |= IPPF_MULTICAST_HOPS;
44680Sstevel@tonic-gate 	}
44690Sstevel@tonic-gate 
44700Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
44710Sstevel@tonic-gate 		/* This is a sticky socket option only */
44720Sstevel@tonic-gate 		option_exists |= IPPF_NO_CKSUM;
44730Sstevel@tonic-gate 		is_sticky |= IPPF_NO_CKSUM;
44740Sstevel@tonic-gate 	}
44750Sstevel@tonic-gate 
44760Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
44770Sstevel@tonic-gate 		/* This is a sticky socket option only */
44780Sstevel@tonic-gate 		option_exists |= IPPF_RAW_CKSUM;
44790Sstevel@tonic-gate 		is_sticky |= IPPF_RAW_CKSUM;
44800Sstevel@tonic-gate 	}
44810Sstevel@tonic-gate 
44820Sstevel@tonic-gate 	if (!(ignore & IPPF_TCLASS)) {
44830Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_TCLASS) {
44840Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
44850Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
44860Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
44870Sstevel@tonic-gate 			is_sticky |= IPPF_TCLASS;
44880Sstevel@tonic-gate 		}
44890Sstevel@tonic-gate 	}
44900Sstevel@tonic-gate 
44910Sstevel@tonic-gate no_options:
44920Sstevel@tonic-gate 
44930Sstevel@tonic-gate 	/*
44940Sstevel@tonic-gate 	 * If any options carried in the ip6i_t were specified, we
44950Sstevel@tonic-gate 	 * need to account for the ip6i_t in the data we'll be sending
44960Sstevel@tonic-gate 	 * down.
44970Sstevel@tonic-gate 	 */
44980Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I)
44990Sstevel@tonic-gate 		ip_hdr_len += sizeof (ip6i_t);
45000Sstevel@tonic-gate 
45010Sstevel@tonic-gate 	/* check/fix buffer config, setup pointers into it */
45020Sstevel@tonic-gate 	mp1 = mp->b_cont;
45030Sstevel@tonic-gate 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
45040Sstevel@tonic-gate 	if ((mp1->b_datap->db_ref != 1) ||
45050Sstevel@tonic-gate 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
45060Sstevel@tonic-gate 	    !OK_32PTR(ip6h)) {
45070Sstevel@tonic-gate 		/* Try to get everything in a single mblk next time */
45080Sstevel@tonic-gate 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
45090Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = ip_hdr_len;
45100Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q),
45110Sstevel@tonic-gate 			    icmp->icmp_max_hdr_len + icmp_wroff_extra);
45120Sstevel@tonic-gate 		}
45130Sstevel@tonic-gate 		mp1 = allocb(ip_hdr_len + icmp_wroff_extra, BPRI_LO);
45140Sstevel@tonic-gate 		if (!mp1) {
45150Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
45160Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
45170Sstevel@tonic-gate 			return;
45180Sstevel@tonic-gate 		}
45190Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
45200Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_datap->db_lim;
45210Sstevel@tonic-gate 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
45220Sstevel@tonic-gate 	}
45230Sstevel@tonic-gate 	mp1->b_rptr = (unsigned char *)ip6h;
45240Sstevel@tonic-gate 	ip6i = (ip6i_t *)ip6h;
45250Sstevel@tonic-gate 
45260Sstevel@tonic-gate #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
45270Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I) {
45280Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
45290Sstevel@tonic-gate 		ip6i->ip6i_flags = 0;
45300Sstevel@tonic-gate 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
45330Sstevel@tonic-gate 		if (option_exists & IPPF_SCOPE_ID) {
45340Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
45350Sstevel@tonic-gate 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
45360Sstevel@tonic-gate 		} else if (option_exists & IPPF_IFINDEX) {
45370Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
45380Sstevel@tonic-gate 			ASSERT(tipp->ipp_ifindex != 0);
45390Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
45400Sstevel@tonic-gate 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
45410Sstevel@tonic-gate 		}
45420Sstevel@tonic-gate 
45430Sstevel@tonic-gate 		if (option_exists & IPPF_RAW_CKSUM) {
45440Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
45450Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
45460Sstevel@tonic-gate 		}
45470Sstevel@tonic-gate 
45480Sstevel@tonic-gate 		if (option_exists & IPPF_NO_CKSUM) {
45490Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
45500Sstevel@tonic-gate 		}
45510Sstevel@tonic-gate 
45520Sstevel@tonic-gate 		if (option_exists & IPPF_ADDR) {
45530Sstevel@tonic-gate 			/*
45540Sstevel@tonic-gate 			 * Enable per-packet source address verification if
45550Sstevel@tonic-gate 			 * IPV6_PKTINFO specified the source address.
45560Sstevel@tonic-gate 			 * ip6_src is set in the transport's _wput function.
45570Sstevel@tonic-gate 			 */
45580Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
45590Sstevel@tonic-gate 		}
45600Sstevel@tonic-gate 
45610Sstevel@tonic-gate 		if (option_exists & IPPF_DONTFRAG) {
45620Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
45630Sstevel@tonic-gate 		}
45640Sstevel@tonic-gate 
45650Sstevel@tonic-gate 		if (option_exists & IPPF_USE_MIN_MTU) {
45660Sstevel@tonic-gate 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
45670Sstevel@tonic-gate 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
45680Sstevel@tonic-gate 		}
45690Sstevel@tonic-gate 
45700Sstevel@tonic-gate 		if (option_exists & IPPF_NEXTHOP) {
45710Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
45720Sstevel@tonic-gate 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
45730Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
45740Sstevel@tonic-gate 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
45750Sstevel@tonic-gate 		}
45760Sstevel@tonic-gate 
45770Sstevel@tonic-gate 		/*
45780Sstevel@tonic-gate 		 * tell IP this is an ip6i_t private header
45790Sstevel@tonic-gate 		 */
45800Sstevel@tonic-gate 		ip6i->ip6i_nxt = IPPROTO_RAW;
45810Sstevel@tonic-gate 	}
45820Sstevel@tonic-gate 
45830Sstevel@tonic-gate 	/* Initialize IPv6 header */
45840Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
45850Sstevel@tonic-gate 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
45860Sstevel@tonic-gate 
4587679Sseb 	/* Set the hoplimit of the outgoing packet. */
45880Sstevel@tonic-gate 	if (option_exists & IPPF_HOPLIMIT) {
4589679Sseb 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4590679Sseb 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4591679Sseb 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4592679Sseb 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
45930Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4594679Sseb 		if (option_exists & IPPF_MULTICAST_HOPS)
4595679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
45960Sstevel@tonic-gate 	} else {
45970Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_ttl;
4598679Sseb 		if (option_exists & IPPF_UNICAST_HOPS)
4599679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
46000Sstevel@tonic-gate 	}
46010Sstevel@tonic-gate 
46020Sstevel@tonic-gate 	if (option_exists & IPPF_ADDR) {
46030Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
46040Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
46050Sstevel@tonic-gate 		ip6h->ip6_src = tipp->ipp_addr;
46060Sstevel@tonic-gate 	} else {
46070Sstevel@tonic-gate 		/*
46080Sstevel@tonic-gate 		 * The source address was not set using IPV6_PKTINFO.
46090Sstevel@tonic-gate 		 * First look at the bound source.
46100Sstevel@tonic-gate 		 * If unspecified fallback to __sin6_src_id.
46110Sstevel@tonic-gate 		 */
46120Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
46130Sstevel@tonic-gate 		if (sin6->__sin6_src_id != 0 &&
46140Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
46150Sstevel@tonic-gate 			ip_srcid_find_id(sin6->__sin6_src_id,
46160Sstevel@tonic-gate 			    &ip6h->ip6_src, icmp->icmp_zoneid);
46170Sstevel@tonic-gate 		}
46180Sstevel@tonic-gate 	}
46190Sstevel@tonic-gate 
46200Sstevel@tonic-gate 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
46210Sstevel@tonic-gate 	cp = (uint8_t *)&ip6h[1];
46220Sstevel@tonic-gate 
46230Sstevel@tonic-gate 	/*
46240Sstevel@tonic-gate 	 * Here's where we have to start stringing together
46250Sstevel@tonic-gate 	 * any extension headers in the right order:
46260Sstevel@tonic-gate 	 * Hop-by-hop, destination, routing, and final destination opts.
46270Sstevel@tonic-gate 	 */
46280Sstevel@tonic-gate 	if (option_exists & IPPF_HOPOPTS) {
46290Sstevel@tonic-gate 		/* Hop-by-hop options */
46300Sstevel@tonic-gate 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
46310Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
46320Sstevel@tonic-gate 
46330Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_HOPOPTS;
46340Sstevel@tonic-gate 		nxthdr_ptr = &hbh->ip6h_nxt;
46350Sstevel@tonic-gate 
46360Sstevel@tonic-gate 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
46370Sstevel@tonic-gate 		cp += tipp->ipp_hopoptslen;
46380Sstevel@tonic-gate 	}
46390Sstevel@tonic-gate 	/*
46400Sstevel@tonic-gate 	 * En-route destination options
46410Sstevel@tonic-gate 	 * Only do them if there's a routing header as well
46420Sstevel@tonic-gate 	 */
46430Sstevel@tonic-gate 	if (option_exists & IPPF_RTDSTOPTS) {
46440Sstevel@tonic-gate 		ip6_dest_t *dst = (ip6_dest_t *)cp;
46450Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
46460Sstevel@tonic-gate 
46470Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
46480Sstevel@tonic-gate 		nxthdr_ptr = &dst->ip6d_nxt;
46490Sstevel@tonic-gate 
46500Sstevel@tonic-gate 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
46510Sstevel@tonic-gate 		cp += tipp->ipp_rtdstoptslen;
46520Sstevel@tonic-gate 	}
46530Sstevel@tonic-gate 	/*
46540Sstevel@tonic-gate 	 * Routing header next
46550Sstevel@tonic-gate 	 */
46560Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
46570Sstevel@tonic-gate 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
46580Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
46590Sstevel@tonic-gate 
46600Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_ROUTING;
46610Sstevel@tonic-gate 		nxthdr_ptr = &rt->ip6r_nxt;
46620Sstevel@tonic-gate 
46630Sstevel@tonic-gate 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
46640Sstevel@tonic-gate 		cp += tipp->ipp_rthdrlen;
46650Sstevel@tonic-gate 	}
46660Sstevel@tonic-gate 	/*
46670Sstevel@tonic-gate 	 * Do ultimate destination options
46680Sstevel@tonic-gate 	 */
46690Sstevel@tonic-gate 	if (option_exists & IPPF_DSTOPTS) {
46700Sstevel@tonic-gate 		ip6_dest_t *dest = (ip6_dest_t *)cp;
46710Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
46740Sstevel@tonic-gate 		nxthdr_ptr = &dest->ip6d_nxt;
46750Sstevel@tonic-gate 
46760Sstevel@tonic-gate 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
46770Sstevel@tonic-gate 		cp += tipp->ipp_dstoptslen;
46780Sstevel@tonic-gate 	}
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate 	/*
46810Sstevel@tonic-gate 	 * Now set the last header pointer to the proto passed in
46820Sstevel@tonic-gate 	 */
46830Sstevel@tonic-gate 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
46840Sstevel@tonic-gate 	*nxthdr_ptr = icmp->icmp_proto;
46850Sstevel@tonic-gate 
46860Sstevel@tonic-gate 	/*
46870Sstevel@tonic-gate 	 * Copy in the destination address
46880Sstevel@tonic-gate 	 */
4689*1676Sjpk 	ip6h->ip6_dst = ip6_dst;
46900Sstevel@tonic-gate 
46910Sstevel@tonic-gate 	ip6h->ip6_vcf =
46920Sstevel@tonic-gate 		(IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
46930Sstevel@tonic-gate 		(sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
46940Sstevel@tonic-gate 
46950Sstevel@tonic-gate 	if (option_exists & IPPF_TCLASS) {
46960Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
46970Sstevel@tonic-gate 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
46980Sstevel@tonic-gate 		    tipp->ipp_tclass);
46990Sstevel@tonic-gate 	}
47000Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
47010Sstevel@tonic-gate 		ip6_rthdr_t	*rth;
47020Sstevel@tonic-gate 
47030Sstevel@tonic-gate 		/*
47040Sstevel@tonic-gate 		 * Perform any processing needed for source routing.
47050Sstevel@tonic-gate 		 * We know that all extension headers will be in the same mblk
47060Sstevel@tonic-gate 		 * as the IPv6 header.
47070Sstevel@tonic-gate 		 */
47080Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
47090Sstevel@tonic-gate 		if (rth != NULL && rth->ip6r_segleft != 0) {
47100Sstevel@tonic-gate 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
47110Sstevel@tonic-gate 				/*
47120Sstevel@tonic-gate 				 * Drop packet - only support Type 0 routing.
47130Sstevel@tonic-gate 				 * Notify the application as well.
47140Sstevel@tonic-gate 				 */
47150Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
47160Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47170Sstevel@tonic-gate 				return;
47180Sstevel@tonic-gate 			}
47190Sstevel@tonic-gate 			/*
47200Sstevel@tonic-gate 			 * rth->ip6r_len is twice the number of
47210Sstevel@tonic-gate 			 * addresses in the header
47220Sstevel@tonic-gate 			 */
47230Sstevel@tonic-gate 			if (rth->ip6r_len & 0x1) {
47240Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
47250Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47260Sstevel@tonic-gate 				return;
47270Sstevel@tonic-gate 			}
47280Sstevel@tonic-gate 			/*
47290Sstevel@tonic-gate 			 * Shuffle the routing header and ip6_dst
47300Sstevel@tonic-gate 			 * addresses, and get the checksum difference
47310Sstevel@tonic-gate 			 * between the first hop (in ip6_dst) and
47320Sstevel@tonic-gate 			 * the destination (in the last routing hdr entry).
47330Sstevel@tonic-gate 			 */
47340Sstevel@tonic-gate 			csum = ip_massage_options_v6(ip6h, rth);
47350Sstevel@tonic-gate 			/*
47360Sstevel@tonic-gate 			 * Verify that the first hop isn't a mapped address.
47370Sstevel@tonic-gate 			 * Routers along the path need to do this verification
47380Sstevel@tonic-gate 			 * for subsequent hops.
47390Sstevel@tonic-gate 			 */
47400Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
47410Sstevel@tonic-gate 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
47420Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47430Sstevel@tonic-gate 				return;
47440Sstevel@tonic-gate 			}
47450Sstevel@tonic-gate 		}
47460Sstevel@tonic-gate 	}
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
47490Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
47500Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
47510Sstevel@tonic-gate 
47520Sstevel@tonic-gate 	/*
47530Sstevel@tonic-gate 	 * Set the length into the IP header.
47540Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
47550Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
47560Sstevel@tonic-gate 	 * as this can cause problems in layers below.
47570Sstevel@tonic-gate 	 */
47580Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
47590Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
47601289Sja97890 		icmp_ud_err(q, mp, EMSGSIZE);
47610Sstevel@tonic-gate 		return;
47620Sstevel@tonic-gate 	}
47630Sstevel@tonic-gate 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
47640Sstevel@tonic-gate 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
47650Sstevel@tonic-gate 		uint16_t *cksum_ptr;
47660Sstevel@tonic-gate 		uint_t	ext_hdrs_len;
47670Sstevel@tonic-gate 
47680Sstevel@tonic-gate 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
47690Sstevel@tonic-gate 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
47700Sstevel@tonic-gate 		    icmp->icmp_checksum_off == 2);
47710Sstevel@tonic-gate 
47720Sstevel@tonic-gate 		/*
47730Sstevel@tonic-gate 		 * We make it easy for IP to include our pseudo header
47740Sstevel@tonic-gate 		 * by putting our length in uh_checksum, modified (if
47750Sstevel@tonic-gate 		 * we have a routing header) by the checksum difference
47760Sstevel@tonic-gate 		 * between the ultimate destination and first hop addresses.
47770Sstevel@tonic-gate 		 * Note: ICMPv6 must always checksum the packet.
47780Sstevel@tonic-gate 		 */
47790Sstevel@tonic-gate 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
47800Sstevel@tonic-gate 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
47810Sstevel@tonic-gate 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
47820Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
47830Sstevel@tonic-gate 				freemsg(mp);
47840Sstevel@tonic-gate 				return;
47850Sstevel@tonic-gate 			}
47860Sstevel@tonic-gate 			ip6i = (ip6i_t *)mp1->b_rptr;
47870Sstevel@tonic-gate 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
47880Sstevel@tonic-gate 				ip6h = (ip6_t *)&ip6i[1];
47890Sstevel@tonic-gate 			else
47900Sstevel@tonic-gate 				ip6h = (ip6_t *)ip6i;
47910Sstevel@tonic-gate 		}
47920Sstevel@tonic-gate 		/* Add payload length to checksum */
47930Sstevel@tonic-gate 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
47940Sstevel@tonic-gate 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
47950Sstevel@tonic-gate 		csum += htons(ip_len - ext_hdrs_len);
47960Sstevel@tonic-gate 
47970Sstevel@tonic-gate 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
47980Sstevel@tonic-gate 		csum = (csum & 0xFFFF) + (csum >> 16);
47990Sstevel@tonic-gate 		*cksum_ptr = (uint16_t)csum;
48000Sstevel@tonic-gate 	}
48010Sstevel@tonic-gate 
48020Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
48030Sstevel@tonic-gate 	ip_len = htons(ip_len);
48040Sstevel@tonic-gate #endif
48050Sstevel@tonic-gate 	ip6h->ip6_plen = (uint16_t)ip_len;
48060Sstevel@tonic-gate 
48070Sstevel@tonic-gate 	freeb(mp);
48080Sstevel@tonic-gate 
48090Sstevel@tonic-gate 	/* We're done. Pass the packet to IP */
48100Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
4811*1676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
48120Sstevel@tonic-gate 	putnext(q, mp1);
48130Sstevel@tonic-gate }
48140Sstevel@tonic-gate 
48150Sstevel@tonic-gate static void
48160Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp)
48170Sstevel@tonic-gate {
48180Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
48190Sstevel@tonic-gate 	struct iocblk *iocp;
48200Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
48210Sstevel@tonic-gate 	icmp_t	*icmp;
48220Sstevel@tonic-gate 	cred_t *cr;
48230Sstevel@tonic-gate 
48240Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
48250Sstevel@tonic-gate 
48260Sstevel@tonic-gate 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
48270Sstevel@tonic-gate 
48280Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
48290Sstevel@tonic-gate 	case M_PROTO:
48300Sstevel@tonic-gate 	case M_PCPROTO:
48310Sstevel@tonic-gate 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
48320Sstevel@tonic-gate 			/*
48330Sstevel@tonic-gate 			 * If the message does not contain a PRIM_type,
48340Sstevel@tonic-gate 			 * throw it away.
48350Sstevel@tonic-gate 			 */
48360Sstevel@tonic-gate 			freemsg(mp);
48370Sstevel@tonic-gate 			return;
48380Sstevel@tonic-gate 		}
48390Sstevel@tonic-gate 		switch (((union T_primitives *)rptr)->type) {
48400Sstevel@tonic-gate 		case T_ADDR_REQ:
48410Sstevel@tonic-gate 			icmp_addr_req(q, mp);
48420Sstevel@tonic-gate 			return;
48430Sstevel@tonic-gate 		case O_T_BIND_REQ:
48440Sstevel@tonic-gate 		case T_BIND_REQ:
48450Sstevel@tonic-gate 			qwriter(q, mp, icmp_bind, PERIM_OUTER);
48460Sstevel@tonic-gate 			return;
48470Sstevel@tonic-gate 		case T_CONN_REQ:
48480Sstevel@tonic-gate 			icmp_connect(q, mp);
48490Sstevel@tonic-gate 			return;
48500Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
48510Sstevel@tonic-gate 			icmp_capability_req(q, mp);
48520Sstevel@tonic-gate 			return;
48530Sstevel@tonic-gate 		case T_INFO_REQ:
48540Sstevel@tonic-gate 			icmp_info_req(q, mp);
48550Sstevel@tonic-gate 			return;
48560Sstevel@tonic-gate 		case T_UNITDATA_REQ:
48570Sstevel@tonic-gate 			/*
48580Sstevel@tonic-gate 			 * If a T_UNITDATA_REQ gets here, the address must
48590Sstevel@tonic-gate 			 * be bad.  Valid T_UNITDATA_REQs are found above
48600Sstevel@tonic-gate 			 * and break to below this switch.
48610Sstevel@tonic-gate 			 */
48620Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
48630Sstevel@tonic-gate 			return;
48640Sstevel@tonic-gate 		case T_UNBIND_REQ:
48650Sstevel@tonic-gate 			icmp_unbind(q, mp);
48660Sstevel@tonic-gate 			return;
48670Sstevel@tonic-gate 
48680Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
48690Sstevel@tonic-gate 			if (!snmpcom_req(q, mp, icmp_snmp_set, icmp_snmp_get,
48700Sstevel@tonic-gate 			    cr))
48710Sstevel@tonic-gate 				/* Only IP can return anything meaningful */
48720Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
48730Sstevel@tonic-gate 				    &icmp_opt_obj);
48740Sstevel@tonic-gate 			return;
48750Sstevel@tonic-gate 
48760Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
48770Sstevel@tonic-gate 			/* Only IP can return anything meaningful */
48780Sstevel@tonic-gate 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
48790Sstevel@tonic-gate 			return;
48800Sstevel@tonic-gate 
48810Sstevel@tonic-gate 		case T_DISCON_REQ:
48820Sstevel@tonic-gate 			icmp_disconnect(q, mp);
48830Sstevel@tonic-gate 			return;
48840Sstevel@tonic-gate 
48850Sstevel@tonic-gate 		/* The following TPI message is not supported by icmp. */
48860Sstevel@tonic-gate 		case O_T_CONN_RES:
48870Sstevel@tonic-gate 		case T_CONN_RES:
48880Sstevel@tonic-gate 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
48890Sstevel@tonic-gate 			return;
48900Sstevel@tonic-gate 
48910Sstevel@tonic-gate 		/* The following 3 TPI requests are illegal for icmp. */
48920Sstevel@tonic-gate 		case T_DATA_REQ:
48930Sstevel@tonic-gate 		case T_EXDATA_REQ:
48940Sstevel@tonic-gate 		case T_ORDREL_REQ:
48950Sstevel@tonic-gate 			freemsg(mp);
48960Sstevel@tonic-gate 			(void) putctl1(RD(q), M_ERROR, EPROTO);
48970Sstevel@tonic-gate 			return;
48980Sstevel@tonic-gate 		default:
48990Sstevel@tonic-gate 			break;
49000Sstevel@tonic-gate 		}
49010Sstevel@tonic-gate 		break;
49020Sstevel@tonic-gate 	case M_IOCTL:
49030Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
49040Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
49050Sstevel@tonic-gate 		case TI_GETPEERNAME:
49060Sstevel@tonic-gate 			if (icmp->icmp_state != TS_DATA_XFER) {
49070Sstevel@tonic-gate 				/*
49080Sstevel@tonic-gate 				 * If a default destination address has not
49090Sstevel@tonic-gate 				 * been associated with the stream, then we
49100Sstevel@tonic-gate 				 * don't know the peer's name.
49110Sstevel@tonic-gate 				 */
49120Sstevel@tonic-gate 				iocp->ioc_error = ENOTCONN;
49130Sstevel@tonic-gate 			    err_ret:;
49140Sstevel@tonic-gate 				iocp->ioc_count = 0;
49150Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
49160Sstevel@tonic-gate 				qreply(q, mp);
49170Sstevel@tonic-gate 				return;
49180Sstevel@tonic-gate 			}
49190Sstevel@tonic-gate 			/* FALLTHRU */
49200Sstevel@tonic-gate 		case TI_GETMYNAME:
49210Sstevel@tonic-gate 			/*
49220Sstevel@tonic-gate 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
49230Sstevel@tonic-gate 			 * need to copyin the user's strbuf structure.
49240Sstevel@tonic-gate 			 * Processing will continue in the M_IOCDATA case
49250Sstevel@tonic-gate 			 * below.
49260Sstevel@tonic-gate 			 */
49270Sstevel@tonic-gate 			mi_copyin(q, mp, NULL,
49280Sstevel@tonic-gate 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
49290Sstevel@tonic-gate 			return;
49300Sstevel@tonic-gate 		case ND_SET:
49310Sstevel@tonic-gate 			/* nd_getset performs the necessary error checking */
49320Sstevel@tonic-gate 		case ND_GET:
49330Sstevel@tonic-gate 			if (nd_getset(q, icmp_g_nd, mp)) {
49340Sstevel@tonic-gate 				qreply(q, mp);
49350Sstevel@tonic-gate 				return;
49360Sstevel@tonic-gate 			}
49370Sstevel@tonic-gate 			break;
49380Sstevel@tonic-gate 		default:
49390Sstevel@tonic-gate 			break;
49400Sstevel@tonic-gate 		}
49410Sstevel@tonic-gate 		break;
49420Sstevel@tonic-gate 	case M_IOCDATA:
49430Sstevel@tonic-gate 		icmp_wput_iocdata(q, mp);
49440Sstevel@tonic-gate 		return;
49450Sstevel@tonic-gate 	default:
49460Sstevel@tonic-gate 		break;
49470Sstevel@tonic-gate 	}
49480Sstevel@tonic-gate 	putnext(q, mp);
49490Sstevel@tonic-gate }
49500Sstevel@tonic-gate 
49510Sstevel@tonic-gate /*
49520Sstevel@tonic-gate  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
49530Sstevel@tonic-gate  * messages.
49540Sstevel@tonic-gate  */
49550Sstevel@tonic-gate static void
49560Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp)
49570Sstevel@tonic-gate {
49580Sstevel@tonic-gate 	mblk_t	*mp1;
49590Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
49600Sstevel@tonic-gate 	icmp_t	*icmp;
49610Sstevel@tonic-gate 	in6_addr_t	v6addr;
49620Sstevel@tonic-gate 	ipaddr_t	v4addr;
49630Sstevel@tonic-gate 	uint32_t	flowinfo = 0;
49640Sstevel@tonic-gate 	int		addrlen;
49650Sstevel@tonic-gate 
49660Sstevel@tonic-gate 	/* Make sure it is one of ours. */
49670Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
49680Sstevel@tonic-gate 	case TI_GETMYNAME:
49690Sstevel@tonic-gate 	case TI_GETPEERNAME:
49700Sstevel@tonic-gate 		break;
49710Sstevel@tonic-gate 	default:
49720Sstevel@tonic-gate 		putnext(q, mp);
49730Sstevel@tonic-gate 		return;
49740Sstevel@tonic-gate 	}
49750Sstevel@tonic-gate 	switch (mi_copy_state(q, mp, &mp1)) {
49760Sstevel@tonic-gate 	case -1:
49770Sstevel@tonic-gate 		return;
49780Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_IN, 1):
49790Sstevel@tonic-gate 		break;
49800Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 1):
49810Sstevel@tonic-gate 		/*
49820Sstevel@tonic-gate 		 * The address has been copied out, so now
49830Sstevel@tonic-gate 		 * copyout the strbuf.
49840Sstevel@tonic-gate 		 */
49850Sstevel@tonic-gate 		mi_copyout(q, mp);
49860Sstevel@tonic-gate 		return;
49870Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 2):
49880Sstevel@tonic-gate 		/*
49890Sstevel@tonic-gate 		 * The address and strbuf have been copied out.
49900Sstevel@tonic-gate 		 * We're done, so just acknowledge the original
49910Sstevel@tonic-gate 		 * M_IOCTL.
49920Sstevel@tonic-gate 		 */
49930Sstevel@tonic-gate 		mi_copy_done(q, mp, 0);
49940Sstevel@tonic-gate 		return;
49950Sstevel@tonic-gate 	default:
49960Sstevel@tonic-gate 		/*
49970Sstevel@tonic-gate 		 * Something strange has happened, so acknowledge
49980Sstevel@tonic-gate 		 * the original M_IOCTL with an EPROTO error.
49990Sstevel@tonic-gate 		 */
50000Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
50010Sstevel@tonic-gate 		return;
50020Sstevel@tonic-gate 	}
50030Sstevel@tonic-gate 	/*
50040Sstevel@tonic-gate 	 * Now we have the strbuf structure for TI_GETMYNAME
50050Sstevel@tonic-gate 	 * and TI_GETPEERNAME.  Next we copyout the requested
50060Sstevel@tonic-gate 	 * address and then we'll copyout the strbuf.
50070Sstevel@tonic-gate 	 */
50080Sstevel@tonic-gate 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
50090Sstevel@tonic-gate 	    (void *)mp1->b_rptr);
50100Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
50110Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET)
50120Sstevel@tonic-gate 		addrlen = sizeof (sin_t);
50130Sstevel@tonic-gate 	else
50140Sstevel@tonic-gate 		addrlen = sizeof (sin6_t);
50150Sstevel@tonic-gate 
50160Sstevel@tonic-gate 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
50170Sstevel@tonic-gate 		mi_copy_done(q, mp, EINVAL);
50180Sstevel@tonic-gate 		return;
50190Sstevel@tonic-gate 	}
50200Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
50210Sstevel@tonic-gate 	case TI_GETMYNAME:
50220Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
50230Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
50240Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
50250Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
50260Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
50270Sstevel@tonic-gate 			} else {
50280Sstevel@tonic-gate 				/*
50290Sstevel@tonic-gate 				 * INADDR_ANY
50300Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
50310Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
50320Sstevel@tonic-gate 				 * local address instead (that could
50330Sstevel@tonic-gate 				 * also still be INADDR_ANY)
50340Sstevel@tonic-gate 				 */
50350Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
50360Sstevel@tonic-gate 			}
50370Sstevel@tonic-gate 		} else {
50380Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6 */
50390Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
50400Sstevel@tonic-gate 				v6addr = icmp->icmp_v6src;
50410Sstevel@tonic-gate 			} else {
50420Sstevel@tonic-gate 				/*
50430Sstevel@tonic-gate 				 * UNSPECIFIED
50440Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
50450Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
50460Sstevel@tonic-gate 				 * local address instead (that could
50470Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
50480Sstevel@tonic-gate 				 */
50490Sstevel@tonic-gate 				v6addr = icmp->icmp_bound_v6src;
50500Sstevel@tonic-gate 			}
50510Sstevel@tonic-gate 		}
50520Sstevel@tonic-gate 		break;
50530Sstevel@tonic-gate 	case TI_GETPEERNAME:
50540Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
50550Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
50560Sstevel@tonic-gate 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
50570Sstevel@tonic-gate 		} else {
50580Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6) */
50590Sstevel@tonic-gate 			v6addr = icmp->icmp_v6dst;
50600Sstevel@tonic-gate 			flowinfo = icmp->icmp_flowinfo;
50610Sstevel@tonic-gate 		}
50620Sstevel@tonic-gate 		break;
50630Sstevel@tonic-gate 	default:
50640Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
50650Sstevel@tonic-gate 		return;
50660Sstevel@tonic-gate 	}
50670Sstevel@tonic-gate 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
50680Sstevel@tonic-gate 	if (!mp1)
50690Sstevel@tonic-gate 		return;
50700Sstevel@tonic-gate 
50710Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
50720Sstevel@tonic-gate 		sin_t *sin;
50730Sstevel@tonic-gate 
50740Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
50750Sstevel@tonic-gate 		sin = (sin_t *)mp1->b_rptr;
50760Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin[1];
50770Sstevel@tonic-gate 		*sin = sin_null;
50780Sstevel@tonic-gate 		sin->sin_family = AF_INET;
50790Sstevel@tonic-gate 		sin->sin_addr.s_addr = v4addr;
50800Sstevel@tonic-gate 	} else {
50810Sstevel@tonic-gate 		/* icmp->icmp_family == AF_INET6 */
50820Sstevel@tonic-gate 		sin6_t *sin6;
50830Sstevel@tonic-gate 
50840Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
50850Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
50860Sstevel@tonic-gate 		sin6 = (sin6_t *)mp1->b_rptr;
50870Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin6[1];
50880Sstevel@tonic-gate 		*sin6 = sin6_null;
50890Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
50900Sstevel@tonic-gate 		sin6->sin6_flowinfo = flowinfo;
50910Sstevel@tonic-gate 		sin6->sin6_addr = v6addr;
50920Sstevel@tonic-gate 	}
50930Sstevel@tonic-gate 	/* Copy out the address */
50940Sstevel@tonic-gate 	mi_copyout(q, mp);
50950Sstevel@tonic-gate }
50960Sstevel@tonic-gate 
50970Sstevel@tonic-gate /*
50980Sstevel@tonic-gate  * Only allow MIB requests and M_FLUSHes to pass.
50990Sstevel@tonic-gate  * All other messages are nacked or dropped.
51000Sstevel@tonic-gate  */
51010Sstevel@tonic-gate static void
51020Sstevel@tonic-gate icmp_wput_restricted(queue_t *q, mblk_t *mp)
51030Sstevel@tonic-gate {
51040Sstevel@tonic-gate 	cred_t	*cr;
51050Sstevel@tonic-gate 	icmp_t	*icmp;
51060Sstevel@tonic-gate 
51070Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
51080Sstevel@tonic-gate 	case M_PROTO:
51090Sstevel@tonic-gate 	case M_PCPROTO:
51100Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
51110Sstevel@tonic-gate 			freemsg(mp);
51120Sstevel@tonic-gate 			return;
51130Sstevel@tonic-gate 		}
51140Sstevel@tonic-gate 		icmp = (icmp_t *)q->q_ptr;
51150Sstevel@tonic-gate 		cr = DB_CREDDEF(mp, icmp->icmp_credp);
51160Sstevel@tonic-gate 
51170Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
51180Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
51190Sstevel@tonic-gate 			if (!snmpcom_req(q, mp,
51200Sstevel@tonic-gate 			    icmp_snmp_set, icmp_snmp_get, cr))
51210Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
51220Sstevel@tonic-gate 				    &icmp_opt_obj);
51230Sstevel@tonic-gate 			return;
51240Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
51250Sstevel@tonic-gate 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
51260Sstevel@tonic-gate 			return;
51270Sstevel@tonic-gate 		default:
51280Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOTSUP);
51290Sstevel@tonic-gate 			return;
51300Sstevel@tonic-gate 		}
51310Sstevel@tonic-gate 		/* NOTREACHED */
51320Sstevel@tonic-gate 	case M_IOCTL:
51330Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOTSUP);
51340Sstevel@tonic-gate 		break;
51350Sstevel@tonic-gate 	case M_FLUSH:
51360Sstevel@tonic-gate 		putnext(q, mp);
51370Sstevel@tonic-gate 		break;
51380Sstevel@tonic-gate 	default:
51390Sstevel@tonic-gate 		freemsg(mp);
51400Sstevel@tonic-gate 		break;
51410Sstevel@tonic-gate 	}
51420Sstevel@tonic-gate }
51430Sstevel@tonic-gate 
51440Sstevel@tonic-gate static int
51450Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
51460Sstevel@tonic-gate     void *thisdg_attrs)
51470Sstevel@tonic-gate {
51480Sstevel@tonic-gate 	icmp_t	*icmp;
51490Sstevel@tonic-gate 	struct T_unitdata_req *udreqp;
51500Sstevel@tonic-gate 	int is_absreq_failure;
51510Sstevel@tonic-gate 	cred_t *cr;
51520Sstevel@tonic-gate 
51530Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
51540Sstevel@tonic-gate 
51550Sstevel@tonic-gate 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
51560Sstevel@tonic-gate 	*errorp = 0;
51570Sstevel@tonic-gate 
51580Sstevel@tonic-gate 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
51590Sstevel@tonic-gate 
51600Sstevel@tonic-gate 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
51610Sstevel@tonic-gate 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
51620Sstevel@tonic-gate 	    thisdg_attrs, &is_absreq_failure);
51630Sstevel@tonic-gate 
51640Sstevel@tonic-gate 	if (*errorp != 0) {
51650Sstevel@tonic-gate 		/*
51660Sstevel@tonic-gate 		 * Note: No special action needed in this
51670Sstevel@tonic-gate 		 * module for "is_absreq_failure"
51680Sstevel@tonic-gate 		 */
51690Sstevel@tonic-gate 		return (-1);		/* failure */
51700Sstevel@tonic-gate 	}
51710Sstevel@tonic-gate 	ASSERT(is_absreq_failure == 0);
51720Sstevel@tonic-gate 	return (0);	/* success */
51730Sstevel@tonic-gate }
51740Sstevel@tonic-gate 
51750Sstevel@tonic-gate void
51760Sstevel@tonic-gate icmp_ddi_init(void)
51770Sstevel@tonic-gate {
51780Sstevel@tonic-gate 	ICMP6_MAJ = ddi_name_to_major(ICMP6);
51790Sstevel@tonic-gate 	icmp_max_optsize =
51800Sstevel@tonic-gate 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
51810Sstevel@tonic-gate 		icmp_opt_obj.odb_opt_arr_cnt);
51820Sstevel@tonic-gate 
51830Sstevel@tonic-gate 	(void) icmp_param_register(icmp_param_arr, A_CNT(icmp_param_arr));
51840Sstevel@tonic-gate 
51850Sstevel@tonic-gate 	rawip_kstat_init();
51860Sstevel@tonic-gate }
51870Sstevel@tonic-gate 
51880Sstevel@tonic-gate void
51890Sstevel@tonic-gate icmp_ddi_destroy(void)
51900Sstevel@tonic-gate {
51910Sstevel@tonic-gate 	nd_free(&icmp_g_nd);
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate 	rawip_kstat_fini();
51940Sstevel@tonic-gate }
51950Sstevel@tonic-gate 
51960Sstevel@tonic-gate static void
51970Sstevel@tonic-gate rawip_kstat_init(void) {
51980Sstevel@tonic-gate 
51990Sstevel@tonic-gate 	rawip_named_kstat_t template = {
52000Sstevel@tonic-gate 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
52010Sstevel@tonic-gate 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
52020Sstevel@tonic-gate 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
52030Sstevel@tonic-gate 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
52040Sstevel@tonic-gate 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
52050Sstevel@tonic-gate 	};
52060Sstevel@tonic-gate 
52070Sstevel@tonic-gate 	rawip_mibkp = kstat_create("icmp", 0, "rawip", "mib2",
52080Sstevel@tonic-gate 					KSTAT_TYPE_NAMED,
52090Sstevel@tonic-gate 					NUM_OF_FIELDS(rawip_named_kstat_t),
52100Sstevel@tonic-gate 					0);
52110Sstevel@tonic-gate 	if (rawip_mibkp == NULL)
52120Sstevel@tonic-gate 		return;
52130Sstevel@tonic-gate 
52140Sstevel@tonic-gate 	bcopy(&template, rawip_mibkp->ks_data, sizeof (template));
52150Sstevel@tonic-gate 
52160Sstevel@tonic-gate 	rawip_mibkp->ks_update = rawip_kstat_update;
52170Sstevel@tonic-gate 
52180Sstevel@tonic-gate 	kstat_install(rawip_mibkp);
52190Sstevel@tonic-gate }
52200Sstevel@tonic-gate 
52210Sstevel@tonic-gate static void
52220Sstevel@tonic-gate rawip_kstat_fini(void) {
52230Sstevel@tonic-gate 	if (rawip_mibkp) {
52240Sstevel@tonic-gate 		kstat_delete(rawip_mibkp);
52250Sstevel@tonic-gate 		rawip_mibkp = NULL;
52260Sstevel@tonic-gate 	}
52270Sstevel@tonic-gate }
52280Sstevel@tonic-gate 
52290Sstevel@tonic-gate static int
52300Sstevel@tonic-gate rawip_kstat_update(kstat_t *kp, int rw) {
52310Sstevel@tonic-gate 	rawip_named_kstat_t *rawipkp;
52320Sstevel@tonic-gate 
52330Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
52340Sstevel@tonic-gate 		return (EIO);
52350Sstevel@tonic-gate 
52360Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
52370Sstevel@tonic-gate 		return (EACCES);
52380Sstevel@tonic-gate 
52390Sstevel@tonic-gate 	rawipkp = (rawip_named_kstat_t *)kp->ks_data;
52400Sstevel@tonic-gate 
52410Sstevel@tonic-gate 	rawipkp->inDatagrams.value.ui32 =	rawip_mib.rawipInDatagrams;
52420Sstevel@tonic-gate 	rawipkp->inCksumErrs.value.ui32 =	rawip_mib.rawipInCksumErrs;
52430Sstevel@tonic-gate 	rawipkp->inErrors.value.ui32 =		rawip_mib.rawipInErrors;
52440Sstevel@tonic-gate 	rawipkp->outDatagrams.value.ui32 =	rawip_mib.rawipOutDatagrams;
52450Sstevel@tonic-gate 	rawipkp->outErrors.value.ui32 =		rawip_mib.rawipOutErrors;
52460Sstevel@tonic-gate 
52470Sstevel@tonic-gate 	return (0);
52480Sstevel@tonic-gate }
5249