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