xref: /onnv-gate/usr/src/uts/common/inet/ip/icmp.c (revision 3388:1a125b15092b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51289Sja97890  * Common Development and Distribution License (the "License").
61289Sja97890  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3388Skcpoon  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/stropts.h>
330Sstevel@tonic-gate #include <sys/strlog.h>
340Sstevel@tonic-gate #include <sys/strsun.h>
350Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
360Sstevel@tonic-gate #include <sys/tihdr.h>
370Sstevel@tonic-gate #include <sys/timod.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
401676Sjpk #include <sys/strsubr.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/policy.h>
451676Sjpk #include <sys/priv.h>
460Sstevel@tonic-gate #include <sys/zone.h>
471673Sgt145670 #include <sys/time.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <sys/isa_defs.h>
510Sstevel@tonic-gate #include <sys/suntpi.h>
520Sstevel@tonic-gate #include <sys/xti_inet.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <net/route.h>
550Sstevel@tonic-gate #include <net/if.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <netinet/in.h>
580Sstevel@tonic-gate #include <netinet/ip6.h>
590Sstevel@tonic-gate #include <netinet/icmp6.h>
600Sstevel@tonic-gate #include <inet/common.h>
610Sstevel@tonic-gate #include <inet/ip.h>
620Sstevel@tonic-gate #include <inet/ip6.h>
630Sstevel@tonic-gate #include <inet/mi.h>
640Sstevel@tonic-gate #include <inet/nd.h>
650Sstevel@tonic-gate #include <inet/optcom.h>
660Sstevel@tonic-gate #include <inet/snmpcom.h>
670Sstevel@tonic-gate #include <inet/kstatcom.h>
680Sstevel@tonic-gate #include <inet/rawip_impl.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <netinet/ip_mroute.h>
710Sstevel@tonic-gate #include <inet/tcp.h>
720Sstevel@tonic-gate #include <net/pfkeyv2.h>
730Sstevel@tonic-gate #include <inet/ipsec_info.h>
740Sstevel@tonic-gate #include <inet/ipclassifier.h>
750Sstevel@tonic-gate 
761676Sjpk #include <sys/tsol/label.h>
771676Sjpk #include <sys/tsol/tnet.h>
781676Sjpk 
793318Srshoaib #include <inet/ip_ire.h>
803318Srshoaib #include <inet/ip_if.h>
813318Srshoaib 
823318Srshoaib #include <inet/ip_impl.h>
833318Srshoaib 
840Sstevel@tonic-gate #define	ICMP6 "icmp6"
850Sstevel@tonic-gate major_t	ICMP6_MAJ;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Object to represent database of options to search passed to
890Sstevel@tonic-gate  * {sock,tpi}optcom_req() interface routine to take care of option
900Sstevel@tonic-gate  * management and associated methods.
910Sstevel@tonic-gate  * XXX These and other extern's should really move to a icmp header.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate extern optdb_obj_t	icmp_opt_obj;
940Sstevel@tonic-gate extern uint_t		icmp_max_optsize;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Synchronization notes:
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * At all points in this code where exclusive access is required, we
1000Sstevel@tonic-gate  * pass a message to a subroutine by invoking qwriter(..., PERIM_OUTER)
1010Sstevel@tonic-gate  * which will arrange to call the routine only after all threads have
1020Sstevel@tonic-gate  * exited the shared resource.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
1060Sstevel@tonic-gate typedef struct icmpparam_s {
1070Sstevel@tonic-gate 	uint_t	icmp_param_min;
1080Sstevel@tonic-gate 	uint_t	icmp_param_max;
1090Sstevel@tonic-gate 	uint_t	icmp_param_value;
1100Sstevel@tonic-gate 	char	*icmp_param_name;
1110Sstevel@tonic-gate } icmpparam_t;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static void	icmp_addr_req(queue_t *q, mblk_t *mp);
1140Sstevel@tonic-gate static void	icmp_bind(queue_t *q, mblk_t *mp);
1150Sstevel@tonic-gate static void	icmp_bind_proto(queue_t *q);
1160Sstevel@tonic-gate static int	icmp_build_hdrs(queue_t *q, icmp_t *icmp);
1170Sstevel@tonic-gate static void	icmp_capability_req(queue_t *q, mblk_t *mp);
1180Sstevel@tonic-gate static int	icmp_close(queue_t *q);
1190Sstevel@tonic-gate static void	icmp_connect(queue_t *q, mblk_t *mp);
1200Sstevel@tonic-gate static void	icmp_disconnect(queue_t *q, mblk_t *mp);
1210Sstevel@tonic-gate static void	icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
1220Sstevel@tonic-gate 		    int sys_error);
1230Sstevel@tonic-gate static void	icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
1240Sstevel@tonic-gate 		    t_scalar_t t_error, int sys_error);
1250Sstevel@tonic-gate static void	icmp_icmp_error(queue_t *q, mblk_t *mp);
1260Sstevel@tonic-gate static void	icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp);
1270Sstevel@tonic-gate static void	icmp_info_req(queue_t *q, mblk_t *mp);
1280Sstevel@tonic-gate static mblk_t	*icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim,
1290Sstevel@tonic-gate 		    t_scalar_t addr_length, in_port_t);
1300Sstevel@tonic-gate static int	icmp_open(queue_t *q, dev_t *devp, int flag,
1310Sstevel@tonic-gate 		    int sflag, cred_t *credp);
1320Sstevel@tonic-gate static int	icmp_unitdata_opt_process(queue_t *q, mblk_t *mp,
1330Sstevel@tonic-gate 		    int *errorp, void *thisdg_attrs);
1340Sstevel@tonic-gate static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
1350Sstevel@tonic-gate int		icmp_opt_set(queue_t *q, uint_t optset_context,
1360Sstevel@tonic-gate 		    int level, int name, uint_t inlen,
1370Sstevel@tonic-gate 		    uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
1380Sstevel@tonic-gate 		    void *thisdg_attrs, cred_t *cr, mblk_t *mblk);
1390Sstevel@tonic-gate int		icmp_opt_get(queue_t *q, int level, int name,
1400Sstevel@tonic-gate 		    uchar_t *ptr);
1410Sstevel@tonic-gate static int	icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
1420Sstevel@tonic-gate static boolean_t icmp_param_register(icmpparam_t *icmppa, int cnt);
1430Sstevel@tonic-gate static int	icmp_param_set(queue_t *q, mblk_t *mp, char *value,
1440Sstevel@tonic-gate 		    caddr_t cp, cred_t *cr);
1450Sstevel@tonic-gate static void	icmp_rput(queue_t *q, mblk_t *mp);
1460Sstevel@tonic-gate static void	icmp_rput_bind_ack(queue_t *q, mblk_t *mp);
1470Sstevel@tonic-gate static int	icmp_snmp_get(queue_t *q, mblk_t *mpctl);
1480Sstevel@tonic-gate static int	icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
1490Sstevel@tonic-gate 		    uchar_t *ptr, int len);
1500Sstevel@tonic-gate static int	icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp,
1510Sstevel@tonic-gate 		    cred_t *cr);
1520Sstevel@tonic-gate static void	icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err);
1530Sstevel@tonic-gate static void	icmp_unbind(queue_t *q, mblk_t *mp);
1540Sstevel@tonic-gate static void	icmp_wput(queue_t *q, mblk_t *mp);
1550Sstevel@tonic-gate static void	icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6,
1560Sstevel@tonic-gate 		    t_scalar_t tudr_optlen);
1570Sstevel@tonic-gate static void	icmp_wput_other(queue_t *q, mblk_t *mp);
1580Sstevel@tonic-gate static void	icmp_wput_iocdata(queue_t *q, mblk_t *mp);
1590Sstevel@tonic-gate static void	icmp_wput_restricted(queue_t *q, mblk_t *mp);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static void	rawip_kstat_init(void);
1620Sstevel@tonic-gate static void	rawip_kstat_fini(void);
1630Sstevel@tonic-gate static int	rawip_kstat_update(kstat_t *kp, int rw);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static struct module_info info =  {
1670Sstevel@tonic-gate 	5707, "icmp", 1, INFPSZ, 512, 128
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static struct qinit rinit = {
1710Sstevel@tonic-gate 	(pfi_t)icmp_rput, NULL, icmp_open, icmp_close, NULL, &info
1720Sstevel@tonic-gate };
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static struct qinit winit = {
1750Sstevel@tonic-gate 	(pfi_t)icmp_wput, NULL, NULL, NULL, NULL, &info
1760Sstevel@tonic-gate };
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate struct streamtab icmpinfo = {
1790Sstevel@tonic-gate 	&rinit, &winit
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static sin_t	sin_null;	/* Zero address for quick clears */
1830Sstevel@tonic-gate static sin6_t	sin6_null;	/* Zero address for quick clears */
1840Sstevel@tonic-gate static void	*icmp_g_head;	/* Head for list of open icmp streams. */
1850Sstevel@tonic-gate static IDP	icmp_g_nd;	/* Points to table of ICMP ND variables. */
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /* MIB-2 stuff for SNMP */
1880Sstevel@tonic-gate static mib2_rawip_t	rawip_mib;	/* SNMP fixed size info */
1890Sstevel@tonic-gate static kstat_t		*rawip_mibkp;	/* kstat exporting rawip_mib data */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages */
1920Sstevel@tonic-gate static struct T_info_ack icmp_g_t_info_ack = {
1930Sstevel@tonic-gate 	T_INFO_ACK,
1940Sstevel@tonic-gate 	IP_MAXPACKET,	 /* TSDU_size.  icmp allows maximum size messages. */
1950Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size.  icmp does not support expedited data. */
1960Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. icmp does not support connect data. */
1970Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. icmp does not support disconnect data. */
1980Sstevel@tonic-gate 	0,		/* ADDR_size - filled in later. */
1990Sstevel@tonic-gate 	0,		/* OPT_size - not initialized here */
2000Sstevel@tonic-gate 	IP_MAXPACKET,	/* TIDU_size.  icmp allows maximum size messages. */
2010Sstevel@tonic-gate 	T_CLTS,		/* SERV_type.  icmp supports connection-less. */
2020Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state.  This is set from icmp_state. */
2030Sstevel@tonic-gate 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
2040Sstevel@tonic-gate };
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * Table of ND variables supported by icmp.  These are loaded into icmp_g_nd
2080Sstevel@tonic-gate  * in icmp_open.
2090Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static icmpparam_t	icmp_param_arr[] = {
2120Sstevel@tonic-gate 	/* min	max	value	name */
2130Sstevel@tonic-gate 	{ 0,	128,	32,	"icmp_wroff_extra" },
2140Sstevel@tonic-gate 	{ 1,	255,	255,	"icmp_ipv4_ttl" },
2150Sstevel@tonic-gate 	{ 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS,	"icmp_ipv6_hoplimit"},
2160Sstevel@tonic-gate 	{ 0,	1,	1,	"icmp_bsd_compat" },
2170Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_xmit_hiwat"},
2180Sstevel@tonic-gate 	{ 0,	65536,	1024,	"icmp_xmit_lowat"},
2190Sstevel@tonic-gate 	{ 4096,	65536,	8192,	"icmp_recv_hiwat"},
2200Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"icmp_max_buf"},
2210Sstevel@tonic-gate };
2220Sstevel@tonic-gate #define	icmp_wroff_extra		icmp_param_arr[0].icmp_param_value
2230Sstevel@tonic-gate #define	icmp_ipv4_ttl			icmp_param_arr[1].icmp_param_value
2240Sstevel@tonic-gate #define	icmp_ipv6_hoplimit		icmp_param_arr[2].icmp_param_value
2250Sstevel@tonic-gate #define	icmp_bsd_compat			icmp_param_arr[3].icmp_param_value
2260Sstevel@tonic-gate #define	icmp_xmit_hiwat			icmp_param_arr[4].icmp_param_value
2270Sstevel@tonic-gate #define	icmp_xmit_lowat			icmp_param_arr[5].icmp_param_value
2280Sstevel@tonic-gate #define	icmp_recv_hiwat			icmp_param_arr[6].icmp_param_value
2290Sstevel@tonic-gate #define	icmp_max_buf			icmp_param_arr[7].icmp_param_value
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
2330Sstevel@tonic-gate  * passed to icmp_wput.
2340Sstevel@tonic-gate  * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP
2350Sstevel@tonic-gate  * protocol type placed in the message following the address. A T_BIND_ACK
2360Sstevel@tonic-gate  * message is passed upstream when ip acknowledges the request.
2370Sstevel@tonic-gate  * (Called as writer.)
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate static void
2400Sstevel@tonic-gate icmp_bind(queue_t *q, mblk_t *mp)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	sin_t	*sin;
2430Sstevel@tonic-gate 	sin6_t	*sin6;
2440Sstevel@tonic-gate 	mblk_t	*mp1;
2450Sstevel@tonic-gate 	struct T_bind_req	*tbr;
2460Sstevel@tonic-gate 	icmp_t	*icmp;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
2490Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
2500Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2510Sstevel@tonic-gate 		    "icmp_bind: bad req, len %u",
2520Sstevel@tonic-gate 		    (uint_t)(mp->b_wptr - mp->b_rptr));
2530Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
2540Sstevel@tonic-gate 		return;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
2570Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2580Sstevel@tonic-gate 		    "icmp_bind: bad state, %d", icmp->icmp_state);
2590Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
2600Sstevel@tonic-gate 		return;
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Reallocate the message to make sure we have enough room for an
2640Sstevel@tonic-gate 	 * address and the protocol type.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1);
2670Sstevel@tonic-gate 	if (!mp1) {
2680Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
2690Sstevel@tonic-gate 		return;
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	mp = mp1;
2720Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
2730Sstevel@tonic-gate 	switch (tbr->ADDR_length) {
2740Sstevel@tonic-gate 	case 0:			/* Generic request */
2750Sstevel@tonic-gate 		tbr->ADDR_offset = sizeof (struct T_bind_req);
2760Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
2770Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin_t);
2780Sstevel@tonic-gate 			sin = (sin_t *)&tbr[1];
2790Sstevel@tonic-gate 			*sin = sin_null;
2800Sstevel@tonic-gate 			sin->sin_family = AF_INET;
2810Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin[1];
2820Sstevel@tonic-gate 		} else {
2830Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
2840Sstevel@tonic-gate 			tbr->ADDR_length = sizeof (sin6_t);
2850Sstevel@tonic-gate 			sin6 = (sin6_t *)&tbr[1];
2860Sstevel@tonic-gate 			*sin6 = sin6_null;
2870Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
2880Sstevel@tonic-gate 			mp->b_wptr = (uchar_t *)&sin6[1];
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		break;
2910Sstevel@tonic-gate 	case sizeof (sin_t):	/* Complete IP address */
2920Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset,
2930Sstevel@tonic-gate 		    sizeof (sin_t));
2940Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
2950Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
2960Sstevel@tonic-gate 			return;
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
2990Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
3000Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3010Sstevel@tonic-gate 			return;
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 		break;
3040Sstevel@tonic-gate 	case sizeof (sin6_t):	/* Complete IP address */
3050Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset,
3060Sstevel@tonic-gate 		    sizeof (sin6_t));
3070Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
3080Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
3090Sstevel@tonic-gate 			return;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
3120Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
3130Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
3140Sstevel@tonic-gate 			return;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
3170Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3180Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
3190Sstevel@tonic-gate 			return;
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 		break;
3220Sstevel@tonic-gate 	default:
3230Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
3240Sstevel@tonic-gate 		    "icmp_bind: bad ADDR_length %d", tbr->ADDR_length);
3250Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
3260Sstevel@tonic-gate 		return;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * Copy the source address into our icmp structure.  This address
3300Sstevel@tonic-gate 	 * may still be zero; if so, ip will fill in the correct address
3310Sstevel@tonic-gate 	 * each time an outbound packet is passed to it.
3320Sstevel@tonic-gate 	 * If we are binding to a broadcast or multicast address icmp_rput
3330Sstevel@tonic-gate 	 * will clear the source address when it receives the T_BIND_ACK.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
3380Sstevel@tonic-gate 		ASSERT(sin != NULL);
3390Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
3400Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr,
3410Sstevel@tonic-gate 		    &icmp->icmp_v6src);
3420Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
3430Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
3440Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3450Sstevel@tonic-gate 	} else {
3460Sstevel@tonic-gate 		int error;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		ASSERT(sin6 != NULL);
3490Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
3500Sstevel@tonic-gate 		icmp->icmp_v6src = sin6->sin6_addr;
3510Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
3520Sstevel@tonic-gate 		icmp->icmp_bound_v6src = icmp->icmp_v6src;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		/* Rebuild the header template */
3550Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
3560Sstevel@tonic-gate 		if (error != 0) {
3570Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
3580Sstevel@tonic-gate 			return;
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following
3630Sstevel@tonic-gate 	 * the address.
3640Sstevel@tonic-gate 	 */
3650Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
3660Sstevel@tonic-gate 	if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) {
3670Sstevel@tonic-gate 		/*
3680Sstevel@tonic-gate 		 * Append a request for an IRE if src not 0 (INADDR_ANY)
3690Sstevel@tonic-gate 		 */
3700Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
3710Sstevel@tonic-gate 		if (!mp->b_cont) {
3720Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOMEM);
3730Sstevel@tonic-gate 			return;
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
3760Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */
3800Sstevel@tonic-gate 	putnext(q, mp);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * Send message to IP to just bind to the protocol.
3850Sstevel@tonic-gate  */
3860Sstevel@tonic-gate static void
3870Sstevel@tonic-gate icmp_bind_proto(queue_t *q)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	mblk_t	*mp;
3900Sstevel@tonic-gate 	struct T_bind_req	*tbr;
3910Sstevel@tonic-gate 	icmp_t	*icmp;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
3940Sstevel@tonic-gate 	mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1,
3950Sstevel@tonic-gate 	    BPRI_MED);
3960Sstevel@tonic-gate 	if (!mp) {
3970Sstevel@tonic-gate 		return;
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
4000Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
4010Sstevel@tonic-gate 	tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */
4020Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (struct T_bind_req);
4030Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
4040Sstevel@tonic-gate 		sin_t	*sin;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin_t);
4070Sstevel@tonic-gate 		sin = (sin_t *)&tbr[1];
4080Sstevel@tonic-gate 		*sin = sin_null;
4090Sstevel@tonic-gate 		sin->sin_family = AF_INET;
4100Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin[1];
4110Sstevel@tonic-gate 	} else {
4120Sstevel@tonic-gate 		sin6_t	*sin6;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
4150Sstevel@tonic-gate 		tbr->ADDR_length = sizeof (sin6_t);
4160Sstevel@tonic-gate 		sin6 = (sin6_t *)&tbr[1];
4170Sstevel@tonic-gate 		*sin6 = sin6_null;
4180Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
4190Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)&sin6[1];
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/* Place protocol type in the O_T_BIND_REQ following the address. */
4230Sstevel@tonic-gate 	*mp->b_wptr++ = icmp->icmp_proto;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/* Pass the O_T_BIND_REQ to ip. */
4260Sstevel@tonic-gate 	putnext(q, mp);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * This routine handles each T_CONN_REQ message passed to icmp.  It
4310Sstevel@tonic-gate  * associates a default destination address with the stream.
4320Sstevel@tonic-gate  *
4330Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
4340Sstevel@tonic-gate  *	T_BIND_REQ	- specifying local and remote address.
4350Sstevel@tonic-gate  *	IRE_DB_REQ_TYPE	- to get an IRE back containing ire_type and src
4360Sstevel@tonic-gate  *	T_OK_ACK	- for the T_CONN_REQ
4370Sstevel@tonic-gate  *	T_CONN_CON	- to keep the TPI user happy
4380Sstevel@tonic-gate  *
4390Sstevel@tonic-gate  * The connect completes in icmp_rput.
4400Sstevel@tonic-gate  * When a T_BIND_ACK is received information is extracted from the IRE
4410Sstevel@tonic-gate  * and the two appended messages are sent to the TPI user.
4420Sstevel@tonic-gate  * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
4430Sstevel@tonic-gate  * it to an error ack for the appropriate primitive.
4440Sstevel@tonic-gate  */
4450Sstevel@tonic-gate static void
4460Sstevel@tonic-gate icmp_connect(queue_t *q, mblk_t *mp)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	sin_t	*sin;
4490Sstevel@tonic-gate 	sin6_t	*sin6;
4500Sstevel@tonic-gate 	mblk_t	*mp1, *mp2;
4510Sstevel@tonic-gate 	struct T_conn_req	*tcr;
4520Sstevel@tonic-gate 	icmp_t	*icmp;
4530Sstevel@tonic-gate 	ipaddr_t	v4dst;
4540Sstevel@tonic-gate 	in6_addr_t	v6dst;
4550Sstevel@tonic-gate 	uint32_t	flowinfo;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
4580Sstevel@tonic-gate 	tcr = (struct T_conn_req *)mp->b_rptr;
4590Sstevel@tonic-gate 	/* Sanity checks */
4600Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr < sizeof (struct T_conn_req))) {
4610Sstevel@tonic-gate 		icmp_err_ack(q, mp, TPROTO, 0);
4620Sstevel@tonic-gate 		return;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (icmp->icmp_state == TS_DATA_XFER) {
4660Sstevel@tonic-gate 		/* Already connected - clear out state */
4670Sstevel@tonic-gate 		icmp->icmp_v6src = icmp->icmp_bound_v6src;
4680Sstevel@tonic-gate 		icmp->icmp_state = TS_IDLE;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (tcr->OPT_length != 0) {
4730Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADOPT, 0);
4740Sstevel@tonic-gate 		return;
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 	switch (tcr->DEST_length) {
4770Sstevel@tonic-gate 	default:
4780Sstevel@tonic-gate 		icmp_err_ack(q, mp, TBADADDR, 0);
4790Sstevel@tonic-gate 		return;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	case sizeof (sin_t):
4820Sstevel@tonic-gate 		sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset,
4830Sstevel@tonic-gate 		    sizeof (sin_t));
4840Sstevel@tonic-gate 		if (sin == NULL || !OK_32PTR((char *)sin)) {
4850Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
4860Sstevel@tonic-gate 			return;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET ||
4890Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
4900Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
4910Sstevel@tonic-gate 			return;
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
4940Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
4950Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
4960Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
4970Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len;
4980Sstevel@tonic-gate 		break;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	case sizeof (sin6_t):
5010Sstevel@tonic-gate 		sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset,
5020Sstevel@tonic-gate 		    sizeof (sin6_t));
5030Sstevel@tonic-gate 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
5040Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EINVAL);
5050Sstevel@tonic-gate 			return;
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6 ||
5080Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
5090Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
5100Sstevel@tonic-gate 			return;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
5130Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5140Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL);
5150Sstevel@tonic-gate 			return;
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 		v6dst = sin6->sin6_addr;
5180Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
5190Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len;
5200Sstevel@tonic-gate 		flowinfo = sin6->sin6_flowinfo;
5210Sstevel@tonic-gate 		break;
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	if (icmp->icmp_ipversion == IPV4_VERSION) {
5240Sstevel@tonic-gate 		/*
5250Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
5260Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
5270Sstevel@tonic-gate 		 * generate the T_CONN_CON.
5280Sstevel@tonic-gate 		 */
5290Sstevel@tonic-gate 		if (v4dst == INADDR_ANY) {
5300Sstevel@tonic-gate 			v4dst = htonl(INADDR_LOOPBACK);
5310Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
5320Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET) {
5330Sstevel@tonic-gate 				sin->sin_addr.s_addr = v4dst;
5340Sstevel@tonic-gate 			} else {
5350Sstevel@tonic-gate 				sin6->sin6_addr = v6dst;
5360Sstevel@tonic-gate 			}
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
5390Sstevel@tonic-gate 		icmp->icmp_flowinfo = 0;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		/*
5420Sstevel@tonic-gate 		 * If the destination address is multicast and
5430Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
5440Sstevel@tonic-gate 		 * use the address of that interface as our
5450Sstevel@tonic-gate 		 * source address if no source address has been set.
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 		if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY &&
5480Sstevel@tonic-gate 		    CLASSD(v4dst) &&
5490Sstevel@tonic-gate 		    icmp->icmp_multicast_if_addr != INADDR_ANY) {
5500Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr,
5510Sstevel@tonic-gate 			    &icmp->icmp_v6src);
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 	} else {
5540Sstevel@tonic-gate 		ASSERT(icmp->icmp_ipversion == IPV6_VERSION);
5550Sstevel@tonic-gate 		/*
5560Sstevel@tonic-gate 		 * Interpret a zero destination to mean loopback.
5570Sstevel@tonic-gate 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
5580Sstevel@tonic-gate 		 * generate the T_CONN_CON.
5590Sstevel@tonic-gate 		 */
5600Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
5610Sstevel@tonic-gate 			v6dst = ipv6_loopback;
5620Sstevel@tonic-gate 			sin6->sin6_addr = v6dst;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 		icmp->icmp_v6dst = v6dst;
5650Sstevel@tonic-gate 		icmp->icmp_flowinfo = flowinfo;
5660Sstevel@tonic-gate 		/*
5670Sstevel@tonic-gate 		 * If the destination address is multicast and
5680Sstevel@tonic-gate 		 * an outgoing multicast interface has been set,
5690Sstevel@tonic-gate 		 * then the ip bind logic will pick the correct source
5700Sstevel@tonic-gate 		 * address (i.e. matching the outgoing multicast interface).
5710Sstevel@tonic-gate 		 */
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	/*
5750Sstevel@tonic-gate 	 * Send down bind to IP to verify that there is a route
5760Sstevel@tonic-gate 	 * and to determine the source address.
5770Sstevel@tonic-gate 	 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput.
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
5800Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t),
5810Sstevel@tonic-gate 		    sin->sin_port);
5820Sstevel@tonic-gate 	} else {
5830Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
5840Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t),
5850Sstevel@tonic-gate 		    sin6->sin6_port);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 	if (mp1 == NULL) {
5880Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
5890Sstevel@tonic-gate 		return;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * We also have to send a connection confirmation to
5940Sstevel@tonic-gate 	 * keep TLI happy. Prepare it for icmp_rput.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
5970Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL,
5980Sstevel@tonic-gate 		    0);
5990Sstevel@tonic-gate 	} else {
6000Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
6010Sstevel@tonic-gate 		mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL,
6020Sstevel@tonic-gate 		    0);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	if (mp2 == NULL) {
6050Sstevel@tonic-gate 		freemsg(mp1);
6060Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
6070Sstevel@tonic-gate 		return;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
6110Sstevel@tonic-gate 	if (mp == NULL) {
6120Sstevel@tonic-gate 		/* Unable to reuse the T_CONN_REQ for the ack. */
6130Sstevel@tonic-gate 		freemsg(mp2);
6140Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
6150Sstevel@tonic-gate 		return;
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	icmp->icmp_state = TS_DATA_XFER;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/* Hang onto the T_OK_ACK and T_CONN_CON for later. */
6210Sstevel@tonic-gate 	linkb(mp1, mp);
6220Sstevel@tonic-gate 	linkb(mp1, mp2);
6230Sstevel@tonic-gate 
6241676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
6250Sstevel@tonic-gate 	putnext(q, mp1);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate static int
6290Sstevel@tonic-gate icmp_close(queue_t *q)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
6320Sstevel@tonic-gate 	int	i1;
6330Sstevel@tonic-gate 
6341676Sjpk 	/* tell IP that if we're not here, he can't trust labels */
6351676Sjpk 	if (is_system_labeled())
6361676Sjpk 		putnext(WR(q), icmp->icmp_delabel);
6371676Sjpk 
6380Sstevel@tonic-gate 	qprocsoff(q);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	/* If there are any options associated with the stream, free them. */
6410Sstevel@tonic-gate 	if (icmp->icmp_ip_snd_options)
6420Sstevel@tonic-gate 		mi_free((char *)icmp->icmp_ip_snd_options);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL)
6450Sstevel@tonic-gate 		kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	/* Free memory associated with sticky options */
6480Sstevel@tonic-gate 	if (icmp->icmp_sticky_hdrs_len != 0) {
6490Sstevel@tonic-gate 		kmem_free(icmp->icmp_sticky_hdrs,
6500Sstevel@tonic-gate 		    icmp->icmp_sticky_hdrs_len);
6510Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = NULL;
6520Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = 0;
6530Sstevel@tonic-gate 	}
6541676Sjpk 
6551676Sjpk 	ip6_pkt_free(&icmp->icmp_sticky_ipp);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	crfree(icmp->icmp_credp);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* Free the icmp structure and release the minor device number. */
6600Sstevel@tonic-gate 	i1 = mi_close_comm(&icmp_g_head, q);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	return (i1);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate /*
6660Sstevel@tonic-gate  * This routine handles each T_DISCON_REQ message passed to icmp
6670Sstevel@tonic-gate  * as an indicating that ICMP is no longer connected. This results
6680Sstevel@tonic-gate  * in sending a T_BIND_REQ to IP to restore the binding to just
6690Sstevel@tonic-gate  * the local address.
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  * This routine sends down a T_BIND_REQ to IP with the following mblks:
6720Sstevel@tonic-gate  *	T_BIND_REQ	- specifying just the local address.
6730Sstevel@tonic-gate  *	T_OK_ACK	- for the T_DISCON_REQ
6740Sstevel@tonic-gate  *
6750Sstevel@tonic-gate  * The disconnect completes in icmp_rput.
6760Sstevel@tonic-gate  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
6770Sstevel@tonic-gate  * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
6780Sstevel@tonic-gate  * it to an error ack for the appropriate primitive.
6790Sstevel@tonic-gate  */
6800Sstevel@tonic-gate static void
6810Sstevel@tonic-gate icmp_disconnect(queue_t *q, mblk_t *mp)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	icmp_t	*icmp;
6840Sstevel@tonic-gate 	mblk_t	*mp1;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (icmp->icmp_state != TS_DATA_XFER) {
6890Sstevel@tonic-gate 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
6900Sstevel@tonic-gate 		    "icmp_disconnect: bad state, %d", icmp->icmp_state);
6910Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
6920Sstevel@tonic-gate 		return;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 	icmp->icmp_v6src = icmp->icmp_bound_v6src;
6950Sstevel@tonic-gate 	icmp->icmp_state = TS_IDLE;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	/*
6980Sstevel@tonic-gate 	 * Send down bind to IP to remove the full binding and revert
6990Sstevel@tonic-gate 	 * to the local address binding.
7000Sstevel@tonic-gate 	 */
7010Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
7020Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0);
7030Sstevel@tonic-gate 	} else {
7040Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
7050Sstevel@tonic-gate 		mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 	if (mp1 == NULL) {
7080Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
7090Sstevel@tonic-gate 		return;
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	mp = mi_tpi_ok_ack_alloc(mp);
7120Sstevel@tonic-gate 	if (mp == NULL) {
7130Sstevel@tonic-gate 		/* Unable to reuse the T_DISCON_REQ for the ack. */
7140Sstevel@tonic-gate 		icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
7150Sstevel@tonic-gate 		return;
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
7190Sstevel@tonic-gate 		int error;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		/* Rebuild the header template */
7220Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
7230Sstevel@tonic-gate 		if (error != 0) {
7240Sstevel@tonic-gate 			icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
7250Sstevel@tonic-gate 			freemsg(mp1);
7260Sstevel@tonic-gate 			return;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	icmp->icmp_discon_pending = 1;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/* Append the T_OK_ACK to the T_BIND_REQ for icmp_rput */
7320Sstevel@tonic-gate 	linkb(mp1, mp);
7330Sstevel@tonic-gate 	putnext(q, mp1);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate /* This routine creates a T_ERROR_ACK message and passes it upstream. */
7370Sstevel@tonic-gate static void
7380Sstevel@tonic-gate icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
7410Sstevel@tonic-gate 		qreply(q, mp);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate /* Shorthand to generate and send TPI error acks to our client */
7450Sstevel@tonic-gate static void
7460Sstevel@tonic-gate icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
7470Sstevel@tonic-gate     t_scalar_t t_error, int sys_error)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	struct T_error_ack	*teackp;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
7520Sstevel@tonic-gate 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
7530Sstevel@tonic-gate 		teackp = (struct T_error_ack *)mp->b_rptr;
7540Sstevel@tonic-gate 		teackp->ERROR_prim = primitive;
7550Sstevel@tonic-gate 		teackp->TLI_error = t_error;
7560Sstevel@tonic-gate 		teackp->UNIX_error = sys_error;
7570Sstevel@tonic-gate 		qreply(q, mp);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /*
7620Sstevel@tonic-gate  * icmp_icmp_error is called by icmp_rput to process ICMP
7630Sstevel@tonic-gate  * messages passed up by IP.
7640Sstevel@tonic-gate  * Generates the appropriate T_UDERROR_IND for permanent
7650Sstevel@tonic-gate  * (non-transient) errors.
7660Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
7670Sstevel@tonic-gate  * the ICMP header.
7680Sstevel@tonic-gate  */
7690Sstevel@tonic-gate static void
7700Sstevel@tonic-gate icmp_icmp_error(queue_t *q, mblk_t *mp)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	icmph_t *icmph;
7730Sstevel@tonic-gate 	ipha_t	*ipha;
7740Sstevel@tonic-gate 	int	iph_hdr_length;
7750Sstevel@tonic-gate 	sin_t	sin;
7760Sstevel@tonic-gate 	sin6_t	sin6;
7770Sstevel@tonic-gate 	mblk_t	*mp1;
7780Sstevel@tonic-gate 	int	error = 0;
7790Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Deliver T_UDERROR_IND when the application has asked for it.
7830Sstevel@tonic-gate 	 * The socket layer enables this automatically when connected.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	if (!icmp->icmp_dgram_errind) {
7860Sstevel@tonic-gate 		freemsg(mp);
7870Sstevel@tonic-gate 		return;
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7930Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
7940Sstevel@tonic-gate 		icmp_icmp_error_ipv6(q, mp);
7950Sstevel@tonic-gate 		return;
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 	ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
8000Sstevel@tonic-gate 	icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]);
8010Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
8020Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	switch (icmph->icmph_type) {
8050Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
8060Sstevel@tonic-gate 		switch (icmph->icmph_code) {
8070Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
8080Sstevel@tonic-gate 			/*
8090Sstevel@tonic-gate 			 * IP has already adjusted the path MTU.
8100Sstevel@tonic-gate 			 * XXX Somehow pass MTU indication to application?
8110Sstevel@tonic-gate 			 */
8120Sstevel@tonic-gate 			break;
8130Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
8140Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
8150Sstevel@tonic-gate 			error = ECONNREFUSED;
8160Sstevel@tonic-gate 			break;
8170Sstevel@tonic-gate 		default:
8180Sstevel@tonic-gate 			/* Transient errors */
8190Sstevel@tonic-gate 			break;
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 		break;
8220Sstevel@tonic-gate 	default:
8230Sstevel@tonic-gate 		/* Transient errors */
8240Sstevel@tonic-gate 		break;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	if (error == 0) {
8270Sstevel@tonic-gate 		freemsg(mp);
8280Sstevel@tonic-gate 		return;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	switch (icmp->icmp_family) {
8320Sstevel@tonic-gate 	case AF_INET:
8330Sstevel@tonic-gate 		sin = sin_null;
8340Sstevel@tonic-gate 		sin.sin_family = AF_INET;
8350Sstevel@tonic-gate 		sin.sin_addr.s_addr = ipha->ipha_dst;
8360Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
8370Sstevel@tonic-gate 		    error);
8380Sstevel@tonic-gate 		break;
8390Sstevel@tonic-gate 	case AF_INET6:
8400Sstevel@tonic-gate 		sin6 = sin6_null;
8410Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
8420Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
8450Sstevel@tonic-gate 		    NULL, 0, error);
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 	if (mp1)
8490Sstevel@tonic-gate 		putnext(q, mp1);
8500Sstevel@tonic-gate 	freemsg(mp);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate /*
8540Sstevel@tonic-gate  * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6
8550Sstevel@tonic-gate  * for IPv6 packets.
8560Sstevel@tonic-gate  * Send permanent (non-transient) errors upstream.
8570Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
8580Sstevel@tonic-gate  * as the ICMPv6 header.
8590Sstevel@tonic-gate  */
8600Sstevel@tonic-gate static void
8610Sstevel@tonic-gate icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate 	icmp6_t		*icmp6;
8640Sstevel@tonic-gate 	ip6_t		*ip6h, *outer_ip6h;
8650Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
8660Sstevel@tonic-gate 	uint8_t		*nexthdrp;
8670Sstevel@tonic-gate 	sin6_t		sin6;
8680Sstevel@tonic-gate 	mblk_t		*mp1;
8690Sstevel@tonic-gate 	int		error = 0;
8700Sstevel@tonic-gate 	icmp_t		*icmp = (icmp_t *)q->q_ptr;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	outer_ip6h = (ip6_t *)mp->b_rptr;
8730Sstevel@tonic-gate 	if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
8740Sstevel@tonic-gate 		iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
8750Sstevel@tonic-gate 	else
8760Sstevel@tonic-gate 		iph_hdr_length = IPV6_HDR_LEN;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
8790Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
8800Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
8810Sstevel@tonic-gate 		freemsg(mp);
8820Sstevel@tonic-gate 		return;
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	if (*nexthdrp != icmp->icmp_proto) {
8850Sstevel@tonic-gate 		/*
8860Sstevel@tonic-gate 		 * Could have switched icmp_proto after while ip did fanout of
8870Sstevel@tonic-gate 		 * this message
8880Sstevel@tonic-gate 		 */
8890Sstevel@tonic-gate 		freemsg(mp);
8900Sstevel@tonic-gate 		return;
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
8930Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
8940Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
8950Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
8960Sstevel@tonic-gate 			error = ECONNREFUSED;
8970Sstevel@tonic-gate 			break;
8980Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
8990Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
9000Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
9010Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
9020Sstevel@tonic-gate 			/* Transient errors */
9030Sstevel@tonic-gate 			break;
9040Sstevel@tonic-gate 		default:
9050Sstevel@tonic-gate 			break;
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 		break;
9080Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG: {
9090Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi;
9100Sstevel@tonic-gate 		struct T_opthdr		*toh;
9110Sstevel@tonic-gate 		size_t			udi_size;
9120Sstevel@tonic-gate 		mblk_t			*newmp;
9130Sstevel@tonic-gate 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
9140Sstevel@tonic-gate 		    sizeof (struct ip6_mtuinfo);
9150Sstevel@tonic-gate 		sin6_t			*sin6;
9160Sstevel@tonic-gate 		struct ip6_mtuinfo	*mtuinfo;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		/*
9190Sstevel@tonic-gate 		 * If the application has requested to receive path mtu
9200Sstevel@tonic-gate 		 * information, send up an empty message containing an
9210Sstevel@tonic-gate 		 * IPV6_PATHMTU ancillary data item.
9220Sstevel@tonic-gate 		 */
9230Sstevel@tonic-gate 		if (!icmp->icmp_ipv6_recvpathmtu)
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
9270Sstevel@tonic-gate 		    opt_length;
9280Sstevel@tonic-gate 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
9290Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipInErrors);
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 		/*
9340Sstevel@tonic-gate 		 * newmp->b_cont is left to NULL on purpose.  This is an
9350Sstevel@tonic-gate 		 * empty message containing only ancillary data.
9360Sstevel@tonic-gate 		 */
9370Sstevel@tonic-gate 		newmp->b_datap->db_type = M_PROTO;
9380Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
9390Sstevel@tonic-gate 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
9400Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
9410Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin6_t);
9420Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
9430Sstevel@tonic-gate 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
9440Sstevel@tonic-gate 		tudi->OPT_length = opt_length;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		sin6 = (sin6_t *)&tudi[1];
9470Sstevel@tonic-gate 		bzero(sin6, sizeof (sin6_t));
9480Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
9490Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_v6dst;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 		toh = (struct T_opthdr *)&sin6[1];
9520Sstevel@tonic-gate 		toh->level = IPPROTO_IPV6;
9530Sstevel@tonic-gate 		toh->name = IPV6_PATHMTU;
9540Sstevel@tonic-gate 		toh->len = opt_length;
9550Sstevel@tonic-gate 		toh->status = 0;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
9580Sstevel@tonic-gate 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
9590Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
9600Sstevel@tonic-gate 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
9610Sstevel@tonic-gate 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
9620Sstevel@tonic-gate 		/*
9630Sstevel@tonic-gate 		 * We've consumed everything we need from the original
9640Sstevel@tonic-gate 		 * message.  Free it, then send our empty message.
9650Sstevel@tonic-gate 		 */
9660Sstevel@tonic-gate 		freemsg(mp);
9670Sstevel@tonic-gate 		putnext(q, newmp);
9680Sstevel@tonic-gate 		return;
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
9710Sstevel@tonic-gate 		/* Transient errors */
9720Sstevel@tonic-gate 		break;
9730Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
9740Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
9750Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
9760Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
9770Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
9780Sstevel@tonic-gate 			error = ECONNREFUSED;
9790Sstevel@tonic-gate 			break;
9800Sstevel@tonic-gate 		}
9810Sstevel@tonic-gate 		break;
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 	if (error == 0) {
9840Sstevel@tonic-gate 		freemsg(mp);
9850Sstevel@tonic-gate 		return;
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	sin6 = sin6_null;
9890Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
9900Sstevel@tonic-gate 	sin6.sin6_addr = ip6h->ip6_dst;
9910Sstevel@tonic-gate 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
9940Sstevel@tonic-gate 	    error);
9950Sstevel@tonic-gate 	if (mp1)
9960Sstevel@tonic-gate 		putnext(q, mp1);
9970Sstevel@tonic-gate 	freemsg(mp);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate  * This routine responds to T_ADDR_REQ messages.  It is called by icmp_wput.
10020Sstevel@tonic-gate  * The local address is filled in if endpoint is bound. The remote address
10030Sstevel@tonic-gate  * is filled in if remote address has been precified ("connected endpoint")
10040Sstevel@tonic-gate  * (The concept of connected CLTS sockets is alien to published TPI
10050Sstevel@tonic-gate  *  but we support it anyway).
10060Sstevel@tonic-gate  */
10070Sstevel@tonic-gate static void
10080Sstevel@tonic-gate icmp_addr_req(queue_t *q, mblk_t *mp)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
10110Sstevel@tonic-gate 	mblk_t	*ackmp;
10120Sstevel@tonic-gate 	struct T_addr_ack *taa;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/* Make it large enough for worst case */
10150Sstevel@tonic-gate 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
10160Sstevel@tonic-gate 	    2 * sizeof (sin6_t), 1);
10170Sstevel@tonic-gate 	if (ackmp == NULL) {
10180Sstevel@tonic-gate 		icmp_err_ack(q, mp, TSYSERR, ENOMEM);
10190Sstevel@tonic-gate 		return;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 	taa = (struct T_addr_ack *)ackmp->b_rptr;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	bzero(taa, sizeof (struct T_addr_ack));
10240Sstevel@tonic-gate 	ackmp->b_wptr = (uchar_t *)&taa[1];
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	taa->PRIM_type = T_ADDR_ACK;
10270Sstevel@tonic-gate 	ackmp->b_datap->db_type = M_PCPROTO;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	 * Note: Following code assumes 32 bit alignment of basic
10310Sstevel@tonic-gate 	 * data structures like sin_t and struct T_addr_ack.
10320Sstevel@tonic-gate 	 */
10330Sstevel@tonic-gate 	if (icmp->icmp_state != TS_UNBND) {
10340Sstevel@tonic-gate 		/*
10350Sstevel@tonic-gate 		 * Fill in local address
10360Sstevel@tonic-gate 		 */
10370Sstevel@tonic-gate 		taa->LOCADDR_offset = sizeof (*taa);
10380Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
10390Sstevel@tonic-gate 			sin_t	*sin;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin_t);
10420Sstevel@tonic-gate 			sin = (sin_t *)&taa[1];
10430Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
10440Sstevel@tonic-gate 			*sin = sin_null;
10450Sstevel@tonic-gate 			sin->sin_family = AF_INET;
10460Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
10470Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
10480Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src,
10490Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
10500Sstevel@tonic-gate 			} else {
10510Sstevel@tonic-gate 				/*
10520Sstevel@tonic-gate 				 * INADDR_ANY
10530Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
10540Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
10550Sstevel@tonic-gate 				 * local address instead (that could
10560Sstevel@tonic-gate 				 * also still be INADDR_ANY)
10570Sstevel@tonic-gate 				 */
10580Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src,
10590Sstevel@tonic-gate 				    sin->sin_addr.s_addr);
10600Sstevel@tonic-gate 			}
10610Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin[1];
10620Sstevel@tonic-gate 		} else {
10630Sstevel@tonic-gate 			sin6_t	*sin6;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 			ASSERT(icmp->icmp_family == AF_INET6);
10660Sstevel@tonic-gate 			taa->LOCADDR_length = sizeof (sin6_t);
10670Sstevel@tonic-gate 			sin6 = (sin6_t *)&taa[1];
10680Sstevel@tonic-gate 			/* Fill zeroes and then intialize non-zero fields */
10690Sstevel@tonic-gate 			*sin6 = sin6_null;
10700Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
10710Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
10720Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_v6src;
10730Sstevel@tonic-gate 			} else {
10740Sstevel@tonic-gate 				/*
10750Sstevel@tonic-gate 				 * UNSPECIFIED
10760Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
10770Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
10780Sstevel@tonic-gate 				 * local address instead (that could
10790Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
10800Sstevel@tonic-gate 				 */
10810Sstevel@tonic-gate 				sin6->sin6_addr = icmp->icmp_bound_v6src;
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			ackmp->b_wptr = (uchar_t *)&sin6[1];
10840Sstevel@tonic-gate 		}
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
10870Sstevel@tonic-gate 	qreply(q, ackmp);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate static void
10910Sstevel@tonic-gate icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	*tap = icmp_g_t_info_ack;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6)
10960Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin6_t);
10970Sstevel@tonic-gate 	else
10980Sstevel@tonic-gate 		tap->ADDR_size = sizeof (sin_t);
10990Sstevel@tonic-gate 	tap->CURRENT_state = icmp->icmp_state;
11000Sstevel@tonic-gate 	tap->OPT_size = icmp_max_optsize;
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
11050Sstevel@tonic-gate  * icmp_wput.  Much of the T_CAPABILITY_ACK information is copied from
11060Sstevel@tonic-gate  * icmp_g_t_info_ack.  The current state of the stream is copied from
11070Sstevel@tonic-gate  * icmp_state.
11080Sstevel@tonic-gate  */
11090Sstevel@tonic-gate static void
11100Sstevel@tonic-gate icmp_capability_req(queue_t *q, mblk_t *mp)
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate 	icmp_t			*icmp = (icmp_t *)q->q_ptr;
11130Sstevel@tonic-gate 	t_uscalar_t		cap_bits1;
11140Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
11190Sstevel@tonic-gate 		mp->b_datap->db_type, T_CAPABILITY_ACK);
11200Sstevel@tonic-gate 	if (!mp)
11210Sstevel@tonic-gate 		return;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
11240Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
11270Sstevel@tonic-gate 		icmp_copy_info(&tcap->INFO_ack, icmp);
11280Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	qreply(q, mp);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages.  It is called by icmp_wput.
11360Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
11370Sstevel@tonic-gate  * The current state of the stream is copied from icmp_state.
11380Sstevel@tonic-gate  */
11390Sstevel@tonic-gate static void
11400Sstevel@tonic-gate icmp_info_req(queue_t *q, mblk_t *mp)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
11450Sstevel@tonic-gate 	    T_INFO_ACK);
11460Sstevel@tonic-gate 	if (!mp)
11470Sstevel@tonic-gate 		return;
11480Sstevel@tonic-gate 	icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
11490Sstevel@tonic-gate 	qreply(q, mp);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate  * IP recognizes seven kinds of bind requests:
11540Sstevel@tonic-gate  *
11550Sstevel@tonic-gate  * - A zero-length address binds only to the protocol number.
11560Sstevel@tonic-gate  *
11570Sstevel@tonic-gate  * - A 4-byte address is treated as a request to
11580Sstevel@tonic-gate  * validate that the address is a valid local IPv4
11590Sstevel@tonic-gate  * address, appropriate for an application to bind to.
11600Sstevel@tonic-gate  * IP does the verification, but does not make any note
11610Sstevel@tonic-gate  * of the address at this time.
11620Sstevel@tonic-gate  *
11630Sstevel@tonic-gate  * - A 16-byte address contains is treated as a request
11640Sstevel@tonic-gate  * to validate a local IPv6 address, as the 4-byte
11650Sstevel@tonic-gate  * address case above.
11660Sstevel@tonic-gate  *
11670Sstevel@tonic-gate  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
11680Sstevel@tonic-gate  * use it for the inbound fanout of packets.
11690Sstevel@tonic-gate  *
11700Sstevel@tonic-gate  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
11710Sstevel@tonic-gate  * use it for the inbound fanout of packets.
11720Sstevel@tonic-gate  *
11730Sstevel@tonic-gate  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
11740Sstevel@tonic-gate  * information consisting of local and remote addresses
11750Sstevel@tonic-gate  * and ports (unused for raw sockets).  In this case, the addresses are both
11760Sstevel@tonic-gate  * validated as appropriate for this operation, and, if
11770Sstevel@tonic-gate  * so, the information is retained for use in the
11780Sstevel@tonic-gate  * inbound fanout.
11790Sstevel@tonic-gate  *
11800Sstevel@tonic-gate  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
11810Sstevel@tonic-gate  * fanout information, like the 12-byte case above.
11820Sstevel@tonic-gate  *
11830Sstevel@tonic-gate  * IP will also fill in the IRE request mblk with information
11840Sstevel@tonic-gate  * regarding our peer.  In all cases, we notify IP of our protocol
11850Sstevel@tonic-gate  * type by appending a single protocol byte to the bind request.
11860Sstevel@tonic-gate  */
11870Sstevel@tonic-gate static mblk_t *
11880Sstevel@tonic-gate icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length,
11890Sstevel@tonic-gate     in_port_t fport)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	char	*cp;
11920Sstevel@tonic-gate 	mblk_t	*mp;
11930Sstevel@tonic-gate 	struct T_bind_req *tbr;
11940Sstevel@tonic-gate 	ipa_conn_t	*ac;
11950Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
11960Sstevel@tonic-gate 	sin_t		*sin;
11970Sstevel@tonic-gate 	sin6_t		*sin6;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
12020Sstevel@tonic-gate 	if (mp == NULL)
12030Sstevel@tonic-gate 		return (NULL);
12040Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
12050Sstevel@tonic-gate 	tbr = (struct T_bind_req *)mp->b_rptr;
12060Sstevel@tonic-gate 	tbr->PRIM_type = bind_prim;
12070Sstevel@tonic-gate 	tbr->ADDR_offset = sizeof (*tbr);
12080Sstevel@tonic-gate 	tbr->CONIND_number = 0;
12090Sstevel@tonic-gate 	tbr->ADDR_length = addr_length;
12100Sstevel@tonic-gate 	cp = (char *)&tbr[1];
12110Sstevel@tonic-gate 	switch (addr_length) {
12120Sstevel@tonic-gate 	case sizeof (ipa_conn_t):
12130Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
12140Sstevel@tonic-gate 		/* Append a request for an IRE */
12150Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12160Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
12170Sstevel@tonic-gate 			freemsg(mp);
12180Sstevel@tonic-gate 			return (NULL);
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12210Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
12240Sstevel@tonic-gate 		ac = (ipa_conn_t *)cp;
12250Sstevel@tonic-gate 		ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src);
12260Sstevel@tonic-gate 		ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst);
12270Sstevel@tonic-gate 		ac->ac_fport = fport;
12280Sstevel@tonic-gate 		ac->ac_lport = 0;
12290Sstevel@tonic-gate 		break;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	case sizeof (ipa6_conn_t):
12320Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
12330Sstevel@tonic-gate 		/* Append a request for an IRE */
12340Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12350Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
12360Sstevel@tonic-gate 			freemsg(mp);
12370Sstevel@tonic-gate 			return (NULL);
12380Sstevel@tonic-gate 		}
12390Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12400Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		/* cp known to be 32 bit aligned */
12430Sstevel@tonic-gate 		ac6 = (ipa6_conn_t *)cp;
12440Sstevel@tonic-gate 		ac6->ac6_laddr = icmp->icmp_v6src;
12450Sstevel@tonic-gate 		ac6->ac6_faddr = icmp->icmp_v6dst;
12460Sstevel@tonic-gate 		ac6->ac6_fport = fport;
12470Sstevel@tonic-gate 		ac6->ac6_lport = 0;
12480Sstevel@tonic-gate 		break;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	case sizeof (sin_t):
12510Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET);
12520Sstevel@tonic-gate 		/* Append a request for an IRE */
12530Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12540Sstevel@tonic-gate 		if (!mp->b_cont) {
12550Sstevel@tonic-gate 			freemsg(mp);
12560Sstevel@tonic-gate 			return (NULL);
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12590Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 		sin = (sin_t *)cp;
12620Sstevel@tonic-gate 		*sin = sin_null;
12630Sstevel@tonic-gate 		sin->sin_family = AF_INET;
12640Sstevel@tonic-gate 		sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
12650Sstevel@tonic-gate 		break;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	case sizeof (sin6_t):
12680Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
12690Sstevel@tonic-gate 		/* Append a request for an IRE */
12700Sstevel@tonic-gate 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
12710Sstevel@tonic-gate 		if (!mp->b_cont) {
12720Sstevel@tonic-gate 			freemsg(mp);
12730Sstevel@tonic-gate 			return (NULL);
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 		mp->b_cont->b_wptr += sizeof (ire_t);
12760Sstevel@tonic-gate 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		sin6 = (sin6_t *)cp;
12790Sstevel@tonic-gate 		*sin6 = sin6_null;
12800Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
12810Sstevel@tonic-gate 		sin6->sin6_addr = icmp->icmp_bound_v6src;
12820Sstevel@tonic-gate 		break;
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 	/* Add protocol number to end */
12850Sstevel@tonic-gate 	cp[addr_length] = icmp->icmp_proto;
12860Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
12870Sstevel@tonic-gate 	return (mp);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate 
12901676Sjpk /* ARGSUSED */
12911676Sjpk static void
12921676Sjpk dummy_func(void *arg)
12931676Sjpk {
12941676Sjpk }
12951676Sjpk 
12961676Sjpk static mblk_t *
12971676Sjpk alloc_wait(queue_t *q, size_t len, int pri, int *errp)
12981676Sjpk {
12991676Sjpk 	mblk_t *mp;
13001676Sjpk 	bufcall_id_t id;
13011676Sjpk 	int retv;
13021676Sjpk 
13031676Sjpk 	while ((mp = allocb(len, pri)) == NULL) {
13041676Sjpk 		id = qbufcall(q, len, pri, dummy_func, NULL);
13051676Sjpk 		if (id == 0) {
13061676Sjpk 			*errp = ENOMEM;
13071676Sjpk 			break;
13081676Sjpk 		}
13091676Sjpk 		retv = qwait_sig(q);
13101676Sjpk 		qunbufcall(q, id);
13111676Sjpk 		if (retv == 0) {
13121676Sjpk 			*errp = EINTR;
13131676Sjpk 			break;
13141676Sjpk 		}
13151676Sjpk 	}
13161676Sjpk 	if (mp != NULL)
13171676Sjpk 		mp->b_wptr += len;
13181676Sjpk 	return (mp);
13191676Sjpk }
13201676Sjpk 
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate  * This is the open routine for icmp.  It allocates a icmp_t structure for
13230Sstevel@tonic-gate  * the stream and, on the first open of the module, creates an ND table.
13240Sstevel@tonic-gate  */
13250Sstevel@tonic-gate static int
13260Sstevel@tonic-gate icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
13270Sstevel@tonic-gate {
13280Sstevel@tonic-gate 	int	err;
13290Sstevel@tonic-gate 	icmp_t	*icmp;
13301676Sjpk 	mblk_t	*mp;
13311676Sjpk 	out_labeled_t *olp;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	/* If the stream is already open, return immediately. */
13340Sstevel@tonic-gate 	if (q->q_ptr != NULL)
13350Sstevel@tonic-gate 		return (0);
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	/* If this is not a push of icmp as a module, fail. */
13380Sstevel@tonic-gate 	if (sflag != MODOPEN)
13390Sstevel@tonic-gate 		return (EINVAL);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	/*
13420Sstevel@tonic-gate 	 * Defer the qprocson until everything is initialized since
13430Sstevel@tonic-gate 	 * we are D_MTPERQ and after qprocson the rput routine can
13440Sstevel@tonic-gate 	 * run. (Could do qprocson earlier since icmp currently
13450Sstevel@tonic-gate 	 * has an outer perimeter.)
13460Sstevel@tonic-gate 	 */
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	/*
13490Sstevel@tonic-gate 	 * Create a icmp_t structure for this stream and link into the
13500Sstevel@tonic-gate 	 * list of open streams.
13510Sstevel@tonic-gate 	 */
13520Sstevel@tonic-gate 	err = mi_open_comm(&icmp_g_head, sizeof (icmp_t), q, devp,
13530Sstevel@tonic-gate 	    flag, sflag, credp);
13541676Sjpk 	if (err != 0)
13550Sstevel@tonic-gate 		return (err);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	/*
13580Sstevel@tonic-gate 	 * The receive hiwat is only looked at on the stream head queue.
13590Sstevel@tonic-gate 	 * Store in q_hiwat in order to return on SO_RCVBUF getsockopts.
13600Sstevel@tonic-gate 	 */
13610Sstevel@tonic-gate 	q->q_hiwat = icmp_recv_hiwat;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	/* Set the initial state of the stream and the privilege status. */
13640Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
13650Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
13660Sstevel@tonic-gate 	icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13670Sstevel@tonic-gate 	icmp->icmp_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
13680Sstevel@tonic-gate 	icmp->icmp_filter = NULL;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	icmp->icmp_credp = credp;
13710Sstevel@tonic-gate 	crhold(credp);
13720Sstevel@tonic-gate 
13731676Sjpk 	/*
13741676Sjpk 	 * If the caller has the process-wide flag set, then default to MAC
13751676Sjpk 	 * exempt mode.  This allows read-down to unlabeled hosts.
13761676Sjpk 	 */
13771676Sjpk 	if (getpflags(NET_MAC_AWARE, credp) != 0)
13781676Sjpk 		icmp->icmp_mac_exempt = B_TRUE;
13791676Sjpk 
13800Sstevel@tonic-gate 	icmp->icmp_zoneid = getzoneid();
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	if (getmajor(*devp) == (major_t)ICMP6_MAJ) {
13830Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV6_VERSION;
13840Sstevel@tonic-gate 		icmp->icmp_family = AF_INET6;
13850Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
13860Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMPV6;
13870Sstevel@tonic-gate 		icmp->icmp_checksum_off = 2;	/* Offset for icmp6_cksum */
13880Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IPV6_HDR_LEN;
13890Sstevel@tonic-gate 		icmp->icmp_ttl = (uint8_t)icmp_ipv6_hoplimit;
13900Sstevel@tonic-gate 	} else {
13910Sstevel@tonic-gate 		icmp->icmp_ipversion = IPV4_VERSION;
13920Sstevel@tonic-gate 		icmp->icmp_family = AF_INET;
13930Sstevel@tonic-gate 		/* May be changed by a SO_PROTOTYPE socket option. */
13940Sstevel@tonic-gate 		icmp->icmp_proto = IPPROTO_ICMP;
13950Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH;
13960Sstevel@tonic-gate 		icmp->icmp_ttl = (uint8_t)icmp_ipv4_ttl;
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 	qprocson(q);
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	/*
14010Sstevel@tonic-gate 	 * Check if icmp is being I_PUSHed by a non-privileged user.
14020Sstevel@tonic-gate 	 * If so, we set icmp_restricted to indicate that only MIB
14030Sstevel@tonic-gate 	 * traffic may pass.
14040Sstevel@tonic-gate 	 */
14050Sstevel@tonic-gate 	if (secpolicy_net_icmpaccess(credp) != 0) {
14060Sstevel@tonic-gate 		icmp->icmp_restricted = 1;
14070Sstevel@tonic-gate 	}
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	/*
14100Sstevel@tonic-gate 	 * The transmit hiwat is only looked at on IP's queue.
14110Sstevel@tonic-gate 	 * Store in q_hiwat in order to return on SO_SNDBUF
14120Sstevel@tonic-gate 	 * getsockopts.
14130Sstevel@tonic-gate 	 */
14140Sstevel@tonic-gate 	WR(q)->q_hiwat = icmp_xmit_hiwat;
14150Sstevel@tonic-gate 	WR(q)->q_next->q_hiwat = WR(q)->q_hiwat;
14160Sstevel@tonic-gate 	WR(q)->q_lowat = icmp_xmit_lowat;
14170Sstevel@tonic-gate 	WR(q)->q_next->q_lowat = WR(q)->q_lowat;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
14200Sstevel@tonic-gate 		/* Build initial header template for transmit */
14211676Sjpk 		err = icmp_build_hdrs(q, icmp);
14221676Sjpk 		if (err != 0)
14231676Sjpk 			goto open_error;
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 	/* Set the Stream head write offset. */
14260Sstevel@tonic-gate 	(void) mi_set_sth_wroff(q, icmp->icmp_max_hdr_len + icmp_wroff_extra);
14270Sstevel@tonic-gate 	(void) mi_set_sth_hiwat(q, q->q_hiwat);
14280Sstevel@tonic-gate 
14291676Sjpk 	if (is_system_labeled()) {
14301676Sjpk 		/* notify IP that we know about labeling */
14311676Sjpk 		mp = alloc_wait(q, sizeof (*olp), BPRI_MED, &err);
14321676Sjpk 		if (mp == NULL)
14331676Sjpk 			goto open_error;
14341676Sjpk 		mp->b_datap->db_type = M_CTL;
14351676Sjpk 		olp = (out_labeled_t *)mp->b_rptr;
14361676Sjpk 		olp->out_labeled_type = IP_ULP_OUT_LABELED;
14371676Sjpk 		olp->out_qnext = WR(q)->q_next;
14381676Sjpk 		putnext(WR(q), mp);
14391676Sjpk 
14401676Sjpk 		/* save off a copy for closing */
14411676Sjpk 		mp = alloc_wait(q, sizeof (*olp), BPRI_MED, &err);
14421676Sjpk 		if (mp == NULL)
14431676Sjpk 			goto open_error;
14441676Sjpk 		mp->b_datap->db_type = M_CTL;
14451676Sjpk 		olp = (out_labeled_t *)mp->b_rptr;
14461676Sjpk 		olp->out_labeled_type = IP_ULP_OUT_LABELED;
14471676Sjpk 		olp->out_qnext = NULL;
14481676Sjpk 		icmp->icmp_delabel = mp;
14491676Sjpk 	}
14501676Sjpk 
14510Sstevel@tonic-gate 	return (0);
14521676Sjpk 
14531676Sjpk open_error:
14541676Sjpk 	qprocsoff(q);
14551676Sjpk 	crfree(credp);
14561676Sjpk 	(void) mi_close_comm(&icmp_g_head, q);
14571676Sjpk 	return (err);
14580Sstevel@tonic-gate }
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate /*
14610Sstevel@tonic-gate  * Which ICMP options OK to set through T_UNITDATA_REQ...
14620Sstevel@tonic-gate  */
14630Sstevel@tonic-gate /* ARGSUSED */
14640Sstevel@tonic-gate static boolean_t
14650Sstevel@tonic-gate icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
14660Sstevel@tonic-gate {
14670Sstevel@tonic-gate 	return (B_TRUE);
14680Sstevel@tonic-gate }
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate /*
14710Sstevel@tonic-gate  * This routine gets default values of certain options whose default
14720Sstevel@tonic-gate  * values are maintained by protcol specific code
14730Sstevel@tonic-gate  */
14740Sstevel@tonic-gate /* ARGSUSED */
14750Sstevel@tonic-gate int
14760Sstevel@tonic-gate icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate 	int *i1 = (int *)ptr;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	switch (level) {
14810Sstevel@tonic-gate 	case IPPROTO_IP:
14820Sstevel@tonic-gate 		switch (name) {
14830Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
14840Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
14850Sstevel@tonic-gate 			return (sizeof (uchar_t));
14860Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
14870Sstevel@tonic-gate 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
14880Sstevel@tonic-gate 			return (sizeof (uchar_t));
14890Sstevel@tonic-gate 		}
14900Sstevel@tonic-gate 		break;
14910Sstevel@tonic-gate 	case IPPROTO_IPV6:
14920Sstevel@tonic-gate 		switch (name) {
14930Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
14940Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_TTL;
14950Sstevel@tonic-gate 			return (sizeof (int));
14960Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
14970Sstevel@tonic-gate 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
14980Sstevel@tonic-gate 			return (sizeof (int));
14990Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
15000Sstevel@tonic-gate 			*i1 = icmp_ipv6_hoplimit;
15010Sstevel@tonic-gate 			return (sizeof (int));
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 		break;
15040Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
15050Sstevel@tonic-gate 		switch (name) {
15060Sstevel@tonic-gate 		case ICMP6_FILTER:
15070Sstevel@tonic-gate 			/* Make it look like "pass all" */
15080Sstevel@tonic-gate 			ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
15090Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 		break;
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate 	return (-1);
15140Sstevel@tonic-gate }
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate /*
15170Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
15180Sstevel@tonic-gate  * It returns the size of the option retrieved.
15190Sstevel@tonic-gate  */
15200Sstevel@tonic-gate int
15210Sstevel@tonic-gate icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
15240Sstevel@tonic-gate 	int	*i1 = (int *)ptr;
15250Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &icmp->icmp_sticky_ipp;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	switch (level) {
15280Sstevel@tonic-gate 	case SOL_SOCKET:
15290Sstevel@tonic-gate 		switch (name) {
15300Sstevel@tonic-gate 		case SO_DEBUG:
15310Sstevel@tonic-gate 			*i1 = icmp->icmp_debug;
15320Sstevel@tonic-gate 			break;
15330Sstevel@tonic-gate 		case SO_TYPE:
15340Sstevel@tonic-gate 			*i1 = SOCK_RAW;
15350Sstevel@tonic-gate 			break;
15360Sstevel@tonic-gate 		case SO_PROTOTYPE:
15370Sstevel@tonic-gate 			*i1 = icmp->icmp_proto;
15380Sstevel@tonic-gate 			break;
15390Sstevel@tonic-gate 		case SO_REUSEADDR:
15400Sstevel@tonic-gate 			*i1 = icmp->icmp_reuseaddr;
15410Sstevel@tonic-gate 			break;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 		/*
15440Sstevel@tonic-gate 		 * The following three items are available here,
15450Sstevel@tonic-gate 		 * but are only meaningful to IP.
15460Sstevel@tonic-gate 		 */
15470Sstevel@tonic-gate 		case SO_DONTROUTE:
15480Sstevel@tonic-gate 			*i1 = icmp->icmp_dontroute;
15490Sstevel@tonic-gate 			break;
15500Sstevel@tonic-gate 		case SO_USELOOPBACK:
15510Sstevel@tonic-gate 			*i1 = icmp->icmp_useloopback;
15520Sstevel@tonic-gate 			break;
15530Sstevel@tonic-gate 		case SO_BROADCAST:
15540Sstevel@tonic-gate 			*i1 = icmp->icmp_broadcast;
15550Sstevel@tonic-gate 			break;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 		case SO_SNDBUF:
15580Sstevel@tonic-gate 			ASSERT(q->q_hiwat <= INT_MAX);
15590Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
15600Sstevel@tonic-gate 			break;
15610Sstevel@tonic-gate 		case SO_RCVBUF:
15620Sstevel@tonic-gate 			ASSERT(RD(q)->q_hiwat <= INT_MAX);
15630Sstevel@tonic-gate 			*i1 = (int)RD(q)->q_hiwat;
15640Sstevel@tonic-gate 			break;
15650Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
15660Sstevel@tonic-gate 			*i1 = icmp->icmp_dgram_errind;
15670Sstevel@tonic-gate 			break;
15681673Sgt145670 		case SO_TIMESTAMP:
15691673Sgt145670 			*i1 = icmp->icmp_timestamp;
15701673Sgt145670 			break;
15711676Sjpk 		case SO_MAC_EXEMPT:
15721676Sjpk 			*i1 = icmp->icmp_mac_exempt;
15731676Sjpk 			break;
1574*3388Skcpoon 		case SO_DOMAIN:
1575*3388Skcpoon 			*i1 = icmp->icmp_family;
1576*3388Skcpoon 			break;
1577*3388Skcpoon 
15780Sstevel@tonic-gate 		/*
15792263Ssommerfe 		 * Following four not meaningful for icmp
15800Sstevel@tonic-gate 		 * Action is same as "default" to which we fallthrough
15810Sstevel@tonic-gate 		 * so we keep them in comments.
15820Sstevel@tonic-gate 		 * case SO_LINGER:
15830Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
15840Sstevel@tonic-gate 		 * case SO_OOBINLINE:
15852263Ssommerfe 		 * case SO_ALLZONES:
15860Sstevel@tonic-gate 		 */
15870Sstevel@tonic-gate 		default:
15880Sstevel@tonic-gate 			return (-1);
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 		break;
15910Sstevel@tonic-gate 	case IPPROTO_IP:
15920Sstevel@tonic-gate 		/*
15930Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
15940Sstevel@tonic-gate 		 */
15950Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET)
15960Sstevel@tonic-gate 			return (-1);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 		switch (name) {
15990Sstevel@tonic-gate 		case IP_OPTIONS:
16000Sstevel@tonic-gate 		case T_IP_OPTIONS:
16010Sstevel@tonic-gate 			/* Options are passed up with each packet */
16020Sstevel@tonic-gate 			return (0);
16030Sstevel@tonic-gate 		case IP_HDRINCL:
16040Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_hdrincl;
16050Sstevel@tonic-gate 			break;
16060Sstevel@tonic-gate 		case IP_TOS:
16070Sstevel@tonic-gate 		case T_IP_TOS:
16080Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_type_of_service;
16090Sstevel@tonic-gate 			break;
16100Sstevel@tonic-gate 		case IP_TTL:
16110Sstevel@tonic-gate 			*i1 = (int)icmp->icmp_ttl;
16120Sstevel@tonic-gate 			break;
16130Sstevel@tonic-gate 		case IP_MULTICAST_IF:
16140Sstevel@tonic-gate 			/* 0 address if not set */
16150Sstevel@tonic-gate 			*(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr;
16160Sstevel@tonic-gate 			return (sizeof (ipaddr_t));
16170Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
16180Sstevel@tonic-gate 			*(uchar_t *)ptr = icmp->icmp_multicast_ttl;
16190Sstevel@tonic-gate 			return (sizeof (uchar_t));
16200Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
16210Sstevel@tonic-gate 			*ptr = icmp->icmp_multicast_loop;
16220Sstevel@tonic-gate 			return (sizeof (uint8_t));
16230Sstevel@tonic-gate 		case IP_BOUND_IF:
16240Sstevel@tonic-gate 			/* Zero if not set */
16250Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
16260Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16270Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
16280Sstevel@tonic-gate 			*ptr = icmp->icmp_unspec_source;
16290Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16300Sstevel@tonic-gate 		case IP_XMIT_IF:
16310Sstevel@tonic-gate 			*i1 = icmp->icmp_xmit_if;
16320Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16330Sstevel@tonic-gate 		case IP_RECVIF:
16340Sstevel@tonic-gate 			*ptr = icmp->icmp_recvif;
16350Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
16363318Srshoaib 		case IP_RECVPKTINFO:
16373318Srshoaib 			/*
16383318Srshoaib 			 * This also handles IP_PKTINFO.
16393318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
16403318Srshoaib 			 * Differentiation is based on the size of the argument
16413318Srshoaib 			 * passed in.
16423318Srshoaib 			 * This option is handled in IP which will return an
16433318Srshoaib 			 * error for IP_PKTINFO as it's not supported as a
16443318Srshoaib 			 * sticky option.
16453318Srshoaib 			 */
16463318Srshoaib 			return (-EINVAL);
16470Sstevel@tonic-gate 		/*
16480Sstevel@tonic-gate 		 * Cannot "get" the value of following options
16490Sstevel@tonic-gate 		 * at this level. Action is same as "default" to
16500Sstevel@tonic-gate 		 * which we fallthrough so we keep them in comments.
16510Sstevel@tonic-gate 		 *
16520Sstevel@tonic-gate 		 * case IP_ADD_MEMBERSHIP:
16530Sstevel@tonic-gate 		 * case IP_DROP_MEMBERSHIP:
16540Sstevel@tonic-gate 		 * case IP_BLOCK_SOURCE:
16550Sstevel@tonic-gate 		 * case IP_UNBLOCK_SOURCE:
16560Sstevel@tonic-gate 		 * case IP_ADD_SOURCE_MEMBERSHIP:
16570Sstevel@tonic-gate 		 * case IP_DROP_SOURCE_MEMBERSHIP:
16580Sstevel@tonic-gate 		 * case MCAST_JOIN_GROUP:
16590Sstevel@tonic-gate 		 * case MCAST_LEAVE_GROUP:
16600Sstevel@tonic-gate 		 * case MCAST_BLOCK_SOURCE:
16610Sstevel@tonic-gate 		 * case MCAST_UNBLOCK_SOURCE:
16620Sstevel@tonic-gate 		 * case MCAST_JOIN_SOURCE_GROUP:
16630Sstevel@tonic-gate 		 * case MCAST_LEAVE_SOURCE_GROUP:
16640Sstevel@tonic-gate 		 * case MRT_INIT:
16650Sstevel@tonic-gate 		 * case MRT_DONE:
16660Sstevel@tonic-gate 		 * case MRT_ADD_VIF:
16670Sstevel@tonic-gate 		 * case MRT_DEL_VIF:
16680Sstevel@tonic-gate 		 * case MRT_ADD_MFC:
16690Sstevel@tonic-gate 		 * case MRT_DEL_MFC:
16700Sstevel@tonic-gate 		 * case MRT_VERSION:
16710Sstevel@tonic-gate 		 * case MRT_ASSERT:
16720Sstevel@tonic-gate 		 * case IP_SEC_OPT:
16730Sstevel@tonic-gate 		 * case IP_DONTFAILOVER_IF:
16741663Spriyanka 		 * case IP_NEXTHOP:
16750Sstevel@tonic-gate 		 */
16760Sstevel@tonic-gate 		default:
16770Sstevel@tonic-gate 			return (-1);
16780Sstevel@tonic-gate 		}
16790Sstevel@tonic-gate 		break;
16800Sstevel@tonic-gate 	case IPPROTO_IPV6:
16810Sstevel@tonic-gate 		/*
16820Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
16830Sstevel@tonic-gate 		 */
16840Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
16850Sstevel@tonic-gate 			return (-1);
16860Sstevel@tonic-gate 		switch (name) {
16870Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
16880Sstevel@tonic-gate 			*i1 = (unsigned int)icmp->icmp_ttl;
16890Sstevel@tonic-gate 			break;
16900Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
16910Sstevel@tonic-gate 			/* 0 index if not set */
16920Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_if_index;
16930Sstevel@tonic-gate 			break;
16940Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
16950Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_ttl;
16960Sstevel@tonic-gate 			break;
16970Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
16980Sstevel@tonic-gate 			*i1 = icmp->icmp_multicast_loop;
16990Sstevel@tonic-gate 			break;
17000Sstevel@tonic-gate 		case IPV6_BOUND_IF:
17010Sstevel@tonic-gate 			/* Zero if not set */
17020Sstevel@tonic-gate 			*i1 = icmp->icmp_bound_if;
17030Sstevel@tonic-gate 			break;
17040Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
17050Sstevel@tonic-gate 			*i1 = icmp->icmp_unspec_source;
17060Sstevel@tonic-gate 			break;
17070Sstevel@tonic-gate 		case IPV6_CHECKSUM:
17080Sstevel@tonic-gate 			/*
17090Sstevel@tonic-gate 			 * Return offset or -1 if no checksum offset.
17100Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6
17110Sstevel@tonic-gate 			 */
17120Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6)
17130Sstevel@tonic-gate 				return (-1);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 			if (icmp->icmp_raw_checksum) {
17160Sstevel@tonic-gate 				*i1 = icmp->icmp_checksum_off;
17170Sstevel@tonic-gate 			} else {
17180Sstevel@tonic-gate 				*i1 = -1;
17190Sstevel@tonic-gate 			}
17200Sstevel@tonic-gate 			break;
17210Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
17220Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
17230Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
17240Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
17250Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
17260Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
17270Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
17280Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
17290Sstevel@tonic-gate 			/* cannot "get" the value for these */
17300Sstevel@tonic-gate 			return (-1);
17310Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
17323318Srshoaib 			*i1 = icmp->icmp_ip_recvpktinfo;
17330Sstevel@tonic-gate 			break;
17340Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
17350Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvtclass;
17360Sstevel@tonic-gate 			break;
17370Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
17380Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvpathmtu;
17390Sstevel@tonic-gate 			break;
17400Sstevel@tonic-gate 		case IPV6_V6ONLY:
17410Sstevel@tonic-gate 			*i1 = 1;
17420Sstevel@tonic-gate 			break;
17430Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
17440Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhoplimit;
17450Sstevel@tonic-gate 			break;
17460Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
17470Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvhopopts;
17480Sstevel@tonic-gate 			break;
17490Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
17500Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvdstopts;
17510Sstevel@tonic-gate 			break;
17520Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
17530Sstevel@tonic-gate 			*i1 = icmp->icmp_old_ipv6_recvdstopts;
17540Sstevel@tonic-gate 			break;
17550Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
17560Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrtdstopts;
17570Sstevel@tonic-gate 			break;
17580Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
17590Sstevel@tonic-gate 			*i1 = icmp->icmp_ipv6_recvrthdr;
17600Sstevel@tonic-gate 			break;
17610Sstevel@tonic-gate 		case IPV6_PKTINFO: {
17620Sstevel@tonic-gate 			/* XXX assumes that caller has room for max size! */
17630Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
17660Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
17670Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
17680Sstevel@tonic-gate 			else
17690Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
17700Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
17710Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
17720Sstevel@tonic-gate 			else
17730Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
17740Sstevel@tonic-gate 			return (sizeof (struct in6_pktinfo));
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
17770Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)ptr;
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
17800Sstevel@tonic-gate 				return (0);
17810Sstevel@tonic-gate 			*sin6 = sin6_null;
17820Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
17830Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
17840Sstevel@tonic-gate 			return (sizeof (sin6_t));
17850Sstevel@tonic-gate 		}
17860Sstevel@tonic-gate 		case IPV6_HOPOPTS:
17870Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
17880Sstevel@tonic-gate 				return (0);
17891676Sjpk 			if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6)
17901676Sjpk 				return (0);
17911676Sjpk 			bcopy((char *)ipp->ipp_hopopts +
17921676Sjpk 			    icmp->icmp_label_len_v6, ptr,
17931676Sjpk 			    ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
17941676Sjpk 			if (icmp->icmp_label_len_v6 > 0) {
17951676Sjpk 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
17961676Sjpk 				ptr[1] = (ipp->ipp_hopoptslen -
17971676Sjpk 				    icmp->icmp_label_len_v6 + 7) / 8 - 1;
17981676Sjpk 			}
17991676Sjpk 			return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6);
18000Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
18010Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
18020Sstevel@tonic-gate 				return (0);
18030Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
18040Sstevel@tonic-gate 			return (ipp->ipp_rtdstoptslen);
18050Sstevel@tonic-gate 		case IPV6_RTHDR:
18060Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
18070Sstevel@tonic-gate 				return (0);
18080Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
18090Sstevel@tonic-gate 			return (ipp->ipp_rthdrlen);
18100Sstevel@tonic-gate 		case IPV6_DSTOPTS:
18110Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
18120Sstevel@tonic-gate 				return (0);
18130Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
18140Sstevel@tonic-gate 			return (ipp->ipp_dstoptslen);
18150Sstevel@tonic-gate 		case IPV6_PATHMTU:
18160Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_PATHMTU))
18170Sstevel@tonic-gate 				return (0);
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 			return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0,
18200Sstevel@tonic-gate 				(struct ip6_mtuinfo *)ptr));
18210Sstevel@tonic-gate 		case IPV6_TCLASS:
18220Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_TCLASS)
18230Sstevel@tonic-gate 				*i1 = ipp->ipp_tclass;
18240Sstevel@tonic-gate 			else
18250Sstevel@tonic-gate 				*i1 = IPV6_FLOW_TCLASS(
18260Sstevel@tonic-gate 				    IPV6_DEFAULT_VERS_AND_FLOW);
18270Sstevel@tonic-gate 			break;
18280Sstevel@tonic-gate 		default:
18290Sstevel@tonic-gate 			return (-1);
18300Sstevel@tonic-gate 		}
18310Sstevel@tonic-gate 		break;
18320Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
18330Sstevel@tonic-gate 		/*
18340Sstevel@tonic-gate 		 * Only allow IPv6 option processing on native IPv6 sockets.
18350Sstevel@tonic-gate 		 */
18360Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6)
18370Sstevel@tonic-gate 			return (-1);
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6)
18400Sstevel@tonic-gate 			return (-1);
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 		switch (name) {
18430Sstevel@tonic-gate 		case ICMP6_FILTER:
18440Sstevel@tonic-gate 			if (icmp->icmp_filter == NULL) {
18450Sstevel@tonic-gate 				/* Make it look like "pass all" */
18460Sstevel@tonic-gate 				ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
18470Sstevel@tonic-gate 			} else {
18480Sstevel@tonic-gate 				(void) bcopy(icmp->icmp_filter, ptr,
18490Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
18500Sstevel@tonic-gate 			}
18510Sstevel@tonic-gate 			return (sizeof (icmp6_filter_t));
18520Sstevel@tonic-gate 		default:
18530Sstevel@tonic-gate 			return (-1);
18540Sstevel@tonic-gate 		}
18550Sstevel@tonic-gate 	default:
18560Sstevel@tonic-gate 		return (-1);
18570Sstevel@tonic-gate 	}
18580Sstevel@tonic-gate 	return (sizeof (int));
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate /* This routine sets socket options. */
18620Sstevel@tonic-gate /* ARGSUSED */
18630Sstevel@tonic-gate int
18640Sstevel@tonic-gate icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name,
18650Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
18660Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
18670Sstevel@tonic-gate {
18680Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
18690Sstevel@tonic-gate 	int	*i1 = (int *)invalp;
18700Sstevel@tonic-gate 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
18710Sstevel@tonic-gate 	boolean_t checkonly;
18720Sstevel@tonic-gate 	int	error;
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	switch (optset_context) {
18750Sstevel@tonic-gate 	case SETFN_OPTCOM_CHECKONLY:
18760Sstevel@tonic-gate 		checkonly = B_TRUE;
18770Sstevel@tonic-gate 		/*
18780Sstevel@tonic-gate 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
18790Sstevel@tonic-gate 		 * inlen != 0 implies value supplied and
18800Sstevel@tonic-gate 		 * 	we have to "pretend" to set it.
18810Sstevel@tonic-gate 		 * inlen == 0 implies that there is no
18820Sstevel@tonic-gate 		 * 	value part in T_CHECK request and just validation
18830Sstevel@tonic-gate 		 * done elsewhere should be enough, we just return here.
18840Sstevel@tonic-gate 		 */
18850Sstevel@tonic-gate 		if (inlen == 0) {
18860Sstevel@tonic-gate 			*outlenp = 0;
18870Sstevel@tonic-gate 			return (0);
18880Sstevel@tonic-gate 		}
18890Sstevel@tonic-gate 		break;
18900Sstevel@tonic-gate 	case SETFN_OPTCOM_NEGOTIATE:
18910Sstevel@tonic-gate 		checkonly = B_FALSE;
18920Sstevel@tonic-gate 		break;
18930Sstevel@tonic-gate 	case SETFN_UD_NEGOTIATE:
18940Sstevel@tonic-gate 	case SETFN_CONN_NEGOTIATE:
18950Sstevel@tonic-gate 		checkonly = B_FALSE;
18960Sstevel@tonic-gate 		/*
18970Sstevel@tonic-gate 		 * Negotiating local and "association-related" options
18980Sstevel@tonic-gate 		 * through T_UNITDATA_REQ.
18990Sstevel@tonic-gate 		 *
19000Sstevel@tonic-gate 		 * Following routine can filter out ones we do not
19010Sstevel@tonic-gate 		 * want to be "set" this way.
19020Sstevel@tonic-gate 		 */
19030Sstevel@tonic-gate 		if (!icmp_opt_allow_udr_set(level, name)) {
19040Sstevel@tonic-gate 			*outlenp = 0;
19050Sstevel@tonic-gate 			return (EINVAL);
19060Sstevel@tonic-gate 		}
19070Sstevel@tonic-gate 		break;
19080Sstevel@tonic-gate 	default:
19090Sstevel@tonic-gate 		/*
19100Sstevel@tonic-gate 		 * We should never get here
19110Sstevel@tonic-gate 		 */
19120Sstevel@tonic-gate 		*outlenp = 0;
19130Sstevel@tonic-gate 		return (EINVAL);
19140Sstevel@tonic-gate 	}
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
19170Sstevel@tonic-gate 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	/*
19200Sstevel@tonic-gate 	 * For fixed length options, no sanity check
19210Sstevel@tonic-gate 	 * of passed in length is done. It is assumed *_optcom_req()
19220Sstevel@tonic-gate 	 * routines do the right thing.
19230Sstevel@tonic-gate 	 */
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	switch (level) {
19260Sstevel@tonic-gate 	case SOL_SOCKET:
19270Sstevel@tonic-gate 		switch (name) {
19280Sstevel@tonic-gate 		case SO_DEBUG:
19290Sstevel@tonic-gate 			if (!checkonly)
19300Sstevel@tonic-gate 				icmp->icmp_debug = onoff;
19310Sstevel@tonic-gate 			break;
19320Sstevel@tonic-gate 		case SO_PROTOTYPE:
19330Sstevel@tonic-gate 			if ((*i1 & 0xFF) != IPPROTO_ICMP &&
19340Sstevel@tonic-gate 			    (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
19350Sstevel@tonic-gate 			    secpolicy_net_rawaccess(cr) != 0) {
19360Sstevel@tonic-gate 				*outlenp = 0;
19370Sstevel@tonic-gate 				return (EACCES);
19380Sstevel@tonic-gate 			}
19390Sstevel@tonic-gate 			/* Can't use IPPROTO_RAW with IPv6 */
19400Sstevel@tonic-gate 			if ((*i1 & 0xFF) == IPPROTO_RAW &&
19410Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET6) {
19420Sstevel@tonic-gate 				*outlenp = 0;
19430Sstevel@tonic-gate 				return (EPROTONOSUPPORT);
19440Sstevel@tonic-gate 			}
19450Sstevel@tonic-gate 			if (checkonly) {
19460Sstevel@tonic-gate 				/* T_CHECK case */
19470Sstevel@tonic-gate 				*(int *)outvalp = (*i1 & 0xFF);
19480Sstevel@tonic-gate 				break;
19490Sstevel@tonic-gate 			}
19500Sstevel@tonic-gate 			icmp->icmp_proto = *i1 & 0xFF;
19510Sstevel@tonic-gate 			if ((icmp->icmp_proto == IPPROTO_RAW ||
19520Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_IGMP) &&
19530Sstevel@tonic-gate 			    icmp->icmp_family == AF_INET)
19540Sstevel@tonic-gate 				icmp->icmp_hdrincl = 1;
19550Sstevel@tonic-gate 			else
19560Sstevel@tonic-gate 				icmp->icmp_hdrincl = 0;
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6 &&
19590Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_ICMPV6) {
19600Sstevel@tonic-gate 				/* Set offset for icmp6_cksum */
19610Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
19620Sstevel@tonic-gate 				icmp->icmp_checksum_off = 2;
19630Sstevel@tonic-gate 			}
19640Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_UDP ||
19650Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_TCP ||
19660Sstevel@tonic-gate 			    icmp->icmp_proto == IPPROTO_SCTP) {
19670Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 1;
19680Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields |=
19690Sstevel@tonic-gate 				    IPPF_NO_CKSUM;
19700Sstevel@tonic-gate 			} else {
19710Sstevel@tonic-gate 				icmp->icmp_no_tp_cksum = 0;
19720Sstevel@tonic-gate 				icmp->icmp_sticky_ipp.ipp_fields &=
19730Sstevel@tonic-gate 				    ~IPPF_NO_CKSUM;
19740Sstevel@tonic-gate 			}
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 			if (icmp->icmp_filter != NULL &&
19770Sstevel@tonic-gate 			    icmp->icmp_proto != IPPROTO_ICMPV6) {
19780Sstevel@tonic-gate 				kmem_free(icmp->icmp_filter,
19790Sstevel@tonic-gate 				    sizeof (icmp6_filter_t));
19800Sstevel@tonic-gate 				icmp->icmp_filter = NULL;
19810Sstevel@tonic-gate 			}
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 			/* Rebuild the header template */
19840Sstevel@tonic-gate 			error = icmp_build_hdrs(q, icmp);
19850Sstevel@tonic-gate 			if (error != 0) {
19860Sstevel@tonic-gate 				*outlenp = 0;
19870Sstevel@tonic-gate 				return (error);
19880Sstevel@tonic-gate 			}
19890Sstevel@tonic-gate 
1990409Skcpoon 			/*
1991409Skcpoon 			 * For SCTP, we don't use icmp_bind_proto() for
1992409Skcpoon 			 * raw socket binding.  Note that we do not need
1993409Skcpoon 			 * to set *outlenp.
1994409Skcpoon 			 */
1995409Skcpoon 			if (icmp->icmp_proto == IPPROTO_SCTP)
1996409Skcpoon 				return (0);
1997409Skcpoon 
19980Sstevel@tonic-gate 			icmp_bind_proto(q);
19990Sstevel@tonic-gate 			*outlenp = sizeof (int);
20000Sstevel@tonic-gate 			*(int *)outvalp = *i1 & 0xFF;
20010Sstevel@tonic-gate 			return (0);
20020Sstevel@tonic-gate 		case SO_REUSEADDR:
20030Sstevel@tonic-gate 			if (!checkonly)
20040Sstevel@tonic-gate 				icmp->icmp_reuseaddr = onoff;
20050Sstevel@tonic-gate 			break;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 		/*
20080Sstevel@tonic-gate 		 * The following three items are available here,
20090Sstevel@tonic-gate 		 * but are only meaningful to IP.
20100Sstevel@tonic-gate 		 */
20110Sstevel@tonic-gate 		case SO_DONTROUTE:
20120Sstevel@tonic-gate 			if (!checkonly)
20130Sstevel@tonic-gate 				icmp->icmp_dontroute = onoff;
20140Sstevel@tonic-gate 			break;
20150Sstevel@tonic-gate 		case SO_USELOOPBACK:
20160Sstevel@tonic-gate 			if (!checkonly)
20170Sstevel@tonic-gate 				icmp->icmp_useloopback = onoff;
20180Sstevel@tonic-gate 			break;
20190Sstevel@tonic-gate 		case SO_BROADCAST:
20200Sstevel@tonic-gate 			if (!checkonly)
20210Sstevel@tonic-gate 				icmp->icmp_broadcast = onoff;
20220Sstevel@tonic-gate 			break;
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 		case SO_SNDBUF:
20250Sstevel@tonic-gate 			if (*i1 > icmp_max_buf) {
20260Sstevel@tonic-gate 				*outlenp = 0;
20270Sstevel@tonic-gate 				return (ENOBUFS);
20280Sstevel@tonic-gate 			}
20290Sstevel@tonic-gate 			if (!checkonly) {
20300Sstevel@tonic-gate 				q->q_hiwat = *i1;
20310Sstevel@tonic-gate 				q->q_next->q_hiwat = *i1;
20320Sstevel@tonic-gate 			}
20330Sstevel@tonic-gate 			break;
20340Sstevel@tonic-gate 		case SO_RCVBUF:
20350Sstevel@tonic-gate 			if (*i1 > icmp_max_buf) {
20360Sstevel@tonic-gate 				*outlenp = 0;
20370Sstevel@tonic-gate 				return (ENOBUFS);
20380Sstevel@tonic-gate 			}
20390Sstevel@tonic-gate 			if (!checkonly) {
20400Sstevel@tonic-gate 				RD(q)->q_hiwat = *i1;
20410Sstevel@tonic-gate 				(void) mi_set_sth_hiwat(RD(q), *i1);
20420Sstevel@tonic-gate 			}
20430Sstevel@tonic-gate 			break;
20440Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
20450Sstevel@tonic-gate 			if (!checkonly)
20460Sstevel@tonic-gate 				icmp->icmp_dgram_errind = onoff;
20470Sstevel@tonic-gate 			break;
20482263Ssommerfe 		case SO_ALLZONES:
20492263Ssommerfe 			/*
20502263Ssommerfe 			 * "soft" error (negative)
20512263Ssommerfe 			 * option not handled at this level
20522263Ssommerfe 			 * Note: Do not modify *outlenp
20532263Ssommerfe 			 */
20542263Ssommerfe 			return (-EINVAL);
20551673Sgt145670 		case SO_TIMESTAMP:
20561673Sgt145670 			if (!checkonly) {
20571673Sgt145670 				icmp->icmp_timestamp = onoff;
20581673Sgt145670 			}
20591673Sgt145670 			break;
20601676Sjpk 		case SO_MAC_EXEMPT:
20611676Sjpk 			if (secpolicy_net_mac_aware(cr) != 0 ||
20621676Sjpk 			    icmp->icmp_state != TS_UNBND)
20631676Sjpk 				return (EACCES);
20641676Sjpk 			if (!checkonly)
20651676Sjpk 				icmp->icmp_mac_exempt = onoff;
20661676Sjpk 			break;
20670Sstevel@tonic-gate 		/*
20680Sstevel@tonic-gate 		 * Following three not meaningful for icmp
20690Sstevel@tonic-gate 		 * Action is same as "default" so we keep them
20700Sstevel@tonic-gate 		 * in comments.
20710Sstevel@tonic-gate 		 * case SO_LINGER:
20720Sstevel@tonic-gate 		 * case SO_KEEPALIVE:
20730Sstevel@tonic-gate 		 * case SO_OOBINLINE:
20740Sstevel@tonic-gate 		 */
20750Sstevel@tonic-gate 		default:
20760Sstevel@tonic-gate 			*outlenp = 0;
20770Sstevel@tonic-gate 			return (EINVAL);
20780Sstevel@tonic-gate 		}
20790Sstevel@tonic-gate 		break;
20800Sstevel@tonic-gate 	case IPPROTO_IP:
20810Sstevel@tonic-gate 		/*
20820Sstevel@tonic-gate 		 * Only allow IPv4 option processing on IPv4 sockets.
20830Sstevel@tonic-gate 		 */
20840Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET) {
20850Sstevel@tonic-gate 			*outlenp = 0;
20860Sstevel@tonic-gate 			return (ENOPROTOOPT);
20870Sstevel@tonic-gate 		}
20880Sstevel@tonic-gate 		switch (name) {
20890Sstevel@tonic-gate 		case IP_OPTIONS:
20900Sstevel@tonic-gate 		case T_IP_OPTIONS:
20910Sstevel@tonic-gate 			/* Save options for use by IP. */
20921676Sjpk 			if ((inlen & 0x3) ||
20931676Sjpk 			    inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) {
20940Sstevel@tonic-gate 				*outlenp = 0;
20950Sstevel@tonic-gate 				return (EINVAL);
20960Sstevel@tonic-gate 			}
20970Sstevel@tonic-gate 			if (checkonly)
20980Sstevel@tonic-gate 				break;
20990Sstevel@tonic-gate 
21001676Sjpk 			if (!tsol_option_set(&icmp->icmp_ip_snd_options,
21011676Sjpk 			    &icmp->icmp_ip_snd_options_len,
21021676Sjpk 			    icmp->icmp_label_len, invalp, inlen)) {
21031676Sjpk 				*outlenp = 0;
21041676Sjpk 				return (ENOMEM);
21050Sstevel@tonic-gate 			}
21061676Sjpk 
21070Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
21080Sstevel@tonic-gate 			    icmp->icmp_ip_snd_options_len;
21090Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
21100Sstevel@tonic-gate 						icmp_wroff_extra);
21110Sstevel@tonic-gate 			break;
21120Sstevel@tonic-gate 		case IP_HDRINCL:
21130Sstevel@tonic-gate 			if (!checkonly)
21140Sstevel@tonic-gate 				icmp->icmp_hdrincl = onoff;
21150Sstevel@tonic-gate 			break;
21160Sstevel@tonic-gate 		case IP_TOS:
21170Sstevel@tonic-gate 		case T_IP_TOS:
21180Sstevel@tonic-gate 			if (!checkonly) {
21190Sstevel@tonic-gate 				icmp->icmp_type_of_service = (uint8_t)*i1;
21200Sstevel@tonic-gate 			}
21210Sstevel@tonic-gate 			break;
21220Sstevel@tonic-gate 		case IP_TTL:
21230Sstevel@tonic-gate 			if (!checkonly) {
21240Sstevel@tonic-gate 				icmp->icmp_ttl = (uint8_t)*i1;
21250Sstevel@tonic-gate 			}
21260Sstevel@tonic-gate 			break;
21270Sstevel@tonic-gate 		case IP_MULTICAST_IF:
21280Sstevel@tonic-gate 			/*
21290Sstevel@tonic-gate 			 * TODO should check OPTMGMT reply and undo this if
21300Sstevel@tonic-gate 			 * there is an error.
21310Sstevel@tonic-gate 			 */
21320Sstevel@tonic-gate 			if (!checkonly)
21330Sstevel@tonic-gate 				icmp->icmp_multicast_if_addr = *i1;
21340Sstevel@tonic-gate 			break;
21350Sstevel@tonic-gate 		case IP_MULTICAST_TTL:
21360Sstevel@tonic-gate 			if (!checkonly)
21370Sstevel@tonic-gate 				icmp->icmp_multicast_ttl = *invalp;
21380Sstevel@tonic-gate 			break;
21390Sstevel@tonic-gate 		case IP_MULTICAST_LOOP:
21400Sstevel@tonic-gate 			if (!checkonly) {
21410Sstevel@tonic-gate 				icmp->icmp_multicast_loop =
21420Sstevel@tonic-gate 				    (*invalp == 0) ? 0 : 1;
21430Sstevel@tonic-gate 			}
21440Sstevel@tonic-gate 			break;
21450Sstevel@tonic-gate 		case IP_BOUND_IF:
21460Sstevel@tonic-gate 			if (!checkonly)
21470Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
21480Sstevel@tonic-gate 			break;
21490Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
21500Sstevel@tonic-gate 			if (!checkonly)
21510Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
21520Sstevel@tonic-gate 			break;
21530Sstevel@tonic-gate 		case IP_XMIT_IF:
21540Sstevel@tonic-gate 			if (!checkonly)
21550Sstevel@tonic-gate 				icmp->icmp_xmit_if = *i1;
21560Sstevel@tonic-gate 			break;
21570Sstevel@tonic-gate 		case IP_RECVIF:
21580Sstevel@tonic-gate 			if (!checkonly)
21590Sstevel@tonic-gate 				icmp->icmp_recvif = onoff;
21600Sstevel@tonic-gate 			break;
21613318Srshoaib 
21623318Srshoaib 		case IP_PKTINFO: {
21633318Srshoaib 			/*
21643318Srshoaib 			 * This also handles IP_RECVPKTINFO.
21653318Srshoaib 			 * IP_PKTINFO and IP_RECVPKTINFO have the same value.
21663318Srshoaib 			 * Differentiation is based on the size of the argument
21673318Srshoaib 			 * passed in.
21683318Srshoaib 			 */
21693318Srshoaib 			struct in_pktinfo *pktinfop;
21703318Srshoaib 			ip4_pkt_t *attr_pktinfop;
21713318Srshoaib 
21723318Srshoaib 			if (checkonly)
21733318Srshoaib 				break;
21743318Srshoaib 
21753318Srshoaib 			if (inlen == sizeof (int)) {
21763318Srshoaib 				/*
21773318Srshoaib 				 * This is IP_RECVPKTINFO option.
21783318Srshoaib 				 * Keep a local copy of wether this option is
21793318Srshoaib 				 * set or not and pass it down to IP for
21803318Srshoaib 				 * processing.
21813318Srshoaib 				 */
21823318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
21833318Srshoaib 				return (-EINVAL);
21843318Srshoaib 			}
21853318Srshoaib 
21863318Srshoaib 
21873318Srshoaib 			if (inlen != sizeof (struct in_pktinfo))
21883318Srshoaib 				return (EINVAL);
21893318Srshoaib 
21903318Srshoaib 			if ((attr_pktinfop = (ip4_pkt_t *)thisdg_attrs)
21913318Srshoaib 			    == NULL) {
21923318Srshoaib 				/*
21933318Srshoaib 				 * sticky option is not supported
21943318Srshoaib 				 */
21953318Srshoaib 				return (EINVAL);
21963318Srshoaib 			}
21973318Srshoaib 
21983318Srshoaib 			pktinfop = (struct in_pktinfo *)invalp;
21993318Srshoaib 
22003318Srshoaib 			/*
22013318Srshoaib 			 * Atleast one of the values should be specified
22023318Srshoaib 			 */
22033318Srshoaib 			if (pktinfop->ipi_ifindex == 0 &&
22043318Srshoaib 			    pktinfop->ipi_spec_dst.s_addr == INADDR_ANY) {
22053318Srshoaib 				return (EINVAL);
22063318Srshoaib 			}
22073318Srshoaib 
22083318Srshoaib 			attr_pktinfop->ip4_addr = pktinfop->ipi_spec_dst.s_addr;
22093318Srshoaib 			attr_pktinfop->ip4_ill_index = pktinfop->ipi_ifindex;
22103318Srshoaib 		}
22113318Srshoaib 			break;
22120Sstevel@tonic-gate 		case IP_ADD_MEMBERSHIP:
22130Sstevel@tonic-gate 		case IP_DROP_MEMBERSHIP:
22140Sstevel@tonic-gate 		case IP_BLOCK_SOURCE:
22150Sstevel@tonic-gate 		case IP_UNBLOCK_SOURCE:
22160Sstevel@tonic-gate 		case IP_ADD_SOURCE_MEMBERSHIP:
22170Sstevel@tonic-gate 		case IP_DROP_SOURCE_MEMBERSHIP:
22180Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
22190Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
22200Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
22210Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
22220Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
22230Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
22240Sstevel@tonic-gate 		case MRT_INIT:
22250Sstevel@tonic-gate 		case MRT_DONE:
22260Sstevel@tonic-gate 		case MRT_ADD_VIF:
22270Sstevel@tonic-gate 		case MRT_DEL_VIF:
22280Sstevel@tonic-gate 		case MRT_ADD_MFC:
22290Sstevel@tonic-gate 		case MRT_DEL_MFC:
22300Sstevel@tonic-gate 		case MRT_VERSION:
22310Sstevel@tonic-gate 		case MRT_ASSERT:
22320Sstevel@tonic-gate 		case IP_SEC_OPT:
22330Sstevel@tonic-gate 		case IP_DONTFAILOVER_IF:
22341663Spriyanka 		case IP_NEXTHOP:
22350Sstevel@tonic-gate 			/*
22360Sstevel@tonic-gate 			 * "soft" error (negative)
22370Sstevel@tonic-gate 			 * option not handled at this level
22380Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
22390Sstevel@tonic-gate 			 */
22400Sstevel@tonic-gate 			return (-EINVAL);
22410Sstevel@tonic-gate 		default:
22420Sstevel@tonic-gate 			*outlenp = 0;
22430Sstevel@tonic-gate 			return (EINVAL);
22440Sstevel@tonic-gate 		}
22450Sstevel@tonic-gate 		break;
22460Sstevel@tonic-gate 	case IPPROTO_IPV6: {
22470Sstevel@tonic-gate 		ip6_pkt_t		*ipp;
22480Sstevel@tonic-gate 		boolean_t		sticky;
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
22510Sstevel@tonic-gate 			*outlenp = 0;
22520Sstevel@tonic-gate 			return (ENOPROTOOPT);
22530Sstevel@tonic-gate 		}
22540Sstevel@tonic-gate 		/*
22550Sstevel@tonic-gate 		 * Deal with both sticky options and ancillary data
22560Sstevel@tonic-gate 		 */
22570Sstevel@tonic-gate 		if (thisdg_attrs == NULL) {
22580Sstevel@tonic-gate 			/* sticky options, or none */
22590Sstevel@tonic-gate 			ipp = &icmp->icmp_sticky_ipp;
22600Sstevel@tonic-gate 			sticky = B_TRUE;
22610Sstevel@tonic-gate 		} else {
22620Sstevel@tonic-gate 			/* ancillary data */
22630Sstevel@tonic-gate 			ipp = (ip6_pkt_t *)thisdg_attrs;
22640Sstevel@tonic-gate 			sticky = B_FALSE;
22650Sstevel@tonic-gate 		}
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 		switch (name) {
22680Sstevel@tonic-gate 		case IPV6_MULTICAST_IF:
22690Sstevel@tonic-gate 			if (!checkonly)
22700Sstevel@tonic-gate 				icmp->icmp_multicast_if_index = *i1;
22710Sstevel@tonic-gate 			break;
22720Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
22730Sstevel@tonic-gate 			/* -1 means use default */
22740Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
22750Sstevel@tonic-gate 				*outlenp = 0;
22760Sstevel@tonic-gate 				return (EINVAL);
22770Sstevel@tonic-gate 			}
22780Sstevel@tonic-gate 			if (!checkonly) {
22790Sstevel@tonic-gate 				if (*i1 == -1) {
2280679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
22810Sstevel@tonic-gate 					    icmp_ipv6_hoplimit;
2282679Sseb 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
22830Sstevel@tonic-gate 					/* Pass modified value to IP. */
22840Sstevel@tonic-gate 					*i1 = ipp->ipp_hoplimit;
22850Sstevel@tonic-gate 				} else {
2286679Sseb 					icmp->icmp_ttl = ipp->ipp_unicast_hops =
22870Sstevel@tonic-gate 					    (uint8_t)*i1;
2288679Sseb 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
22890Sstevel@tonic-gate 				}
22900Sstevel@tonic-gate 				/* Rebuild the header template */
22910Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
22920Sstevel@tonic-gate 				if (error != 0) {
22930Sstevel@tonic-gate 					*outlenp = 0;
22940Sstevel@tonic-gate 					return (error);
22950Sstevel@tonic-gate 				}
22960Sstevel@tonic-gate 			}
22970Sstevel@tonic-gate 			break;
22980Sstevel@tonic-gate 		case IPV6_MULTICAST_HOPS:
22990Sstevel@tonic-gate 			/* -1 means use default */
23000Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
23010Sstevel@tonic-gate 				*outlenp = 0;
23020Sstevel@tonic-gate 				return (EINVAL);
23030Sstevel@tonic-gate 			}
23040Sstevel@tonic-gate 			if (!checkonly) {
23050Sstevel@tonic-gate 				if (*i1 == -1) {
23060Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2307679Sseb 					    ipp->ipp_multicast_hops =
23080Sstevel@tonic-gate 					    IP_DEFAULT_MULTICAST_TTL;
2309679Sseb 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
23100Sstevel@tonic-gate 					/* Pass modified value to IP. */
2311679Sseb 					*i1 = icmp->icmp_multicast_ttl;
23120Sstevel@tonic-gate 				} else {
23130Sstevel@tonic-gate 					icmp->icmp_multicast_ttl =
2314679Sseb 					    ipp->ipp_multicast_hops =
23150Sstevel@tonic-gate 					    (uint8_t)*i1;
2316679Sseb 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
23170Sstevel@tonic-gate 				}
23180Sstevel@tonic-gate 			}
23190Sstevel@tonic-gate 			break;
23200Sstevel@tonic-gate 		case IPV6_MULTICAST_LOOP:
23210Sstevel@tonic-gate 			if (*i1 != 0 && *i1 != 1) {
23220Sstevel@tonic-gate 				*outlenp = 0;
23230Sstevel@tonic-gate 				return (EINVAL);
23240Sstevel@tonic-gate 			}
23250Sstevel@tonic-gate 			if (!checkonly)
23260Sstevel@tonic-gate 				icmp->icmp_multicast_loop = *i1;
23270Sstevel@tonic-gate 			break;
23280Sstevel@tonic-gate 		case IPV6_CHECKSUM:
23290Sstevel@tonic-gate 			/*
23300Sstevel@tonic-gate 			 * Integer offset into the user data of where the
23310Sstevel@tonic-gate 			 * checksum is located.
23320Sstevel@tonic-gate 			 * Offset of -1 disables option.
23330Sstevel@tonic-gate 			 * Does not apply to IPPROTO_ICMPV6.
23340Sstevel@tonic-gate 			 */
23350Sstevel@tonic-gate 			if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) {
23360Sstevel@tonic-gate 				*outlenp = 0;
23370Sstevel@tonic-gate 				return (EINVAL);
23380Sstevel@tonic-gate 			}
23390Sstevel@tonic-gate 			if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
23400Sstevel@tonic-gate 				/* Negative or not 16 bit aligned offset */
23410Sstevel@tonic-gate 				*outlenp = 0;
23420Sstevel@tonic-gate 				return (EINVAL);
23430Sstevel@tonic-gate 			}
23440Sstevel@tonic-gate 			if (checkonly)
23450Sstevel@tonic-gate 				break;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 			if (*i1 == -1) {
23480Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 0;
23490Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RAW_CKSUM;
23500Sstevel@tonic-gate 			} else {
23510Sstevel@tonic-gate 				icmp->icmp_raw_checksum = 1;
23520Sstevel@tonic-gate 				icmp->icmp_checksum_off = *i1;
23530Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RAW_CKSUM;
23540Sstevel@tonic-gate 			}
23550Sstevel@tonic-gate 			/* Rebuild the header template */
23560Sstevel@tonic-gate 			error = icmp_build_hdrs(q, icmp);
23570Sstevel@tonic-gate 			if (error != 0) {
23580Sstevel@tonic-gate 				*outlenp = 0;
23590Sstevel@tonic-gate 				return (error);
23600Sstevel@tonic-gate 			}
23610Sstevel@tonic-gate 			break;
23620Sstevel@tonic-gate 		case IPV6_JOIN_GROUP:
23630Sstevel@tonic-gate 		case IPV6_LEAVE_GROUP:
23640Sstevel@tonic-gate 		case MCAST_JOIN_GROUP:
23650Sstevel@tonic-gate 		case MCAST_LEAVE_GROUP:
23660Sstevel@tonic-gate 		case MCAST_BLOCK_SOURCE:
23670Sstevel@tonic-gate 		case MCAST_UNBLOCK_SOURCE:
23680Sstevel@tonic-gate 		case MCAST_JOIN_SOURCE_GROUP:
23690Sstevel@tonic-gate 		case MCAST_LEAVE_SOURCE_GROUP:
23700Sstevel@tonic-gate 			/*
23710Sstevel@tonic-gate 			 * "soft" error (negative)
23720Sstevel@tonic-gate 			 * option not handled at this level
23730Sstevel@tonic-gate 			 * Note: Do not modify *outlenp
23740Sstevel@tonic-gate 			 */
23750Sstevel@tonic-gate 			return (-EINVAL);
23760Sstevel@tonic-gate 		case IPV6_BOUND_IF:
23770Sstevel@tonic-gate 			if (!checkonly)
23780Sstevel@tonic-gate 				icmp->icmp_bound_if = *i1;
23790Sstevel@tonic-gate 			break;
23800Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
23810Sstevel@tonic-gate 			if (!checkonly)
23820Sstevel@tonic-gate 				icmp->icmp_unspec_source = onoff;
23830Sstevel@tonic-gate 			break;
23840Sstevel@tonic-gate 		case IPV6_RECVTCLASS:
23850Sstevel@tonic-gate 			if (!checkonly)
23860Sstevel@tonic-gate 				icmp->icmp_ipv6_recvtclass = onoff;
23870Sstevel@tonic-gate 			break;
23880Sstevel@tonic-gate 		/*
23890Sstevel@tonic-gate 		 * Set boolean switches for ancillary data delivery
23900Sstevel@tonic-gate 		 */
23910Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
23920Sstevel@tonic-gate 			if (!checkonly)
23933318Srshoaib 				icmp->icmp_ip_recvpktinfo = onoff;
23940Sstevel@tonic-gate 			break;
23950Sstevel@tonic-gate 		case IPV6_RECVPATHMTU:
23960Sstevel@tonic-gate 			if (!checkonly)
23970Sstevel@tonic-gate 				icmp->icmp_ipv6_recvpathmtu = onoff;
23980Sstevel@tonic-gate 			break;
23990Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
24000Sstevel@tonic-gate 			if (!checkonly)
24010Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhoplimit = onoff;
24020Sstevel@tonic-gate 			break;
24030Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
24040Sstevel@tonic-gate 			if (!checkonly)
24050Sstevel@tonic-gate 				icmp->icmp_ipv6_recvhopopts = onoff;
24060Sstevel@tonic-gate 			break;
24070Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
24080Sstevel@tonic-gate 			if (!checkonly)
24090Sstevel@tonic-gate 				icmp->icmp_ipv6_recvdstopts = onoff;
24100Sstevel@tonic-gate 			break;
24110Sstevel@tonic-gate 		case _OLD_IPV6_RECVDSTOPTS:
24120Sstevel@tonic-gate 			if (!checkonly)
24130Sstevel@tonic-gate 				icmp->icmp_old_ipv6_recvdstopts = onoff;
24140Sstevel@tonic-gate 			break;
24150Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
24160Sstevel@tonic-gate 			if (!checkonly)
24170Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrtdstopts = onoff;
24180Sstevel@tonic-gate 			break;
24190Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
24200Sstevel@tonic-gate 			if (!checkonly)
24210Sstevel@tonic-gate 				icmp->icmp_ipv6_recvrthdr = onoff;
24220Sstevel@tonic-gate 			break;
24230Sstevel@tonic-gate 		/*
24240Sstevel@tonic-gate 		 * Set sticky options or ancillary data.
24250Sstevel@tonic-gate 		 * If sticky options, (re)build any extension headers
24260Sstevel@tonic-gate 		 * that might be needed as a result.
24270Sstevel@tonic-gate 		 */
24280Sstevel@tonic-gate 		case IPV6_PKTINFO:
24290Sstevel@tonic-gate 			/*
24300Sstevel@tonic-gate 			 * The source address and ifindex are verified
24310Sstevel@tonic-gate 			 * in ip_opt_set(). For ancillary data the
24320Sstevel@tonic-gate 			 * source address is checked in ip_wput_v6.
24330Sstevel@tonic-gate 			 */
24340Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
24350Sstevel@tonic-gate 				return (EINVAL);
24360Sstevel@tonic-gate 			if (checkonly)
24370Sstevel@tonic-gate 				break;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 			if (inlen == 0) {
24400Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
24410Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |=
24420Sstevel@tonic-gate 				    (IPPF_IFINDEX|IPPF_ADDR);
24430Sstevel@tonic-gate 			} else {
24440Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
24470Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
24480Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
24490Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
24500Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
24510Sstevel@tonic-gate 				else
24520Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
24530Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
24540Sstevel@tonic-gate 				    &ipp->ipp_addr))
24550Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
24560Sstevel@tonic-gate 				else
24570Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
24580Sstevel@tonic-gate 			}
24590Sstevel@tonic-gate 			if (sticky) {
24600Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
24610Sstevel@tonic-gate 				if (error != 0)
24620Sstevel@tonic-gate 					return (error);
24630Sstevel@tonic-gate 			}
24640Sstevel@tonic-gate 			break;
24650Sstevel@tonic-gate 		case IPV6_HOPLIMIT:
2466679Sseb 			/* This option can only be used as ancillary data. */
2467679Sseb 			if (sticky)
2468679Sseb 				return (EINVAL);
24690Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
24700Sstevel@tonic-gate 				return (EINVAL);
24710Sstevel@tonic-gate 			if (checkonly)
24720Sstevel@tonic-gate 				break;
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 			if (inlen == 0) {
24750Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
24760Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
24770Sstevel@tonic-gate 			} else {
24780Sstevel@tonic-gate 				if (*i1 > 255 || *i1 < -1)
24790Sstevel@tonic-gate 					return (EINVAL);
24800Sstevel@tonic-gate 				if (*i1 == -1)
24810Sstevel@tonic-gate 					ipp->ipp_hoplimit = icmp_ipv6_hoplimit;
24820Sstevel@tonic-gate 				else
24830Sstevel@tonic-gate 					ipp->ipp_hoplimit = *i1;
24840Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPLIMIT;
24850Sstevel@tonic-gate 			}
24860Sstevel@tonic-gate 			break;
24870Sstevel@tonic-gate 		case IPV6_TCLASS:
24880Sstevel@tonic-gate 			/*
24890Sstevel@tonic-gate 			 * IPV6_RECVTCLASS accepts -1 as use kernel default
24900Sstevel@tonic-gate 			 * and [0, 255] as the actualy traffic class.
24910Sstevel@tonic-gate 			 */
24920Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (int))
24930Sstevel@tonic-gate 				return (EINVAL);
24940Sstevel@tonic-gate 			if (checkonly)
24950Sstevel@tonic-gate 				break;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 			if (inlen == 0) {
24980Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_TCLASS;
24990Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
25000Sstevel@tonic-gate 			} else {
25010Sstevel@tonic-gate 				if (*i1 >= 256 || *i1 < -1)
25020Sstevel@tonic-gate 					return (EINVAL);
25030Sstevel@tonic-gate 				if (*i1 == -1) {
25040Sstevel@tonic-gate 					ipp->ipp_tclass =
25050Sstevel@tonic-gate 					    IPV6_FLOW_TCLASS(
25060Sstevel@tonic-gate 					    IPV6_DEFAULT_VERS_AND_FLOW);
25070Sstevel@tonic-gate 				} else {
25080Sstevel@tonic-gate 					ipp->ipp_tclass = *i1;
25090Sstevel@tonic-gate 				}
25100Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_TCLASS;
25110Sstevel@tonic-gate 			}
25120Sstevel@tonic-gate 			if (sticky) {
25130Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25140Sstevel@tonic-gate 				if (error != 0)
25150Sstevel@tonic-gate 					return (error);
25160Sstevel@tonic-gate 			}
25170Sstevel@tonic-gate 			break;
25180Sstevel@tonic-gate 		case IPV6_NEXTHOP:
25190Sstevel@tonic-gate 			/*
25200Sstevel@tonic-gate 			 * IP will verify that the nexthop is reachable
25210Sstevel@tonic-gate 			 * and fail for sticky options.
25220Sstevel@tonic-gate 			 */
25230Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t))
25240Sstevel@tonic-gate 				return (EINVAL);
25250Sstevel@tonic-gate 			if (checkonly)
25260Sstevel@tonic-gate 				break;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 			if (inlen == 0) {
25290Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
25300Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
25310Sstevel@tonic-gate 			} else {
25320Sstevel@tonic-gate 				sin6_t *sin6 = (sin6_t *)invalp;
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6)
25350Sstevel@tonic-gate 					return (EAFNOSUPPORT);
25360Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
25370Sstevel@tonic-gate 					return (EADDRNOTAVAIL);
25380Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
25390Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(
25400Sstevel@tonic-gate 				    &ipp->ipp_nexthop))
25410Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
25420Sstevel@tonic-gate 				else
25430Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
25440Sstevel@tonic-gate 			}
25450Sstevel@tonic-gate 			if (sticky) {
25460Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25470Sstevel@tonic-gate 				if (error != 0)
25480Sstevel@tonic-gate 					return (error);
25490Sstevel@tonic-gate 			}
25500Sstevel@tonic-gate 			break;
25510Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
25520Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
25530Sstevel@tonic-gate 			/*
25540Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25550Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25560Sstevel@tonic-gate 			 */
25570Sstevel@tonic-gate 			if (inlen != 0 &&
25580Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1)))
25590Sstevel@tonic-gate 				return (EINVAL);
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 			if (checkonly)
25620Sstevel@tonic-gate 				break;
25631676Sjpk 			error = optcom_pkt_set(invalp, inlen, sticky,
25641676Sjpk 			    (uchar_t **)&ipp->ipp_hopopts,
25651676Sjpk 			    &ipp->ipp_hopoptslen,
25661676Sjpk 			    sticky ? icmp->icmp_label_len_v6 : 0);
25671676Sjpk 			if (error != 0)
25681676Sjpk 				return (error);
25691676Sjpk 			if (ipp->ipp_hopoptslen == 0) {
25700Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
25710Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
25720Sstevel@tonic-gate 			} else {
25730Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
25740Sstevel@tonic-gate 			}
25750Sstevel@tonic-gate 			if (sticky) {
25760Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
25770Sstevel@tonic-gate 				if (error != 0)
25780Sstevel@tonic-gate 					return (error);
25790Sstevel@tonic-gate 			}
25800Sstevel@tonic-gate 			break;
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
25830Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 			/*
25860Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
25870Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
25880Sstevel@tonic-gate 			 */
25890Sstevel@tonic-gate 			if (inlen != 0 &&
25900Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
25910Sstevel@tonic-gate 				return (EINVAL);
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 			if (checkonly)
25940Sstevel@tonic-gate 				break;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 			if (inlen == 0) {
25970Sstevel@tonic-gate 				if (sticky &&
25980Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
25990Sstevel@tonic-gate 					kmem_free(ipp->ipp_rtdstopts,
26000Sstevel@tonic-gate 					    ipp->ipp_rtdstoptslen);
26010Sstevel@tonic-gate 					ipp->ipp_rtdstopts = NULL;
26020Sstevel@tonic-gate 					ipp->ipp_rtdstoptslen = 0;
26030Sstevel@tonic-gate 				}
26040Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
26050Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
26060Sstevel@tonic-gate 			} else {
26071676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
26080Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rtdstopts,
26091676Sjpk 				    &ipp->ipp_rtdstoptslen, 0);
26100Sstevel@tonic-gate 				if (error != 0)
26110Sstevel@tonic-gate 					return (error);
26120Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
26130Sstevel@tonic-gate 			}
26140Sstevel@tonic-gate 			if (sticky) {
26150Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
26160Sstevel@tonic-gate 				if (error != 0)
26170Sstevel@tonic-gate 					return (error);
26180Sstevel@tonic-gate 			}
26190Sstevel@tonic-gate 			break;
26200Sstevel@tonic-gate 		}
26210Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
26220Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 			/*
26250Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
26260Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
26270Sstevel@tonic-gate 			 */
26280Sstevel@tonic-gate 			if (inlen != 0 &&
26290Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1)))
26300Sstevel@tonic-gate 				return (EINVAL);
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 			if (checkonly)
26330Sstevel@tonic-gate 				break;
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 			if (inlen == 0) {
26360Sstevel@tonic-gate 				if (sticky &&
26370Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
26380Sstevel@tonic-gate 					kmem_free(ipp->ipp_dstopts,
26390Sstevel@tonic-gate 					    ipp->ipp_dstoptslen);
26400Sstevel@tonic-gate 					ipp->ipp_dstopts = NULL;
26410Sstevel@tonic-gate 					ipp->ipp_dstoptslen = 0;
26420Sstevel@tonic-gate 				}
26430Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
26440Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
26450Sstevel@tonic-gate 			} else {
26461676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
26470Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_dstopts,
26481676Sjpk 				    &ipp->ipp_dstoptslen, 0);
26490Sstevel@tonic-gate 				if (error != 0)
26500Sstevel@tonic-gate 					return (error);
26510Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
26520Sstevel@tonic-gate 			}
26530Sstevel@tonic-gate 			if (sticky) {
26540Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
26550Sstevel@tonic-gate 				if (error != 0)
26560Sstevel@tonic-gate 					return (error);
26570Sstevel@tonic-gate 			}
26580Sstevel@tonic-gate 			break;
26590Sstevel@tonic-gate 		}
26600Sstevel@tonic-gate 		case IPV6_RTHDR: {
26610Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 			/*
26640Sstevel@tonic-gate 			 * Sanity checks - minimum size, size a multiple of
26650Sstevel@tonic-gate 			 * eight bytes, and matching size passed in.
26660Sstevel@tonic-gate 			 */
26670Sstevel@tonic-gate 			if (inlen != 0 &&
26680Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1)))
26690Sstevel@tonic-gate 				return (EINVAL);
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 			if (checkonly)
26720Sstevel@tonic-gate 				break;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 			if (inlen == 0) {
26750Sstevel@tonic-gate 				if (sticky &&
26760Sstevel@tonic-gate 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
26770Sstevel@tonic-gate 					kmem_free(ipp->ipp_rthdr,
26780Sstevel@tonic-gate 					    ipp->ipp_rthdrlen);
26790Sstevel@tonic-gate 					ipp->ipp_rthdr = NULL;
26800Sstevel@tonic-gate 					ipp->ipp_rthdrlen = 0;
26810Sstevel@tonic-gate 				}
26820Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
26830Sstevel@tonic-gate 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
26840Sstevel@tonic-gate 			} else {
26851676Sjpk 				error = optcom_pkt_set(invalp, inlen, sticky,
26860Sstevel@tonic-gate 				    (uchar_t **)&ipp->ipp_rthdr,
26871676Sjpk 				    &ipp->ipp_rthdrlen, 0);
26880Sstevel@tonic-gate 				if (error != 0)
26890Sstevel@tonic-gate 					return (error);
26900Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
26910Sstevel@tonic-gate 			}
26920Sstevel@tonic-gate 			if (sticky) {
26930Sstevel@tonic-gate 				error = icmp_build_hdrs(q, icmp);
26940Sstevel@tonic-gate 				if (error != 0)
26950Sstevel@tonic-gate 					return (error);
26960Sstevel@tonic-gate 			}
26970Sstevel@tonic-gate 			break;
26980Sstevel@tonic-gate 		}
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 		case IPV6_DONTFRAG:
27010Sstevel@tonic-gate 			if (checkonly)
27020Sstevel@tonic-gate 				break;
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 			if (onoff) {
27050Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DONTFRAG;
27060Sstevel@tonic-gate 			} else {
27070Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
27080Sstevel@tonic-gate 			}
27090Sstevel@tonic-gate 			break;
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 		case IPV6_USE_MIN_MTU:
27120Sstevel@tonic-gate 			if (inlen != sizeof (int))
27130Sstevel@tonic-gate 				return (EINVAL);
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > 1)
27160Sstevel@tonic-gate 				return (EINVAL);
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 			if (checkonly)
27190Sstevel@tonic-gate 				break;
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
27220Sstevel@tonic-gate 			ipp->ipp_use_min_mtu = *i1;
27230Sstevel@tonic-gate 			break;
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 		/*
27260Sstevel@tonic-gate 		 * This option can't be set.  Its only returned via
27270Sstevel@tonic-gate 		 * getsockopt() or ancillary data.
27280Sstevel@tonic-gate 		 */
27290Sstevel@tonic-gate 		case IPV6_PATHMTU:
27300Sstevel@tonic-gate 			return (EINVAL);
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 		case IPV6_BOUND_PIF:
27330Sstevel@tonic-gate 		case IPV6_SEC_OPT:
27340Sstevel@tonic-gate 		case IPV6_DONTFAILOVER_IF:
27350Sstevel@tonic-gate 		case IPV6_SRC_PREFERENCES:
27360Sstevel@tonic-gate 		case IPV6_V6ONLY:
27370Sstevel@tonic-gate 			/* Handled at IP level */
27380Sstevel@tonic-gate 			return (-EINVAL);
27390Sstevel@tonic-gate 		default:
27400Sstevel@tonic-gate 			*outlenp = 0;
27410Sstevel@tonic-gate 			return (EINVAL);
27420Sstevel@tonic-gate 		}
27430Sstevel@tonic-gate 		break;
27440Sstevel@tonic-gate 	}		/* end IPPROTO_IPV6 */
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	case IPPROTO_ICMPV6:
27470Sstevel@tonic-gate 		/*
27480Sstevel@tonic-gate 		 * Only allow IPv6 option processing on IPv6 sockets.
27490Sstevel@tonic-gate 		 */
27500Sstevel@tonic-gate 		if (icmp->icmp_family != AF_INET6) {
27510Sstevel@tonic-gate 			*outlenp = 0;
27520Sstevel@tonic-gate 			return (ENOPROTOOPT);
27530Sstevel@tonic-gate 		}
27540Sstevel@tonic-gate 		if (icmp->icmp_proto != IPPROTO_ICMPV6) {
27550Sstevel@tonic-gate 			*outlenp = 0;
27560Sstevel@tonic-gate 			return (ENOPROTOOPT);
27570Sstevel@tonic-gate 		}
27580Sstevel@tonic-gate 		switch (name) {
27590Sstevel@tonic-gate 		case ICMP6_FILTER:
27600Sstevel@tonic-gate 			if (!checkonly) {
27610Sstevel@tonic-gate 				if ((inlen != 0) &&
27620Sstevel@tonic-gate 				    (inlen != sizeof (icmp6_filter_t)))
27630Sstevel@tonic-gate 					return (EINVAL);
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 				if (inlen == 0) {
27660Sstevel@tonic-gate 					if (icmp->icmp_filter != NULL) {
27670Sstevel@tonic-gate 						kmem_free(icmp->icmp_filter,
27680Sstevel@tonic-gate 						    sizeof (icmp6_filter_t));
27690Sstevel@tonic-gate 						icmp->icmp_filter = NULL;
27700Sstevel@tonic-gate 					}
27710Sstevel@tonic-gate 				} else {
27720Sstevel@tonic-gate 					if (icmp->icmp_filter == NULL) {
27730Sstevel@tonic-gate 						icmp->icmp_filter = kmem_alloc(
27740Sstevel@tonic-gate 						    sizeof (icmp6_filter_t),
27750Sstevel@tonic-gate 						    KM_NOSLEEP);
27760Sstevel@tonic-gate 						if (icmp->icmp_filter == NULL) {
27770Sstevel@tonic-gate 							*outlenp = 0;
27780Sstevel@tonic-gate 							return (ENOBUFS);
27790Sstevel@tonic-gate 						}
27800Sstevel@tonic-gate 					}
27810Sstevel@tonic-gate 					(void) bcopy(invalp, icmp->icmp_filter,
27820Sstevel@tonic-gate 					    inlen);
27830Sstevel@tonic-gate 				}
27840Sstevel@tonic-gate 			}
27850Sstevel@tonic-gate 			break;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 		default:
27880Sstevel@tonic-gate 			*outlenp = 0;
27890Sstevel@tonic-gate 			return (EINVAL);
27900Sstevel@tonic-gate 		}
27910Sstevel@tonic-gate 		break;
27920Sstevel@tonic-gate 	default:
27930Sstevel@tonic-gate 		*outlenp = 0;
27940Sstevel@tonic-gate 		return (EINVAL);
27950Sstevel@tonic-gate 	}
27960Sstevel@tonic-gate 	/*
27970Sstevel@tonic-gate 	 * Common case of OK return with outval same as inval.
27980Sstevel@tonic-gate 	 */
27990Sstevel@tonic-gate 	if (invalp != outvalp) {
28000Sstevel@tonic-gate 		/* don't trust bcopy for identical src/dst */
28010Sstevel@tonic-gate 		(void) bcopy(invalp, outvalp, inlen);
28020Sstevel@tonic-gate 	}
28030Sstevel@tonic-gate 	*outlenp = inlen;
28040Sstevel@tonic-gate 	return (0);
28050Sstevel@tonic-gate }
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate /*
28080Sstevel@tonic-gate  * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl,
28090Sstevel@tonic-gate  * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum.
28100Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, and any sticky extension
28110Sstevel@tonic-gate  * headers.
28120Sstevel@tonic-gate  * Returns failure if can't allocate memory.
28130Sstevel@tonic-gate  */
28140Sstevel@tonic-gate static int
28150Sstevel@tonic-gate icmp_build_hdrs(queue_t *q, icmp_t *icmp)
28160Sstevel@tonic-gate {
28170Sstevel@tonic-gate 	uchar_t	*hdrs;
28180Sstevel@tonic-gate 	uint_t	hdrs_len;
28190Sstevel@tonic-gate 	ip6_t	*ip6h;
28200Sstevel@tonic-gate 	ip6i_t	*ip6i;
28210Sstevel@tonic-gate 	ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp;
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp);
28240Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
28250Sstevel@tonic-gate 	if (hdrs_len != icmp->icmp_sticky_hdrs_len) {
28260Sstevel@tonic-gate 		/* Need to reallocate */
28270Sstevel@tonic-gate 		if (hdrs_len != 0) {
28280Sstevel@tonic-gate 			hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
28290Sstevel@tonic-gate 			if (hdrs == NULL)
28300Sstevel@tonic-gate 				return (ENOMEM);
28310Sstevel@tonic-gate 		} else {
28320Sstevel@tonic-gate 			hdrs = NULL;
28330Sstevel@tonic-gate 		}
28340Sstevel@tonic-gate 		if (icmp->icmp_sticky_hdrs_len != 0) {
28350Sstevel@tonic-gate 			kmem_free(icmp->icmp_sticky_hdrs,
28360Sstevel@tonic-gate 			    icmp->icmp_sticky_hdrs_len);
28370Sstevel@tonic-gate 		}
28380Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs = hdrs;
28390Sstevel@tonic-gate 		icmp->icmp_sticky_hdrs_len = hdrs_len;
28400Sstevel@tonic-gate 	}
28410Sstevel@tonic-gate 	ip_build_hdrs_v6(icmp->icmp_sticky_hdrs,
28420Sstevel@tonic-gate 	    icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	/* Set header fields not in ipp */
28450Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
28460Sstevel@tonic-gate 		ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs;
28470Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RAW_CKSUM) {
28500Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
28510Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
28520Sstevel@tonic-gate 		}
28530Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NO_CKSUM) {
28540Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
28550Sstevel@tonic-gate 		}
28560Sstevel@tonic-gate 	} else {
28570Sstevel@tonic-gate 		ip6h = (ip6_t *)icmp->icmp_sticky_hdrs;
28580Sstevel@tonic-gate 	}
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_ADDR))
28610Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	/* Try to get everything in a single mblk */
28640Sstevel@tonic-gate 	if (hdrs_len > icmp->icmp_max_hdr_len) {
28650Sstevel@tonic-gate 		icmp->icmp_max_hdr_len = hdrs_len;
28660Sstevel@tonic-gate 		(void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len +
28670Sstevel@tonic-gate 		    icmp_wroff_extra);
28680Sstevel@tonic-gate 	}
28690Sstevel@tonic-gate 	return (0);
28700Sstevel@tonic-gate }
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate /*
28730Sstevel@tonic-gate  * This routine retrieves the value of an ND variable in a icmpparam_t
28740Sstevel@tonic-gate  * structure.  It is called through nd_getset when a user reads the
28750Sstevel@tonic-gate  * variable.
28760Sstevel@tonic-gate  */
28770Sstevel@tonic-gate /* ARGSUSED */
28780Sstevel@tonic-gate static int
28790Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
28800Sstevel@tonic-gate {
28810Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value);
28840Sstevel@tonic-gate 	return (0);
28850Sstevel@tonic-gate }
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate /*
28880Sstevel@tonic-gate  * Walk through the param array specified registering each element with the
28890Sstevel@tonic-gate  * named dispatch (ND) handler.
28900Sstevel@tonic-gate  */
28910Sstevel@tonic-gate static boolean_t
28920Sstevel@tonic-gate icmp_param_register(icmpparam_t *icmppa, int cnt)
28930Sstevel@tonic-gate {
28940Sstevel@tonic-gate 	for (; cnt-- > 0; icmppa++) {
28950Sstevel@tonic-gate 		if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) {
28960Sstevel@tonic-gate 			if (!nd_load(&icmp_g_nd, icmppa->icmp_param_name,
28970Sstevel@tonic-gate 			    icmp_param_get, icmp_param_set,
28980Sstevel@tonic-gate 			    (caddr_t)icmppa)) {
28990Sstevel@tonic-gate 				nd_free(&icmp_g_nd);
29000Sstevel@tonic-gate 				return (B_FALSE);
29010Sstevel@tonic-gate 			}
29020Sstevel@tonic-gate 		}
29030Sstevel@tonic-gate 	}
29040Sstevel@tonic-gate 	if (!nd_load(&icmp_g_nd, "icmp_status", icmp_status_report, NULL,
29050Sstevel@tonic-gate 	    NULL)) {
29060Sstevel@tonic-gate 		nd_free(&icmp_g_nd);
29070Sstevel@tonic-gate 		return (B_FALSE);
29080Sstevel@tonic-gate 	}
29090Sstevel@tonic-gate 	return (B_TRUE);
29100Sstevel@tonic-gate }
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */
29130Sstevel@tonic-gate /* ARGSUSED */
29140Sstevel@tonic-gate static int
29150Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
29160Sstevel@tonic-gate {
29170Sstevel@tonic-gate 	long		new_value;
29180Sstevel@tonic-gate 	icmpparam_t	*icmppa = (icmpparam_t *)cp;
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	/*
29210Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
29220Sstevel@tonic-gate 	 * required bounds.
29230Sstevel@tonic-gate 	 */
29240Sstevel@tonic-gate 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
29250Sstevel@tonic-gate 	    new_value < icmppa->icmp_param_min ||
29260Sstevel@tonic-gate 	    new_value > icmppa->icmp_param_max) {
29270Sstevel@tonic-gate 		return (EINVAL);
29280Sstevel@tonic-gate 	}
29290Sstevel@tonic-gate 	/* Set the new value */
29300Sstevel@tonic-gate 	icmppa->icmp_param_value = new_value;
29310Sstevel@tonic-gate 	return (0);
29320Sstevel@tonic-gate }
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate static void
29350Sstevel@tonic-gate icmp_rput(queue_t *q, mblk_t *mp)
29360Sstevel@tonic-gate {
29370Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
29380Sstevel@tonic-gate 	uchar_t			*rptr;
29390Sstevel@tonic-gate 	struct T_error_ack	*tea;
29400Sstevel@tonic-gate 	icmp_t			*icmp;
29410Sstevel@tonic-gate 	sin_t			*sin;
29420Sstevel@tonic-gate 	sin6_t			*sin6;
29430Sstevel@tonic-gate 	ip6_t			*ip6h;
29440Sstevel@tonic-gate 	ip6i_t			*ip6i;
29450Sstevel@tonic-gate 	mblk_t			*mp1;
29460Sstevel@tonic-gate 	int			hdr_len;
29470Sstevel@tonic-gate 	ipha_t			*ipha;
29480Sstevel@tonic-gate 	int			udi_size;	/* Size of T_unitdata_ind */
29490Sstevel@tonic-gate 	uint_t			ipvers;
29500Sstevel@tonic-gate 	ip6_pkt_t		ipp;
29510Sstevel@tonic-gate 	uint8_t			nexthdr;
29520Sstevel@tonic-gate 	boolean_t		recvif = B_FALSE;
29533318Srshoaib 	ip_pktinfo_t		*pinfo = NULL;
29540Sstevel@tonic-gate 	mblk_t			*options_mp = NULL;
29550Sstevel@tonic-gate 	uint_t			icmp_opt = 0;
29560Sstevel@tonic-gate 	boolean_t		icmp_ipv6_recvhoplimit = B_FALSE;
29571676Sjpk 	uint_t			hopstrip;
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
29600Sstevel@tonic-gate 	if (icmp->icmp_restricted) {
29610Sstevel@tonic-gate 		putnext(q, mp);
29620Sstevel@tonic-gate 		return;
29630Sstevel@tonic-gate 	}
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_CTL) {
29660Sstevel@tonic-gate 		/*
29670Sstevel@tonic-gate 		 * IP sends up the IPSEC_IN message for handling IPSEC
29680Sstevel@tonic-gate 		 * policy at the TCP level. We don't need it here.
29690Sstevel@tonic-gate 		 */
29700Sstevel@tonic-gate 		if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) {
29710Sstevel@tonic-gate 			mp1 = mp->b_cont;
29720Sstevel@tonic-gate 			freeb(mp);
29730Sstevel@tonic-gate 			mp = mp1;
29740Sstevel@tonic-gate 		} else {
29753318Srshoaib 			pinfo = (ip_pktinfo_t *)mp->b_rptr;
29763318Srshoaib 			if ((icmp->icmp_recvif != 0 ||
29773318Srshoaib 			    icmp->icmp_ip_recvpktinfo) &&
29783318Srshoaib 			    (pinfo->ip_pkt_ulp_type == IN_PKTINFO)) {
29790Sstevel@tonic-gate 				/*
29800Sstevel@tonic-gate 				 * IP has passed the options in mp and the
29810Sstevel@tonic-gate 				 * actual data is in b_cont.
29820Sstevel@tonic-gate 				 */
29830Sstevel@tonic-gate 				recvif = B_TRUE;
29840Sstevel@tonic-gate 				/*
29850Sstevel@tonic-gate 				 * We are here bcos IP_RECVIF is set so we need
29860Sstevel@tonic-gate 				 * to extract the options mblk and adjust the
29870Sstevel@tonic-gate 				 * rptr
29880Sstevel@tonic-gate 				 */
29890Sstevel@tonic-gate 				options_mp = mp;
29900Sstevel@tonic-gate 				mp = mp->b_cont;
29910Sstevel@tonic-gate 			}
29920Sstevel@tonic-gate 		}
29930Sstevel@tonic-gate 	}
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	rptr = mp->b_rptr;
29960Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
29970Sstevel@tonic-gate 	case M_DATA:
29980Sstevel@tonic-gate 		/*
29990Sstevel@tonic-gate 		 * M_DATA messages contain IP packets.  They are handled
30000Sstevel@tonic-gate 		 * following the switch.
30010Sstevel@tonic-gate 		 */
30020Sstevel@tonic-gate 		break;
30030Sstevel@tonic-gate 	case M_PROTO:
30040Sstevel@tonic-gate 	case M_PCPROTO:
30050Sstevel@tonic-gate 		/* M_PROTO messages contain some type of TPI message. */
30060Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
30070Sstevel@tonic-gate 			freemsg(mp);
30080Sstevel@tonic-gate 			return;
30090Sstevel@tonic-gate 		}
30100Sstevel@tonic-gate 		tea = (struct T_error_ack *)rptr;
30110Sstevel@tonic-gate 		switch (tea->PRIM_type) {
30120Sstevel@tonic-gate 		case T_ERROR_ACK:
30130Sstevel@tonic-gate 			switch (tea->ERROR_prim) {
30140Sstevel@tonic-gate 			case O_T_BIND_REQ:
30150Sstevel@tonic-gate 			case T_BIND_REQ:
30160Sstevel@tonic-gate 				/*
30170Sstevel@tonic-gate 				 * If our O_T_BIND_REQ/T_BIND_REQ fails,
30180Sstevel@tonic-gate 				 * clear out the source address before
30190Sstevel@tonic-gate 				 * passing the message upstream.
30200Sstevel@tonic-gate 				 * If this was caused by a T_CONN_REQ
30210Sstevel@tonic-gate 				 * revert back to bound state.
30220Sstevel@tonic-gate 				 */
30230Sstevel@tonic-gate 				if (icmp->icmp_state == TS_UNBND) {
30240Sstevel@tonic-gate 					/*
30250Sstevel@tonic-gate 					 * TPI has not yet bound - bind sent by
30260Sstevel@tonic-gate 					 * icmp_bind_proto.
30270Sstevel@tonic-gate 					 */
30280Sstevel@tonic-gate 					freemsg(mp);
30290Sstevel@tonic-gate 					return;
30300Sstevel@tonic-gate 				}
30310Sstevel@tonic-gate 				if (icmp->icmp_state == TS_DATA_XFER) {
30320Sstevel@tonic-gate 					/* Connect failed */
30330Sstevel@tonic-gate 					tea->ERROR_prim = T_CONN_REQ;
30340Sstevel@tonic-gate 					icmp->icmp_v6src =
30350Sstevel@tonic-gate 					    icmp->icmp_bound_v6src;
30360Sstevel@tonic-gate 					icmp->icmp_state = TS_IDLE;
30370Sstevel@tonic-gate 					if (icmp->icmp_family == AF_INET6)
30380Sstevel@tonic-gate 						(void) icmp_build_hdrs(q, icmp);
30390Sstevel@tonic-gate 					break;
30400Sstevel@tonic-gate 				}
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 				if (icmp->icmp_discon_pending) {
30430Sstevel@tonic-gate 					tea->ERROR_prim = T_DISCON_REQ;
30440Sstevel@tonic-gate 					icmp->icmp_discon_pending = 0;
30450Sstevel@tonic-gate 				}
30460Sstevel@tonic-gate 				V6_SET_ZERO(icmp->icmp_v6src);
30470Sstevel@tonic-gate 				V6_SET_ZERO(icmp->icmp_bound_v6src);
30480Sstevel@tonic-gate 				icmp->icmp_state = TS_UNBND;
30490Sstevel@tonic-gate 				if (icmp->icmp_family == AF_INET6)
30500Sstevel@tonic-gate 					(void) icmp_build_hdrs(q, icmp);
30510Sstevel@tonic-gate 				break;
30520Sstevel@tonic-gate 			default:
30530Sstevel@tonic-gate 				break;
30540Sstevel@tonic-gate 			}
30550Sstevel@tonic-gate 			break;
30560Sstevel@tonic-gate 		case T_BIND_ACK:
30570Sstevel@tonic-gate 			icmp_rput_bind_ack(q, mp);
30580Sstevel@tonic-gate 			return;
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 		case T_OPTMGMT_ACK:
30610Sstevel@tonic-gate 		case T_OK_ACK:
30620Sstevel@tonic-gate 			if (tea->PRIM_type == T_OK_ACK) {
30630Sstevel@tonic-gate 				struct T_ok_ack *toa;
30640Sstevel@tonic-gate 				toa = (struct T_ok_ack *)rptr;
30650Sstevel@tonic-gate 				if (toa->CORRECT_prim == T_UNBIND_REQ) {
30660Sstevel@tonic-gate 					/*
30670Sstevel@tonic-gate 					 * If somebody sets IPSEC options, IP
30680Sstevel@tonic-gate 					 * sends some IPSEC info which is used
30690Sstevel@tonic-gate 					 * by the TCP for detached connections.
30700Sstevel@tonic-gate 					 * We don't need it here.
30710Sstevel@tonic-gate 					 */
30720Sstevel@tonic-gate 					if ((mp1 = mp->b_cont) != NULL) {
30730Sstevel@tonic-gate 						freemsg(mp1);
30740Sstevel@tonic-gate 						mp->b_cont = NULL;
30750Sstevel@tonic-gate 					}
30760Sstevel@tonic-gate 				}
30770Sstevel@tonic-gate 			}
30780Sstevel@tonic-gate 			break;
30790Sstevel@tonic-gate 		default:
30800Sstevel@tonic-gate 			freemsg(mp);
30810Sstevel@tonic-gate 			return;
30820Sstevel@tonic-gate 		}
30830Sstevel@tonic-gate 		putnext(q, mp);
30840Sstevel@tonic-gate 		return;
30850Sstevel@tonic-gate 	case M_CTL:
30860Sstevel@tonic-gate 		if (recvif) {
30870Sstevel@tonic-gate 			/*
30880Sstevel@tonic-gate 			 * IP has passed the options in mp and the actual data
30890Sstevel@tonic-gate 			 * is in b_cont. Jump to normal data processing.
30900Sstevel@tonic-gate 			 */
30910Sstevel@tonic-gate 			break;
30920Sstevel@tonic-gate 		}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 		/* Contains ICMP packet from IP */
30950Sstevel@tonic-gate 		icmp_icmp_error(q, mp);
30960Sstevel@tonic-gate 		return;
30970Sstevel@tonic-gate 	default:
30980Sstevel@tonic-gate 		putnext(q, mp);
30990Sstevel@tonic-gate 		return;
31000Sstevel@tonic-gate 	}
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	/*
31030Sstevel@tonic-gate 	 * Discard message if it is misaligned or smaller than the IP header.
31040Sstevel@tonic-gate 	 */
31050Sstevel@tonic-gate 	if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) {
31060Sstevel@tonic-gate 		freemsg(mp);
31070Sstevel@tonic-gate 		if (options_mp != NULL)
31080Sstevel@tonic-gate 			freeb(options_mp);
31090Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
31100Sstevel@tonic-gate 		return;
31110Sstevel@tonic-gate 	}
31120Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION((ipha_t *)rptr);
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 	/* Handle M_DATA messages containing IP packets messages */
31150Sstevel@tonic-gate 	if (ipvers == IPV4_VERSION) {
31160Sstevel@tonic-gate 		/*
31170Sstevel@tonic-gate 		 * Special case where IP attaches
31180Sstevel@tonic-gate 		 * the IRE needs to be handled so that we don't send up
31190Sstevel@tonic-gate 		 * IRE to the user land.
31200Sstevel@tonic-gate 		 */
31210Sstevel@tonic-gate 		ipha = (ipha_t *)rptr;
31220Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 		if (ipha->ipha_protocol == IPPROTO_TCP) {
31250Sstevel@tonic-gate 			tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 			if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) ==
31280Sstevel@tonic-gate 			    TH_SYN) && mp->b_cont != NULL) {
31290Sstevel@tonic-gate 				mp1 = mp->b_cont;
31300Sstevel@tonic-gate 				if (mp1->b_datap->db_type == IRE_DB_TYPE) {
31310Sstevel@tonic-gate 					freeb(mp1);
31320Sstevel@tonic-gate 					mp->b_cont = NULL;
31330Sstevel@tonic-gate 				}
31340Sstevel@tonic-gate 			}
31350Sstevel@tonic-gate 		}
31360Sstevel@tonic-gate 		if (icmp_bsd_compat) {
31370Sstevel@tonic-gate 			ushort_t len;
31380Sstevel@tonic-gate 			len = ntohs(ipha->ipha_length);
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 			if (mp->b_datap->db_ref > 1) {
31410Sstevel@tonic-gate 				/*
31420Sstevel@tonic-gate 				 * Allocate a new IP header so that we can
31430Sstevel@tonic-gate 				 * modify ipha_length.
31440Sstevel@tonic-gate 				 */
31450Sstevel@tonic-gate 				mblk_t	*mp1;
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate 				mp1 = allocb(hdr_len, BPRI_MED);
31480Sstevel@tonic-gate 				if (!mp1) {
31490Sstevel@tonic-gate 					freemsg(mp);
31500Sstevel@tonic-gate 					if (options_mp != NULL)
31510Sstevel@tonic-gate 						freeb(options_mp);
31520Sstevel@tonic-gate 					BUMP_MIB(&rawip_mib, rawipInErrors);
31530Sstevel@tonic-gate 					return;
31540Sstevel@tonic-gate 				}
31550Sstevel@tonic-gate 				bcopy(rptr, mp1->b_rptr, hdr_len);
31560Sstevel@tonic-gate 				mp->b_rptr = rptr + hdr_len;
31570Sstevel@tonic-gate 				rptr = mp1->b_rptr;
31580Sstevel@tonic-gate 				ipha = (ipha_t *)rptr;
31590Sstevel@tonic-gate 				mp1->b_cont = mp;
31600Sstevel@tonic-gate 				mp1->b_wptr = rptr + hdr_len;
31610Sstevel@tonic-gate 				mp = mp1;
31620Sstevel@tonic-gate 			}
31630Sstevel@tonic-gate 			len -= hdr_len;
31640Sstevel@tonic-gate 			ipha->ipha_length = htons(len);
31650Sstevel@tonic-gate 		}
31660Sstevel@tonic-gate 	}
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	/*
31690Sstevel@tonic-gate 	 * This is the inbound data path.  Packets are passed upstream as
31700Sstevel@tonic-gate 	 * T_UNITDATA_IND messages with full IP headers still attached.
31710Sstevel@tonic-gate 	 */
31720Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
31730Sstevel@tonic-gate 		ASSERT(ipvers == IPV4_VERSION);
31740Sstevel@tonic-gate 		udi_size =  sizeof (struct T_unitdata_ind) + sizeof (sin_t);
31753318Srshoaib 		if (icmp->icmp_recvif && recvif &&
31763318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
31770Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
31780Sstevel@tonic-gate 			    sizeof (uint_t);
31790Sstevel@tonic-gate 		}
31803318Srshoaib 
31813318Srshoaib 		if (icmp->icmp_ip_recvpktinfo && recvif &&
31823318Srshoaib 		    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
31833318Srshoaib 			udi_size += sizeof (struct T_opthdr) +
31843318Srshoaib 			    sizeof (struct in_pktinfo);
31853318Srshoaib 		}
31863318Srshoaib 
31871673Sgt145670 		/*
31881673Sgt145670 		 * If SO_TIMESTAMP is set allocate the appropriate sized
31891673Sgt145670 		 * buffer. Since gethrestime() expects a pointer aligned
31901673Sgt145670 		 * argument, we allocate space necessary for extra
31911673Sgt145670 		 * alignment (even though it might not be used).
31921673Sgt145670 		 */
31931673Sgt145670 		if (icmp->icmp_timestamp) {
31941673Sgt145670 			udi_size += sizeof (struct T_opthdr) +
31951673Sgt145670 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
31961673Sgt145670 		}
31970Sstevel@tonic-gate 		mp1 = allocb(udi_size, BPRI_MED);
31980Sstevel@tonic-gate 		if (mp1 == NULL) {
31990Sstevel@tonic-gate 			freemsg(mp);
32000Sstevel@tonic-gate 			if (options_mp != NULL)
32010Sstevel@tonic-gate 				freeb(options_mp);
32020Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipInErrors);
32030Sstevel@tonic-gate 			return;
32040Sstevel@tonic-gate 		}
32050Sstevel@tonic-gate 		mp1->b_cont = mp;
32060Sstevel@tonic-gate 		mp = mp1;
32070Sstevel@tonic-gate 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
32080Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
32090Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)tudi + udi_size;
32100Sstevel@tonic-gate 		tudi->PRIM_type = T_UNITDATA_IND;
32110Sstevel@tonic-gate 		tudi->SRC_length = sizeof (sin_t);
32120Sstevel@tonic-gate 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
32130Sstevel@tonic-gate 		sin = (sin_t *)&tudi[1];
32140Sstevel@tonic-gate 		*sin = sin_null;
32150Sstevel@tonic-gate 		sin->sin_family = AF_INET;
32160Sstevel@tonic-gate 		sin->sin_addr.s_addr = ipha->ipha_src;
32170Sstevel@tonic-gate 		tudi->OPT_offset =  sizeof (struct T_unitdata_ind) +
32180Sstevel@tonic-gate 		    sizeof (sin_t);
32190Sstevel@tonic-gate 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
32200Sstevel@tonic-gate 		tudi->OPT_length = udi_size;
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 		/*
32230Sstevel@tonic-gate 		 * Add options if IP_RECVIF is set
32240Sstevel@tonic-gate 		 */
32250Sstevel@tonic-gate 		if (udi_size != 0) {
32260Sstevel@tonic-gate 			char *dstopt;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 			dstopt = (char *)&sin[1];
32293318Srshoaib 			if (icmp->icmp_recvif && recvif &&
32303318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVIF)) {
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 				struct T_opthdr *toh;
32330Sstevel@tonic-gate 				uint_t		*dstptr;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 				toh = (struct T_opthdr *)dstopt;
32360Sstevel@tonic-gate 				toh->level = IPPROTO_IP;
32370Sstevel@tonic-gate 				toh->name = IP_RECVIF;
32380Sstevel@tonic-gate 				toh->len = sizeof (struct T_opthdr) +
32390Sstevel@tonic-gate 					sizeof (uint_t);
32400Sstevel@tonic-gate 				toh->status = 0;
32410Sstevel@tonic-gate 				dstopt += sizeof (struct T_opthdr);
32420Sstevel@tonic-gate 				dstptr = (uint_t *)dstopt;
32433318Srshoaib 				*dstptr = pinfo->ip_pkt_ifindex;
32440Sstevel@tonic-gate 				dstopt += sizeof (uint_t);
32450Sstevel@tonic-gate 				freeb(options_mp);
32460Sstevel@tonic-gate 				udi_size -= toh->len;
32470Sstevel@tonic-gate 			}
32481673Sgt145670 			if (icmp->icmp_timestamp) {
32491673Sgt145670 				struct	T_opthdr *toh;
32501673Sgt145670 
32511673Sgt145670 				toh = (struct T_opthdr *)dstopt;
32521673Sgt145670 				toh->level = SOL_SOCKET;
32531673Sgt145670 				toh->name = SCM_TIMESTAMP;
32541673Sgt145670 				toh->len = sizeof (struct T_opthdr) +
32551673Sgt145670 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
32561673Sgt145670 				toh->status = 0;
32571673Sgt145670 				dstopt += sizeof (struct T_opthdr);
32581673Sgt145670 				/* Align for gethrestime() */
32591673Sgt145670 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
32601673Sgt145670 				    sizeof (intptr_t));
32611673Sgt145670 				gethrestime((timestruc_t *)dstopt);
32623318Srshoaib 				dstopt = (char *)toh + toh->len;
32633318Srshoaib 				udi_size -= toh->len;
32643318Srshoaib 			}
32653318Srshoaib 			if (icmp->icmp_ip_recvpktinfo && recvif &&
32663318Srshoaib 			    (pinfo->ip_pkt_flags & IPF_RECVADDR)) {
32673318Srshoaib 				struct	T_opthdr *toh;
32683318Srshoaib 				struct	in_pktinfo *pktinfop;
32693318Srshoaib 
32703318Srshoaib 				toh = (struct T_opthdr *)dstopt;
32713318Srshoaib 				toh->level = IPPROTO_IP;
32723318Srshoaib 				toh->name = IP_PKTINFO;
32733318Srshoaib 				toh->len = sizeof (struct T_opthdr) +
32743318Srshoaib 				    sizeof (in_pktinfo_t);
32753318Srshoaib 				toh->status = 0;
32763318Srshoaib 				dstopt += sizeof (struct T_opthdr);
32773318Srshoaib 				pktinfop = (struct in_pktinfo *)dstopt;
32783318Srshoaib 				pktinfop->ipi_ifindex = pinfo->ip_pkt_ifindex;
32793318Srshoaib 				pktinfop->ipi_spec_dst =
32803318Srshoaib 				    pinfo->ip_pkt_match_addr;
32813318Srshoaib 
32823318Srshoaib 				pktinfop->ipi_addr.s_addr = ipha->ipha_dst;
32833318Srshoaib 
32843318Srshoaib 				dstopt += sizeof (struct in_pktinfo);
32851673Sgt145670 				udi_size -= toh->len;
32861673Sgt145670 			}
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate 			/* Consumed all of allocated space */
32890Sstevel@tonic-gate 			ASSERT(udi_size == 0);
32900Sstevel@tonic-gate 		}
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInDatagrams);
32930Sstevel@tonic-gate 		putnext(q, mp);
32940Sstevel@tonic-gate 		return;
32950Sstevel@tonic-gate 	}
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate 	/*
32980Sstevel@tonic-gate 	 * We don't need options_mp in the IPv6 path.
32990Sstevel@tonic-gate 	 */
33000Sstevel@tonic-gate 	if (options_mp != NULL) {
33010Sstevel@tonic-gate 		freeb(options_mp);
33020Sstevel@tonic-gate 		options_mp = NULL;
33030Sstevel@tonic-gate 	}
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 	/*
33060Sstevel@tonic-gate 	 * Discard message if it is smaller than the IPv6 header
33070Sstevel@tonic-gate 	 * or if the header is malformed.
33080Sstevel@tonic-gate 	 */
33090Sstevel@tonic-gate 	if ((mp->b_wptr - rptr) < sizeof (ip6_t) ||
33100Sstevel@tonic-gate 	    IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION ||
33110Sstevel@tonic-gate 	    icmp->icmp_family != AF_INET6) {
33120Sstevel@tonic-gate 		freemsg(mp);
33130Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
33140Sstevel@tonic-gate 		return;
33150Sstevel@tonic-gate 	}
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 	/* Initialize */
33180Sstevel@tonic-gate 	ipp.ipp_fields = 0;
33191676Sjpk 	hopstrip = 0;
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 	ip6h = (ip6_t *)rptr;
33220Sstevel@tonic-gate 	/*
33230Sstevel@tonic-gate 	 * Call on ip_find_hdr_v6 which gets the total hdr len
33240Sstevel@tonic-gate 	 * as well as individual lenghts of ext hdrs (and ptrs to
33250Sstevel@tonic-gate 	 * them).
33260Sstevel@tonic-gate 	 */
33270Sstevel@tonic-gate 	if (ip6h->ip6_nxt != icmp->icmp_proto) {
33280Sstevel@tonic-gate 		/* Look for ifindex information */
33290Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
33300Sstevel@tonic-gate 			ip6i = (ip6i_t *)ip6h;
33310Sstevel@tonic-gate 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
33320Sstevel@tonic-gate 				ASSERT(ip6i->ip6i_ifindex != 0);
33330Sstevel@tonic-gate 				ipp.ipp_fields |= IPPF_IFINDEX;
33340Sstevel@tonic-gate 				ipp.ipp_ifindex = ip6i->ip6i_ifindex;
33350Sstevel@tonic-gate 			}
33360Sstevel@tonic-gate 			rptr = (uchar_t *)&ip6i[1];
33370Sstevel@tonic-gate 			mp->b_rptr = rptr;
33380Sstevel@tonic-gate 			if (rptr == mp->b_wptr) {
33390Sstevel@tonic-gate 				mp1 = mp->b_cont;
33400Sstevel@tonic-gate 				freeb(mp);
33410Sstevel@tonic-gate 				mp = mp1;
33420Sstevel@tonic-gate 				rptr = mp->b_rptr;
33430Sstevel@tonic-gate 			}
33440Sstevel@tonic-gate 			ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN);
33450Sstevel@tonic-gate 			ip6h = (ip6_t *)rptr;
33460Sstevel@tonic-gate 		}
33470Sstevel@tonic-gate 		hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr);
33481676Sjpk 
33491676Sjpk 		/*
33501676Sjpk 		 * We need to lie a bit to the user because users inside
33511676Sjpk 		 * labeled compartments should not see their own labels.  We
33521676Sjpk 		 * assume that in all other respects IP has checked the label,
33531676Sjpk 		 * and that the label is always first among the options.  (If
33541676Sjpk 		 * it's not first, then this code won't see it, and the option
33551676Sjpk 		 * will be passed along to the user.)
33561676Sjpk 		 *
33571676Sjpk 		 * If we had multilevel ICMP sockets, then the following code
33581676Sjpk 		 * should be skipped for them to allow the user to see the
33591676Sjpk 		 * label.
33601676Sjpk 		 *
33611676Sjpk 		 * Alignment restrictions in the definition of IP options
33621676Sjpk 		 * (namely, the requirement that the 4-octet DOI goes on a
33631676Sjpk 		 * 4-octet boundary) mean that we know exactly where the option
33641676Sjpk 		 * should start, but we're lenient for other hosts.
33651676Sjpk 		 *
33661676Sjpk 		 * Note that there are no multilevel ICMP or raw IP sockets
33671676Sjpk 		 * yet, thus nobody ever sees the IP6OPT_LS option.
33681676Sjpk 		 */
33691676Sjpk 		if ((ipp.ipp_fields & IPPF_HOPOPTS) &&
33701676Sjpk 		    ipp.ipp_hopoptslen > 5 && is_system_labeled()) {
33711676Sjpk 			const uchar_t *ucp =
33721676Sjpk 			    (const uchar_t *)ipp.ipp_hopopts + 2;
33731676Sjpk 			int remlen = ipp.ipp_hopoptslen - 2;
33741676Sjpk 
33751676Sjpk 			while (remlen > 0) {
33761676Sjpk 				if (*ucp == IP6OPT_PAD1) {
33771676Sjpk 					remlen--;
33781676Sjpk 					ucp++;
33791676Sjpk 				} else if (*ucp == IP6OPT_PADN) {
33801676Sjpk 					remlen -= ucp[1] + 2;
33811676Sjpk 					ucp += ucp[1] + 2;
33821676Sjpk 				} else if (*ucp == ip6opt_ls) {
33831676Sjpk 					hopstrip = (ucp -
33841676Sjpk 					    (const uchar_t *)ipp.ipp_hopopts) +
33851676Sjpk 					    ucp[1] + 2;
33861676Sjpk 					hopstrip = (hopstrip + 7) & ~7;
33871676Sjpk 					break;
33881676Sjpk 				} else {
33891676Sjpk 					/* label option must be first */
33901676Sjpk 					break;
33911676Sjpk 				}
33921676Sjpk 			}
33931676Sjpk 		}
33940Sstevel@tonic-gate 	} else {
33950Sstevel@tonic-gate 		hdr_len = IPV6_HDR_LEN;
33960Sstevel@tonic-gate 		ip6i = NULL;
33970Sstevel@tonic-gate 		nexthdr = ip6h->ip6_nxt;
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 	/*
34000Sstevel@tonic-gate 	 * One special case where IP attaches the IRE needs to
34010Sstevel@tonic-gate 	 * be handled so that we don't send up IRE to the user land.
34020Sstevel@tonic-gate 	 */
34030Sstevel@tonic-gate 	if (nexthdr == IPPROTO_TCP) {
34040Sstevel@tonic-gate 		tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len];
34050Sstevel@tonic-gate 
34060Sstevel@tonic-gate 		if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) &&
34070Sstevel@tonic-gate 		    mp->b_cont != NULL) {
34080Sstevel@tonic-gate 			mp1 = mp->b_cont;
34090Sstevel@tonic-gate 			if (mp1->b_datap->db_type == IRE_DB_TYPE) {
34100Sstevel@tonic-gate 				freeb(mp1);
34110Sstevel@tonic-gate 				mp->b_cont = NULL;
34120Sstevel@tonic-gate 			}
34130Sstevel@tonic-gate 		}
34140Sstevel@tonic-gate 	}
34150Sstevel@tonic-gate 	/*
34160Sstevel@tonic-gate 	 * Check a filter for ICMPv6 types if needed.
34170Sstevel@tonic-gate 	 * Verify raw checksums if needed.
34180Sstevel@tonic-gate 	 */
34190Sstevel@tonic-gate 	if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) {
34200Sstevel@tonic-gate 		if (icmp->icmp_filter != NULL) {
34210Sstevel@tonic-gate 			int type;
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 			/* Assumes that IP has done the pullupmsg */
34240Sstevel@tonic-gate 			type = mp->b_rptr[hdr_len];
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 			ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr);
34270Sstevel@tonic-gate 			if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
34280Sstevel@tonic-gate 				freemsg(mp);
34290Sstevel@tonic-gate 				return;
34300Sstevel@tonic-gate 			}
34310Sstevel@tonic-gate 		} else {
34320Sstevel@tonic-gate 			/* Checksum */
34330Sstevel@tonic-gate 			uint16_t	*up;
34340Sstevel@tonic-gate 			uint32_t	sum;
34350Sstevel@tonic-gate 			int		remlen;
34360Sstevel@tonic-gate 
34370Sstevel@tonic-gate 			up = (uint16_t *)&ip6h->ip6_src;
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 			remlen = msgdsize(mp) - hdr_len;
34400Sstevel@tonic-gate 			sum = htons(icmp->icmp_proto + remlen)
34410Sstevel@tonic-gate 			    + up[0] + up[1] + up[2] + up[3]
34420Sstevel@tonic-gate 			    + up[4] + up[5] + up[6] + up[7]
34430Sstevel@tonic-gate 			    + up[8] + up[9] + up[10] + up[11]
34440Sstevel@tonic-gate 			    + up[12] + up[13] + up[14] + up[15];
34450Sstevel@tonic-gate 			sum = (sum & 0xffff) + (sum >> 16);
34460Sstevel@tonic-gate 			sum = IP_CSUM(mp, hdr_len, sum);
34470Sstevel@tonic-gate 			if (sum != 0) {
34480Sstevel@tonic-gate 				/* IPv6 RAW checksum failed */
34490Sstevel@tonic-gate 				ip0dbg(("icmp_rput: RAW checksum "
34500Sstevel@tonic-gate 				    "failed %x\n", sum));
34510Sstevel@tonic-gate 				freemsg(mp);
34520Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipInCksumErrs);
34530Sstevel@tonic-gate 				return;
34540Sstevel@tonic-gate 			}
34550Sstevel@tonic-gate 		}
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate 	/* Skip all the IPv6 headers per API */
34580Sstevel@tonic-gate 	mp->b_rptr += hdr_len;
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	/*
34630Sstevel@tonic-gate 	 * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to
34640Sstevel@tonic-gate 	 * maintain state information, instead of relying on icmp_t
34650Sstevel@tonic-gate 	 * structure, since there arent any locks protecting these members
34660Sstevel@tonic-gate 	 * and there is a window where there might be a race between a
34670Sstevel@tonic-gate 	 * thread setting options on the write side and a thread reading
34680Sstevel@tonic-gate 	 * these options on the read size.
34690Sstevel@tonic-gate 	 */
34700Sstevel@tonic-gate 	if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
34710Sstevel@tonic-gate 	    IPPF_RTHDR|IPPF_IFINDEX)) {
34720Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvhopopts &&
34731676Sjpk 		    (ipp.ipp_fields & IPPF_HOPOPTS) &&
34741676Sjpk 		    ipp.ipp_hopoptslen > hopstrip) {
34750Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
34761676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
34770Sstevel@tonic-gate 			icmp_opt |= IPPF_HOPOPTS;
34780Sstevel@tonic-gate 		}
34790Sstevel@tonic-gate 		if ((icmp->icmp_ipv6_recvdstopts ||
34800Sstevel@tonic-gate 			icmp->icmp_old_ipv6_recvdstopts) &&
34810Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_DSTOPTS)) {
34820Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
34830Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
34840Sstevel@tonic-gate 			icmp_opt |= IPPF_DSTOPTS;
34850Sstevel@tonic-gate 		}
34860Sstevel@tonic-gate 		if (((icmp->icmp_ipv6_recvdstopts &&
34870Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrthdr &&
34880Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) ||
34890Sstevel@tonic-gate 		    icmp->icmp_ipv6_recvrtdstopts) &&
34900Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
34910Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
34920Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
34930Sstevel@tonic-gate 			icmp_opt |= IPPF_RTDSTOPTS;
34940Sstevel@tonic-gate 		}
34950Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvrthdr &&
34960Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_RTHDR)) {
34970Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
34980Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
34990Sstevel@tonic-gate 			icmp_opt |= IPPF_RTHDR;
35000Sstevel@tonic-gate 		}
35013318Srshoaib 		if (icmp->icmp_ip_recvpktinfo &&
35020Sstevel@tonic-gate 		    (ipp.ipp_fields & IPPF_IFINDEX)) {
35030Sstevel@tonic-gate 			udi_size += sizeof (struct T_opthdr) +
35040Sstevel@tonic-gate 			    sizeof (struct in6_pktinfo);
35050Sstevel@tonic-gate 			icmp_opt |= IPPF_IFINDEX;
35060Sstevel@tonic-gate 		}
35070Sstevel@tonic-gate 	}
35080Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvhoplimit) {
35090Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
35100Sstevel@tonic-gate 		icmp_ipv6_recvhoplimit = B_TRUE;
35110Sstevel@tonic-gate 	}
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	if (icmp->icmp_ipv6_recvtclass)
35140Sstevel@tonic-gate 		udi_size += sizeof (struct T_opthdr) + sizeof (int);
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	mp1 = allocb(udi_size, BPRI_MED);
35170Sstevel@tonic-gate 	if (mp1 == NULL) {
35180Sstevel@tonic-gate 		freemsg(mp);
35190Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipInErrors);
35200Sstevel@tonic-gate 		return;
35210Sstevel@tonic-gate 	}
35220Sstevel@tonic-gate 	mp1->b_cont = mp;
35230Sstevel@tonic-gate 	mp = mp1;
35240Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
35250Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
35260Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)tudi + udi_size;
35270Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
35280Sstevel@tonic-gate 	tudi->SRC_length = sizeof (sin6_t);
35290Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
35300Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
35310Sstevel@tonic-gate 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
35320Sstevel@tonic-gate 	tudi->OPT_length = udi_size;
35330Sstevel@tonic-gate 	sin6 = (sin6_t *)&tudi[1];
35340Sstevel@tonic-gate 	sin6->sin6_port = 0;
35350Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
35360Sstevel@tonic-gate 
35370Sstevel@tonic-gate 	sin6->sin6_addr = ip6h->ip6_src;
35380Sstevel@tonic-gate 	/* No sin6_flowinfo per API */
35390Sstevel@tonic-gate 	sin6->sin6_flowinfo = 0;
35400Sstevel@tonic-gate 	/* For link-scope source pass up scope id */
35410Sstevel@tonic-gate 	if ((ipp.ipp_fields & IPPF_IFINDEX) &&
35420Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
35430Sstevel@tonic-gate 		sin6->sin6_scope_id = ipp.ipp_ifindex;
35440Sstevel@tonic-gate 	else
35450Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 	sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
35480Sstevel@tonic-gate 	    icmp->icmp_zoneid);
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	if (udi_size != 0) {
35510Sstevel@tonic-gate 		uchar_t *dstopt;
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 		dstopt = (uchar_t *)&sin6[1];
35540Sstevel@tonic-gate 		if (icmp_opt & IPPF_IFINDEX) {
35550Sstevel@tonic-gate 			struct T_opthdr *toh;
35560Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35590Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35600Sstevel@tonic-gate 			toh->name = IPV6_PKTINFO;
35610Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35620Sstevel@tonic-gate 			    sizeof (*pkti);
35630Sstevel@tonic-gate 			toh->status = 0;
35640Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35650Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)dstopt;
35660Sstevel@tonic-gate 			pkti->ipi6_addr = ip6h->ip6_dst;
35670Sstevel@tonic-gate 			pkti->ipi6_ifindex = ipp.ipp_ifindex;
35680Sstevel@tonic-gate 			dstopt += sizeof (*pkti);
35690Sstevel@tonic-gate 			udi_size -= toh->len;
35700Sstevel@tonic-gate 		}
35710Sstevel@tonic-gate 		if (icmp_ipv6_recvhoplimit) {
35720Sstevel@tonic-gate 			struct T_opthdr *toh;
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35750Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35760Sstevel@tonic-gate 			toh->name = IPV6_HOPLIMIT;
35770Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35780Sstevel@tonic-gate 			    sizeof (uint_t);
35790Sstevel@tonic-gate 			toh->status = 0;
35800Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35810Sstevel@tonic-gate 			*(uint_t *)dstopt = ip6h->ip6_hops;
35820Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
35830Sstevel@tonic-gate 			udi_size -= toh->len;
35840Sstevel@tonic-gate 		}
35850Sstevel@tonic-gate 		if (icmp->icmp_ipv6_recvtclass) {
35860Sstevel@tonic-gate 			struct T_opthdr *toh;
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
35890Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
35900Sstevel@tonic-gate 			toh->name = IPV6_TCLASS;
35910Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
35920Sstevel@tonic-gate 			    sizeof (uint_t);
35930Sstevel@tonic-gate 			toh->status = 0;
35940Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
35950Sstevel@tonic-gate 			*(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow);
35960Sstevel@tonic-gate 			dstopt += sizeof (uint_t);
35970Sstevel@tonic-gate 			udi_size -= toh->len;
35980Sstevel@tonic-gate 		}
35990Sstevel@tonic-gate 		if (icmp_opt & IPPF_HOPOPTS) {
36000Sstevel@tonic-gate 			struct T_opthdr *toh;
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36030Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36040Sstevel@tonic-gate 			toh->name = IPV6_HOPOPTS;
36050Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36061676Sjpk 			    ipp.ipp_hopoptslen - hopstrip;
36070Sstevel@tonic-gate 			toh->status = 0;
36080Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36091676Sjpk 			bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt,
36101676Sjpk 			    ipp.ipp_hopoptslen - hopstrip);
36111676Sjpk 			if (hopstrip > 0) {
36121676Sjpk 				/* copy next header value and fake length */
36131676Sjpk 				dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0];
36141676Sjpk 				dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] -
36151676Sjpk 				    hopstrip / 8;
36161676Sjpk 			}
36171676Sjpk 			dstopt += ipp.ipp_hopoptslen - hopstrip;
36180Sstevel@tonic-gate 			udi_size -= toh->len;
36190Sstevel@tonic-gate 		}
36200Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTDSTOPTS) {
36210Sstevel@tonic-gate 			struct T_opthdr *toh;
36220Sstevel@tonic-gate 
36230Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36240Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36250Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
36260Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36270Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen;
36280Sstevel@tonic-gate 			toh->status = 0;
36290Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36300Sstevel@tonic-gate 			bcopy(ipp.ipp_rtdstopts, dstopt,
36310Sstevel@tonic-gate 			    ipp.ipp_rtdstoptslen);
36320Sstevel@tonic-gate 			dstopt += ipp.ipp_rtdstoptslen;
36330Sstevel@tonic-gate 			udi_size -= toh->len;
36340Sstevel@tonic-gate 		}
36350Sstevel@tonic-gate 		if (icmp_opt & IPPF_RTHDR) {
36360Sstevel@tonic-gate 			struct T_opthdr *toh;
36370Sstevel@tonic-gate 
36380Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36390Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36400Sstevel@tonic-gate 			toh->name = IPV6_RTHDR;
36410Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36420Sstevel@tonic-gate 			    ipp.ipp_rthdrlen;
36430Sstevel@tonic-gate 			toh->status = 0;
36440Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36450Sstevel@tonic-gate 			bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
36460Sstevel@tonic-gate 			dstopt += ipp.ipp_rthdrlen;
36470Sstevel@tonic-gate 			udi_size -= toh->len;
36480Sstevel@tonic-gate 		}
36490Sstevel@tonic-gate 		if (icmp_opt & IPPF_DSTOPTS) {
36500Sstevel@tonic-gate 			struct T_opthdr *toh;
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 			toh = (struct T_opthdr *)dstopt;
36530Sstevel@tonic-gate 			toh->level = IPPROTO_IPV6;
36540Sstevel@tonic-gate 			toh->name = IPV6_DSTOPTS;
36550Sstevel@tonic-gate 			toh->len = sizeof (struct T_opthdr) +
36560Sstevel@tonic-gate 			    ipp.ipp_dstoptslen;
36570Sstevel@tonic-gate 			toh->status = 0;
36580Sstevel@tonic-gate 			dstopt += sizeof (struct T_opthdr);
36590Sstevel@tonic-gate 			bcopy(ipp.ipp_dstopts, dstopt,
36600Sstevel@tonic-gate 			    ipp.ipp_dstoptslen);
36610Sstevel@tonic-gate 			dstopt += ipp.ipp_dstoptslen;
36620Sstevel@tonic-gate 			udi_size -= toh->len;
36630Sstevel@tonic-gate 		}
36640Sstevel@tonic-gate 		/* Consumed all of allocated space */
36650Sstevel@tonic-gate 		ASSERT(udi_size == 0);
36660Sstevel@tonic-gate 	}
36670Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipInDatagrams);
36680Sstevel@tonic-gate 	putnext(q, mp);
36690Sstevel@tonic-gate }
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate /*
36720Sstevel@tonic-gate  * Process a T_BIND_ACK
36730Sstevel@tonic-gate  */
36740Sstevel@tonic-gate static void
36750Sstevel@tonic-gate icmp_rput_bind_ack(queue_t *q, mblk_t *mp)
36760Sstevel@tonic-gate {
36770Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
36780Sstevel@tonic-gate 	mblk_t	*mp1;
36790Sstevel@tonic-gate 	ire_t	*ire;
36800Sstevel@tonic-gate 	struct T_bind_ack *tba;
36810Sstevel@tonic-gate 	uchar_t *addrp;
36820Sstevel@tonic-gate 	ipa_conn_t	*ac;
36830Sstevel@tonic-gate 	ipa6_conn_t	*ac6;
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate 	/*
36860Sstevel@tonic-gate 	 * We know if headers are included or not so we can
36870Sstevel@tonic-gate 	 * safely do this.
36880Sstevel@tonic-gate 	 */
36890Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
36900Sstevel@tonic-gate 		/*
36910Sstevel@tonic-gate 		 * TPI has not yet bound - bind sent by
36920Sstevel@tonic-gate 		 * icmp_bind_proto.
36930Sstevel@tonic-gate 		 */
36940Sstevel@tonic-gate 		freemsg(mp);
36950Sstevel@tonic-gate 		return;
36960Sstevel@tonic-gate 	}
36970Sstevel@tonic-gate 	if (icmp->icmp_discon_pending)
36980Sstevel@tonic-gate 		icmp->icmp_discon_pending = 0;
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 	/*
37010Sstevel@tonic-gate 	 * If a broadcast/multicast address was bound set
37020Sstevel@tonic-gate 	 * the source address to 0.
37030Sstevel@tonic-gate 	 * This ensures no datagrams with broadcast address
37040Sstevel@tonic-gate 	 * as source address are emitted (which would violate
37050Sstevel@tonic-gate 	 * RFC1122 - Hosts requirements)
37060Sstevel@tonic-gate 	 *
37070Sstevel@tonic-gate 	 * Note that when connecting the returned IRE is
37080Sstevel@tonic-gate 	 * for the destination address and we only perform
37090Sstevel@tonic-gate 	 * the broadcast check for the source address (it
37100Sstevel@tonic-gate 	 * is OK to connect to a broadcast/multicast address.)
37110Sstevel@tonic-gate 	 */
37120Sstevel@tonic-gate 	mp1 = mp->b_cont;
37130Sstevel@tonic-gate 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
37140Sstevel@tonic-gate 		ire = (ire_t *)mp1->b_rptr;
37150Sstevel@tonic-gate 
37160Sstevel@tonic-gate 		/*
37170Sstevel@tonic-gate 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
37180Sstevel@tonic-gate 		 * local address.
37190Sstevel@tonic-gate 		 */
37200Sstevel@tonic-gate 		if (ire->ire_type == IRE_BROADCAST &&
37210Sstevel@tonic-gate 		    icmp->icmp_state != TS_DATA_XFER) {
37220Sstevel@tonic-gate 			/* This was just a local bind to a MC/broadcast addr */
37230Sstevel@tonic-gate 			V6_SET_ZERO(icmp->icmp_v6src);
37240Sstevel@tonic-gate 			if (icmp->icmp_family == AF_INET6)
37250Sstevel@tonic-gate 				(void) icmp_build_hdrs(q, icmp);
37260Sstevel@tonic-gate 		} else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) {
37270Sstevel@tonic-gate 			/*
37280Sstevel@tonic-gate 			 * Local address not yet set - pick it from the
37290Sstevel@tonic-gate 			 * T_bind_ack
37300Sstevel@tonic-gate 			 */
37310Sstevel@tonic-gate 			tba = (struct T_bind_ack *)mp->b_rptr;
37320Sstevel@tonic-gate 			addrp = &mp->b_rptr[tba->ADDR_offset];
37330Sstevel@tonic-gate 			switch (icmp->icmp_family) {
37340Sstevel@tonic-gate 			case AF_INET:
37350Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
37360Sstevel@tonic-gate 					ac = (ipa_conn_t *)addrp;
37370Sstevel@tonic-gate 				} else {
37380Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
37390Sstevel@tonic-gate 					    sizeof (ipa_conn_x_t));
37400Sstevel@tonic-gate 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
37410Sstevel@tonic-gate 				}
37420Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
37430Sstevel@tonic-gate 				    &icmp->icmp_v6src);
37440Sstevel@tonic-gate 				break;
37450Sstevel@tonic-gate 			case AF_INET6:
37460Sstevel@tonic-gate 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
37470Sstevel@tonic-gate 					ac6 = (ipa6_conn_t *)addrp;
37480Sstevel@tonic-gate 				} else {
37490Sstevel@tonic-gate 					ASSERT(tba->ADDR_length ==
37500Sstevel@tonic-gate 					    sizeof (ipa6_conn_x_t));
37510Sstevel@tonic-gate 					ac6 = &((ipa6_conn_x_t *)
37520Sstevel@tonic-gate 					    addrp)->ac6x_conn;
37530Sstevel@tonic-gate 				}
37540Sstevel@tonic-gate 				icmp->icmp_v6src = ac6->ac6_laddr;
37550Sstevel@tonic-gate 				(void) icmp_build_hdrs(q, icmp);
37560Sstevel@tonic-gate 			}
37570Sstevel@tonic-gate 		}
37580Sstevel@tonic-gate 		mp1 = mp1->b_cont;
37590Sstevel@tonic-gate 	}
37600Sstevel@tonic-gate 	/*
37610Sstevel@tonic-gate 	 * Look for one or more appended ACK message added by
37620Sstevel@tonic-gate 	 * icmp_connect or icmp_disconnect.
37630Sstevel@tonic-gate 	 * If none found just send up the T_BIND_ACK.
37640Sstevel@tonic-gate 	 * icmp_connect has appended a T_OK_ACK and a
37650Sstevel@tonic-gate 	 * T_CONN_CON.
37660Sstevel@tonic-gate 	 * icmp_disconnect has appended a T_OK_ACK.
37670Sstevel@tonic-gate 	 */
37680Sstevel@tonic-gate 	if (mp1 != NULL) {
37690Sstevel@tonic-gate 		if (mp->b_cont == mp1)
37700Sstevel@tonic-gate 			mp->b_cont = NULL;
37710Sstevel@tonic-gate 		else {
37720Sstevel@tonic-gate 			ASSERT(mp->b_cont->b_cont == mp1);
37730Sstevel@tonic-gate 			mp->b_cont->b_cont = NULL;
37740Sstevel@tonic-gate 		}
37750Sstevel@tonic-gate 		freemsg(mp);
37760Sstevel@tonic-gate 		mp = mp1;
37770Sstevel@tonic-gate 		while (mp != NULL) {
37780Sstevel@tonic-gate 			mp1 = mp->b_cont;
37790Sstevel@tonic-gate 			mp->b_cont = NULL;
37800Sstevel@tonic-gate 			putnext(q, mp);
37810Sstevel@tonic-gate 			mp = mp1;
37820Sstevel@tonic-gate 		}
37830Sstevel@tonic-gate 		return;
37840Sstevel@tonic-gate 	}
37850Sstevel@tonic-gate 	freemsg(mp->b_cont);
37860Sstevel@tonic-gate 	mp->b_cont = NULL;
37870Sstevel@tonic-gate 	putnext(q, mp);
37880Sstevel@tonic-gate }
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate /*
37910Sstevel@tonic-gate  * return SNMP stuff in buffer in mpdata
37920Sstevel@tonic-gate  */
37930Sstevel@tonic-gate static int
37940Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl)
37950Sstevel@tonic-gate {
37960Sstevel@tonic-gate 	mblk_t			*mpdata;
37970Sstevel@tonic-gate 	struct opthdr		*optp;
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 	if (mpctl == NULL ||
38000Sstevel@tonic-gate 	    (mpdata = mpctl->b_cont) == NULL) {
38010Sstevel@tonic-gate 		return (0);
38020Sstevel@tonic-gate 	}
38030Sstevel@tonic-gate 
38040Sstevel@tonic-gate 	/* fixed length structure for IPv4 and IPv6 counters */
38050Sstevel@tonic-gate 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
38060Sstevel@tonic-gate 	optp->level = EXPER_RAWIP;
38070Sstevel@tonic-gate 	optp->name = 0;
38080Sstevel@tonic-gate 	(void) snmp_append_data(mpdata, (char *)&rawip_mib, sizeof (rawip_mib));
38090Sstevel@tonic-gate 	optp->len = msgdsize(mpdata);
38100Sstevel@tonic-gate 	qreply(q, mpctl);
38110Sstevel@tonic-gate 
38120Sstevel@tonic-gate 	return (1);
38130Sstevel@tonic-gate }
38140Sstevel@tonic-gate 
38150Sstevel@tonic-gate /*
38160Sstevel@tonic-gate  * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
38170Sstevel@tonic-gate  * TODO:  If this ever actually tries to set anything, it needs to be
38180Sstevel@tonic-gate  * to do the appropriate locking.
38190Sstevel@tonic-gate  */
38200Sstevel@tonic-gate /* ARGSUSED */
38210Sstevel@tonic-gate static int
38220Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
38230Sstevel@tonic-gate     uchar_t *ptr, int len)
38240Sstevel@tonic-gate {
38250Sstevel@tonic-gate 	switch (level) {
38260Sstevel@tonic-gate 	case EXPER_RAWIP:
38270Sstevel@tonic-gate 		return (0);
38280Sstevel@tonic-gate 	default:
38290Sstevel@tonic-gate 		return (1);
38300Sstevel@tonic-gate 	}
38310Sstevel@tonic-gate }
38320Sstevel@tonic-gate 
38330Sstevel@tonic-gate /* Report for ndd "icmp_status" */
38340Sstevel@tonic-gate /* ARGSUSED */
38350Sstevel@tonic-gate static int
38360Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
38370Sstevel@tonic-gate {
38380Sstevel@tonic-gate 	IDP	idp;
38390Sstevel@tonic-gate 	icmp_t	*icmp;
38400Sstevel@tonic-gate 	char	*state;
38410Sstevel@tonic-gate 	char	laddrbuf[INET6_ADDRSTRLEN];
38420Sstevel@tonic-gate 	char	faddrbuf[INET6_ADDRSTRLEN];
38430Sstevel@tonic-gate 
38440Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
38450Sstevel@tonic-gate 	    "RAWIP    " MI_COL_HDRPAD_STR
38460Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
38470Sstevel@tonic-gate 	    "  src addr        dest addr       state");
38480Sstevel@tonic-gate 	/*   xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	for (idp = mi_first_ptr(&icmp_g_head);
38520Sstevel@tonic-gate 	    (icmp = (icmp_t *)idp) != NULL;
38530Sstevel@tonic-gate 	    idp = mi_next_ptr(&icmp_g_head, idp)) {
38540Sstevel@tonic-gate 		if (icmp->icmp_state == TS_UNBND)
38550Sstevel@tonic-gate 			state = "UNBOUND";
38560Sstevel@tonic-gate 		else if (icmp->icmp_state == TS_IDLE)
38570Sstevel@tonic-gate 			state = "IDLE";
38580Sstevel@tonic-gate 		else if (icmp->icmp_state == TS_DATA_XFER)
38590Sstevel@tonic-gate 			state = "CONNECTED";
38600Sstevel@tonic-gate 		else
38610Sstevel@tonic-gate 			state = "UnkState";
38620Sstevel@tonic-gate 
38630Sstevel@tonic-gate 		(void) mi_mpprintf(mp,
38640Sstevel@tonic-gate 		    MI_COL_PTRFMT_STR "%s %s %s",
38650Sstevel@tonic-gate 		    (void *)icmp,
38660Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf,
38670Sstevel@tonic-gate 		    sizeof (faddrbuf)),
38680Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf,
38690Sstevel@tonic-gate 		    sizeof (laddrbuf)),
38700Sstevel@tonic-gate 		    state);
38710Sstevel@tonic-gate 	}
38720Sstevel@tonic-gate 	return (0);
38730Sstevel@tonic-gate }
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate /*
38760Sstevel@tonic-gate  * This routine creates a T_UDERROR_IND message and passes it upstream.
38770Sstevel@tonic-gate  * The address and options are copied from the T_UNITDATA_REQ message
38780Sstevel@tonic-gate  * passed in mp.  This message is freed.
38790Sstevel@tonic-gate  */
38800Sstevel@tonic-gate static void
38810Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
38820Sstevel@tonic-gate {
38830Sstevel@tonic-gate 	mblk_t	*mp1;
38840Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
38850Sstevel@tonic-gate 	struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr;
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate 	mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset],
38880Sstevel@tonic-gate 	    tudr->DEST_length, (char *)&rptr[tudr->OPT_offset],
38890Sstevel@tonic-gate 	    tudr->OPT_length, err);
38900Sstevel@tonic-gate 	if (mp1)
38910Sstevel@tonic-gate 		qreply(q, mp1);
38920Sstevel@tonic-gate 	freemsg(mp);
38930Sstevel@tonic-gate }
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate /*
38960Sstevel@tonic-gate  * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
38970Sstevel@tonic-gate  * After some error checking, the message is passed downstream to ip.
38980Sstevel@tonic-gate  */
38990Sstevel@tonic-gate static void
39000Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp)
39010Sstevel@tonic-gate {
39020Sstevel@tonic-gate 	icmp_t	*icmp = (icmp_t *)q->q_ptr;
39030Sstevel@tonic-gate 
39040Sstevel@tonic-gate 	/* If a bind has not been done, we can't unbind. */
39050Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
39060Sstevel@tonic-gate 		icmp_err_ack(q, mp, TOUTSTATE, 0);
39070Sstevel@tonic-gate 		return;
39080Sstevel@tonic-gate 	}
39090Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_v6src);
39100Sstevel@tonic-gate 	V6_SET_ZERO(icmp->icmp_bound_v6src);
39110Sstevel@tonic-gate 	icmp->icmp_state = TS_UNBND;
39120Sstevel@tonic-gate 
39130Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET6) {
39140Sstevel@tonic-gate 		int error;
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 		/* Rebuild the header template */
39170Sstevel@tonic-gate 		error = icmp_build_hdrs(q, icmp);
39180Sstevel@tonic-gate 		if (error != 0) {
39190Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, error);
39200Sstevel@tonic-gate 			return;
39210Sstevel@tonic-gate 		}
39220Sstevel@tonic-gate 	}
39230Sstevel@tonic-gate 	/* Pass the unbind to IP. */
39240Sstevel@tonic-gate 	putnext(q, mp);
39250Sstevel@tonic-gate }
39260Sstevel@tonic-gate 
39270Sstevel@tonic-gate /*
39280Sstevel@tonic-gate  * Process IPv4 packets that already include an IP header.
39290Sstevel@tonic-gate  * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
39300Sstevel@tonic-gate  * IPPROTO_IGMP).
39310Sstevel@tonic-gate  */
39320Sstevel@tonic-gate static void
39333318Srshoaib icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp, ip4_pkt_t *pktinfop,
39343318Srshoaib boolean_t use_putnext)
39350Sstevel@tonic-gate {
39360Sstevel@tonic-gate 	ipha_t	*ipha;
39370Sstevel@tonic-gate 	int	ip_hdr_length;
39380Sstevel@tonic-gate 	int	tp_hdr_len;
39390Sstevel@tonic-gate 	mblk_t	*mp1;
39400Sstevel@tonic-gate 	uint_t	pkt_len;
39413318Srshoaib 	ip_opt_info_t optinfo;
39423318Srshoaib 
39433318Srshoaib 	optinfo.ip_opt_flags = 0;
39443318Srshoaib 	optinfo.ip_opt_ill_index = 0;
39450Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
39460Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
39470Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
39480Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
39490Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
39500Sstevel@tonic-gate 			freemsg(mp);
39510Sstevel@tonic-gate 			return;
39520Sstevel@tonic-gate 		}
39530Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
39540Sstevel@tonic-gate 	}
39550Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length =
39560Sstevel@tonic-gate 	    (IP_VERSION<<4) | (ip_hdr_length>>2);
39570Sstevel@tonic-gate 
39580Sstevel@tonic-gate 	/*
39590Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
39600Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
39610Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
39620Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
39630Sstevel@tonic-gate 	 */
39640Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
39650Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
39660Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
39670Sstevel@tonic-gate 	/* Insert options if any */
39680Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
39690Sstevel@tonic-gate 		/*
39700Sstevel@tonic-gate 		 * Put the IP header plus any transport header that is
39710Sstevel@tonic-gate 		 * checksumed by ip_wput into the first mblk. (ip_wput assumes
39720Sstevel@tonic-gate 		 * that at least the checksum field is in the first mblk.)
39730Sstevel@tonic-gate 		 */
39740Sstevel@tonic-gate 		switch (ipha->ipha_protocol) {
39750Sstevel@tonic-gate 		case IPPROTO_UDP:
39760Sstevel@tonic-gate 			tp_hdr_len = 8;
39770Sstevel@tonic-gate 			break;
39780Sstevel@tonic-gate 		case IPPROTO_TCP:
39790Sstevel@tonic-gate 			tp_hdr_len = 20;
39800Sstevel@tonic-gate 			break;
39810Sstevel@tonic-gate 		default:
39820Sstevel@tonic-gate 			tp_hdr_len = 0;
39830Sstevel@tonic-gate 			break;
39840Sstevel@tonic-gate 		}
39850Sstevel@tonic-gate 		/*
39860Sstevel@tonic-gate 		 * The code below assumes that IP_SIMPLE_HDR_LENGTH plus
39870Sstevel@tonic-gate 		 * tp_hdr_len bytes will be in a single mblk.
39880Sstevel@tonic-gate 		 */
39890Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH +
39900Sstevel@tonic-gate 		    tp_hdr_len)) {
39910Sstevel@tonic-gate 			if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH +
39920Sstevel@tonic-gate 			    tp_hdr_len)) {
39930Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
39940Sstevel@tonic-gate 				freemsg(mp);
39950Sstevel@tonic-gate 				return;
39960Sstevel@tonic-gate 			}
39970Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
39980Sstevel@tonic-gate 		}
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate 		/*
40010Sstevel@tonic-gate 		 * if the length is larger then the max allowed IP packet,
40020Sstevel@tonic-gate 		 * then send an error and abort the processing.
40030Sstevel@tonic-gate 		 */
40040Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length)
40050Sstevel@tonic-gate 		    + icmp->icmp_ip_snd_options_len;
40060Sstevel@tonic-gate 		if (pkt_len > IP_MAXPACKET) {
40070Sstevel@tonic-gate 			icmp_ud_err(q, mp, EMSGSIZE);
40080Sstevel@tonic-gate 			return;
40090Sstevel@tonic-gate 		}
40100Sstevel@tonic-gate 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra +
40110Sstevel@tonic-gate 		    tp_hdr_len, BPRI_LO))) {
40120Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
40130Sstevel@tonic-gate 			return;
40140Sstevel@tonic-gate 		}
40150Sstevel@tonic-gate 		mp1->b_rptr += icmp_wroff_extra;
40160Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_rptr + ip_hdr_length;
40170Sstevel@tonic-gate 
40180Sstevel@tonic-gate 		ipha->ipha_length = htons((uint16_t)pkt_len);
40190Sstevel@tonic-gate 		bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH);
40200Sstevel@tonic-gate 
40210Sstevel@tonic-gate 		/* Copy transport header if any */
40220Sstevel@tonic-gate 		bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len);
40230Sstevel@tonic-gate 		mp1->b_wptr += tp_hdr_len;
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 		/* Add options */
40260Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_rptr;
40270Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options, &ipha[1],
40280Sstevel@tonic-gate 		    icmp->icmp_ip_snd_options_len);
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 		/* Drop IP header and transport header from original */
40310Sstevel@tonic-gate 		(void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len);
40320Sstevel@tonic-gate 
40330Sstevel@tonic-gate 		mp1->b_cont = mp;
40340Sstevel@tonic-gate 		mp = mp1;
40350Sstevel@tonic-gate 		/*
40360Sstevel@tonic-gate 		 * Massage source route putting first source
40370Sstevel@tonic-gate 		 * route in ipha_dst.
40380Sstevel@tonic-gate 		 */
40390Sstevel@tonic-gate 		(void) ip_massage_options(ipha);
40400Sstevel@tonic-gate 	}
40413318Srshoaib 
40423318Srshoaib 	if (pktinfop != NULL) {
40433318Srshoaib 		/*
40443318Srshoaib 		 * Over write the source address provided in the header
40453318Srshoaib 		 */
40463318Srshoaib 		if (pktinfop->ip4_addr != INADDR_ANY) {
40473318Srshoaib 			ipha->ipha_src = pktinfop->ip4_addr;
40483318Srshoaib 			optinfo.ip_opt_flags = IP_VERIFY_SRC;
40493318Srshoaib 			ASSERT(use_putnext == B_FALSE);
40503318Srshoaib 		}
40513318Srshoaib 
40523318Srshoaib 		if (pktinfop->ip4_ill_index != 0) {
40533318Srshoaib 			optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
40543318Srshoaib 			ASSERT(use_putnext == B_FALSE);
40553318Srshoaib 		}
40563318Srshoaib 	}
40573318Srshoaib 
40581676Sjpk 	mblk_setcred(mp, icmp->icmp_credp);
40593318Srshoaib 	if (use_putnext) {
40603318Srshoaib 		putnext(q, mp);
40613318Srshoaib 	} else {
40623318Srshoaib 		ip_output_options(Q_TO_CONN(q->q_next), mp, q->q_next, IP_WPUT,
40633318Srshoaib 		    &optinfo);
40643318Srshoaib 	}
40650Sstevel@tonic-gate }
40660Sstevel@tonic-gate 
40671676Sjpk static boolean_t
40681676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst)
40691676Sjpk {
40701676Sjpk 	int err;
40711676Sjpk 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
40721676Sjpk 
40731676Sjpk 	err = tsol_compute_label(DB_CREDDEF(mp, icmp->icmp_credp), dst,
40741676Sjpk 	    opt_storage, icmp->icmp_mac_exempt);
40751676Sjpk 	if (err == 0) {
40761676Sjpk 		err = tsol_update_options(&icmp->icmp_ip_snd_options,
40771676Sjpk 		    &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len,
40781676Sjpk 		    opt_storage);
40791676Sjpk 	}
40801676Sjpk 	if (err != 0) {
40811676Sjpk 		BUMP_MIB(&rawip_mib, rawipOutErrors);
40821676Sjpk 		DTRACE_PROBE4(
40831676Sjpk 		    tx__ip__log__drop__updatelabel__icmp,
40841676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
40851676Sjpk 		    queue_t *, q, char *, opt_storage, mblk_t *, mp);
40861676Sjpk 		icmp_ud_err(q, mp, err);
40871676Sjpk 		return (B_FALSE);
40881676Sjpk 	}
40891676Sjpk 	IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst);
40901676Sjpk 	return (B_TRUE);
40911676Sjpk }
40921676Sjpk 
40930Sstevel@tonic-gate /*
40940Sstevel@tonic-gate  * This routine handles all messages passed downstream.  It either
40950Sstevel@tonic-gate  * consumes the message or passes it downstream; it never queues a
40960Sstevel@tonic-gate  * a message.
40970Sstevel@tonic-gate  */
40980Sstevel@tonic-gate static void
40990Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp)
41000Sstevel@tonic-gate {
41010Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
41020Sstevel@tonic-gate 	ipha_t	*ipha;
41030Sstevel@tonic-gate 	mblk_t	*mp1;
41040Sstevel@tonic-gate 	int	ip_hdr_length;
41050Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
41060Sstevel@tonic-gate 	size_t	ip_len;
41070Sstevel@tonic-gate 	icmp_t	*icmp;
41080Sstevel@tonic-gate 	sin6_t	*sin6;
41090Sstevel@tonic-gate 	sin_t	*sin;
41100Sstevel@tonic-gate 	ipaddr_t	v4dst;
41113318Srshoaib 	ip4_pkt_t	pktinfo;
41123318Srshoaib 	ip4_pkt_t	*pktinfop = &pktinfo;
41133318Srshoaib 	ip_opt_info_t	optinfo;
41143318Srshoaib 	queue_t		*ip_wq;
41153318Srshoaib 	boolean_t	use_putnext = B_TRUE;
41160Sstevel@tonic-gate 
41170Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
41180Sstevel@tonic-gate 	if (icmp->icmp_restricted) {
41190Sstevel@tonic-gate 		icmp_wput_restricted(q, mp);
41200Sstevel@tonic-gate 		return;
41210Sstevel@tonic-gate 	}
41220Sstevel@tonic-gate 
41230Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
41240Sstevel@tonic-gate 	case M_DATA:
41250Sstevel@tonic-gate 		if (icmp->icmp_hdrincl) {
41260Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
41271676Sjpk 			ipha = (ipha_t *)mp->b_rptr;
41281676Sjpk 			if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
41291676Sjpk 				if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
41301676Sjpk 					BUMP_MIB(&rawip_mib, rawipOutErrors);
41311676Sjpk 					freemsg(mp);
41321676Sjpk 					return;
41331676Sjpk 				}
41341676Sjpk 				ipha = (ipha_t *)mp->b_rptr;
41351676Sjpk 			}
41361676Sjpk 			/*
41371676Sjpk 			 * If this connection was used for v6 (inconceivable!)
41381676Sjpk 			 * or if we have a new destination, then it's time to
41391676Sjpk 			 * figure a new label.
41401676Sjpk 			 */
41411676Sjpk 			if (is_system_labeled() &&
41421676Sjpk 			    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
41431676Sjpk 			    V4_PART_OF_V6(icmp->icmp_v6lastdst) !=
41441676Sjpk 			    ipha->ipha_dst) &&
41451676Sjpk 			    !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) {
41461676Sjpk 				return;
41471676Sjpk 			}
41483318Srshoaib 			icmp_wput_hdrincl(q, mp, icmp, NULL, use_putnext);
41490Sstevel@tonic-gate 			return;
41500Sstevel@tonic-gate 		}
41510Sstevel@tonic-gate 		freemsg(mp);
41520Sstevel@tonic-gate 		return;
41530Sstevel@tonic-gate 	case M_PROTO:
41540Sstevel@tonic-gate 	case M_PCPROTO:
41550Sstevel@tonic-gate 		ip_len = mp->b_wptr - rptr;
41560Sstevel@tonic-gate 		if (ip_len >= sizeof (struct T_unitdata_req)) {
41570Sstevel@tonic-gate 			/* Expedite valid T_UNITDATA_REQ to below the switch */
41580Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type
41590Sstevel@tonic-gate 			    == T_UNITDATA_REQ)
41600Sstevel@tonic-gate 				break;
41610Sstevel@tonic-gate 		}
41620Sstevel@tonic-gate 		/* FALLTHRU */
41630Sstevel@tonic-gate 	default:
41640Sstevel@tonic-gate 		icmp_wput_other(q, mp);
41650Sstevel@tonic-gate 		return;
41660Sstevel@tonic-gate 	}
41670Sstevel@tonic-gate 
41680Sstevel@tonic-gate 	/* Handle T_UNITDATA_REQ messages here. */
41690Sstevel@tonic-gate 
41703318Srshoaib 
41713318Srshoaib 
41720Sstevel@tonic-gate 	if (icmp->icmp_state == TS_UNBND) {
41730Sstevel@tonic-gate 		/* If a port has not been bound to the stream, fail. */
41740Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
41750Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
41760Sstevel@tonic-gate 		return;
41770Sstevel@tonic-gate 	}
41780Sstevel@tonic-gate 	mp1 = mp->b_cont;
41790Sstevel@tonic-gate 	if (mp1 == NULL) {
41800Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
41810Sstevel@tonic-gate 		icmp_ud_err(q, mp, EPROTO);
41820Sstevel@tonic-gate 		return;
41830Sstevel@tonic-gate 	}
41840Sstevel@tonic-gate 
41850Sstevel@tonic-gate 	if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) {
41860Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
41870Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
41880Sstevel@tonic-gate 		return;
41890Sstevel@tonic-gate 	}
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	switch (icmp->icmp_family) {
41920Sstevel@tonic-gate 	case AF_INET6:
41930Sstevel@tonic-gate 		sin6 = (sin6_t *)&rptr[tudr->DEST_offset];
41940Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin6) ||
41950Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin6_t) ||
41960Sstevel@tonic-gate 		    sin6->sin6_family != AF_INET6) {
41970Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
41980Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
41990Sstevel@tonic-gate 			return;
42000Sstevel@tonic-gate 		}
42010Sstevel@tonic-gate 
42020Sstevel@tonic-gate 		/* No support for mapped addresses on raw sockets */
42030Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
42040Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
42050Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
42060Sstevel@tonic-gate 			return;
42070Sstevel@tonic-gate 		}
42080Sstevel@tonic-gate 
42090Sstevel@tonic-gate 		/*
42100Sstevel@tonic-gate 		 * Destination is a native IPv6 address.
42110Sstevel@tonic-gate 		 * Send out an IPv6 format packet.
42120Sstevel@tonic-gate 		 */
42130Sstevel@tonic-gate 		icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length);
42140Sstevel@tonic-gate 		return;
42150Sstevel@tonic-gate 
42160Sstevel@tonic-gate 	case AF_INET:
42170Sstevel@tonic-gate 		sin = (sin_t *)&rptr[tudr->DEST_offset];
42180Sstevel@tonic-gate 		if (!OK_32PTR((char *)sin) ||
42190Sstevel@tonic-gate 		    tudr->DEST_length != sizeof (sin_t) ||
42200Sstevel@tonic-gate 		    sin->sin_family != AF_INET) {
42210Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
42220Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
42230Sstevel@tonic-gate 			return;
42240Sstevel@tonic-gate 		}
42250Sstevel@tonic-gate 		/* Extract and ipaddr */
42260Sstevel@tonic-gate 		v4dst = sin->sin_addr.s_addr;
42270Sstevel@tonic-gate 		break;
42281676Sjpk 
42291676Sjpk 	default:
42301676Sjpk 		ASSERT(0);
42310Sstevel@tonic-gate 	}
42320Sstevel@tonic-gate 
42333318Srshoaib 	pktinfop->ip4_ill_index = 0;
42343318Srshoaib 	pktinfop->ip4_addr = INADDR_ANY;
42353318Srshoaib 	optinfo.ip_opt_flags = 0;
42363318Srshoaib 	optinfo.ip_opt_ill_index = 0;
42373318Srshoaib 
42383318Srshoaib 
42390Sstevel@tonic-gate 	/*
42400Sstevel@tonic-gate 	 * If options passed in, feed it for verification and handling
42410Sstevel@tonic-gate 	 */
42420Sstevel@tonic-gate 	if (tudr->OPT_length != 0) {
42430Sstevel@tonic-gate 		int error;
42440Sstevel@tonic-gate 
42453318Srshoaib 		error = 0;
42460Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
42473318Srshoaib 		    (void *)pktinfop) < 0) {
42480Sstevel@tonic-gate 			/* failure */
42490Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
42500Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
42510Sstevel@tonic-gate 			return;
42520Sstevel@tonic-gate 		}
42533318Srshoaib 		ASSERT(error == 0);
42540Sstevel@tonic-gate 		/*
42550Sstevel@tonic-gate 		 * Note: Success in processing options.
42560Sstevel@tonic-gate 		 * mp option buffer represented by
42570Sstevel@tonic-gate 		 * OPT_length/offset now potentially modified
42580Sstevel@tonic-gate 		 * and contain option setting results
42590Sstevel@tonic-gate 		 */
42603318Srshoaib 
42613318Srshoaib 		if (pktinfop->ip4_ill_index != 0 ||
42623318Srshoaib 		    pktinfop->ip4_addr != INADDR_ANY) {
42633318Srshoaib 			/*
42643318Srshoaib 			 * PKTINFO option is supported only when ICMP is
42653318Srshoaib 			 * over IP.
42663318Srshoaib 			 */
42673318Srshoaib 			ip_wq = WR(q)->q_next;
42683318Srshoaib 			if (NOT_OVER_IP(ip_wq)) {
42693318Srshoaib 				icmp_ud_err(q, mp, EINVAL);
42703318Srshoaib 				return;
42713318Srshoaib 			}
42723318Srshoaib 			use_putnext = B_FALSE;
42733318Srshoaib 		}
42740Sstevel@tonic-gate 	}
42750Sstevel@tonic-gate 
42761676Sjpk 	if (v4dst == INADDR_ANY)
42771676Sjpk 		v4dst = htonl(INADDR_LOOPBACK);
42781676Sjpk 
42791676Sjpk 	/* Check if our saved options are valid; update if not */
42801676Sjpk 	if (is_system_labeled() &&
42811676Sjpk 	    (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) ||
42821676Sjpk 	    V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) &&
42831676Sjpk 	    !icmp_update_label(q, icmp, mp, v4dst)) {
42841676Sjpk 		return;
42851676Sjpk 	}
42861676Sjpk 
42870Sstevel@tonic-gate 	/* Protocol 255 contains full IP headers */
42880Sstevel@tonic-gate 	if (icmp->icmp_hdrincl) {
42890Sstevel@tonic-gate 		freeb(mp);
42903318Srshoaib 		icmp_wput_hdrincl(q, mp1, icmp, pktinfop, use_putnext);
42910Sstevel@tonic-gate 		return;
42920Sstevel@tonic-gate 	}
42931676Sjpk 
42943318Srshoaib 
42950Sstevel@tonic-gate 	/* Add an IP header */
42960Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len;
42970Sstevel@tonic-gate 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
42980Sstevel@tonic-gate 	if ((uchar_t *)ipha < mp1->b_datap->db_base ||
42990Sstevel@tonic-gate 	    mp1->b_datap->db_ref != 1 ||
43000Sstevel@tonic-gate 	    !OK_32PTR(ipha)) {
43010Sstevel@tonic-gate 		if (!(mp1 = allocb(ip_hdr_length + icmp_wroff_extra,
43020Sstevel@tonic-gate 		    BPRI_LO))) {
43030Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
43041289Sja97890 			icmp_ud_err(q, mp, ENOMEM);
43050Sstevel@tonic-gate 			return;
43060Sstevel@tonic-gate 		}
43070Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
43080Sstevel@tonic-gate 		ipha = (ipha_t *)mp1->b_datap->db_lim;
43090Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)ipha;
43100Sstevel@tonic-gate 		ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length);
43110Sstevel@tonic-gate 	}
43120Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
43130Sstevel@tonic-gate 	/* Set version, header length, and tos */
43140Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
43150Sstevel@tonic-gate 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
43160Sstevel@tonic-gate 		icmp->icmp_type_of_service);
43170Sstevel@tonic-gate 	/* Set ttl and protocol */
43180Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto;
43190Sstevel@tonic-gate #else
43200Sstevel@tonic-gate 	/* Set version, header length, and tos */
43210Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
43220Sstevel@tonic-gate 	    ((icmp->icmp_type_of_service << 8) |
43230Sstevel@tonic-gate 		((IP_VERSION << 4) | (ip_hdr_length>>2)));
43240Sstevel@tonic-gate 	/* Set ttl and protocol */
43250Sstevel@tonic-gate 	*(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl;
43260Sstevel@tonic-gate #endif
43273318Srshoaib 	if (pktinfop->ip4_addr != INADDR_ANY) {
43283318Srshoaib 		ASSERT(use_putnext == B_FALSE);
43293318Srshoaib 		ipha->ipha_src = pktinfop->ip4_addr;
43303318Srshoaib 		optinfo.ip_opt_flags = IP_VERIFY_SRC;
43313318Srshoaib 	} else {
43323318Srshoaib 
43333318Srshoaib 		/*
43343318Srshoaib 		 * Copy our address into the packet.  If this is zero,
43353318Srshoaib 		 * ip will fill in the real source address.
43363318Srshoaib 		 */
43373318Srshoaib 		IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src);
43383318Srshoaib 	}
43393318Srshoaib 
43400Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
43410Sstevel@tonic-gate 
43423318Srshoaib 	if (pktinfop->ip4_ill_index != 0) {
43433318Srshoaib 		optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index;
43443318Srshoaib 		ASSERT(use_putnext == B_FALSE);
43453318Srshoaib 	}
43463318Srshoaib 
43473318Srshoaib 
43480Sstevel@tonic-gate 	/*
43490Sstevel@tonic-gate 	 * For the socket of SOCK_RAW type, the checksum is provided in the
43500Sstevel@tonic-gate 	 * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to
43510Sstevel@tonic-gate 	 * tell IP that the application has sent a complete IP header and not
43520Sstevel@tonic-gate 	 * to compute the transport checksum nor change the DF flag.
43530Sstevel@tonic-gate 	 */
43540Sstevel@tonic-gate 	ipha->ipha_ident = IP_HDR_INCLUDED;
43550Sstevel@tonic-gate 
43560Sstevel@tonic-gate 	/* Finish common formatting of the packet. */
43570Sstevel@tonic-gate 	mp1->b_rptr = (uchar_t *)ipha;
43580Sstevel@tonic-gate 
43590Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ipha;
43600Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
43610Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
43620Sstevel@tonic-gate 
43630Sstevel@tonic-gate 	/*
43640Sstevel@tonic-gate 	 * Set the length into the IP header.
43650Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
43660Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
43670Sstevel@tonic-gate 	 * as this can cause problems in layers below.
43680Sstevel@tonic-gate 	 */
43690Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
43700Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
43710Sstevel@tonic-gate 		icmp_ud_err(q, mp, EMSGSIZE);
43720Sstevel@tonic-gate 		return;
43730Sstevel@tonic-gate 	}
43740Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)ip_len);
43750Sstevel@tonic-gate 	/*
43760Sstevel@tonic-gate 	 * Copy in the destination address from the T_UNITDATA
43770Sstevel@tonic-gate 	 * request
43780Sstevel@tonic-gate 	 */
43791676Sjpk 	ipha->ipha_dst = v4dst;
43800Sstevel@tonic-gate 
43810Sstevel@tonic-gate 	/*
43820Sstevel@tonic-gate 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
43830Sstevel@tonic-gate 	 */
43840Sstevel@tonic-gate 	if (CLASSD(v4dst))
43850Sstevel@tonic-gate 		ipha->ipha_ttl = icmp->icmp_multicast_ttl;
43860Sstevel@tonic-gate 
43870Sstevel@tonic-gate 	/* Copy in options if any */
43880Sstevel@tonic-gate 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
43890Sstevel@tonic-gate 		bcopy(icmp->icmp_ip_snd_options,
43900Sstevel@tonic-gate 		    &ipha[1], icmp->icmp_ip_snd_options_len);
43910Sstevel@tonic-gate 		/*
43920Sstevel@tonic-gate 		 * Massage source route putting first source route in ipha_dst.
43930Sstevel@tonic-gate 		 * Ignore the destination in the T_unitdata_req.
43940Sstevel@tonic-gate 		 */
43950Sstevel@tonic-gate 		(void) ip_massage_options(ipha);
43960Sstevel@tonic-gate 	}
43973318Srshoaib 
43980Sstevel@tonic-gate 	freeb(mp);
43990Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
44001676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
44013318Srshoaib 	if (use_putnext) {
44023318Srshoaib 		putnext(q, mp1);
44033318Srshoaib 	} else {
44043318Srshoaib 		ip_output_options(Q_TO_CONN(q->q_next), mp1, q->q_next, IP_WPUT,
44053318Srshoaib 		    &optinfo);
44063318Srshoaib 	}
44070Sstevel@tonic-gate #undef	ipha
44080Sstevel@tonic-gate #undef tudr
44090Sstevel@tonic-gate }
44100Sstevel@tonic-gate 
44111676Sjpk static boolean_t
44121676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst)
44131676Sjpk {
44141676Sjpk 	int err;
44151676Sjpk 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
44161676Sjpk 
44171676Sjpk 	err = tsol_compute_label_v6(DB_CREDDEF(mp, icmp->icmp_credp), dst,
44181676Sjpk 	    opt_storage, icmp->icmp_mac_exempt);
44191676Sjpk 	if (err == 0) {
44201676Sjpk 		err = tsol_update_sticky(&icmp->icmp_sticky_ipp,
44211676Sjpk 		    &icmp->icmp_label_len_v6, opt_storage);
44221676Sjpk 	}
44231676Sjpk 	if (err != 0) {
44241676Sjpk 		BUMP_MIB(&rawip_mib, rawipOutErrors);
44251676Sjpk 		DTRACE_PROBE4(
44261676Sjpk 		    tx__ip__log__drop__updatelabel__icmp6,
44271676Sjpk 		    char *, "queue(1) failed to update options(2) on mp(3)",
44281676Sjpk 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
44291676Sjpk 		icmp_ud_err(wq, mp, err);
44301676Sjpk 		return (B_FALSE);
44311676Sjpk 	}
44321676Sjpk 
44331676Sjpk 	icmp->icmp_v6lastdst = *dst;
44341676Sjpk 	return (B_TRUE);
44351676Sjpk }
44361676Sjpk 
44370Sstevel@tonic-gate /*
44380Sstevel@tonic-gate  * icmp_wput_ipv6():
44390Sstevel@tonic-gate  * Assumes that icmp_wput did some sanity checking on the destination
44401676Sjpk  * address, but that the label may not yet be correct.
44410Sstevel@tonic-gate  */
44420Sstevel@tonic-gate void
44430Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen)
44440Sstevel@tonic-gate {
44450Sstevel@tonic-gate 	ip6_t			*ip6h;
44460Sstevel@tonic-gate 	ip6i_t			*ip6i;	/* mp1->b_rptr even if no ip6i_t */
44470Sstevel@tonic-gate 	mblk_t			*mp1;
44480Sstevel@tonic-gate 	int			ip_hdr_len = IPV6_HDR_LEN;
44490Sstevel@tonic-gate 	size_t			ip_len;
44500Sstevel@tonic-gate 	icmp_t			*icmp;
44510Sstevel@tonic-gate 	ip6_pkt_t		ipp_s;	/* For ancillary data options */
44520Sstevel@tonic-gate 	ip6_pkt_t		*ipp = &ipp_s;
44530Sstevel@tonic-gate 	ip6_pkt_t		*tipp;
44540Sstevel@tonic-gate 	uint32_t		csum = 0;
44550Sstevel@tonic-gate 	uint_t			ignore = 0;
44560Sstevel@tonic-gate 	uint_t			option_exists = 0, is_sticky = 0;
44570Sstevel@tonic-gate 	uint8_t			*cp;
44580Sstevel@tonic-gate 	uint8_t			*nxthdr_ptr;
44591676Sjpk 	in6_addr_t		ip6_dst;
44600Sstevel@tonic-gate 
44610Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
44620Sstevel@tonic-gate 
44630Sstevel@tonic-gate 	/*
44640Sstevel@tonic-gate 	 * If the local address is a mapped address return
44650Sstevel@tonic-gate 	 * an error.
44660Sstevel@tonic-gate 	 * It would be possible to send an IPv6 packet but the
44670Sstevel@tonic-gate 	 * response would never make it back to the application
44680Sstevel@tonic-gate 	 * since it is bound to a mapped address.
44690Sstevel@tonic-gate 	 */
44700Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) {
44710Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
44720Sstevel@tonic-gate 		icmp_ud_err(q, mp, EADDRNOTAVAIL);
44730Sstevel@tonic-gate 		return;
44740Sstevel@tonic-gate 	}
44750Sstevel@tonic-gate 
44760Sstevel@tonic-gate 	ipp->ipp_fields = 0;
44770Sstevel@tonic-gate 	ipp->ipp_sticky_ignored = 0;
44780Sstevel@tonic-gate 
44790Sstevel@tonic-gate 	/*
44800Sstevel@tonic-gate 	 * If TPI options passed in, feed it for verification and handling
44810Sstevel@tonic-gate 	 */
44820Sstevel@tonic-gate 	if (tudr_optlen != 0) {
44830Sstevel@tonic-gate 		int error;
44840Sstevel@tonic-gate 
44850Sstevel@tonic-gate 		if (icmp_unitdata_opt_process(q, mp, &error,
44860Sstevel@tonic-gate 		    (void *)ipp) < 0) {
44870Sstevel@tonic-gate 			/* failure */
44880Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
44890Sstevel@tonic-gate 			icmp_ud_err(q, mp, error);
44900Sstevel@tonic-gate 			return;
44910Sstevel@tonic-gate 		}
44920Sstevel@tonic-gate 		ignore = ipp->ipp_sticky_ignored;
44930Sstevel@tonic-gate 		ASSERT(error == 0);
44940Sstevel@tonic-gate 	}
44950Sstevel@tonic-gate 
44960Sstevel@tonic-gate 	if (sin6->sin6_scope_id != 0 &&
44970Sstevel@tonic-gate 	    IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
44980Sstevel@tonic-gate 		/*
44990Sstevel@tonic-gate 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
45000Sstevel@tonic-gate 		 * option nor ancillary data.  It needs to be
45010Sstevel@tonic-gate 		 * explicitly set in options_exists.
45020Sstevel@tonic-gate 		 */
45030Sstevel@tonic-gate 		option_exists |= IPPF_SCOPE_ID;
45040Sstevel@tonic-gate 	}
45050Sstevel@tonic-gate 
45061676Sjpk 	/*
45071676Sjpk 	 * Compute the destination address
45081676Sjpk 	 */
45091676Sjpk 	ip6_dst = sin6->sin6_addr;
45101676Sjpk 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
45111676Sjpk 		ip6_dst = ipv6_loopback;
45121676Sjpk 
45131676Sjpk 	/*
45141676Sjpk 	 * If we're not going to the same destination as last time, then
45151676Sjpk 	 * recompute the label required.  This is done in a separate routine to
45161676Sjpk 	 * avoid blowing up our stack here.
45171676Sjpk 	 */
45181676Sjpk 	if (is_system_labeled() &&
45191676Sjpk 	    !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) &&
45201676Sjpk 	    !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) {
45211676Sjpk 		return;
45221676Sjpk 	}
45231676Sjpk 
45241676Sjpk 	/*
45251676Sjpk 	 * If there's a security label here, then we ignore any options the
45261676Sjpk 	 * user may try to set.  We keep the peer's label as a hidden sticky
45271676Sjpk 	 * option.
45281676Sjpk 	 */
45291676Sjpk 	if (icmp->icmp_label_len_v6 > 0) {
45301676Sjpk 		ignore &= ~IPPF_HOPOPTS;
45311676Sjpk 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
45321676Sjpk 	}
45331676Sjpk 
45340Sstevel@tonic-gate 	if ((icmp->icmp_sticky_ipp.ipp_fields == 0) &&
45350Sstevel@tonic-gate 	    (ipp->ipp_fields == 0)) {
45360Sstevel@tonic-gate 		/* No sticky options nor ancillary data. */
45370Sstevel@tonic-gate 		goto no_options;
45380Sstevel@tonic-gate 	}
45390Sstevel@tonic-gate 
45400Sstevel@tonic-gate 	/*
45410Sstevel@tonic-gate 	 * Go through the options figuring out where each is going to
45420Sstevel@tonic-gate 	 * come from and build two masks.  The first mask indicates if
45430Sstevel@tonic-gate 	 * the option exists at all.  The second mask indicates if the
45440Sstevel@tonic-gate 	 * option is sticky or ancillary.
45450Sstevel@tonic-gate 	 */
45460Sstevel@tonic-gate 	if (!(ignore & IPPF_HOPOPTS)) {
45470Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
45480Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
45490Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_hopoptslen;
45500Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
45510Sstevel@tonic-gate 			option_exists |= IPPF_HOPOPTS;
45520Sstevel@tonic-gate 			is_sticky |= IPPF_HOPOPTS;
45530Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen;
45540Sstevel@tonic-gate 		}
45550Sstevel@tonic-gate 	}
45560Sstevel@tonic-gate 
45570Sstevel@tonic-gate 	if (!(ignore & IPPF_RTHDR)) {
45580Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTHDR) {
45590Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
45600Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rthdrlen;
45610Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
45620Sstevel@tonic-gate 			option_exists |= IPPF_RTHDR;
45630Sstevel@tonic-gate 			is_sticky |= IPPF_RTHDR;
45640Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen;
45650Sstevel@tonic-gate 		}
45660Sstevel@tonic-gate 	}
45670Sstevel@tonic-gate 
45680Sstevel@tonic-gate 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
45690Sstevel@tonic-gate 		/*
45700Sstevel@tonic-gate 		 * Need to have a router header to use these.
45710Sstevel@tonic-gate 		 */
45720Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
45730Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
45740Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_rtdstoptslen;
45750Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
45760Sstevel@tonic-gate 			option_exists |= IPPF_RTDSTOPTS;
45770Sstevel@tonic-gate 			is_sticky |= IPPF_RTDSTOPTS;
45780Sstevel@tonic-gate 			ip_hdr_len +=
45790Sstevel@tonic-gate 			    icmp->icmp_sticky_ipp.ipp_rtdstoptslen;
45800Sstevel@tonic-gate 		}
45810Sstevel@tonic-gate 	}
45820Sstevel@tonic-gate 
45830Sstevel@tonic-gate 	if (!(ignore & IPPF_DSTOPTS)) {
45840Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
45850Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
45860Sstevel@tonic-gate 			ip_hdr_len += ipp->ipp_dstoptslen;
45870Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
45880Sstevel@tonic-gate 			option_exists |= IPPF_DSTOPTS;
45890Sstevel@tonic-gate 			is_sticky |= IPPF_DSTOPTS;
45900Sstevel@tonic-gate 			ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen;
45910Sstevel@tonic-gate 		}
45920Sstevel@tonic-gate 	}
45930Sstevel@tonic-gate 
45940Sstevel@tonic-gate 	if (!(ignore & IPPF_IFINDEX)) {
45950Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_IFINDEX) {
45960Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
45970Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
45980Sstevel@tonic-gate 			option_exists |= IPPF_IFINDEX;
45990Sstevel@tonic-gate 			is_sticky |= IPPF_IFINDEX;
46000Sstevel@tonic-gate 		}
46010Sstevel@tonic-gate 	}
46020Sstevel@tonic-gate 
46030Sstevel@tonic-gate 	if (!(ignore & IPPF_ADDR)) {
46040Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_ADDR) {
46050Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
46060Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) {
46070Sstevel@tonic-gate 			option_exists |= IPPF_ADDR;
46080Sstevel@tonic-gate 			is_sticky |= IPPF_ADDR;
46090Sstevel@tonic-gate 		}
46100Sstevel@tonic-gate 	}
46110Sstevel@tonic-gate 
46120Sstevel@tonic-gate 	if (!(ignore & IPPF_DONTFRAG)) {
46130Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
46140Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
46150Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
46160Sstevel@tonic-gate 			option_exists |= IPPF_DONTFRAG;
46170Sstevel@tonic-gate 			is_sticky |= IPPF_DONTFRAG;
46180Sstevel@tonic-gate 		}
46190Sstevel@tonic-gate 	}
46200Sstevel@tonic-gate 
46210Sstevel@tonic-gate 	if (!(ignore & IPPF_USE_MIN_MTU)) {
46220Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
46230Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
46240Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields &
46250Sstevel@tonic-gate 		    IPPF_USE_MIN_MTU) {
46260Sstevel@tonic-gate 			option_exists |= IPPF_USE_MIN_MTU;
46270Sstevel@tonic-gate 			is_sticky |= IPPF_USE_MIN_MTU;
46280Sstevel@tonic-gate 		}
46290Sstevel@tonic-gate 	}
46300Sstevel@tonic-gate 
46310Sstevel@tonic-gate 	if (!(ignore & IPPF_NEXTHOP)) {
46320Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
46330Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
46340Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) {
46350Sstevel@tonic-gate 			option_exists |= IPPF_NEXTHOP;
46360Sstevel@tonic-gate 			is_sticky |= IPPF_NEXTHOP;
46370Sstevel@tonic-gate 		}
46380Sstevel@tonic-gate 	}
46390Sstevel@tonic-gate 
4640679Sseb 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
4641679Sseb 		option_exists |= IPPF_HOPLIMIT;
4642679Sseb 	/* IPV6_HOPLIMIT can never be sticky */
4643679Sseb 	ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
4644679Sseb 
4645679Sseb 	if (!(ignore & IPPF_UNICAST_HOPS) &&
4646679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
4647679Sseb 		option_exists |= IPPF_UNICAST_HOPS;
4648679Sseb 		is_sticky |= IPPF_UNICAST_HOPS;
4649679Sseb 	}
4650679Sseb 
4651679Sseb 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
4652679Sseb 	    (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
4653679Sseb 		option_exists |= IPPF_MULTICAST_HOPS;
4654679Sseb 		is_sticky |= IPPF_MULTICAST_HOPS;
46550Sstevel@tonic-gate 	}
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) {
46580Sstevel@tonic-gate 		/* This is a sticky socket option only */
46590Sstevel@tonic-gate 		option_exists |= IPPF_NO_CKSUM;
46600Sstevel@tonic-gate 		is_sticky |= IPPF_NO_CKSUM;
46610Sstevel@tonic-gate 	}
46620Sstevel@tonic-gate 
46630Sstevel@tonic-gate 	if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) {
46640Sstevel@tonic-gate 		/* This is a sticky socket option only */
46650Sstevel@tonic-gate 		option_exists |= IPPF_RAW_CKSUM;
46660Sstevel@tonic-gate 		is_sticky |= IPPF_RAW_CKSUM;
46670Sstevel@tonic-gate 	}
46680Sstevel@tonic-gate 
46690Sstevel@tonic-gate 	if (!(ignore & IPPF_TCLASS)) {
46700Sstevel@tonic-gate 		if (ipp->ipp_fields & IPPF_TCLASS) {
46710Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
46720Sstevel@tonic-gate 		} else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
46730Sstevel@tonic-gate 			option_exists |= IPPF_TCLASS;
46740Sstevel@tonic-gate 			is_sticky |= IPPF_TCLASS;
46750Sstevel@tonic-gate 		}
46760Sstevel@tonic-gate 	}
46770Sstevel@tonic-gate 
46780Sstevel@tonic-gate no_options:
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate 	/*
46810Sstevel@tonic-gate 	 * If any options carried in the ip6i_t were specified, we
46820Sstevel@tonic-gate 	 * need to account for the ip6i_t in the data we'll be sending
46830Sstevel@tonic-gate 	 * down.
46840Sstevel@tonic-gate 	 */
46850Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I)
46860Sstevel@tonic-gate 		ip_hdr_len += sizeof (ip6i_t);
46870Sstevel@tonic-gate 
46880Sstevel@tonic-gate 	/* check/fix buffer config, setup pointers into it */
46890Sstevel@tonic-gate 	mp1 = mp->b_cont;
46900Sstevel@tonic-gate 	ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len];
46910Sstevel@tonic-gate 	if ((mp1->b_datap->db_ref != 1) ||
46920Sstevel@tonic-gate 	    ((unsigned char *)ip6h < mp1->b_datap->db_base) ||
46930Sstevel@tonic-gate 	    !OK_32PTR(ip6h)) {
46940Sstevel@tonic-gate 		/* Try to get everything in a single mblk next time */
46950Sstevel@tonic-gate 		if (ip_hdr_len > icmp->icmp_max_hdr_len) {
46960Sstevel@tonic-gate 			icmp->icmp_max_hdr_len = ip_hdr_len;
46970Sstevel@tonic-gate 			(void) mi_set_sth_wroff(RD(q),
46980Sstevel@tonic-gate 			    icmp->icmp_max_hdr_len + icmp_wroff_extra);
46990Sstevel@tonic-gate 		}
47000Sstevel@tonic-gate 		mp1 = allocb(ip_hdr_len + icmp_wroff_extra, BPRI_LO);
47010Sstevel@tonic-gate 		if (!mp1) {
47020Sstevel@tonic-gate 			BUMP_MIB(&rawip_mib, rawipOutErrors);
47030Sstevel@tonic-gate 			icmp_ud_err(q, mp, ENOMEM);
47040Sstevel@tonic-gate 			return;
47050Sstevel@tonic-gate 		}
47060Sstevel@tonic-gate 		mp1->b_cont = mp->b_cont;
47070Sstevel@tonic-gate 		mp1->b_wptr = mp1->b_datap->db_lim;
47080Sstevel@tonic-gate 		ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len);
47090Sstevel@tonic-gate 	}
47100Sstevel@tonic-gate 	mp1->b_rptr = (unsigned char *)ip6h;
47110Sstevel@tonic-gate 	ip6i = (ip6i_t *)ip6h;
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp)
47140Sstevel@tonic-gate 	if (option_exists & IPPF_HAS_IP6I) {
47150Sstevel@tonic-gate 		ip6h = (ip6_t *)&ip6i[1];
47160Sstevel@tonic-gate 		ip6i->ip6i_flags = 0;
47170Sstevel@tonic-gate 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
47180Sstevel@tonic-gate 
47190Sstevel@tonic-gate 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
47200Sstevel@tonic-gate 		if (option_exists & IPPF_SCOPE_ID) {
47210Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
47220Sstevel@tonic-gate 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
47230Sstevel@tonic-gate 		} else if (option_exists & IPPF_IFINDEX) {
47240Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
47250Sstevel@tonic-gate 			ASSERT(tipp->ipp_ifindex != 0);
47260Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_IFINDEX;
47270Sstevel@tonic-gate 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
47280Sstevel@tonic-gate 		}
47290Sstevel@tonic-gate 
47300Sstevel@tonic-gate 		if (option_exists & IPPF_RAW_CKSUM) {
47310Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM;
47320Sstevel@tonic-gate 			ip6i->ip6i_checksum_off = icmp->icmp_checksum_off;
47330Sstevel@tonic-gate 		}
47340Sstevel@tonic-gate 
47350Sstevel@tonic-gate 		if (option_exists & IPPF_NO_CKSUM) {
47360Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM;
47370Sstevel@tonic-gate 		}
47380Sstevel@tonic-gate 
47390Sstevel@tonic-gate 		if (option_exists & IPPF_ADDR) {
47400Sstevel@tonic-gate 			/*
47410Sstevel@tonic-gate 			 * Enable per-packet source address verification if
47420Sstevel@tonic-gate 			 * IPV6_PKTINFO specified the source address.
47430Sstevel@tonic-gate 			 * ip6_src is set in the transport's _wput function.
47440Sstevel@tonic-gate 			 */
47450Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
47460Sstevel@tonic-gate 		}
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 		if (option_exists & IPPF_DONTFRAG) {
47490Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
47500Sstevel@tonic-gate 		}
47510Sstevel@tonic-gate 
47520Sstevel@tonic-gate 		if (option_exists & IPPF_USE_MIN_MTU) {
47530Sstevel@tonic-gate 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
47540Sstevel@tonic-gate 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
47550Sstevel@tonic-gate 		}
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate 		if (option_exists & IPPF_NEXTHOP) {
47580Sstevel@tonic-gate 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
47590Sstevel@tonic-gate 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
47600Sstevel@tonic-gate 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
47610Sstevel@tonic-gate 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
47620Sstevel@tonic-gate 		}
47630Sstevel@tonic-gate 
47640Sstevel@tonic-gate 		/*
47650Sstevel@tonic-gate 		 * tell IP this is an ip6i_t private header
47660Sstevel@tonic-gate 		 */
47670Sstevel@tonic-gate 		ip6i->ip6i_nxt = IPPROTO_RAW;
47680Sstevel@tonic-gate 	}
47690Sstevel@tonic-gate 
47700Sstevel@tonic-gate 	/* Initialize IPv6 header */
47710Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
47720Sstevel@tonic-gate 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
47730Sstevel@tonic-gate 
4774679Sseb 	/* Set the hoplimit of the outgoing packet. */
47750Sstevel@tonic-gate 	if (option_exists & IPPF_HOPLIMIT) {
4776679Sseb 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
4777679Sseb 		ip6h->ip6_hops = ipp->ipp_hoplimit;
4778679Sseb 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
4779679Sseb 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
47800Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_multicast_ttl;
4781679Sseb 		if (option_exists & IPPF_MULTICAST_HOPS)
4782679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
47830Sstevel@tonic-gate 	} else {
47840Sstevel@tonic-gate 		ip6h->ip6_hops = icmp->icmp_ttl;
4785679Sseb 		if (option_exists & IPPF_UNICAST_HOPS)
4786679Sseb 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
47870Sstevel@tonic-gate 	}
47880Sstevel@tonic-gate 
47890Sstevel@tonic-gate 	if (option_exists & IPPF_ADDR) {
47900Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
47910Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
47920Sstevel@tonic-gate 		ip6h->ip6_src = tipp->ipp_addr;
47930Sstevel@tonic-gate 	} else {
47940Sstevel@tonic-gate 		/*
47950Sstevel@tonic-gate 		 * The source address was not set using IPV6_PKTINFO.
47960Sstevel@tonic-gate 		 * First look at the bound source.
47970Sstevel@tonic-gate 		 * If unspecified fallback to __sin6_src_id.
47980Sstevel@tonic-gate 		 */
47990Sstevel@tonic-gate 		ip6h->ip6_src = icmp->icmp_v6src;
48000Sstevel@tonic-gate 		if (sin6->__sin6_src_id != 0 &&
48010Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
48020Sstevel@tonic-gate 			ip_srcid_find_id(sin6->__sin6_src_id,
48030Sstevel@tonic-gate 			    &ip6h->ip6_src, icmp->icmp_zoneid);
48040Sstevel@tonic-gate 		}
48050Sstevel@tonic-gate 	}
48060Sstevel@tonic-gate 
48070Sstevel@tonic-gate 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
48080Sstevel@tonic-gate 	cp = (uint8_t *)&ip6h[1];
48090Sstevel@tonic-gate 
48100Sstevel@tonic-gate 	/*
48110Sstevel@tonic-gate 	 * Here's where we have to start stringing together
48120Sstevel@tonic-gate 	 * any extension headers in the right order:
48130Sstevel@tonic-gate 	 * Hop-by-hop, destination, routing, and final destination opts.
48140Sstevel@tonic-gate 	 */
48150Sstevel@tonic-gate 	if (option_exists & IPPF_HOPOPTS) {
48160Sstevel@tonic-gate 		/* Hop-by-hop options */
48170Sstevel@tonic-gate 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
48180Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
48190Sstevel@tonic-gate 
48200Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_HOPOPTS;
48210Sstevel@tonic-gate 		nxthdr_ptr = &hbh->ip6h_nxt;
48220Sstevel@tonic-gate 
48230Sstevel@tonic-gate 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
48240Sstevel@tonic-gate 		cp += tipp->ipp_hopoptslen;
48250Sstevel@tonic-gate 	}
48260Sstevel@tonic-gate 	/*
48270Sstevel@tonic-gate 	 * En-route destination options
48280Sstevel@tonic-gate 	 * Only do them if there's a routing header as well
48290Sstevel@tonic-gate 	 */
48300Sstevel@tonic-gate 	if (option_exists & IPPF_RTDSTOPTS) {
48310Sstevel@tonic-gate 		ip6_dest_t *dst = (ip6_dest_t *)cp;
48320Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
48330Sstevel@tonic-gate 
48340Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
48350Sstevel@tonic-gate 		nxthdr_ptr = &dst->ip6d_nxt;
48360Sstevel@tonic-gate 
48370Sstevel@tonic-gate 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
48380Sstevel@tonic-gate 		cp += tipp->ipp_rtdstoptslen;
48390Sstevel@tonic-gate 	}
48400Sstevel@tonic-gate 	/*
48410Sstevel@tonic-gate 	 * Routing header next
48420Sstevel@tonic-gate 	 */
48430Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
48440Sstevel@tonic-gate 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
48450Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_ROUTING;
48480Sstevel@tonic-gate 		nxthdr_ptr = &rt->ip6r_nxt;
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
48510Sstevel@tonic-gate 		cp += tipp->ipp_rthdrlen;
48520Sstevel@tonic-gate 	}
48530Sstevel@tonic-gate 	/*
48540Sstevel@tonic-gate 	 * Do ultimate destination options
48550Sstevel@tonic-gate 	 */
48560Sstevel@tonic-gate 	if (option_exists & IPPF_DSTOPTS) {
48570Sstevel@tonic-gate 		ip6_dest_t *dest = (ip6_dest_t *)cp;
48580Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
48590Sstevel@tonic-gate 
48600Sstevel@tonic-gate 		*nxthdr_ptr = IPPROTO_DSTOPTS;
48610Sstevel@tonic-gate 		nxthdr_ptr = &dest->ip6d_nxt;
48620Sstevel@tonic-gate 
48630Sstevel@tonic-gate 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
48640Sstevel@tonic-gate 		cp += tipp->ipp_dstoptslen;
48650Sstevel@tonic-gate 	}
48660Sstevel@tonic-gate 
48670Sstevel@tonic-gate 	/*
48680Sstevel@tonic-gate 	 * Now set the last header pointer to the proto passed in
48690Sstevel@tonic-gate 	 */
48700Sstevel@tonic-gate 	ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len);
48710Sstevel@tonic-gate 	*nxthdr_ptr = icmp->icmp_proto;
48720Sstevel@tonic-gate 
48730Sstevel@tonic-gate 	/*
48740Sstevel@tonic-gate 	 * Copy in the destination address
48750Sstevel@tonic-gate 	 */
48761676Sjpk 	ip6h->ip6_dst = ip6_dst;
48770Sstevel@tonic-gate 
48780Sstevel@tonic-gate 	ip6h->ip6_vcf =
48790Sstevel@tonic-gate 		(IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
48800Sstevel@tonic-gate 		(sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
48810Sstevel@tonic-gate 
48820Sstevel@tonic-gate 	if (option_exists & IPPF_TCLASS) {
48830Sstevel@tonic-gate 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
48840Sstevel@tonic-gate 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
48850Sstevel@tonic-gate 		    tipp->ipp_tclass);
48860Sstevel@tonic-gate 	}
48870Sstevel@tonic-gate 	if (option_exists & IPPF_RTHDR) {
48880Sstevel@tonic-gate 		ip6_rthdr_t	*rth;
48890Sstevel@tonic-gate 
48900Sstevel@tonic-gate 		/*
48910Sstevel@tonic-gate 		 * Perform any processing needed for source routing.
48920Sstevel@tonic-gate 		 * We know that all extension headers will be in the same mblk
48930Sstevel@tonic-gate 		 * as the IPv6 header.
48940Sstevel@tonic-gate 		 */
48950Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
48960Sstevel@tonic-gate 		if (rth != NULL && rth->ip6r_segleft != 0) {
48970Sstevel@tonic-gate 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
48980Sstevel@tonic-gate 				/*
48990Sstevel@tonic-gate 				 * Drop packet - only support Type 0 routing.
49000Sstevel@tonic-gate 				 * Notify the application as well.
49010Sstevel@tonic-gate 				 */
49020Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
49030Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
49040Sstevel@tonic-gate 				return;
49050Sstevel@tonic-gate 			}
49060Sstevel@tonic-gate 			/*
49070Sstevel@tonic-gate 			 * rth->ip6r_len is twice the number of
49080Sstevel@tonic-gate 			 * addresses in the header
49090Sstevel@tonic-gate 			 */
49100Sstevel@tonic-gate 			if (rth->ip6r_len & 0x1) {
49110Sstevel@tonic-gate 				icmp_ud_err(q, mp, EPROTO);
49120Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
49130Sstevel@tonic-gate 				return;
49140Sstevel@tonic-gate 			}
49150Sstevel@tonic-gate 			/*
49160Sstevel@tonic-gate 			 * Shuffle the routing header and ip6_dst
49170Sstevel@tonic-gate 			 * addresses, and get the checksum difference
49180Sstevel@tonic-gate 			 * between the first hop (in ip6_dst) and
49190Sstevel@tonic-gate 			 * the destination (in the last routing hdr entry).
49200Sstevel@tonic-gate 			 */
49210Sstevel@tonic-gate 			csum = ip_massage_options_v6(ip6h, rth);
49220Sstevel@tonic-gate 			/*
49230Sstevel@tonic-gate 			 * Verify that the first hop isn't a mapped address.
49240Sstevel@tonic-gate 			 * Routers along the path need to do this verification
49250Sstevel@tonic-gate 			 * for subsequent hops.
49260Sstevel@tonic-gate 			 */
49270Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
49280Sstevel@tonic-gate 				icmp_ud_err(q, mp, EADDRNOTAVAIL);
49290Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
49300Sstevel@tonic-gate 				return;
49310Sstevel@tonic-gate 			}
49320Sstevel@tonic-gate 		}
49330Sstevel@tonic-gate 	}
49340Sstevel@tonic-gate 
49350Sstevel@tonic-gate 	ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN;
49360Sstevel@tonic-gate 	if (mp1->b_cont != NULL)
49370Sstevel@tonic-gate 		ip_len += msgdsize(mp1->b_cont);
49380Sstevel@tonic-gate 
49390Sstevel@tonic-gate 	/*
49400Sstevel@tonic-gate 	 * Set the length into the IP header.
49410Sstevel@tonic-gate 	 * If the length is greater than the maximum allowed by IP,
49420Sstevel@tonic-gate 	 * then free the message and return. Do not try and send it
49430Sstevel@tonic-gate 	 * as this can cause problems in layers below.
49440Sstevel@tonic-gate 	 */
49450Sstevel@tonic-gate 	if (ip_len > IP_MAXPACKET) {
49460Sstevel@tonic-gate 		BUMP_MIB(&rawip_mib, rawipOutErrors);
49471289Sja97890 		icmp_ud_err(q, mp, EMSGSIZE);
49480Sstevel@tonic-gate 		return;
49490Sstevel@tonic-gate 	}
49500Sstevel@tonic-gate 	if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) {
49510Sstevel@tonic-gate 		uint_t	cksum_off;	/* From ip6i == mp1->b_rptr */
49520Sstevel@tonic-gate 		uint16_t *cksum_ptr;
49530Sstevel@tonic-gate 		uint_t	ext_hdrs_len;
49540Sstevel@tonic-gate 
49550Sstevel@tonic-gate 		/* ICMPv6 must have an offset matching icmp6_cksum offset */
49560Sstevel@tonic-gate 		ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 ||
49570Sstevel@tonic-gate 		    icmp->icmp_checksum_off == 2);
49580Sstevel@tonic-gate 
49590Sstevel@tonic-gate 		/*
49600Sstevel@tonic-gate 		 * We make it easy for IP to include our pseudo header
49610Sstevel@tonic-gate 		 * by putting our length in uh_checksum, modified (if
49620Sstevel@tonic-gate 		 * we have a routing header) by the checksum difference
49630Sstevel@tonic-gate 		 * between the ultimate destination and first hop addresses.
49640Sstevel@tonic-gate 		 * Note: ICMPv6 must always checksum the packet.
49650Sstevel@tonic-gate 		 */
49660Sstevel@tonic-gate 		cksum_off = ip_hdr_len + icmp->icmp_checksum_off;
49670Sstevel@tonic-gate 		if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) {
49680Sstevel@tonic-gate 			if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) {
49690Sstevel@tonic-gate 				BUMP_MIB(&rawip_mib, rawipOutErrors);
49700Sstevel@tonic-gate 				freemsg(mp);
49710Sstevel@tonic-gate 				return;
49720Sstevel@tonic-gate 			}
49730Sstevel@tonic-gate 			ip6i = (ip6i_t *)mp1->b_rptr;
49740Sstevel@tonic-gate 			if (ip6i->ip6i_nxt == IPPROTO_RAW)
49750Sstevel@tonic-gate 				ip6h = (ip6_t *)&ip6i[1];
49760Sstevel@tonic-gate 			else
49770Sstevel@tonic-gate 				ip6h = (ip6_t *)ip6i;
49780Sstevel@tonic-gate 		}
49790Sstevel@tonic-gate 		/* Add payload length to checksum */
49800Sstevel@tonic-gate 		ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN -
49810Sstevel@tonic-gate 		    (int)((uchar_t *)ip6h - (uchar_t *)ip6i);
49820Sstevel@tonic-gate 		csum += htons(ip_len - ext_hdrs_len);
49830Sstevel@tonic-gate 
49840Sstevel@tonic-gate 		cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off);
49850Sstevel@tonic-gate 		csum = (csum & 0xFFFF) + (csum >> 16);
49860Sstevel@tonic-gate 		*cksum_ptr = (uint16_t)csum;
49870Sstevel@tonic-gate 	}
49880Sstevel@tonic-gate 
49890Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
49900Sstevel@tonic-gate 	ip_len = htons(ip_len);
49910Sstevel@tonic-gate #endif
49920Sstevel@tonic-gate 	ip6h->ip6_plen = (uint16_t)ip_len;
49930Sstevel@tonic-gate 
49940Sstevel@tonic-gate 	freeb(mp);
49950Sstevel@tonic-gate 
49960Sstevel@tonic-gate 	/* We're done. Pass the packet to IP */
49970Sstevel@tonic-gate 	BUMP_MIB(&rawip_mib, rawipOutDatagrams);
49981676Sjpk 	mblk_setcred(mp1, icmp->icmp_credp);
49990Sstevel@tonic-gate 	putnext(q, mp1);
50000Sstevel@tonic-gate }
50010Sstevel@tonic-gate 
50020Sstevel@tonic-gate static void
50030Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp)
50040Sstevel@tonic-gate {
50050Sstevel@tonic-gate 	uchar_t	*rptr = mp->b_rptr;
50060Sstevel@tonic-gate 	struct iocblk *iocp;
50070Sstevel@tonic-gate #define	tudr ((struct T_unitdata_req *)rptr)
50080Sstevel@tonic-gate 	icmp_t	*icmp;
50090Sstevel@tonic-gate 	cred_t *cr;
50100Sstevel@tonic-gate 
50110Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
50120Sstevel@tonic-gate 
50130Sstevel@tonic-gate 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
50140Sstevel@tonic-gate 
50150Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
50160Sstevel@tonic-gate 	case M_PROTO:
50170Sstevel@tonic-gate 	case M_PCPROTO:
50180Sstevel@tonic-gate 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
50190Sstevel@tonic-gate 			/*
50200Sstevel@tonic-gate 			 * If the message does not contain a PRIM_type,
50210Sstevel@tonic-gate 			 * throw it away.
50220Sstevel@tonic-gate 			 */
50230Sstevel@tonic-gate 			freemsg(mp);
50240Sstevel@tonic-gate 			return;
50250Sstevel@tonic-gate 		}
50260Sstevel@tonic-gate 		switch (((union T_primitives *)rptr)->type) {
50270Sstevel@tonic-gate 		case T_ADDR_REQ:
50280Sstevel@tonic-gate 			icmp_addr_req(q, mp);
50290Sstevel@tonic-gate 			return;
50300Sstevel@tonic-gate 		case O_T_BIND_REQ:
50310Sstevel@tonic-gate 		case T_BIND_REQ:
50320Sstevel@tonic-gate 			qwriter(q, mp, icmp_bind, PERIM_OUTER);
50330Sstevel@tonic-gate 			return;
50340Sstevel@tonic-gate 		case T_CONN_REQ:
50350Sstevel@tonic-gate 			icmp_connect(q, mp);
50360Sstevel@tonic-gate 			return;
50370Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
50380Sstevel@tonic-gate 			icmp_capability_req(q, mp);
50390Sstevel@tonic-gate 			return;
50400Sstevel@tonic-gate 		case T_INFO_REQ:
50410Sstevel@tonic-gate 			icmp_info_req(q, mp);
50420Sstevel@tonic-gate 			return;
50430Sstevel@tonic-gate 		case T_UNITDATA_REQ:
50440Sstevel@tonic-gate 			/*
50450Sstevel@tonic-gate 			 * If a T_UNITDATA_REQ gets here, the address must
50460Sstevel@tonic-gate 			 * be bad.  Valid T_UNITDATA_REQs are found above
50470Sstevel@tonic-gate 			 * and break to below this switch.
50480Sstevel@tonic-gate 			 */
50490Sstevel@tonic-gate 			icmp_ud_err(q, mp, EADDRNOTAVAIL);
50500Sstevel@tonic-gate 			return;
50510Sstevel@tonic-gate 		case T_UNBIND_REQ:
50520Sstevel@tonic-gate 			icmp_unbind(q, mp);
50530Sstevel@tonic-gate 			return;
50540Sstevel@tonic-gate 
50550Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
50560Sstevel@tonic-gate 			if (!snmpcom_req(q, mp, icmp_snmp_set, icmp_snmp_get,
50570Sstevel@tonic-gate 			    cr))
50580Sstevel@tonic-gate 				/* Only IP can return anything meaningful */
50590Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
50600Sstevel@tonic-gate 				    &icmp_opt_obj);
50610Sstevel@tonic-gate 			return;
50620Sstevel@tonic-gate 
50630Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
50640Sstevel@tonic-gate 			/* Only IP can return anything meaningful */
50650Sstevel@tonic-gate 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
50660Sstevel@tonic-gate 			return;
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate 		case T_DISCON_REQ:
50690Sstevel@tonic-gate 			icmp_disconnect(q, mp);
50700Sstevel@tonic-gate 			return;
50710Sstevel@tonic-gate 
50720Sstevel@tonic-gate 		/* The following TPI message is not supported by icmp. */
50730Sstevel@tonic-gate 		case O_T_CONN_RES:
50740Sstevel@tonic-gate 		case T_CONN_RES:
50750Sstevel@tonic-gate 			icmp_err_ack(q, mp, TNOTSUPPORT, 0);
50760Sstevel@tonic-gate 			return;
50770Sstevel@tonic-gate 
50780Sstevel@tonic-gate 		/* The following 3 TPI requests are illegal for icmp. */
50790Sstevel@tonic-gate 		case T_DATA_REQ:
50800Sstevel@tonic-gate 		case T_EXDATA_REQ:
50810Sstevel@tonic-gate 		case T_ORDREL_REQ:
50820Sstevel@tonic-gate 			freemsg(mp);
50830Sstevel@tonic-gate 			(void) putctl1(RD(q), M_ERROR, EPROTO);
50840Sstevel@tonic-gate 			return;
50850Sstevel@tonic-gate 		default:
50860Sstevel@tonic-gate 			break;
50870Sstevel@tonic-gate 		}
50880Sstevel@tonic-gate 		break;
50890Sstevel@tonic-gate 	case M_IOCTL:
50900Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
50910Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
50920Sstevel@tonic-gate 		case TI_GETPEERNAME:
50930Sstevel@tonic-gate 			if (icmp->icmp_state != TS_DATA_XFER) {
50940Sstevel@tonic-gate 				/*
50950Sstevel@tonic-gate 				 * If a default destination address has not
50960Sstevel@tonic-gate 				 * been associated with the stream, then we
50970Sstevel@tonic-gate 				 * don't know the peer's name.
50980Sstevel@tonic-gate 				 */
50990Sstevel@tonic-gate 				iocp->ioc_error = ENOTCONN;
51000Sstevel@tonic-gate 			    err_ret:;
51010Sstevel@tonic-gate 				iocp->ioc_count = 0;
51020Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
51030Sstevel@tonic-gate 				qreply(q, mp);
51040Sstevel@tonic-gate 				return;
51050Sstevel@tonic-gate 			}
51060Sstevel@tonic-gate 			/* FALLTHRU */
51070Sstevel@tonic-gate 		case TI_GETMYNAME:
51080Sstevel@tonic-gate 			/*
51090Sstevel@tonic-gate 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
51100Sstevel@tonic-gate 			 * need to copyin the user's strbuf structure.
51110Sstevel@tonic-gate 			 * Processing will continue in the M_IOCDATA case
51120Sstevel@tonic-gate 			 * below.
51130Sstevel@tonic-gate 			 */
51140Sstevel@tonic-gate 			mi_copyin(q, mp, NULL,
51150Sstevel@tonic-gate 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
51160Sstevel@tonic-gate 			return;
51170Sstevel@tonic-gate 		case ND_SET:
51180Sstevel@tonic-gate 			/* nd_getset performs the necessary error checking */
51190Sstevel@tonic-gate 		case ND_GET:
51200Sstevel@tonic-gate 			if (nd_getset(q, icmp_g_nd, mp)) {
51210Sstevel@tonic-gate 				qreply(q, mp);
51220Sstevel@tonic-gate 				return;
51230Sstevel@tonic-gate 			}
51240Sstevel@tonic-gate 			break;
51250Sstevel@tonic-gate 		default:
51260Sstevel@tonic-gate 			break;
51270Sstevel@tonic-gate 		}
51280Sstevel@tonic-gate 		break;
51290Sstevel@tonic-gate 	case M_IOCDATA:
51300Sstevel@tonic-gate 		icmp_wput_iocdata(q, mp);
51310Sstevel@tonic-gate 		return;
51320Sstevel@tonic-gate 	default:
51330Sstevel@tonic-gate 		break;
51340Sstevel@tonic-gate 	}
51350Sstevel@tonic-gate 	putnext(q, mp);
51360Sstevel@tonic-gate }
51370Sstevel@tonic-gate 
51380Sstevel@tonic-gate /*
51390Sstevel@tonic-gate  * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA
51400Sstevel@tonic-gate  * messages.
51410Sstevel@tonic-gate  */
51420Sstevel@tonic-gate static void
51430Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp)
51440Sstevel@tonic-gate {
51450Sstevel@tonic-gate 	mblk_t	*mp1;
51460Sstevel@tonic-gate 	STRUCT_HANDLE(strbuf, sb);
51470Sstevel@tonic-gate 	icmp_t	*icmp;
51480Sstevel@tonic-gate 	in6_addr_t	v6addr;
51490Sstevel@tonic-gate 	ipaddr_t	v4addr;
51500Sstevel@tonic-gate 	uint32_t	flowinfo = 0;
51510Sstevel@tonic-gate 	int		addrlen;
51520Sstevel@tonic-gate 
51530Sstevel@tonic-gate 	/* Make sure it is one of ours. */
51540Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
51550Sstevel@tonic-gate 	case TI_GETMYNAME:
51560Sstevel@tonic-gate 	case TI_GETPEERNAME:
51570Sstevel@tonic-gate 		break;
51580Sstevel@tonic-gate 	default:
51590Sstevel@tonic-gate 		putnext(q, mp);
51600Sstevel@tonic-gate 		return;
51610Sstevel@tonic-gate 	}
51620Sstevel@tonic-gate 	switch (mi_copy_state(q, mp, &mp1)) {
51630Sstevel@tonic-gate 	case -1:
51640Sstevel@tonic-gate 		return;
51650Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_IN, 1):
51660Sstevel@tonic-gate 		break;
51670Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 1):
51680Sstevel@tonic-gate 		/*
51690Sstevel@tonic-gate 		 * The address has been copied out, so now
51700Sstevel@tonic-gate 		 * copyout the strbuf.
51710Sstevel@tonic-gate 		 */
51720Sstevel@tonic-gate 		mi_copyout(q, mp);
51730Sstevel@tonic-gate 		return;
51740Sstevel@tonic-gate 	case MI_COPY_CASE(MI_COPY_OUT, 2):
51750Sstevel@tonic-gate 		/*
51760Sstevel@tonic-gate 		 * The address and strbuf have been copied out.
51770Sstevel@tonic-gate 		 * We're done, so just acknowledge the original
51780Sstevel@tonic-gate 		 * M_IOCTL.
51790Sstevel@tonic-gate 		 */
51800Sstevel@tonic-gate 		mi_copy_done(q, mp, 0);
51810Sstevel@tonic-gate 		return;
51820Sstevel@tonic-gate 	default:
51830Sstevel@tonic-gate 		/*
51840Sstevel@tonic-gate 		 * Something strange has happened, so acknowledge
51850Sstevel@tonic-gate 		 * the original M_IOCTL with an EPROTO error.
51860Sstevel@tonic-gate 		 */
51870Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
51880Sstevel@tonic-gate 		return;
51890Sstevel@tonic-gate 	}
51900Sstevel@tonic-gate 	/*
51910Sstevel@tonic-gate 	 * Now we have the strbuf structure for TI_GETMYNAME
51920Sstevel@tonic-gate 	 * and TI_GETPEERNAME.  Next we copyout the requested
51930Sstevel@tonic-gate 	 * address and then we'll copyout the strbuf.
51940Sstevel@tonic-gate 	 */
51950Sstevel@tonic-gate 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
51960Sstevel@tonic-gate 	    (void *)mp1->b_rptr);
51970Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
51980Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET)
51990Sstevel@tonic-gate 		addrlen = sizeof (sin_t);
52000Sstevel@tonic-gate 	else
52010Sstevel@tonic-gate 		addrlen = sizeof (sin6_t);
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
52040Sstevel@tonic-gate 		mi_copy_done(q, mp, EINVAL);
52050Sstevel@tonic-gate 		return;
52060Sstevel@tonic-gate 	}
52070Sstevel@tonic-gate 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
52080Sstevel@tonic-gate 	case TI_GETMYNAME:
52090Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
52100Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
52110Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) &&
52120Sstevel@tonic-gate 			    !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
52130Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_v6src);
52140Sstevel@tonic-gate 			} else {
52150Sstevel@tonic-gate 				/*
52160Sstevel@tonic-gate 				 * INADDR_ANY
52170Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
52180Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
52190Sstevel@tonic-gate 				 * local address instead (that could
52200Sstevel@tonic-gate 				 * also still be INADDR_ANY)
52210Sstevel@tonic-gate 				 */
52220Sstevel@tonic-gate 				v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src);
52230Sstevel@tonic-gate 			}
52240Sstevel@tonic-gate 		} else {
52250Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6 */
52260Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) {
52270Sstevel@tonic-gate 				v6addr = icmp->icmp_v6src;
52280Sstevel@tonic-gate 			} else {
52290Sstevel@tonic-gate 				/*
52300Sstevel@tonic-gate 				 * UNSPECIFIED
52310Sstevel@tonic-gate 				 * icmp_v6src is not set, we might be bound to
52320Sstevel@tonic-gate 				 * broadcast/multicast. Use icmp_bound_v6src as
52330Sstevel@tonic-gate 				 * local address instead (that could
52340Sstevel@tonic-gate 				 * also still be UNSPECIFIED)
52350Sstevel@tonic-gate 				 */
52360Sstevel@tonic-gate 				v6addr = icmp->icmp_bound_v6src;
52370Sstevel@tonic-gate 			}
52380Sstevel@tonic-gate 		}
52390Sstevel@tonic-gate 		break;
52400Sstevel@tonic-gate 	case TI_GETPEERNAME:
52410Sstevel@tonic-gate 		if (icmp->icmp_family == AF_INET) {
52420Sstevel@tonic-gate 			ASSERT(icmp->icmp_ipversion == IPV4_VERSION);
52430Sstevel@tonic-gate 			v4addr = V4_PART_OF_V6(icmp->icmp_v6dst);
52440Sstevel@tonic-gate 		} else {
52450Sstevel@tonic-gate 			/* icmp->icmp_family == AF_INET6) */
52460Sstevel@tonic-gate 			v6addr = icmp->icmp_v6dst;
52470Sstevel@tonic-gate 			flowinfo = icmp->icmp_flowinfo;
52480Sstevel@tonic-gate 		}
52490Sstevel@tonic-gate 		break;
52500Sstevel@tonic-gate 	default:
52510Sstevel@tonic-gate 		mi_copy_done(q, mp, EPROTO);
52520Sstevel@tonic-gate 		return;
52530Sstevel@tonic-gate 	}
52540Sstevel@tonic-gate 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
52550Sstevel@tonic-gate 	if (!mp1)
52560Sstevel@tonic-gate 		return;
52570Sstevel@tonic-gate 
52580Sstevel@tonic-gate 	if (icmp->icmp_family == AF_INET) {
52590Sstevel@tonic-gate 		sin_t *sin;
52600Sstevel@tonic-gate 
52610Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
52620Sstevel@tonic-gate 		sin = (sin_t *)mp1->b_rptr;
52630Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin[1];
52640Sstevel@tonic-gate 		*sin = sin_null;
52650Sstevel@tonic-gate 		sin->sin_family = AF_INET;
52660Sstevel@tonic-gate 		sin->sin_addr.s_addr = v4addr;
52670Sstevel@tonic-gate 	} else {
52680Sstevel@tonic-gate 		/* icmp->icmp_family == AF_INET6 */
52690Sstevel@tonic-gate 		sin6_t *sin6;
52700Sstevel@tonic-gate 
52710Sstevel@tonic-gate 		ASSERT(icmp->icmp_family == AF_INET6);
52720Sstevel@tonic-gate 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
52730Sstevel@tonic-gate 		sin6 = (sin6_t *)mp1->b_rptr;
52740Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)&sin6[1];
52750Sstevel@tonic-gate 		*sin6 = sin6_null;
52760Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
52770Sstevel@tonic-gate 		sin6->sin6_flowinfo = flowinfo;
52780Sstevel@tonic-gate 		sin6->sin6_addr = v6addr;
52790Sstevel@tonic-gate 	}
52800Sstevel@tonic-gate 	/* Copy out the address */
52810Sstevel@tonic-gate 	mi_copyout(q, mp);
52820Sstevel@tonic-gate }
52830Sstevel@tonic-gate 
52840Sstevel@tonic-gate /*
52850Sstevel@tonic-gate  * Only allow MIB requests and M_FLUSHes to pass.
52860Sstevel@tonic-gate  * All other messages are nacked or dropped.
52870Sstevel@tonic-gate  */
52880Sstevel@tonic-gate static void
52890Sstevel@tonic-gate icmp_wput_restricted(queue_t *q, mblk_t *mp)
52900Sstevel@tonic-gate {
52910Sstevel@tonic-gate 	cred_t	*cr;
52920Sstevel@tonic-gate 	icmp_t	*icmp;
52930Sstevel@tonic-gate 
52940Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
52950Sstevel@tonic-gate 	case M_PROTO:
52960Sstevel@tonic-gate 	case M_PCPROTO:
52970Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
52980Sstevel@tonic-gate 			freemsg(mp);
52990Sstevel@tonic-gate 			return;
53000Sstevel@tonic-gate 		}
53010Sstevel@tonic-gate 		icmp = (icmp_t *)q->q_ptr;
53020Sstevel@tonic-gate 		cr = DB_CREDDEF(mp, icmp->icmp_credp);
53030Sstevel@tonic-gate 
53040Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
53050Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
53060Sstevel@tonic-gate 			if (!snmpcom_req(q, mp,
53070Sstevel@tonic-gate 			    icmp_snmp_set, icmp_snmp_get, cr))
53080Sstevel@tonic-gate 				(void) svr4_optcom_req(q, mp, cr,
53090Sstevel@tonic-gate 				    &icmp_opt_obj);
53100Sstevel@tonic-gate 			return;
53110Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
53120Sstevel@tonic-gate 			(void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
53130Sstevel@tonic-gate 			return;
53140Sstevel@tonic-gate 		default:
53150Sstevel@tonic-gate 			icmp_err_ack(q, mp, TSYSERR, ENOTSUP);
53160Sstevel@tonic-gate 			return;
53170Sstevel@tonic-gate 		}
53180Sstevel@tonic-gate 		/* NOTREACHED */
53190Sstevel@tonic-gate 	case M_IOCTL:
53200Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOTSUP);
53210Sstevel@tonic-gate 		break;
53220Sstevel@tonic-gate 	case M_FLUSH:
53230Sstevel@tonic-gate 		putnext(q, mp);
53240Sstevel@tonic-gate 		break;
53250Sstevel@tonic-gate 	default:
53260Sstevel@tonic-gate 		freemsg(mp);
53270Sstevel@tonic-gate 		break;
53280Sstevel@tonic-gate 	}
53290Sstevel@tonic-gate }
53300Sstevel@tonic-gate 
53310Sstevel@tonic-gate static int
53320Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
53330Sstevel@tonic-gate     void *thisdg_attrs)
53340Sstevel@tonic-gate {
53350Sstevel@tonic-gate 	icmp_t	*icmp;
53360Sstevel@tonic-gate 	struct T_unitdata_req *udreqp;
53370Sstevel@tonic-gate 	int is_absreq_failure;
53380Sstevel@tonic-gate 	cred_t *cr;
53390Sstevel@tonic-gate 
53400Sstevel@tonic-gate 	icmp = (icmp_t *)q->q_ptr;
53410Sstevel@tonic-gate 
53420Sstevel@tonic-gate 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
53430Sstevel@tonic-gate 	*errorp = 0;
53440Sstevel@tonic-gate 
53450Sstevel@tonic-gate 	cr = DB_CREDDEF(mp, icmp->icmp_credp);
53460Sstevel@tonic-gate 
53470Sstevel@tonic-gate 	*errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length,
53480Sstevel@tonic-gate 	    udreqp->OPT_offset, cr, &icmp_opt_obj,
53490Sstevel@tonic-gate 	    thisdg_attrs, &is_absreq_failure);
53500Sstevel@tonic-gate 
53510Sstevel@tonic-gate 	if (*errorp != 0) {
53520Sstevel@tonic-gate 		/*
53530Sstevel@tonic-gate 		 * Note: No special action needed in this
53540Sstevel@tonic-gate 		 * module for "is_absreq_failure"
53550Sstevel@tonic-gate 		 */
53560Sstevel@tonic-gate 		return (-1);		/* failure */
53570Sstevel@tonic-gate 	}
53580Sstevel@tonic-gate 	ASSERT(is_absreq_failure == 0);
53590Sstevel@tonic-gate 	return (0);	/* success */
53600Sstevel@tonic-gate }
53610Sstevel@tonic-gate 
53620Sstevel@tonic-gate void
53630Sstevel@tonic-gate icmp_ddi_init(void)
53640Sstevel@tonic-gate {
53650Sstevel@tonic-gate 	ICMP6_MAJ = ddi_name_to_major(ICMP6);
53660Sstevel@tonic-gate 	icmp_max_optsize =
53670Sstevel@tonic-gate 	    optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
53680Sstevel@tonic-gate 		icmp_opt_obj.odb_opt_arr_cnt);
53690Sstevel@tonic-gate 
53700Sstevel@tonic-gate 	(void) icmp_param_register(icmp_param_arr, A_CNT(icmp_param_arr));
53710Sstevel@tonic-gate 
53720Sstevel@tonic-gate 	rawip_kstat_init();
53730Sstevel@tonic-gate }
53740Sstevel@tonic-gate 
53750Sstevel@tonic-gate void
53760Sstevel@tonic-gate icmp_ddi_destroy(void)
53770Sstevel@tonic-gate {
53780Sstevel@tonic-gate 	nd_free(&icmp_g_nd);
53790Sstevel@tonic-gate 
53800Sstevel@tonic-gate 	rawip_kstat_fini();
53810Sstevel@tonic-gate }
53820Sstevel@tonic-gate 
53830Sstevel@tonic-gate static void
53840Sstevel@tonic-gate rawip_kstat_init(void) {
53850Sstevel@tonic-gate 
53860Sstevel@tonic-gate 	rawip_named_kstat_t template = {
53870Sstevel@tonic-gate 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
53880Sstevel@tonic-gate 		{ "inCksumErrs",	KSTAT_DATA_UINT32, 0 },
53890Sstevel@tonic-gate 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
53900Sstevel@tonic-gate 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
53910Sstevel@tonic-gate 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
53920Sstevel@tonic-gate 	};
53930Sstevel@tonic-gate 
53940Sstevel@tonic-gate 	rawip_mibkp = kstat_create("icmp", 0, "rawip", "mib2",
53950Sstevel@tonic-gate 					KSTAT_TYPE_NAMED,
53960Sstevel@tonic-gate 					NUM_OF_FIELDS(rawip_named_kstat_t),
53970Sstevel@tonic-gate 					0);
53980Sstevel@tonic-gate 	if (rawip_mibkp == NULL)
53990Sstevel@tonic-gate 		return;
54000Sstevel@tonic-gate 
54010Sstevel@tonic-gate 	bcopy(&template, rawip_mibkp->ks_data, sizeof (template));
54020Sstevel@tonic-gate 
54030Sstevel@tonic-gate 	rawip_mibkp->ks_update = rawip_kstat_update;
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate 	kstat_install(rawip_mibkp);
54060Sstevel@tonic-gate }
54070Sstevel@tonic-gate 
54080Sstevel@tonic-gate static void
54090Sstevel@tonic-gate rawip_kstat_fini(void) {
54100Sstevel@tonic-gate 	if (rawip_mibkp) {
54110Sstevel@tonic-gate 		kstat_delete(rawip_mibkp);
54120Sstevel@tonic-gate 		rawip_mibkp = NULL;
54130Sstevel@tonic-gate 	}
54140Sstevel@tonic-gate }
54150Sstevel@tonic-gate 
54160Sstevel@tonic-gate static int
54170Sstevel@tonic-gate rawip_kstat_update(kstat_t *kp, int rw) {
54180Sstevel@tonic-gate 	rawip_named_kstat_t *rawipkp;
54190Sstevel@tonic-gate 
54200Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
54210Sstevel@tonic-gate 		return (EIO);
54220Sstevel@tonic-gate 
54230Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
54240Sstevel@tonic-gate 		return (EACCES);
54250Sstevel@tonic-gate 
54260Sstevel@tonic-gate 	rawipkp = (rawip_named_kstat_t *)kp->ks_data;
54270Sstevel@tonic-gate 
54280Sstevel@tonic-gate 	rawipkp->inDatagrams.value.ui32 =	rawip_mib.rawipInDatagrams;
54290Sstevel@tonic-gate 	rawipkp->inCksumErrs.value.ui32 =	rawip_mib.rawipInCksumErrs;
54300Sstevel@tonic-gate 	rawipkp->inErrors.value.ui32 =		rawip_mib.rawipInErrors;
54310Sstevel@tonic-gate 	rawipkp->outDatagrams.value.ui32 =	rawip_mib.rawipOutDatagrams;
54320Sstevel@tonic-gate 	rawipkp->outErrors.value.ui32 =		rawip_mib.rawipOutErrors;
54330Sstevel@tonic-gate 
54340Sstevel@tonic-gate 	return (0);
54350Sstevel@tonic-gate }
5436