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