xref: /titanic_50/usr/src/uts/common/inet/ip/ip6.c (revision 657b1f3d64bcf8eaa2385dba72a6047f089433b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 1990 Mentat Inc.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/sysmacros.h>
36 #include <sys/strsun.h>
37 #include <sys/strlog.h>
38 #include <sys/strsubr.h>
39 #define	_SUN_TPI_VERSION	2
40 #include <sys/tihdr.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/cmn_err.h>
44 #include <sys/debug.h>
45 #include <sys/sdt.h>
46 #include <sys/kobj.h>
47 #include <sys/zone.h>
48 #include <sys/neti.h>
49 #include <sys/hook.h>
50 
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/vtrace.h>
56 #include <sys/isa_defs.h>
57 #include <sys/atomic.h>
58 #include <sys/iphada.h>
59 #include <sys/policy.h>
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <net/if_dl.h>
64 #include <sys/sockio.h>
65 #include <netinet/in.h>
66 #include <netinet/ip6.h>
67 #include <netinet/icmp6.h>
68 #include <netinet/sctp.h>
69 
70 #include <inet/common.h>
71 #include <inet/mi.h>
72 #include <inet/mib2.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 
76 #include <inet/ip.h>
77 #include <inet/ip_impl.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/tcp_impl.h>
82 #include <inet/udp_impl.h>
83 #include <inet/ipp_common.h>
84 
85 #include <inet/ip_multi.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_ire.h>
88 #include <inet/ip_rts.h>
89 #include <inet/optcom.h>
90 #include <inet/ip_ndp.h>
91 #include <net/pfkeyv2.h>
92 #include <inet/ipsec_info.h>
93 #include <inet/sadb.h>
94 #include <inet/ipsec_impl.h>
95 #include <inet/tun.h>
96 #include <inet/sctp_ip.h>
97 #include <sys/pattr.h>
98 #include <inet/ipclassifier.h>
99 #include <inet/ipsecah.h>
100 #include <inet/udp_impl.h>
101 #include <sys/squeue.h>
102 
103 #include <sys/tsol/label.h>
104 #include <sys/tsol/tnet.h>
105 
106 #include <rpc/pmap_prot.h>
107 
108 /* Temporary; for CR 6451644 work-around */
109 #include <sys/ethernet.h>
110 
111 extern squeue_func_t ip_input_proc;
112 
113 /*
114  * IP statistics.
115  */
116 #define	IP6_STAT(x)		(ip6_statistics.x.value.ui64++)
117 #define	IP6_STAT_UPDATE(x, n)	(ip6_statistics.x.value.ui64 += (n))
118 
119 typedef struct ip6_stat {
120 	kstat_named_t	ip6_udp_fast_path;
121 	kstat_named_t	ip6_udp_slow_path;
122 	kstat_named_t	ip6_udp_fannorm;
123 	kstat_named_t	ip6_udp_fanmb;
124 	kstat_named_t   ip6_out_sw_cksum;
125 	kstat_named_t   ip6_in_sw_cksum;
126 	kstat_named_t	ip6_tcp_in_full_hw_cksum_err;
127 	kstat_named_t	ip6_tcp_in_part_hw_cksum_err;
128 	kstat_named_t	ip6_tcp_in_sw_cksum_err;
129 	kstat_named_t	ip6_tcp_out_sw_cksum_bytes;
130 	kstat_named_t	ip6_udp_in_full_hw_cksum_err;
131 	kstat_named_t	ip6_udp_in_part_hw_cksum_err;
132 	kstat_named_t	ip6_udp_in_sw_cksum_err;
133 	kstat_named_t	ip6_udp_out_sw_cksum_bytes;
134 	kstat_named_t	ip6_frag_mdt_pkt_out;
135 	kstat_named_t	ip6_frag_mdt_discarded;
136 	kstat_named_t	ip6_frag_mdt_allocfail;
137 	kstat_named_t	ip6_frag_mdt_addpdescfail;
138 	kstat_named_t	ip6_frag_mdt_allocd;
139 } ip6_stat_t;
140 
141 static ip6_stat_t ip6_statistics = {
142 	{ "ip6_udp_fast_path",			KSTAT_DATA_UINT64 },
143 	{ "ip6_udp_slow_path",			KSTAT_DATA_UINT64 },
144 	{ "ip6_udp_fannorm",			KSTAT_DATA_UINT64 },
145 	{ "ip6_udp_fanmb",			KSTAT_DATA_UINT64 },
146 	{ "ip6_out_sw_cksum",			KSTAT_DATA_UINT64 },
147 	{ "ip6_in_sw_cksum",			KSTAT_DATA_UINT64 },
148 	{ "ip6_tcp_in_full_hw_cksum_err",	KSTAT_DATA_UINT64 },
149 	{ "ip6_tcp_in_part_hw_cksum_err",	KSTAT_DATA_UINT64 },
150 	{ "ip6_tcp_in_sw_cksum_err",		KSTAT_DATA_UINT64 },
151 	{ "ip6_tcp_out_sw_cksum_bytes",		KSTAT_DATA_UINT64 },
152 	{ "ip6_udp_in_full_hw_cksum_err",	KSTAT_DATA_UINT64 },
153 	{ "ip6_udp_in_part_hw_cksum_err",	KSTAT_DATA_UINT64 },
154 	{ "ip6_udp_in_sw_cksum_err",		KSTAT_DATA_UINT64 },
155 	{ "ip6_udp_out_sw_cksum_bytes",		KSTAT_DATA_UINT64 },
156 	{ "ip6_frag_mdt_pkt_out",		KSTAT_DATA_UINT64 },
157 	{ "ip6_frag_mdt_discarded",		KSTAT_DATA_UINT64 },
158 	{ "ip6_frag_mdt_allocfail",		KSTAT_DATA_UINT64 },
159 	{ "ip6_frag_mdt_addpdescfail",		KSTAT_DATA_UINT64 },
160 	{ "ip6_frag_mdt_allocd",		KSTAT_DATA_UINT64 },
161 };
162 
163 static kstat_t *ip6_kstat;
164 
165 /*
166  * Naming conventions:
167  *      These rules should be judiciously applied
168  *	if there is a need to identify something as IPv6 versus IPv4
169  *	IPv6 funcions will end with _v6 in the ip module.
170  *	IPv6 funcions will end with _ipv6 in the transport modules.
171  *	IPv6 macros:
172  *		Some macros end with _V6; e.g. ILL_FRAG_HASH_V6
173  *		Some macros start with V6_; e.g. V6_OR_V4_INADDR_ANY
174  *		And then there are ..V4_PART_OF_V6.
175  *		The intent is that macros in the ip module end with _V6.
176  *	IPv6 global variables will start with ipv6_
177  *	IPv6 structures will start with ipv6
178  *	IPv6 defined constants should start with IPV6_
179  *		(but then there are NDP_DEFAULT_VERS_PRI_AND_FLOW, etc)
180  */
181 
182 /*
183  * IPv6 mibs when the interface (ill) is not known.
184  * When the ill is known the per-interface mib in the ill is used.
185  */
186 mib2_ipv6IfStatsEntry_t	ip6_mib;
187 mib2_ipv6IfIcmpEntry_t	icmp6_mib;
188 
189 /*
190  * ip6opt_ls is used to enable IPv6 (via /etc/system on TX systems).
191  * We need to do this because we didn't obtain the IP6OPT_LS (0x0a)
192  * from IANA. This mechanism will remain in effect until an official
193  * number is obtained.
194  */
195 uchar_t ip6opt_ls;
196 
197 uint_t ipv6_ire_default_count;	/* Number of IPv6 IRE_DEFAULT entries */
198 uint_t ipv6_ire_default_index;	/* Walking IPv6 index used to mod in */
199 
200 const in6_addr_t ipv6_all_ones =
201 	{ 0xffffffffU, 0xffffffffU, 0xffffffffU, 0xffffffffU };
202 const in6_addr_t ipv6_all_zeros = { 0, 0, 0, 0 };
203 
204 #ifdef	_BIG_ENDIAN
205 const in6_addr_t ipv6_unspecified_group = { 0xff000000U, 0, 0, 0 };
206 #else	/* _BIG_ENDIAN */
207 const in6_addr_t ipv6_unspecified_group = { 0x000000ffU, 0, 0, 0 };
208 #endif	/* _BIG_ENDIAN */
209 
210 #ifdef	_BIG_ENDIAN
211 const in6_addr_t ipv6_loopback = { 0, 0, 0, 0x00000001U };
212 #else  /* _BIG_ENDIAN */
213 const in6_addr_t ipv6_loopback = { 0, 0, 0, 0x01000000U };
214 #endif /* _BIG_ENDIAN */
215 
216 #ifdef _BIG_ENDIAN
217 const in6_addr_t ipv6_all_hosts_mcast = { 0xff020000U, 0, 0, 0x00000001U };
218 #else  /* _BIG_ENDIAN */
219 const in6_addr_t ipv6_all_hosts_mcast = { 0x000002ffU, 0, 0, 0x01000000U };
220 #endif /* _BIG_ENDIAN */
221 
222 #ifdef _BIG_ENDIAN
223 const in6_addr_t ipv6_all_rtrs_mcast = { 0xff020000U, 0, 0, 0x00000002U };
224 #else  /* _BIG_ENDIAN */
225 const in6_addr_t ipv6_all_rtrs_mcast = { 0x000002ffU, 0, 0, 0x02000000U };
226 #endif /* _BIG_ENDIAN */
227 
228 #ifdef _BIG_ENDIAN
229 const in6_addr_t ipv6_all_v2rtrs_mcast = { 0xff020000U, 0, 0, 0x00000016U };
230 #else  /* _BIG_ENDIAN */
231 const in6_addr_t ipv6_all_v2rtrs_mcast = { 0x000002ffU, 0, 0, 0x16000000U };
232 #endif /* _BIG_ENDIAN */
233 
234 #ifdef _BIG_ENDIAN
235 const in6_addr_t ipv6_solicited_node_mcast =
236 			{ 0xff020000U, 0, 0x00000001U, 0xff000000U };
237 #else  /* _BIG_ENDIAN */
238 const in6_addr_t ipv6_solicited_node_mcast =
239 			{ 0x000002ffU, 0, 0x01000000U, 0x000000ffU };
240 #endif /* _BIG_ENDIAN */
241 
242 /*
243  * Used by icmp_send_redirect_v6 for picking random src.
244  */
245 uint_t	icmp_redirect_v6_src_index;
246 
247 /* Leave room for ip_newroute to tack on the src and target addresses */
248 #define	OK_RESOLVER_MP_V6(mp)						\
249 		((mp) && ((mp)->b_wptr - (mp)->b_rptr) >= (2 * IPV6_ADDR_LEN))
250 
251 static void	icmp_inbound_too_big_v6(queue_t *, mblk_t *, ill_t *ill,
252     boolean_t, zoneid_t);
253 static void	icmp_pkt_v6(queue_t *, mblk_t *, void *, size_t,
254     const in6_addr_t *, boolean_t, zoneid_t);
255 static void	icmp_redirect_v6(queue_t *, mblk_t *, ill_t *ill);
256 static int	ip_bind_connected_v6(conn_t *, mblk_t *, in6_addr_t *,
257     uint16_t, const in6_addr_t *, ip6_pkt_t *, uint16_t,
258     boolean_t, boolean_t, boolean_t, boolean_t);
259 static boolean_t ip_bind_insert_ire_v6(mblk_t *, ire_t *, const in6_addr_t *,
260     iulp_t *);
261 static int	ip_bind_laddr_v6(conn_t *, mblk_t *, const in6_addr_t *,
262     uint16_t, boolean_t, boolean_t, boolean_t);
263 static void	ip_fanout_proto_v6(queue_t *, mblk_t *, ip6_t *, ill_t *,
264     ill_t *, uint8_t, uint_t, uint_t, boolean_t, zoneid_t);
265 static void	ip_fanout_tcp_v6(queue_t *, mblk_t *, ip6_t *, ill_t *,
266     ill_t *, uint_t, uint_t, boolean_t, zoneid_t);
267 static void	ip_fanout_udp_v6(queue_t *, mblk_t *, ip6_t *, uint32_t,
268     ill_t *, ill_t *, uint_t, boolean_t, zoneid_t);
269 static int	ip_process_options_v6(queue_t *, mblk_t *, ip6_t *,
270     uint8_t *, uint_t, uint8_t);
271 static mblk_t	*ip_rput_frag_v6(queue_t *, mblk_t *, ip6_t *,
272     ip6_frag_t *, uint_t, uint_t *, uint32_t *, uint16_t *);
273 static boolean_t	ip_source_routed_v6(ip6_t *, mblk_t *);
274 static void	ip_wput_ire_v6(queue_t *, mblk_t *, ire_t *, int, int,
275     conn_t *, int, int, int, zoneid_t);
276 static boolean_t ip_ulp_cando_pkt2big(int);
277 
278 void ip_rput_v6(queue_t *, mblk_t *);
279 static void ip_wput_v6(queue_t *, mblk_t *);
280 
281 /*
282  * A template for an IPv6 AR_ENTRY_QUERY
283  */
284 static areq_t	ipv6_areq_template = {
285 	AR_ENTRY_QUERY,				/* cmd */
286 	sizeof (areq_t)+(2*IPV6_ADDR_LEN),	/* name offset */
287 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
288 	IP6_DL_SAP,		/* protocol, from arps perspective */
289 	sizeof (areq_t),	/* target addr offset */
290 	IPV6_ADDR_LEN,		/* target addr_length */
291 	0,			/* flags */
292 	sizeof (areq_t) + IPV6_ADDR_LEN,	/* sender addr offset */
293 	IPV6_ADDR_LEN,		/* sender addr length */
294 	6,			/* xmit_count */
295 	1000,			/* (re)xmit_interval in milliseconds */
296 	4			/* max # of requests to buffer */
297 	/* anything else filled in by the code */
298 };
299 
300 struct qinit rinit_ipv6 = {
301 	(pfi_t)ip_rput_v6,
302 	NULL,
303 	ip_open,
304 	ip_close,
305 	NULL,
306 	&ip_mod_info
307 };
308 
309 struct qinit winit_ipv6 = {
310 	(pfi_t)ip_wput_v6,
311 	(pfi_t)ip_wsrv,
312 	ip_open,
313 	ip_close,
314 	NULL,
315 	&ip_mod_info
316 };
317 
318 /*
319  * Handle IPv6 ICMP packets sent to us.  Consume the mblk passed in.
320  * The message has already been checksummed and if needed,
321  * a copy has been made to be sent any interested ICMP client (conn)
322  * Note that this is different than icmp_inbound() which does the fanout
323  * to conn's as well as local processing of the ICMP packets.
324  *
325  * All error messages are passed to the matching transport stream.
326  *
327  * Zones notes:
328  * The packet is only processed in the context of the specified zone: typically
329  * only this zone will reply to an echo request. This means that the caller must
330  * call icmp_inbound_v6() for each relevant zone.
331  */
332 static void
333 icmp_inbound_v6(queue_t *q, mblk_t *mp, ill_t *ill, uint_t hdr_length,
334     boolean_t mctl_present, uint_t flags, zoneid_t zoneid, mblk_t *dl_mp)
335 {
336 	icmp6_t		*icmp6;
337 	ip6_t		*ip6h;
338 	boolean_t	interested;
339 	ip6i_t		*ip6i;
340 	in6_addr_t	origsrc;
341 	ire_t		*ire;
342 	mblk_t		*first_mp;
343 	ipsec_in_t	*ii;
344 
345 	ASSERT(ill != NULL);
346 	first_mp = mp;
347 	if (mctl_present) {
348 		mp = first_mp->b_cont;
349 		ASSERT(mp != NULL);
350 
351 		ii = (ipsec_in_t *)first_mp->b_rptr;
352 		ASSERT(ii->ipsec_in_type == IPSEC_IN);
353 	}
354 
355 	ip6h = (ip6_t *)mp->b_rptr;
356 
357 	BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInMsgs);
358 
359 	if ((mp->b_wptr - mp->b_rptr) < (hdr_length + ICMP6_MINLEN)) {
360 		if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) {
361 			ip1dbg(("icmp_inbound_v6: pullupmsg failed\n"));
362 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
363 			freemsg(first_mp);
364 			return;
365 		}
366 		ip6h = (ip6_t *)mp->b_rptr;
367 	}
368 	if (icmp_accept_clear_messages == 0) {
369 		first_mp = ipsec_check_global_policy(first_mp, NULL,
370 		    NULL, ip6h, mctl_present);
371 		if (first_mp == NULL)
372 			return;
373 	}
374 
375 	/*
376 	 * On a labeled system, we have to check whether the zone itself is
377 	 * permitted to receive raw traffic.
378 	 */
379 	if (is_system_labeled()) {
380 		if (zoneid == ALL_ZONES)
381 			zoneid = tsol_packet_to_zoneid(mp);
382 		if (!tsol_can_accept_raw(mp, B_FALSE)) {
383 			ip1dbg(("icmp_inbound_v6: zone %d can't receive raw",
384 			    zoneid));
385 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
386 			freemsg(first_mp);
387 			return;
388 		}
389 	}
390 
391 	icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]);
392 	ip2dbg(("icmp_inbound_v6: type %d code %d\n", icmp6->icmp6_type,
393 	    icmp6->icmp6_code));
394 	interested = !(icmp6->icmp6_type & ICMP6_INFOMSG_MASK);
395 
396 	/* Initiate IPPF processing here */
397 	if (IP6_IN_IPP(flags)) {
398 
399 		/*
400 		 * If the ifindex changes due to SIOCSLIFINDEX
401 		 * packet may return to IP on the wrong ill.
402 		 */
403 		ip_process(IPP_LOCAL_IN, &mp, ill->ill_phyint->phyint_ifindex);
404 		if (mp == NULL) {
405 			if (mctl_present) {
406 				freeb(first_mp);
407 			}
408 			return;
409 		}
410 	}
411 
412 	switch (icmp6->icmp6_type) {
413 	case ICMP6_DST_UNREACH:
414 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInDestUnreachs);
415 		if (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN)
416 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInAdminProhibs);
417 		break;
418 
419 	case ICMP6_TIME_EXCEEDED:
420 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInTimeExcds);
421 		break;
422 
423 	case ICMP6_PARAM_PROB:
424 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInParmProblems);
425 		break;
426 
427 	case ICMP6_PACKET_TOO_BIG:
428 		icmp_inbound_too_big_v6(q, first_mp, ill, mctl_present,
429 		    zoneid);
430 		return;
431 	case ICMP6_ECHO_REQUEST:
432 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInEchos);
433 		if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) &&
434 		    !ipv6_resp_echo_mcast)
435 			break;
436 
437 		/*
438 		 * We must have exclusive use of the mblk to convert it to
439 		 * a response.
440 		 * If not, we copy it.
441 		 */
442 		if (mp->b_datap->db_ref > 1) {
443 			mblk_t	*mp1;
444 
445 			mp1 = copymsg(mp);
446 			freemsg(mp);
447 			if (mp1 == NULL) {
448 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
449 				if (mctl_present)
450 					freeb(first_mp);
451 				return;
452 			}
453 			mp = mp1;
454 			ip6h = (ip6_t *)mp->b_rptr;
455 			icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]);
456 			if (mctl_present)
457 				first_mp->b_cont = mp;
458 			else
459 				first_mp = mp;
460 		}
461 
462 		/*
463 		 * Turn the echo into an echo reply.
464 		 * Remove any extension headers (do not reverse a source route)
465 		 * and clear the flow id (keep traffic class for now).
466 		 */
467 		if (hdr_length != IPV6_HDR_LEN) {
468 			int	i;
469 
470 			for (i = 0; i < IPV6_HDR_LEN; i++)
471 				mp->b_rptr[hdr_length - i - 1] =
472 				    mp->b_rptr[IPV6_HDR_LEN - i - 1];
473 			mp->b_rptr += (hdr_length - IPV6_HDR_LEN);
474 			ip6h = (ip6_t *)mp->b_rptr;
475 			ip6h->ip6_nxt = IPPROTO_ICMPV6;
476 			hdr_length = IPV6_HDR_LEN;
477 		}
478 		ip6h->ip6_vcf &= ~IPV6_FLOWINFO_FLOWLABEL;
479 		icmp6->icmp6_type = ICMP6_ECHO_REPLY;
480 
481 		ip6h->ip6_plen =
482 		    htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN));
483 		origsrc = ip6h->ip6_src;
484 		/*
485 		 * Reverse the source and destination addresses.
486 		 * If the return address is a multicast, zero out the source
487 		 * (ip_wput_v6 will set an address).
488 		 */
489 		if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
490 			ip6h->ip6_src = ipv6_all_zeros;
491 			ip6h->ip6_dst = origsrc;
492 		} else {
493 			ip6h->ip6_src = ip6h->ip6_dst;
494 			ip6h->ip6_dst = origsrc;
495 		}
496 
497 		/* set the hop limit */
498 		ip6h->ip6_hops = ipv6_def_hops;
499 
500 		/*
501 		 * Prepare for checksum by putting icmp length in the icmp
502 		 * checksum field. The checksum is calculated in ip_wput_v6.
503 		 */
504 		icmp6->icmp6_cksum = ip6h->ip6_plen;
505 		/*
506 		 * ICMP echo replies should go out on the same interface
507 		 * the request came on as probes used by in.mpathd for
508 		 * detecting NIC failures are ECHO packets. We turn-off load
509 		 * spreading by allocating a ip6i and setting ip6i_attach_if
510 		 * to B_TRUE which is handled both by ip_wput_v6 and
511 		 * ip_newroute_v6. If we don't turnoff load spreading,
512 		 * the packets might get dropped if there are no
513 		 * non-FAILED/INACTIVE interfaces for it to go out on and
514 		 * in.mpathd would wrongly detect a failure or mis-detect
515 		 * a NIC failure as a link failure. As load spreading can
516 		 * happen only if ill_group is not NULL, we do only for
517 		 * that case and this does not affect the normal case.
518 		 *
519 		 * We force this only on echo packets that came from on-link
520 		 * hosts. We restrict this to link-local addresses which
521 		 * is used by in.mpathd for probing. In the IPv6 case,
522 		 * default routes typically have an ire_ipif pointer and
523 		 * hence a MATCH_IRE_ILL later in ip_newroute_v6/ip_wput_v6
524 		 * might work. As a default route out of this interface
525 		 * may not be present, enforcing this packet to go out in
526 		 * this case may not work.
527 		 */
528 		if (ill->ill_group != NULL &&
529 		    IN6_IS_ADDR_LINKLOCAL(&origsrc)) {
530 			/*
531 			 * If we are sending replies to ourselves, don't
532 			 * set ATTACH_IF as we may not be able to find
533 			 * the IRE_LOCAL on this ill i.e setting ATTACH_IF
534 			 * causes ip_wput_v6 to look for an IRE_LOCAL on
535 			 * "ill" which it may not find and will try to
536 			 * create an IRE_CACHE for our local address. Once
537 			 * we do this, we will try to forward all packets
538 			 * meant to our LOCAL address.
539 			 */
540 			ire = ire_cache_lookup_v6(&ip6h->ip6_dst, ALL_ZONES,
541 			    NULL);
542 			if (ire == NULL || ire->ire_type != IRE_LOCAL) {
543 				mp = ip_add_info_v6(mp, NULL, &ip6h->ip6_dst);
544 				if (mp == NULL) {
545 					BUMP_MIB(ill->ill_icmp6_mib,
546 					    ipv6IfIcmpInErrors);
547 					if (ire != NULL)
548 						ire_refrele(ire);
549 					if (mctl_present)
550 						freeb(first_mp);
551 					return;
552 				} else if (mctl_present) {
553 					first_mp->b_cont = mp;
554 				} else {
555 					first_mp = mp;
556 				}
557 				ip6i = (ip6i_t *)mp->b_rptr;
558 				ip6i->ip6i_flags = IP6I_ATTACH_IF;
559 				ip6i->ip6i_ifindex =
560 				    ill->ill_phyint->phyint_ifindex;
561 			}
562 			if (ire != NULL)
563 				ire_refrele(ire);
564 		}
565 
566 		if (!mctl_present) {
567 			/*
568 			 * This packet should go out the same way as it
569 			 * came in i.e in clear. To make sure that global
570 			 * policy will not be applied to this in ip_wput,
571 			 * we attach a IPSEC_IN mp and clear ipsec_in_secure.
572 			 */
573 			ASSERT(first_mp == mp);
574 			if ((first_mp = ipsec_in_alloc(B_FALSE)) == NULL) {
575 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
576 				freemsg(mp);
577 				return;
578 			}
579 			ii = (ipsec_in_t *)first_mp->b_rptr;
580 
581 			/* This is not a secure packet */
582 			ii->ipsec_in_secure = B_FALSE;
583 			first_mp->b_cont = mp;
584 		}
585 		ii->ipsec_in_zoneid = zoneid;
586 		ASSERT(zoneid != ALL_ZONES);
587 		if (!ipsec_in_to_out(first_mp, NULL, ip6h)) {
588 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
589 			return;
590 		}
591 		put(WR(q), first_mp);
592 		return;
593 
594 	case ICMP6_ECHO_REPLY:
595 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInEchoReplies);
596 		break;
597 
598 	case ND_ROUTER_SOLICIT:
599 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInRouterSolicits);
600 		break;
601 
602 	case ND_ROUTER_ADVERT:
603 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInRouterAdvertisements);
604 		break;
605 
606 	case ND_NEIGHBOR_SOLICIT:
607 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInNeighborSolicits);
608 		if (mctl_present)
609 			freeb(first_mp);
610 		/* XXX may wish to pass first_mp up to ndp_input someday. */
611 		ndp_input(ill, mp, dl_mp);
612 		return;
613 
614 	case ND_NEIGHBOR_ADVERT:
615 		BUMP_MIB(ill->ill_icmp6_mib,
616 		    ipv6IfIcmpInNeighborAdvertisements);
617 		if (mctl_present)
618 			freeb(first_mp);
619 		/* XXX may wish to pass first_mp up to ndp_input someday. */
620 		ndp_input(ill, mp, dl_mp);
621 		return;
622 
623 	case ND_REDIRECT: {
624 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInRedirects);
625 
626 		if (ipv6_ignore_redirect)
627 			break;
628 
629 		/*
630 		 * As there is no upper client to deliver, we don't
631 		 * need the first_mp any more.
632 		 */
633 		if (mctl_present)
634 			freeb(first_mp);
635 		if (!pullupmsg(mp, -1)) {
636 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects);
637 			break;
638 		}
639 		icmp_redirect_v6(q, mp, ill);
640 		return;
641 	}
642 
643 	/*
644 	 * The next three icmp messages will be handled by MLD.
645 	 * Pass all valid MLD packets up to any process(es)
646 	 * listening on a raw ICMP socket. MLD messages are
647 	 * freed by mld_input function.
648 	 */
649 	case MLD_LISTENER_QUERY:
650 	case MLD_LISTENER_REPORT:
651 	case MLD_LISTENER_REDUCTION:
652 		if (mctl_present)
653 			freeb(first_mp);
654 		mld_input(q, mp, ill);
655 		return;
656 	default:
657 		break;
658 	}
659 	if (interested) {
660 		icmp_inbound_error_fanout_v6(q, first_mp, ip6h, icmp6, ill,
661 		    mctl_present, zoneid);
662 	} else {
663 		freemsg(first_mp);
664 	}
665 }
666 
667 /*
668  * Process received IPv6 ICMP Packet too big.
669  * After updating any IRE it does the fanout to any matching transport streams.
670  * Assumes the IPv6 plus ICMPv6 headers have been pulled up but nothing else.
671  */
672 /* ARGSUSED */
673 static void
674 icmp_inbound_too_big_v6(queue_t *q, mblk_t *mp, ill_t *ill,
675     boolean_t mctl_present, zoneid_t zoneid)
676 {
677 	ip6_t		*ip6h;
678 	ip6_t		*inner_ip6h;
679 	icmp6_t		*icmp6;
680 	uint16_t	hdr_length;
681 	uint32_t	mtu;
682 	ire_t		*ire, *first_ire;
683 	mblk_t		*first_mp;
684 
685 	first_mp = mp;
686 	if (mctl_present)
687 		mp = first_mp->b_cont;
688 	/*
689 	 * We must have exclusive use of the mblk to update the MTU
690 	 * in the packet.
691 	 * If not, we copy it.
692 	 *
693 	 * If there's an M_CTL present, we know that allocated first_mp
694 	 * earlier in this function, so we know first_mp has refcnt of one.
695 	 */
696 	ASSERT(!mctl_present || first_mp->b_datap->db_ref == 1);
697 	if (mp->b_datap->db_ref > 1) {
698 		mblk_t	*mp1;
699 
700 		mp1 = copymsg(mp);
701 		freemsg(mp);
702 		if (mp1 == NULL) {
703 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
704 			if (mctl_present)
705 				freeb(first_mp);
706 			return;
707 		}
708 		mp = mp1;
709 		if (mctl_present)
710 			first_mp->b_cont = mp;
711 		else
712 			first_mp = mp;
713 	}
714 	ip6h = (ip6_t *)mp->b_rptr;
715 	if (ip6h->ip6_nxt != IPPROTO_ICMPV6)
716 		hdr_length = ip_hdr_length_v6(mp, ip6h);
717 	else
718 		hdr_length = IPV6_HDR_LEN;
719 
720 	icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]);
721 	ASSERT((size_t)(mp->b_wptr - mp->b_rptr) >= hdr_length + ICMP6_MINLEN);
722 	inner_ip6h = (ip6_t *)&icmp6[1];	/* Packet in error */
723 	if ((uchar_t *)&inner_ip6h[1] > mp->b_wptr) {
724 		if (!pullupmsg(mp, (uchar_t *)&inner_ip6h[1] - mp->b_rptr)) {
725 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
726 			freemsg(first_mp);
727 			return;
728 		}
729 		ip6h = (ip6_t *)mp->b_rptr;
730 		icmp6 = (icmp6_t *)&mp->b_rptr[hdr_length];
731 		inner_ip6h = (ip6_t *)&icmp6[1];
732 	}
733 
734 	/*
735 	 * For link local destinations matching simply on IRE type is not
736 	 * sufficient. Same link local addresses for different ILL's is
737 	 * possible.
738 	 */
739 
740 	if (IN6_IS_ADDR_LINKLOCAL(&inner_ip6h->ip6_dst)) {
741 		first_ire = ire_ctable_lookup_v6(&inner_ip6h->ip6_dst, NULL,
742 		    IRE_CACHE, ill->ill_ipif, ALL_ZONES, NULL,
743 		    MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP);
744 
745 		if (first_ire == NULL) {
746 			if (ip_debug > 2) {
747 				/* ip1dbg */
748 				pr_addr_dbg("icmp_inbound_too_big_v6:"
749 				    "no ire for dst %s\n", AF_INET6,
750 				    &inner_ip6h->ip6_dst);
751 			}
752 			freemsg(first_mp);
753 			return;
754 		}
755 
756 		mtu = ntohl(icmp6->icmp6_mtu);
757 		rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER);
758 		for (ire = first_ire; ire != NULL &&
759 		    IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &inner_ip6h->ip6_dst);
760 		    ire = ire->ire_next) {
761 			mutex_enter(&ire->ire_lock);
762 			if (mtu < IPV6_MIN_MTU) {
763 				ip1dbg(("Received mtu less than IPv6 "
764 				    "min mtu %d: %d\n", IPV6_MIN_MTU, mtu));
765 				mtu = IPV6_MIN_MTU;
766 				/*
767 				 * If an mtu less than IPv6 min mtu is received,
768 				 * we must include a fragment header in
769 				 * subsequent packets.
770 				 */
771 				ire->ire_frag_flag |= IPH_FRAG_HDR;
772 			}
773 			ip1dbg(("Received mtu from router: %d\n", mtu));
774 			ire->ire_max_frag = MIN(ire->ire_max_frag, mtu);
775 			/* Record the new max frag size for the ULP. */
776 			if (ire->ire_frag_flag & IPH_FRAG_HDR) {
777 				/*
778 				 * If we need a fragment header in every packet
779 				 * (above case or multirouting), make sure the
780 				 * ULP takes it into account when computing the
781 				 * payload size.
782 				 */
783 				icmp6->icmp6_mtu = htonl(ire->ire_max_frag -
784 				    sizeof (ip6_frag_t));
785 			} else {
786 				icmp6->icmp6_mtu = htonl(ire->ire_max_frag);
787 			}
788 			mutex_exit(&ire->ire_lock);
789 		}
790 		rw_exit(&first_ire->ire_bucket->irb_lock);
791 		ire_refrele(first_ire);
792 	} else {
793 		irb_t	*irb = NULL;
794 		/*
795 		 * for non-link local destinations we match only on the IRE type
796 		 */
797 		ire = ire_ctable_lookup_v6(&inner_ip6h->ip6_dst, NULL,
798 		    IRE_CACHE, ill->ill_ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE);
799 		if (ire == NULL) {
800 			if (ip_debug > 2) {
801 				/* ip1dbg */
802 				pr_addr_dbg("icmp_inbound_too_big_v6:"
803 				    "no ire for dst %s\n",
804 				    AF_INET6, &inner_ip6h->ip6_dst);
805 			}
806 			freemsg(first_mp);
807 			return;
808 		}
809 		irb = ire->ire_bucket;
810 		ire_refrele(ire);
811 		rw_enter(&irb->irb_lock, RW_READER);
812 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
813 			if (IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6,
814 				&inner_ip6h->ip6_dst)) {
815 				mtu = ntohl(icmp6->icmp6_mtu);
816 				mutex_enter(&ire->ire_lock);
817 				if (mtu < IPV6_MIN_MTU) {
818 					ip1dbg(("Received mtu less than IPv6"
819 						"min mtu %d: %d\n",
820 						IPV6_MIN_MTU, mtu));
821 					mtu = IPV6_MIN_MTU;
822 					/*
823 					 * If an mtu less than IPv6 min mtu is
824 					 * received, we must include a fragment
825 					 * header in subsequent packets.
826 					 */
827 					ire->ire_frag_flag |= IPH_FRAG_HDR;
828 				}
829 
830 				ip1dbg(("Received mtu from router: %d\n", mtu));
831 				ire->ire_max_frag = MIN(ire->ire_max_frag, mtu);
832 				/* Record the new max frag size for the ULP. */
833 				if (ire->ire_frag_flag & IPH_FRAG_HDR) {
834 					/*
835 					 * If we need a fragment header in
836 					 * every packet (above case or
837 					 * multirouting), make sure the ULP
838 					 * takes it into account when computing
839 					 * the payload size.
840 					 */
841 					icmp6->icmp6_mtu =
842 					    htonl(ire->ire_max_frag -
843 					    sizeof (ip6_frag_t));
844 				} else {
845 					icmp6->icmp6_mtu =
846 					    htonl(ire->ire_max_frag);
847 				}
848 				mutex_exit(&ire->ire_lock);
849 			}
850 		}
851 		rw_exit(&irb->irb_lock);
852 	}
853 	icmp_inbound_error_fanout_v6(q, first_mp, ip6h, icmp6, ill,
854 	    mctl_present, zoneid);
855 }
856 
857 static void
858 pkt_too_big(conn_t *connp, void *arg)
859 {
860 	mblk_t *mp;
861 
862 	if (!connp->conn_ipv6_recvpathmtu)
863 		return;
864 
865 	/* create message and drop it on this connections read queue */
866 	if ((mp = dupb((mblk_t *)arg)) == NULL) {
867 		return;
868 	}
869 	mp->b_datap->db_type = M_CTL;
870 
871 	putnext(connp->conn_rq, mp);
872 }
873 
874 /*
875  * Fanout received ICMPv6 error packets to the transports.
876  * Assumes the IPv6 plus ICMPv6 headers have been pulled up but nothing else.
877  */
878 void
879 icmp_inbound_error_fanout_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h,
880     icmp6_t *icmp6, ill_t *ill, boolean_t mctl_present, zoneid_t zoneid)
881 {
882 	uint16_t *up;	/* Pointer to ports in ULP header */
883 	uint32_t ports;	/* reversed ports for fanout */
884 	ip6_t rip6h;	/* With reversed addresses */
885 	uint16_t	hdr_length;
886 	uint8_t		*nexthdrp;
887 	uint8_t		nexthdr;
888 	mblk_t *first_mp;
889 	ipsec_in_t *ii;
890 	tcpha_t	*tcpha;
891 	conn_t	*connp;
892 
893 	first_mp = mp;
894 	if (mctl_present) {
895 		mp = first_mp->b_cont;
896 		ASSERT(mp != NULL);
897 
898 		ii = (ipsec_in_t *)first_mp->b_rptr;
899 		ASSERT(ii->ipsec_in_type == IPSEC_IN);
900 	} else {
901 		ii = NULL;
902 	}
903 
904 	hdr_length = (uint16_t)((uchar_t *)icmp6 - (uchar_t *)ip6h);
905 	ASSERT((size_t)(mp->b_wptr - (uchar_t *)icmp6) >= ICMP6_MINLEN);
906 
907 	/*
908 	 * Need to pullup everything in order to use
909 	 * ip_hdr_length_nexthdr_v6()
910 	 */
911 	if (mp->b_cont != NULL) {
912 		if (!pullupmsg(mp, -1)) {
913 			ip1dbg(("icmp_inbound_error_fanout_v6: "
914 			    "pullupmsg failed\n"));
915 			goto drop_pkt;
916 		}
917 		ip6h = (ip6_t *)mp->b_rptr;
918 		icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]);
919 	}
920 
921 	ip6h = (ip6_t *)&icmp6[1];	/* Packet in error */
922 	if ((uchar_t *)&ip6h[1] > mp->b_wptr)
923 		goto drop_pkt;
924 
925 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp))
926 		goto drop_pkt;
927 	nexthdr = *nexthdrp;
928 
929 	/* Set message type, must be done after pullups */
930 	mp->b_datap->db_type = M_CTL;
931 
932 	if (icmp6->icmp6_type == ICMP6_PACKET_TOO_BIG) {
933 		/*
934 		 * Deliver indication of ICMP6_PACKET_TOO_BIG to interested
935 		 * sockets.
936 		 *
937 		 * Note I don't like walking every connection to deliver
938 		 * this information to a set of listeners.  A separate
939 		 * list could be kept to keep the cost of this down.
940 		 */
941 		ipcl_walk(pkt_too_big, (void *)mp);
942 	}
943 
944 	/* Try to pass the ICMP message to clients who need it */
945 	switch (nexthdr) {
946 	case IPPROTO_UDP: {
947 		/*
948 		 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
949 		 * UDP header to get the port information.
950 		 */
951 		if ((uchar_t *)ip6h + hdr_length + ICMP_MIN_TP_HDR_LEN >
952 		    mp->b_wptr) {
953 			break;
954 		}
955 		/*
956 		 * Attempt to find a client stream based on port.
957 		 * Note that we do a reverse lookup since the header is
958 		 * in the form we sent it out.
959 		 * The rip6h header is only used for the IPCL_UDP_MATCH_V6
960 		 * and we only set the src and dst addresses and nexthdr.
961 		 */
962 		up = (uint16_t *)((uchar_t *)ip6h + hdr_length);
963 		rip6h.ip6_src = ip6h->ip6_dst;
964 		rip6h.ip6_dst = ip6h->ip6_src;
965 		rip6h.ip6_nxt = nexthdr;
966 		((uint16_t *)&ports)[0] = up[1];
967 		((uint16_t *)&ports)[1] = up[0];
968 
969 		ip_fanout_udp_v6(q, first_mp, &rip6h, ports, ill, ill,
970 		    IP6_NO_IPPOLICY, mctl_present, zoneid);
971 		return;
972 	}
973 	case IPPROTO_TCP: {
974 		/*
975 		 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
976 		 * the TCP header to get the port information.
977 		 */
978 		if ((uchar_t *)ip6h + hdr_length + ICMP_MIN_TP_HDR_LEN >
979 		    mp->b_wptr) {
980 			break;
981 		}
982 
983 		/*
984 		 * Attempt to find a client stream based on port.
985 		 * Note that we do a reverse lookup since the header is
986 		 * in the form we sent it out.
987 		 * The rip6h header is only used for the IP_TCP_*MATCH_V6 and
988 		 * we only set the src and dst addresses and nexthdr.
989 		 */
990 
991 		tcpha = (tcpha_t *)((char *)ip6h + hdr_length);
992 		connp = ipcl_tcp_lookup_reversed_ipv6(ip6h, tcpha,
993 		    TCPS_LISTEN, ill->ill_phyint->phyint_ifindex);
994 		if (connp == NULL) {
995 			goto drop_pkt;
996 		}
997 
998 		squeue_fill(connp->conn_sqp, first_mp, tcp_input,
999 		    connp, SQTAG_TCP6_INPUT_ICMP_ERR);
1000 		return;
1001 
1002 	}
1003 	case IPPROTO_SCTP:
1004 		/*
1005 		 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1006 		 * the SCTP header to get the port information.
1007 		 */
1008 		if ((uchar_t *)ip6h + hdr_length + ICMP_MIN_TP_HDR_LEN >
1009 		    mp->b_wptr) {
1010 			break;
1011 		}
1012 
1013 		up = (uint16_t *)((uchar_t *)ip6h + hdr_length);
1014 		((uint16_t *)&ports)[0] = up[1];
1015 		((uint16_t *)&ports)[1] = up[0];
1016 		ip_fanout_sctp(mp, ill, (ipha_t *)ip6h, ports, 0, mctl_present,
1017 		    IP6_NO_IPPOLICY, 0, zoneid);
1018 		return;
1019 	case IPPROTO_ESP:
1020 	case IPPROTO_AH: {
1021 		int ipsec_rc;
1022 
1023 		/*
1024 		 * We need a IPSEC_IN in the front to fanout to AH/ESP.
1025 		 * We will re-use the IPSEC_IN if it is already present as
1026 		 * AH/ESP will not affect any fields in the IPSEC_IN for
1027 		 * ICMP errors. If there is no IPSEC_IN, allocate a new
1028 		 * one and attach it in the front.
1029 		 */
1030 		if (ii != NULL) {
1031 			/*
1032 			 * ip_fanout_proto_again converts the ICMP errors
1033 			 * that come back from AH/ESP to M_DATA so that
1034 			 * if it is non-AH/ESP and we do a pullupmsg in
1035 			 * this function, it would work. Convert it back
1036 			 * to M_CTL before we send up as this is a ICMP
1037 			 * error. This could have been generated locally or
1038 			 * by some router. Validate the inner IPSEC
1039 			 * headers.
1040 			 *
1041 			 * NOTE : ill_index is used by ip_fanout_proto_again
1042 			 * to locate the ill.
1043 			 */
1044 			ASSERT(ill != NULL);
1045 			ii->ipsec_in_ill_index =
1046 			    ill->ill_phyint->phyint_ifindex;
1047 			ii->ipsec_in_rill_index = ii->ipsec_in_ill_index;
1048 			first_mp->b_cont->b_datap->db_type = M_CTL;
1049 		} else {
1050 			/*
1051 			 * IPSEC_IN is not present. We attach a ipsec_in
1052 			 * message and send up to IPSEC for validating
1053 			 * and removing the IPSEC headers. Clear
1054 			 * ipsec_in_secure so that when we return
1055 			 * from IPSEC, we don't mistakenly think that this
1056 			 * is a secure packet came from the network.
1057 			 *
1058 			 * NOTE : ill_index is used by ip_fanout_proto_again
1059 			 * to locate the ill.
1060 			 */
1061 			ASSERT(first_mp == mp);
1062 			first_mp = ipsec_in_alloc(B_FALSE);
1063 			if (first_mp == NULL) {
1064 				freemsg(mp);
1065 				BUMP_MIB(&ip_mib, ipInDiscards);
1066 				return;
1067 			}
1068 			ii = (ipsec_in_t *)first_mp->b_rptr;
1069 
1070 			/* This is not a secure packet */
1071 			ii->ipsec_in_secure = B_FALSE;
1072 			first_mp->b_cont = mp;
1073 			mp->b_datap->db_type = M_CTL;
1074 			ASSERT(ill != NULL);
1075 			ii->ipsec_in_ill_index =
1076 			    ill->ill_phyint->phyint_ifindex;
1077 			ii->ipsec_in_rill_index = ii->ipsec_in_ill_index;
1078 		}
1079 
1080 		if (!ipsec_loaded()) {
1081 			ip_proto_not_sup(q, first_mp, 0, zoneid);
1082 			return;
1083 		}
1084 
1085 		if (nexthdr == IPPROTO_ESP)
1086 			ipsec_rc = ipsecesp_icmp_error(first_mp);
1087 		else
1088 			ipsec_rc = ipsecah_icmp_error(first_mp);
1089 		if (ipsec_rc == IPSEC_STATUS_FAILED)
1090 			return;
1091 
1092 		ip_fanout_proto_again(first_mp, ill, ill, NULL);
1093 		return;
1094 	}
1095 	case IPPROTO_ENCAP:
1096 	case IPPROTO_IPV6:
1097 		if ((uint8_t *)ip6h + hdr_length +
1098 		    (nexthdr == IPPROTO_ENCAP ? sizeof (ipha_t) :
1099 			sizeof (ip6_t)) > mp->b_wptr)
1100 			goto drop_pkt;
1101 
1102 		if (nexthdr == IPPROTO_ENCAP ||
1103 		    !IN6_ARE_ADDR_EQUAL(
1104 			&((ip6_t *)(((uint8_t *)ip6h) + hdr_length))->ip6_src,
1105 			&ip6h->ip6_src) ||
1106 		    !IN6_ARE_ADDR_EQUAL(
1107 			&((ip6_t *)(((uint8_t *)ip6h) + hdr_length))->ip6_dst,
1108 			&ip6h->ip6_dst)) {
1109 			/*
1110 			 * For tunnels that have used IPsec protection,
1111 			 * we need to adjust the MTU to take into account
1112 			 * the IPsec overhead.
1113 			 */
1114 			if (ii != NULL)
1115 				icmp6->icmp6_mtu = htons(
1116 				    ntohs(icmp6->icmp6_mtu) -
1117 					ipsec_in_extra_length(first_mp));
1118 		} else {
1119 			/*
1120 			 * Self-encapsulated case. As in the ipv4 case,
1121 			 * we need to strip the 2nd IP header. Since mp
1122 			 * is already pulled-up, we can simply bcopy
1123 			 * the 3rd header + data over the 2nd header.
1124 			 */
1125 			uint16_t unused_len;
1126 			ip6_t *inner_ip6h = (ip6_t *)
1127 			    ((uchar_t *)ip6h + hdr_length);
1128 
1129 			/*
1130 			 * Make sure we don't do recursion more than once.
1131 			 */
1132 			if (!ip_hdr_length_nexthdr_v6(mp, inner_ip6h,
1133 			    &unused_len, &nexthdrp) ||
1134 			    *nexthdrp == IPPROTO_IPV6) {
1135 				goto drop_pkt;
1136 			}
1137 
1138 			/*
1139 			 * We are about to modify the packet. Make a copy if
1140 			 * someone else has a reference to it.
1141 			 */
1142 			if (DB_REF(mp) > 1) {
1143 				mblk_t	*mp1;
1144 				uint16_t icmp6_offset;
1145 
1146 				mp1 = copymsg(mp);
1147 				if (mp1 == NULL) {
1148 					goto drop_pkt;
1149 				}
1150 				icmp6_offset = (uint16_t)
1151 				    ((uchar_t *)icmp6 - mp->b_rptr);
1152 				freemsg(mp);
1153 				mp = mp1;
1154 
1155 				icmp6 = (icmp6_t *)(mp->b_rptr + icmp6_offset);
1156 				ip6h = (ip6_t *)&icmp6[1];
1157 				inner_ip6h = (ip6_t *)
1158 				    ((uchar_t *)ip6h + hdr_length);
1159 
1160 				if (mctl_present)
1161 					first_mp->b_cont = mp;
1162 				else
1163 					first_mp = mp;
1164 			}
1165 
1166 			/*
1167 			 * Need to set db_type back to M_DATA before
1168 			 * refeeding mp into this function.
1169 			 */
1170 			DB_TYPE(mp) = M_DATA;
1171 
1172 			/*
1173 			 * Copy the 3rd header + remaining data on top
1174 			 * of the 2nd header.
1175 			 */
1176 			bcopy(inner_ip6h, ip6h,
1177 			    mp->b_wptr - (uchar_t *)inner_ip6h);
1178 
1179 			/*
1180 			 * Subtract length of the 2nd header.
1181 			 */
1182 			mp->b_wptr -= hdr_length;
1183 
1184 			/*
1185 			 * Now recurse, and see what I _really_ should be
1186 			 * doing here.
1187 			 */
1188 			icmp_inbound_error_fanout_v6(q, first_mp,
1189 			    (ip6_t *)mp->b_rptr, icmp6, ill, mctl_present,
1190 			    zoneid);
1191 			return;
1192 		}
1193 		/* FALLTHRU */
1194 	default:
1195 		/*
1196 		 * The rip6h header is only used for the lookup and we
1197 		 * only set the src and dst addresses and nexthdr.
1198 		 */
1199 		rip6h.ip6_src = ip6h->ip6_dst;
1200 		rip6h.ip6_dst = ip6h->ip6_src;
1201 		rip6h.ip6_nxt = nexthdr;
1202 		ip_fanout_proto_v6(q, first_mp, &rip6h, ill, ill, nexthdr, 0,
1203 		    IP6_NO_IPPOLICY, mctl_present, zoneid);
1204 		return;
1205 	}
1206 	/* NOTREACHED */
1207 drop_pkt:
1208 	BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
1209 	ip1dbg(("icmp_inbound_error_fanout_v6: drop pkt\n"));
1210 	freemsg(first_mp);
1211 }
1212 
1213 /*
1214  * Process received IPv6 ICMP Redirect messages.
1215  */
1216 /* ARGSUSED */
1217 static void
1218 icmp_redirect_v6(queue_t *q, mblk_t *mp, ill_t *ill)
1219 {
1220 	ip6_t		*ip6h;
1221 	uint16_t	hdr_length;
1222 	nd_redirect_t	*rd;
1223 	ire_t		*ire;
1224 	ire_t		*prev_ire;
1225 	ire_t		*redir_ire;
1226 	in6_addr_t	*src, *dst, *gateway;
1227 	nd_opt_hdr_t	*opt;
1228 	nce_t		*nce;
1229 	int		nce_flags = 0;
1230 	int		err = 0;
1231 	boolean_t	redirect_to_router = B_FALSE;
1232 	int		len;
1233 	int		optlen;
1234 	iulp_t		ulp_info = { 0 };
1235 	ill_t		*prev_ire_ill;
1236 	ipif_t		*ipif;
1237 
1238 	ip6h = (ip6_t *)mp->b_rptr;
1239 	if (ip6h->ip6_nxt != IPPROTO_ICMPV6)
1240 		hdr_length = ip_hdr_length_v6(mp, ip6h);
1241 	else
1242 		hdr_length = IPV6_HDR_LEN;
1243 
1244 	rd = (nd_redirect_t *)&mp->b_rptr[hdr_length];
1245 	len = mp->b_wptr - mp->b_rptr -  hdr_length;
1246 	src = &ip6h->ip6_src;
1247 	dst = &rd->nd_rd_dst;
1248 	gateway = &rd->nd_rd_target;
1249 
1250 	/* Verify if it is a valid redirect */
1251 	if (!IN6_IS_ADDR_LINKLOCAL(src) ||
1252 	    (ip6h->ip6_hops != IPV6_MAX_HOPS) ||
1253 	    (rd->nd_rd_code != 0) ||
1254 	    (len < sizeof (nd_redirect_t)) ||
1255 	    (IN6_IS_ADDR_V4MAPPED(dst)) ||
1256 	    (IN6_IS_ADDR_MULTICAST(dst))) {
1257 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects);
1258 		freemsg(mp);
1259 		return;
1260 	}
1261 
1262 	if (!(IN6_IS_ADDR_LINKLOCAL(gateway) ||
1263 	    IN6_ARE_ADDR_EQUAL(gateway, dst))) {
1264 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects);
1265 		freemsg(mp);
1266 		return;
1267 	}
1268 
1269 	if (len > sizeof (nd_redirect_t)) {
1270 		if (!ndp_verify_optlen((nd_opt_hdr_t *)&rd[1],
1271 		    len - sizeof (nd_redirect_t))) {
1272 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects);
1273 			freemsg(mp);
1274 			return;
1275 		}
1276 	}
1277 
1278 	if (!IN6_ARE_ADDR_EQUAL(gateway, dst)) {
1279 		redirect_to_router = B_TRUE;
1280 		nce_flags |= NCE_F_ISROUTER;
1281 	}
1282 
1283 	/* ipif will be refreleased afterwards */
1284 	ipif = ipif_get_next_ipif(NULL, ill);
1285 	if (ipif == NULL) {
1286 		freemsg(mp);
1287 		return;
1288 	}
1289 
1290 	/*
1291 	 * Verify that the IP source address of the redirect is
1292 	 * the same as the current first-hop router for the specified
1293 	 * ICMP destination address.
1294 	 * Also, Make sure we had a route for the dest in question and
1295 	 * that route was pointing to the old gateway (the source of the
1296 	 * redirect packet.)
1297 	 */
1298 
1299 	prev_ire = ire_route_lookup_v6(dst, 0, src, 0, ipif, NULL,
1300 	    ALL_ZONES, NULL, MATCH_IRE_GW | MATCH_IRE_ILL_GROUP |
1301 	    MATCH_IRE_DEFAULT);
1302 
1303 	/*
1304 	 * Check that
1305 	 *	the redirect was not from ourselves
1306 	 *	old gateway is still directly reachable
1307 	 */
1308 	if (prev_ire == NULL ||
1309 	    prev_ire->ire_type == IRE_LOCAL) {
1310 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects);
1311 		ipif_refrele(ipif);
1312 		goto fail_redirect;
1313 	}
1314 	prev_ire_ill = ire_to_ill(prev_ire);
1315 	ASSERT(prev_ire_ill != NULL);
1316 	if (prev_ire_ill->ill_flags & ILLF_NONUD)
1317 		nce_flags |= NCE_F_NONUD;
1318 
1319 	/*
1320 	 * Should we use the old ULP info to create the new gateway?  From
1321 	 * a user's perspective, we should inherit the info so that it
1322 	 * is a "smooth" transition.  If we do not do that, then new
1323 	 * connections going thru the new gateway will have no route metrics,
1324 	 * which is counter-intuitive to user.  From a network point of
1325 	 * view, this may or may not make sense even though the new gateway
1326 	 * is still directly connected to us so the route metrics should not
1327 	 * change much.
1328 	 *
1329 	 * But if the old ire_uinfo is not initialized, we do another
1330 	 * recursive lookup on the dest using the new gateway.  There may
1331 	 * be a route to that.  If so, use it to initialize the redirect
1332 	 * route.
1333 	 */
1334 	if (prev_ire->ire_uinfo.iulp_set) {
1335 		bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t));
1336 	} else if (redirect_to_router) {
1337 		/*
1338 		 * Only do the following if the redirection is really to
1339 		 * a router.
1340 		 */
1341 		ire_t *tmp_ire;
1342 		ire_t *sire;
1343 
1344 		tmp_ire = ire_ftable_lookup_v6(dst, 0, gateway, 0, NULL, &sire,
1345 		    ALL_ZONES, 0, NULL,
1346 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT));
1347 		if (sire != NULL) {
1348 			bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t));
1349 			ASSERT(tmp_ire != NULL);
1350 			ire_refrele(tmp_ire);
1351 			ire_refrele(sire);
1352 		} else if (tmp_ire != NULL) {
1353 			bcopy(&tmp_ire->ire_uinfo, &ulp_info,
1354 			    sizeof (iulp_t));
1355 			ire_refrele(tmp_ire);
1356 		}
1357 	}
1358 
1359 	optlen = mp->b_wptr - mp->b_rptr -  hdr_length - sizeof (nd_redirect_t);
1360 	opt = (nd_opt_hdr_t *)&rd[1];
1361 	opt = ndp_get_option(opt, optlen, ND_OPT_TARGET_LINKADDR);
1362 	if (opt != NULL) {
1363 		err = ndp_lookup_then_add(ill,
1364 		    (uchar_t *)&opt[1],		/* Link layer address */
1365 		    gateway,
1366 		    &ipv6_all_ones,		/* prefix mask */
1367 		    &ipv6_all_zeros,		/* Mapping mask */
1368 		    0,
1369 		    nce_flags,
1370 		    ND_STALE,
1371 		    &nce,
1372 		    NULL,
1373 		    NULL);
1374 		switch (err) {
1375 		case 0:
1376 			NCE_REFRELE(nce);
1377 			break;
1378 		case EEXIST:
1379 			/*
1380 			 * Check to see if link layer address has changed and
1381 			 * process the nce_state accordingly.
1382 			 */
1383 			ndp_process(nce, (uchar_t *)&opt[1], 0, B_FALSE);
1384 			NCE_REFRELE(nce);
1385 			break;
1386 		default:
1387 			ip1dbg(("icmp_redirect_v6: NCE create failed %d\n",
1388 			    err));
1389 			ipif_refrele(ipif);
1390 			goto fail_redirect;
1391 		}
1392 	}
1393 	if (redirect_to_router) {
1394 		/* icmp_redirect_ok_v6() must  have already verified this  */
1395 		ASSERT(IN6_IS_ADDR_LINKLOCAL(gateway));
1396 
1397 		/*
1398 		 * Create a Route Association.  This will allow us to remember
1399 		 * a router told us to use the particular gateway.
1400 		 */
1401 		ire = ire_create_v6(
1402 		    dst,
1403 		    &ipv6_all_ones,		/* mask */
1404 		    &prev_ire->ire_src_addr_v6,	/* source addr */
1405 		    gateway,			/* gateway addr */
1406 		    &prev_ire->ire_max_frag,	/* max frag */
1407 		    NULL,			/* Fast Path header */
1408 		    NULL, 			/* no rfq */
1409 		    NULL,			/* no stq */
1410 		    IRE_HOST,
1411 		    NULL,
1412 		    prev_ire->ire_ipif,
1413 		    NULL,
1414 		    0,
1415 		    0,
1416 		    (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
1417 		    &ulp_info,
1418 		    NULL,
1419 		    NULL);
1420 	} else {
1421 		queue_t *stq;
1422 
1423 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
1424 		    ? ipif->ipif_rq : ipif->ipif_wq;
1425 
1426 		/*
1427 		 * Just create an on link entry, i.e. interface route.
1428 		 */
1429 		ire = ire_create_v6(
1430 		    dst,				/* gateway == dst */
1431 		    &ipv6_all_ones,			/* mask */
1432 		    &prev_ire->ire_src_addr_v6,		/* source addr */
1433 		    &ipv6_all_zeros,			/* gateway addr */
1434 		    &prev_ire->ire_max_frag,		/* max frag */
1435 		    NULL,				/* Fast Path header */
1436 		    NULL,				/* ire rfq */
1437 		    stq,				/* ire stq */
1438 		    ipif->ipif_net_type,		/* IF_[NO]RESOLVER */
1439 		    NULL,
1440 		    prev_ire->ire_ipif,
1441 		    &ipv6_all_ones,
1442 		    0,
1443 		    0,
1444 		    (RTF_DYNAMIC | RTF_HOST),
1445 		    &ulp_info,
1446 		    NULL,
1447 		    NULL);
1448 	}
1449 
1450 	/* Release reference from earlier ipif_get_next_ipif() */
1451 	ipif_refrele(ipif);
1452 
1453 	if (ire == NULL)
1454 		goto fail_redirect;
1455 
1456 	if (ire_add(&ire, NULL, NULL, NULL, B_FALSE) == 0) {
1457 
1458 		/* tell routing sockets that we received a redirect */
1459 		ip_rts_change_v6(RTM_REDIRECT,
1460 		    &rd->nd_rd_dst,
1461 		    &rd->nd_rd_target,
1462 		    &ipv6_all_ones, 0, &ire->ire_src_addr_v6,
1463 		    (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
1464 		    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR));
1465 
1466 		/*
1467 		 * Delete any existing IRE_HOST type ires for this destination.
1468 		 * This together with the added IRE has the effect of
1469 		 * modifying an existing redirect.
1470 		 */
1471 		redir_ire = ire_ftable_lookup_v6(dst, 0, src, IRE_HOST,
1472 		    ire->ire_ipif, NULL, ALL_ZONES, 0, NULL,
1473 		    (MATCH_IRE_GW | MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP));
1474 
1475 		ire_refrele(ire);		/* Held in ire_add_v6 */
1476 
1477 		if (redir_ire != NULL) {
1478 			if (redir_ire->ire_flags & RTF_DYNAMIC)
1479 				ire_delete(redir_ire);
1480 			ire_refrele(redir_ire);
1481 		}
1482 	}
1483 
1484 	if (prev_ire->ire_type == IRE_CACHE)
1485 		ire_delete(prev_ire);
1486 	ire_refrele(prev_ire);
1487 	prev_ire = NULL;
1488 
1489 fail_redirect:
1490 	if (prev_ire != NULL)
1491 		ire_refrele(prev_ire);
1492 	freemsg(mp);
1493 }
1494 
1495 static ill_t *
1496 ip_queue_to_ill_v6(queue_t *q)
1497 {
1498 	ill_t *ill;
1499 
1500 	ASSERT(WR(q) == q);
1501 
1502 	if (q->q_next != NULL) {
1503 		ill = (ill_t *)q->q_ptr;
1504 		if (ILL_CAN_LOOKUP(ill))
1505 			ill_refhold(ill);
1506 		else
1507 			ill = NULL;
1508 	} else {
1509 		ill = ill_lookup_on_name(ipif_loopback_name, B_FALSE, B_TRUE,
1510 		    NULL, NULL, NULL, NULL, NULL);
1511 	}
1512 	if (ill == NULL)
1513 		ip0dbg(("ip_queue_to_ill_v6: no ill\n"));
1514 	return (ill);
1515 }
1516 
1517 /*
1518  * Assigns an appropriate source address to the packet.
1519  * If origdst is one of our IP addresses that use it as the source.
1520  * If the queue is an ill queue then select a source from that ill.
1521  * Otherwise pick a source based on a route lookup back to the origsrc.
1522  *
1523  * src is the return parameter. Returns a pointer to src or NULL if failure.
1524  */
1525 static in6_addr_t *
1526 icmp_pick_source_v6(queue_t *wq, in6_addr_t *origsrc, in6_addr_t *origdst,
1527     in6_addr_t *src, zoneid_t zoneid)
1528 {
1529 	ill_t	*ill;
1530 	ire_t	*ire;
1531 	ipif_t	*ipif;
1532 
1533 	ASSERT(!(wq->q_flag & QREADR));
1534 	if (wq->q_next != NULL) {
1535 		ill = (ill_t *)wq->q_ptr;
1536 	} else {
1537 		ill = NULL;
1538 	}
1539 
1540 	ire = ire_route_lookup_v6(origdst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK),
1541 	    NULL, NULL, zoneid, NULL, (MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY));
1542 	if (ire != NULL) {
1543 		/* Destined to one of our addresses */
1544 		*src = *origdst;
1545 		ire_refrele(ire);
1546 		return (src);
1547 	}
1548 	if (ire != NULL) {
1549 		ire_refrele(ire);
1550 		ire = NULL;
1551 	}
1552 	if (ill == NULL) {
1553 		/* What is the route back to the original source? */
1554 		ire = ire_route_lookup_v6(origsrc, 0, 0, 0,
1555 		    NULL, NULL, zoneid, NULL,
1556 		    (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE));
1557 		if (ire == NULL) {
1558 			BUMP_MIB(&ip6_mib, ipv6OutNoRoutes);
1559 			return (NULL);
1560 		}
1561 		/*
1562 		 * Does not matter whether we use ire_stq or ire_ipif here.
1563 		 * Just pick an ill for ICMP replies.
1564 		 */
1565 		ASSERT(ire->ire_ipif != NULL);
1566 		ill = ire->ire_ipif->ipif_ill;
1567 		ire_refrele(ire);
1568 	}
1569 	ipif = ipif_select_source_v6(ill, origsrc, RESTRICT_TO_NONE,
1570 	    IPV6_PREFER_SRC_DEFAULT, zoneid);
1571 	if (ipif != NULL) {
1572 		*src = ipif->ipif_v6src_addr;
1573 		ipif_refrele(ipif);
1574 		return (src);
1575 	}
1576 	/*
1577 	 * Unusual case - can't find a usable source address to reach the
1578 	 * original source. Use what in the route to the source.
1579 	 */
1580 	ire = ire_route_lookup_v6(origsrc, 0, 0, 0,
1581 	    NULL, NULL, zoneid, NULL,
1582 	    (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE));
1583 	if (ire == NULL) {
1584 		BUMP_MIB(&ip6_mib, ipv6OutNoRoutes);
1585 		return (NULL);
1586 	}
1587 	ASSERT(ire != NULL);
1588 	*src = ire->ire_src_addr_v6;
1589 	ire_refrele(ire);
1590 	return (src);
1591 }
1592 
1593 /*
1594  * Build and ship an IPv6 ICMP message using the packet data in mp,
1595  * and the ICMP header pointed to by "stuff".  (May be called as
1596  * writer.)
1597  * Note: assumes that icmp_pkt_err_ok_v6 has been called to
1598  * verify that an icmp error packet can be sent.
1599  *
1600  * If q is an ill write side queue (which is the case when packets
1601  * arrive from ip_rput) then ip_wput code will ensure that packets to
1602  * link-local destinations are sent out that ill.
1603  *
1604  * If v6src_ptr is set use it as a source. Otherwise select a reasonable
1605  * source address (see above function).
1606  */
1607 static void
1608 icmp_pkt_v6(queue_t *q, mblk_t *mp, void *stuff, size_t len,
1609     const in6_addr_t *v6src_ptr, boolean_t mctl_present, zoneid_t zoneid)
1610 {
1611 	ip6_t		*ip6h;
1612 	in6_addr_t	v6dst;
1613 	size_t		len_needed;
1614 	size_t		msg_len;
1615 	mblk_t		*mp1;
1616 	icmp6_t		*icmp6;
1617 	ill_t		*ill;
1618 	in6_addr_t	v6src;
1619 	mblk_t *ipsec_mp;
1620 	ipsec_out_t *io;
1621 
1622 	ill = ip_queue_to_ill_v6(q);
1623 	if (ill == NULL) {
1624 		freemsg(mp);
1625 		return;
1626 	}
1627 
1628 	if (mctl_present) {
1629 		/*
1630 		 * If it is :
1631 		 *
1632 		 * 1) a IPSEC_OUT, then this is caused by outbound
1633 		 *    datagram originating on this host. IPSEC processing
1634 		 *    may or may not have been done. Refer to comments above
1635 		 *    icmp_inbound_error_fanout for details.
1636 		 *
1637 		 * 2) a IPSEC_IN if we are generating a icmp_message
1638 		 *    for an incoming datagram destined for us i.e called
1639 		 *    from ip_fanout_send_icmp.
1640 		 */
1641 		ipsec_info_t *in;
1642 
1643 		ipsec_mp = mp;
1644 		mp = ipsec_mp->b_cont;
1645 
1646 		in = (ipsec_info_t *)ipsec_mp->b_rptr;
1647 		ip6h = (ip6_t *)mp->b_rptr;
1648 
1649 		ASSERT(in->ipsec_info_type == IPSEC_OUT ||
1650 		    in->ipsec_info_type == IPSEC_IN);
1651 
1652 		if (in->ipsec_info_type == IPSEC_IN) {
1653 			/*
1654 			 * Convert the IPSEC_IN to IPSEC_OUT.
1655 			 */
1656 			if (!ipsec_in_to_out(ipsec_mp, NULL, ip6h)) {
1657 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
1658 				ill_refrele(ill);
1659 				return;
1660 			}
1661 		} else {
1662 			ASSERT(in->ipsec_info_type == IPSEC_OUT);
1663 			io = (ipsec_out_t *)in;
1664 			/*
1665 			 * Clear out ipsec_out_proc_begin, so we do a fresh
1666 			 * ire lookup.
1667 			 */
1668 			io->ipsec_out_proc_begin = B_FALSE;
1669 		}
1670 	} else {
1671 		/*
1672 		 * This is in clear. The icmp message we are building
1673 		 * here should go out in clear.
1674 		 */
1675 		ipsec_in_t *ii;
1676 		ASSERT(mp->b_datap->db_type == M_DATA);
1677 		if ((ipsec_mp = ipsec_in_alloc(B_FALSE)) == NULL) {
1678 			freemsg(mp);
1679 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
1680 			ill_refrele(ill);
1681 			return;
1682 		}
1683 		ii = (ipsec_in_t *)ipsec_mp->b_rptr;
1684 
1685 		/* This is not a secure packet */
1686 		ii->ipsec_in_secure = B_FALSE;
1687 		/*
1688 		 * For trusted extensions using a shared IP address we can
1689 		 * send using any zoneid.
1690 		 */
1691 		if (zoneid == ALL_ZONES)
1692 			ii->ipsec_in_zoneid = GLOBAL_ZONEID;
1693 		else
1694 			ii->ipsec_in_zoneid = zoneid;
1695 		ipsec_mp->b_cont = mp;
1696 		ip6h = (ip6_t *)mp->b_rptr;
1697 		/*
1698 		 * Convert the IPSEC_IN to IPSEC_OUT.
1699 		 */
1700 		if (!ipsec_in_to_out(ipsec_mp, NULL, ip6h)) {
1701 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
1702 			ill_refrele(ill);
1703 			return;
1704 		}
1705 	}
1706 	io = (ipsec_out_t *)ipsec_mp->b_rptr;
1707 
1708 	if (v6src_ptr != NULL) {
1709 		v6src = *v6src_ptr;
1710 	} else {
1711 		if (icmp_pick_source_v6(q, &ip6h->ip6_src, &ip6h->ip6_dst,
1712 		    &v6src, zoneid) == NULL) {
1713 			freemsg(ipsec_mp);
1714 			ill_refrele(ill);
1715 			return;
1716 		}
1717 	}
1718 	v6dst = ip6h->ip6_src;
1719 	len_needed = ipv6_icmp_return - IPV6_HDR_LEN - len;
1720 	msg_len = msgdsize(mp);
1721 	if (msg_len > len_needed) {
1722 		if (!adjmsg(mp, len_needed - msg_len)) {
1723 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutErrors);
1724 			freemsg(ipsec_mp);
1725 			ill_refrele(ill);
1726 			return;
1727 		}
1728 		msg_len = len_needed;
1729 	}
1730 	mp1 = allocb(IPV6_HDR_LEN + len, BPRI_HI);
1731 	if (mp1 == NULL) {
1732 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutErrors);
1733 		freemsg(ipsec_mp);
1734 		ill_refrele(ill);
1735 		return;
1736 	}
1737 	ill_refrele(ill);
1738 	mp1->b_cont = mp;
1739 	mp = mp1;
1740 	ASSERT(ipsec_mp->b_datap->db_type == M_CTL &&
1741 	    io->ipsec_out_type == IPSEC_OUT);
1742 	ipsec_mp->b_cont = mp;
1743 
1744 	/*
1745 	 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this
1746 	 * node generates be accepted in peace by all on-host destinations.
1747 	 * If we do NOT assume that all on-host destinations trust
1748 	 * self-generated ICMP messages, then rework here, ip.c, and spd.c.
1749 	 * (Look for ipsec_out_icmp_loopback).
1750 	 */
1751 	io->ipsec_out_icmp_loopback = B_TRUE;
1752 
1753 	ip6h = (ip6_t *)mp->b_rptr;
1754 	mp1->b_wptr = (uchar_t *)ip6h + (IPV6_HDR_LEN + len);
1755 
1756 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
1757 	ip6h->ip6_nxt = IPPROTO_ICMPV6;
1758 	ip6h->ip6_hops = ipv6_def_hops;
1759 	ip6h->ip6_dst = v6dst;
1760 	ip6h->ip6_src = v6src;
1761 	msg_len += IPV6_HDR_LEN + len;
1762 	if (msg_len > IP_MAXPACKET + IPV6_HDR_LEN) {
1763 		(void) adjmsg(mp, IP_MAXPACKET + IPV6_HDR_LEN - msg_len);
1764 		msg_len = IP_MAXPACKET + IPV6_HDR_LEN;
1765 	}
1766 	ip6h->ip6_plen = htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN));
1767 	icmp6 = (icmp6_t *)&ip6h[1];
1768 	bcopy(stuff, (char *)icmp6, len);
1769 	/*
1770 	 * Prepare for checksum by putting icmp length in the icmp
1771 	 * checksum field. The checksum is calculated in ip_wput_v6.
1772 	 */
1773 	icmp6->icmp6_cksum = ip6h->ip6_plen;
1774 	if (icmp6->icmp6_type == ND_REDIRECT) {
1775 		ip6h->ip6_hops = IPV6_MAX_HOPS;
1776 	}
1777 	/* Send to V6 writeside put routine */
1778 	put(q, ipsec_mp);
1779 }
1780 
1781 /*
1782  * Update the output mib when ICMPv6 packets are sent.
1783  */
1784 static void
1785 icmp_update_out_mib_v6(ill_t *ill, icmp6_t *icmp6)
1786 {
1787 	BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutMsgs);
1788 
1789 	switch (icmp6->icmp6_type) {
1790 	case ICMP6_DST_UNREACH:
1791 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutDestUnreachs);
1792 		if (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN)
1793 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutAdminProhibs);
1794 		break;
1795 
1796 	case ICMP6_TIME_EXCEEDED:
1797 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutTimeExcds);
1798 		break;
1799 
1800 	case ICMP6_PARAM_PROB:
1801 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutParmProblems);
1802 		break;
1803 
1804 	case ICMP6_PACKET_TOO_BIG:
1805 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutPktTooBigs);
1806 		break;
1807 
1808 	case ICMP6_ECHO_REQUEST:
1809 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutEchos);
1810 		break;
1811 
1812 	case ICMP6_ECHO_REPLY:
1813 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutEchoReplies);
1814 		break;
1815 
1816 	case ND_ROUTER_SOLICIT:
1817 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutRouterSolicits);
1818 		break;
1819 
1820 	case ND_ROUTER_ADVERT:
1821 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutRouterAdvertisements);
1822 		break;
1823 
1824 	case ND_NEIGHBOR_SOLICIT:
1825 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutNeighborSolicits);
1826 		break;
1827 
1828 	case ND_NEIGHBOR_ADVERT:
1829 		BUMP_MIB(ill->ill_icmp6_mib,
1830 		    ipv6IfIcmpOutNeighborAdvertisements);
1831 		break;
1832 
1833 	case ND_REDIRECT:
1834 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutRedirects);
1835 		break;
1836 
1837 	case MLD_LISTENER_QUERY:
1838 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutGroupMembQueries);
1839 		break;
1840 
1841 	case MLD_LISTENER_REPORT:
1842 	case MLD_V2_LISTENER_REPORT:
1843 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutGroupMembResponses);
1844 		break;
1845 
1846 	case MLD_LISTENER_REDUCTION:
1847 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutGroupMembReductions);
1848 		break;
1849 	}
1850 }
1851 
1852 /*
1853  * Check if it is ok to send an ICMPv6 error packet in
1854  * response to the IP packet in mp.
1855  * Free the message and return null if no
1856  * ICMP error packet should be sent.
1857  */
1858 static mblk_t *
1859 icmp_pkt_err_ok_v6(queue_t *q, mblk_t *mp,
1860     boolean_t llbcast, boolean_t mcast_ok)
1861 {
1862 	ip6_t	*ip6h;
1863 
1864 	if (!mp)
1865 		return (NULL);
1866 
1867 	ip6h = (ip6_t *)mp->b_rptr;
1868 
1869 	/* Check if source address uniquely identifies the host */
1870 
1871 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src) ||
1872 	    IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_src) ||
1873 	    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
1874 		freemsg(mp);
1875 		return (NULL);
1876 	}
1877 
1878 	if (ip6h->ip6_nxt == IPPROTO_ICMPV6) {
1879 		size_t	len_needed = IPV6_HDR_LEN + ICMP6_MINLEN;
1880 		icmp6_t		*icmp6;
1881 
1882 		if (mp->b_wptr - mp->b_rptr < len_needed) {
1883 			if (!pullupmsg(mp, len_needed)) {
1884 				ill_t	*ill;
1885 
1886 				ill = ip_queue_to_ill_v6(q);
1887 				if (ill == NULL) {
1888 					BUMP_MIB(&icmp6_mib,
1889 					    ipv6IfIcmpInErrors);
1890 				} else {
1891 					BUMP_MIB(ill->ill_icmp6_mib,
1892 					    ipv6IfIcmpInErrors);
1893 					ill_refrele(ill);
1894 				}
1895 				freemsg(mp);
1896 				return (NULL);
1897 			}
1898 			ip6h = (ip6_t *)mp->b_rptr;
1899 		}
1900 		icmp6 = (icmp6_t *)&ip6h[1];
1901 		/* Explicitly do not generate errors in response to redirects */
1902 		if (ICMP6_IS_ERROR(icmp6->icmp6_type) ||
1903 		    icmp6->icmp6_type == ND_REDIRECT) {
1904 			freemsg(mp);
1905 			return (NULL);
1906 		}
1907 	}
1908 	/*
1909 	 * Check that the destination is not multicast and that the packet
1910 	 * was not sent on link layer broadcast or multicast.  (Exception
1911 	 * is Packet too big message as per the draft - when mcast_ok is set.)
1912 	 */
1913 	if (!mcast_ok &&
1914 	    (llbcast || IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst))) {
1915 		freemsg(mp);
1916 		return (NULL);
1917 	}
1918 	if (icmp_err_rate_limit()) {
1919 		/*
1920 		 * Only send ICMP error packets every so often.
1921 		 * This should be done on a per port/source basis,
1922 		 * but for now this will suffice.
1923 		 */
1924 		freemsg(mp);
1925 		return (NULL);
1926 	}
1927 	return (mp);
1928 }
1929 
1930 /*
1931  * Generate an ICMPv6 redirect message.
1932  * Include target link layer address option if it exits.
1933  * Always include redirect header.
1934  */
1935 static void
1936 icmp_send_redirect_v6(queue_t *q, mblk_t *mp, in6_addr_t *targetp,
1937     in6_addr_t *dest, ill_t *ill, boolean_t llbcast)
1938 {
1939 	nd_redirect_t	*rd;
1940 	nd_opt_rd_hdr_t	*rdh;
1941 	uchar_t		*buf;
1942 	nce_t		*nce = NULL;
1943 	nd_opt_hdr_t	*opt;
1944 	int		len;
1945 	int		ll_opt_len = 0;
1946 	int		max_redir_hdr_data_len;
1947 	int		pkt_len;
1948 	in6_addr_t	*srcp;
1949 
1950 	/*
1951 	 * We are called from ip_rput where we could
1952 	 * not have attached an IPSEC_IN.
1953 	 */
1954 	ASSERT(mp->b_datap->db_type == M_DATA);
1955 
1956 	mp = icmp_pkt_err_ok_v6(q, mp, llbcast, B_FALSE);
1957 	if (mp == NULL)
1958 		return;
1959 	nce = ndp_lookup_v6(ill, targetp, B_FALSE);
1960 	if (nce != NULL && nce->nce_state != ND_INCOMPLETE) {
1961 		ll_opt_len = (sizeof (nd_opt_hdr_t) +
1962 		    ill->ill_phys_addr_length + 7)/8 * 8;
1963 	}
1964 	len = sizeof (nd_redirect_t) + sizeof (nd_opt_rd_hdr_t) + ll_opt_len;
1965 	ASSERT(len % 4 == 0);
1966 	buf = kmem_alloc(len, KM_NOSLEEP);
1967 	if (buf == NULL) {
1968 		if (nce != NULL)
1969 			NCE_REFRELE(nce);
1970 		freemsg(mp);
1971 		return;
1972 	}
1973 
1974 	rd = (nd_redirect_t *)buf;
1975 	rd->nd_rd_type = (uint8_t)ND_REDIRECT;
1976 	rd->nd_rd_code = 0;
1977 	rd->nd_rd_reserved = 0;
1978 	rd->nd_rd_target = *targetp;
1979 	rd->nd_rd_dst = *dest;
1980 
1981 	opt = (nd_opt_hdr_t *)(buf + sizeof (nd_redirect_t));
1982 	if (nce != NULL && ll_opt_len != 0) {
1983 		opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1984 		opt->nd_opt_len = ll_opt_len/8;
1985 		bcopy((char *)nce->nce_res_mp->b_rptr +
1986 		    NCE_LL_ADDR_OFFSET(ill), &opt[1],
1987 		    ill->ill_phys_addr_length);
1988 	}
1989 	if (nce != NULL)
1990 		NCE_REFRELE(nce);
1991 	rdh = (nd_opt_rd_hdr_t *)(buf + sizeof (nd_redirect_t) + ll_opt_len);
1992 	rdh->nd_opt_rh_type = (uint8_t)ND_OPT_REDIRECTED_HEADER;
1993 	/* max_redir_hdr_data_len and nd_opt_rh_len must be multiple of 8 */
1994 	max_redir_hdr_data_len = (ipv6_icmp_return - IPV6_HDR_LEN - len)/8*8;
1995 	pkt_len = msgdsize(mp);
1996 	/* Make sure mp is 8 byte aligned */
1997 	if (pkt_len > max_redir_hdr_data_len) {
1998 		rdh->nd_opt_rh_len = (max_redir_hdr_data_len +
1999 		    sizeof (nd_opt_rd_hdr_t))/8;
2000 		(void) adjmsg(mp, max_redir_hdr_data_len - pkt_len);
2001 	} else {
2002 		rdh->nd_opt_rh_len = (pkt_len + sizeof (nd_opt_rd_hdr_t))/8;
2003 		(void) adjmsg(mp, -(pkt_len % 8));
2004 	}
2005 	rdh->nd_opt_rh_reserved1 = 0;
2006 	rdh->nd_opt_rh_reserved2 = 0;
2007 	/* ipif_v6src_addr contains the link-local source address */
2008 	rw_enter(&ill_g_lock, RW_READER);
2009 	if (ill->ill_group != NULL) {
2010 		/*
2011 		 * The receiver of the redirect will verify whether it
2012 		 * had a route through us (srcp that we will use in
2013 		 * the redirect) or not. As we load spread even link-locals,
2014 		 * we don't know which source address the receiver of
2015 		 * redirect has in its route for communicating with us.
2016 		 * Thus we randomly choose a source here and finally we
2017 		 * should get to the right one and it will eventually
2018 		 * accept the redirect from us. We can't call
2019 		 * ip_lookup_scope_v6 because we don't have the right
2020 		 * link-local address here. Thus we randomly choose one.
2021 		 */
2022 		int cnt = ill->ill_group->illgrp_ill_count;
2023 
2024 		ill = ill->ill_group->illgrp_ill;
2025 		cnt = ++icmp_redirect_v6_src_index % cnt;
2026 		while (cnt--)
2027 			ill = ill->ill_group_next;
2028 		srcp = &ill->ill_ipif->ipif_v6src_addr;
2029 	} else {
2030 		srcp = &ill->ill_ipif->ipif_v6src_addr;
2031 	}
2032 	rw_exit(&ill_g_lock);
2033 	/* Redirects sent by router, and router is global zone */
2034 	icmp_pkt_v6(q, mp, buf, len, srcp, B_FALSE, GLOBAL_ZONEID);
2035 	kmem_free(buf, len);
2036 }
2037 
2038 
2039 /* Generate an ICMP time exceeded message.  (May be called as writer.) */
2040 void
2041 icmp_time_exceeded_v6(queue_t *q, mblk_t *mp, uint8_t code,
2042     boolean_t llbcast, boolean_t mcast_ok, zoneid_t zoneid)
2043 {
2044 	icmp6_t	icmp6;
2045 	boolean_t mctl_present;
2046 	mblk_t *first_mp;
2047 
2048 	EXTRACT_PKT_MP(mp, first_mp, mctl_present);
2049 
2050 	mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok);
2051 	if (mp == NULL) {
2052 		if (mctl_present)
2053 			freeb(first_mp);
2054 		return;
2055 	}
2056 	bzero(&icmp6, sizeof (icmp6_t));
2057 	icmp6.icmp6_type = ICMP6_TIME_EXCEEDED;
2058 	icmp6.icmp6_code = code;
2059 	icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present,
2060 	    zoneid);
2061 }
2062 
2063 /*
2064  * Generate an ICMP unreachable message.
2065  */
2066 void
2067 icmp_unreachable_v6(queue_t *q, mblk_t *mp, uint8_t code,
2068     boolean_t llbcast, boolean_t mcast_ok, zoneid_t zoneid)
2069 {
2070 	icmp6_t	icmp6;
2071 	boolean_t mctl_present;
2072 	mblk_t *first_mp;
2073 
2074 	EXTRACT_PKT_MP(mp, first_mp, mctl_present);
2075 
2076 	mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok);
2077 	if (mp == NULL) {
2078 		if (mctl_present)
2079 			freeb(first_mp);
2080 		return;
2081 	}
2082 	bzero(&icmp6, sizeof (icmp6_t));
2083 	icmp6.icmp6_type = ICMP6_DST_UNREACH;
2084 	icmp6.icmp6_code = code;
2085 	icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present,
2086 	    zoneid);
2087 }
2088 
2089 /*
2090  * Generate an ICMP pkt too big message.
2091  */
2092 static void
2093 icmp_pkt2big_v6(queue_t *q, mblk_t *mp, uint32_t mtu,
2094     boolean_t llbcast, boolean_t mcast_ok, zoneid_t zoneid)
2095 {
2096 	icmp6_t	icmp6;
2097 	mblk_t *first_mp;
2098 	boolean_t mctl_present;
2099 
2100 	EXTRACT_PKT_MP(mp, first_mp, mctl_present);
2101 
2102 	mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok);
2103 	if (mp == NULL) {
2104 		if (mctl_present)
2105 			freeb(first_mp);
2106 		return;
2107 	}
2108 	bzero(&icmp6, sizeof (icmp6_t));
2109 	icmp6.icmp6_type = ICMP6_PACKET_TOO_BIG;
2110 	icmp6.icmp6_code = 0;
2111 	icmp6.icmp6_mtu = htonl(mtu);
2112 
2113 	icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present,
2114 	    zoneid);
2115 }
2116 
2117 /*
2118  * Generate an ICMP parameter problem message. (May be called as writer.)
2119  * 'offset' is the offset from the beginning of the packet in error.
2120  */
2121 static void
2122 icmp_param_problem_v6(queue_t *q, mblk_t *mp, uint8_t code,
2123     uint32_t offset, boolean_t llbcast, boolean_t mcast_ok, zoneid_t zoneid)
2124 {
2125 	icmp6_t	icmp6;
2126 	boolean_t mctl_present;
2127 	mblk_t *first_mp;
2128 
2129 	EXTRACT_PKT_MP(mp, first_mp, mctl_present);
2130 
2131 	mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok);
2132 	if (mp == NULL) {
2133 		if (mctl_present)
2134 			freeb(first_mp);
2135 		return;
2136 	}
2137 	bzero((char *)&icmp6, sizeof (icmp6_t));
2138 	icmp6.icmp6_type = ICMP6_PARAM_PROB;
2139 	icmp6.icmp6_code = code;
2140 	icmp6.icmp6_pptr = htonl(offset);
2141 	icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present,
2142 	    zoneid);
2143 }
2144 
2145 /*
2146  * This code will need to take into account the possibility of binding
2147  * to a link local address on a multi-homed host, in which case the
2148  * outgoing interface (from the conn) will need to be used when getting
2149  * an ire for the dst. Going through proper outgoing interface and
2150  * choosing the source address corresponding to the outgoing interface
2151  * is necessary when the destination address is a link-local address and
2152  * IPV6_BOUND_IF or IPV6_PKTINFO or scope_id has been set.
2153  * This can happen when active connection is setup; thus ipp pointer
2154  * is passed here from tcp_connect_*() routines, in non-TCP cases NULL
2155  * pointer is passed as ipp pointer.
2156  */
2157 mblk_t *
2158 ip_bind_v6(queue_t *q, mblk_t *mp, conn_t *connp, ip6_pkt_t *ipp)
2159 {
2160 	ssize_t			len;
2161 	int			protocol;
2162 	struct T_bind_req	*tbr;
2163 	sin6_t			*sin6;
2164 	ipa6_conn_t		*ac6;
2165 	in6_addr_t		*v6srcp;
2166 	in6_addr_t		*v6dstp;
2167 	uint16_t		lport;
2168 	uint16_t		fport;
2169 	uchar_t			*ucp;
2170 	mblk_t			*mp1;
2171 	boolean_t		ire_requested;
2172 	boolean_t		ipsec_policy_set;
2173 	int			error = 0;
2174 	boolean_t		local_bind;
2175 	boolean_t		orig_pkt_isv6 = connp->conn_pkt_isv6;
2176 	ipa6_conn_x_t		*acx6;
2177 	boolean_t		verify_dst;
2178 
2179 	ASSERT(connp->conn_af_isv6);
2180 	len = mp->b_wptr - mp->b_rptr;
2181 	if (len < (sizeof (*tbr) + 1)) {
2182 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2183 		    "ip_bind_v6: bogus msg, len %ld", len);
2184 		goto bad_addr;
2185 	}
2186 	/* Back up and extract the protocol identifier. */
2187 	mp->b_wptr--;
2188 	tbr = (struct T_bind_req *)mp->b_rptr;
2189 	/* Reset the message type in preparation for shipping it back. */
2190 	mp->b_datap->db_type = M_PCPROTO;
2191 
2192 	protocol = *mp->b_wptr & 0xFF;
2193 	connp->conn_ulp = (uint8_t)protocol;
2194 
2195 	/*
2196 	 * Check for a zero length address.  This is from a protocol that
2197 	 * wants to register to receive all packets of its type.
2198 	 */
2199 	if (tbr->ADDR_length == 0) {
2200 		if ((protocol == IPPROTO_TCP || protocol == IPPROTO_SCTP ||
2201 		    protocol == IPPROTO_ESP || protocol == IPPROTO_AH) &&
2202 		    ipcl_proto_fanout_v6[protocol].connf_head != NULL) {
2203 			/*
2204 			 * TCP, SCTP, AH, and ESP have single protocol fanouts.
2205 			 * Do not allow others to bind to these.
2206 			 */
2207 			goto bad_addr;
2208 		}
2209 
2210 		/*
2211 		 *
2212 		 * The udp module never sends down a zero-length address,
2213 		 * and allowing this on a labeled system will break MLP
2214 		 * functionality.
2215 		 */
2216 		if (is_system_labeled() && protocol == IPPROTO_UDP)
2217 			goto bad_addr;
2218 
2219 		/* Allow ipsec plumbing */
2220 		if (connp->conn_mac_exempt && protocol != IPPROTO_AH &&
2221 		    protocol != IPPROTO_ESP)
2222 			goto bad_addr;
2223 
2224 		connp->conn_srcv6 = ipv6_all_zeros;
2225 		ipcl_proto_insert_v6(connp, protocol);
2226 
2227 		tbr->PRIM_type = T_BIND_ACK;
2228 		return (mp);
2229 	}
2230 
2231 	/* Extract the address pointer from the message. */
2232 	ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset,
2233 	    tbr->ADDR_length);
2234 	if (ucp == NULL) {
2235 		ip1dbg(("ip_bind_v6: no address\n"));
2236 		goto bad_addr;
2237 	}
2238 	if (!OK_32PTR(ucp)) {
2239 		ip1dbg(("ip_bind_v6: unaligned address\n"));
2240 		goto bad_addr;
2241 	}
2242 	mp1 = mp->b_cont;	/* trailing mp if any */
2243 	ire_requested = (mp1 && mp1->b_datap->db_type == IRE_DB_REQ_TYPE);
2244 	ipsec_policy_set = (mp1 && mp1->b_datap->db_type == IPSEC_POLICY_SET);
2245 
2246 	switch (tbr->ADDR_length) {
2247 	default:
2248 		ip1dbg(("ip_bind_v6: bad address length %d\n",
2249 		    (int)tbr->ADDR_length));
2250 		goto bad_addr;
2251 
2252 	case IPV6_ADDR_LEN:
2253 		/* Verification of local address only */
2254 		v6srcp = (in6_addr_t *)ucp;
2255 		lport = 0;
2256 		local_bind = B_TRUE;
2257 		break;
2258 
2259 	case sizeof (sin6_t):
2260 		sin6 = (sin6_t *)ucp;
2261 		v6srcp = &sin6->sin6_addr;
2262 		lport = sin6->sin6_port;
2263 		local_bind = B_TRUE;
2264 		break;
2265 
2266 	case sizeof (ipa6_conn_t):
2267 		/*
2268 		 * Verify that both the source and destination addresses
2269 		 * are valid.
2270 		 * Note that we allow connect to broadcast and multicast
2271 		 * addresses when ire_requested is set. Thus the ULP
2272 		 * has to check for IRE_BROADCAST and multicast.
2273 		 */
2274 		ac6 = (ipa6_conn_t *)ucp;
2275 		v6srcp = &ac6->ac6_laddr;
2276 		v6dstp = &ac6->ac6_faddr;
2277 		fport = ac6->ac6_fport;
2278 		/* For raw socket, the local port is not set. */
2279 		lport = ac6->ac6_lport != 0 ? ac6->ac6_lport :
2280 		    connp->conn_lport;
2281 		local_bind = B_FALSE;
2282 		/* Always verify destination reachability. */
2283 		verify_dst = B_TRUE;
2284 		break;
2285 
2286 	case sizeof (ipa6_conn_x_t):
2287 		/*
2288 		 * Verify that the source address is valid.
2289 		 * Note that we allow connect to broadcast and multicast
2290 		 * addresses when ire_requested is set. Thus the ULP
2291 		 * has to check for IRE_BROADCAST and multicast.
2292 		 */
2293 		acx6 = (ipa6_conn_x_t *)ucp;
2294 		ac6 = &acx6->ac6x_conn;
2295 		v6srcp = &ac6->ac6_laddr;
2296 		v6dstp = &ac6->ac6_faddr;
2297 		fport = ac6->ac6_fport;
2298 		lport = ac6->ac6_lport;
2299 		local_bind = B_FALSE;
2300 		/*
2301 		 * Client that passed ipa6_conn_x_t to us specifies whether to
2302 		 * verify destination reachability.
2303 		 */
2304 		verify_dst = (acx6->ac6x_flags & ACX_VERIFY_DST) != 0;
2305 		break;
2306 	}
2307 	if (local_bind) {
2308 		if (IN6_IS_ADDR_V4MAPPED(v6srcp) && !connp->conn_ipv6_v6only) {
2309 			/* Bind to IPv4 address */
2310 			ipaddr_t v4src;
2311 
2312 			IN6_V4MAPPED_TO_IPADDR(v6srcp, v4src);
2313 
2314 			error = ip_bind_laddr(connp, mp, v4src, lport,
2315 			    ire_requested, ipsec_policy_set,
2316 			    tbr->ADDR_length != IPV6_ADDR_LEN);
2317 			if (error != 0)
2318 				goto bad_addr;
2319 			connp->conn_pkt_isv6 = B_FALSE;
2320 		} else {
2321 			if (IN6_IS_ADDR_V4MAPPED(v6srcp)) {
2322 				error = 0;
2323 				goto bad_addr;
2324 			}
2325 			error = ip_bind_laddr_v6(connp, mp, v6srcp, lport,
2326 			    ire_requested, ipsec_policy_set,
2327 			    (tbr->ADDR_length != IPV6_ADDR_LEN));
2328 			if (error != 0)
2329 				goto bad_addr;
2330 			connp->conn_pkt_isv6 = B_TRUE;
2331 		}
2332 	} else {
2333 		/*
2334 		 * Bind to local and remote address. Local might be
2335 		 * unspecified in which case it will be extracted from
2336 		 * ire_src_addr_v6
2337 		 */
2338 		if (IN6_IS_ADDR_V4MAPPED(v6dstp) && !connp->conn_ipv6_v6only) {
2339 			/* Connect to IPv4 address */
2340 			ipaddr_t v4src;
2341 			ipaddr_t v4dst;
2342 
2343 			/* Is the source unspecified or mapped? */
2344 			if (!IN6_IS_ADDR_V4MAPPED(v6srcp) &&
2345 			    !IN6_IS_ADDR_UNSPECIFIED(v6srcp)) {
2346 				ip1dbg(("ip_bind_v6: "
2347 				    "dst is mapped, but not the src\n"));
2348 				goto bad_addr;
2349 			}
2350 			IN6_V4MAPPED_TO_IPADDR(v6srcp, v4src);
2351 			IN6_V4MAPPED_TO_IPADDR(v6dstp, v4dst);
2352 
2353 			/*
2354 			 * XXX Fix needed. Need to pass ipsec_policy_set
2355 			 * instead of B_FALSE.
2356 			 */
2357 
2358 			/* Always verify destination reachability. */
2359 			error = ip_bind_connected(connp, mp, &v4src, lport,
2360 			    v4dst, fport, ire_requested, ipsec_policy_set,
2361 			    B_TRUE, B_TRUE);
2362 			if (error != 0)
2363 				goto bad_addr;
2364 			IN6_IPADDR_TO_V4MAPPED(v4src, v6srcp);
2365 			connp->conn_pkt_isv6 = B_FALSE;
2366 		} else if (IN6_IS_ADDR_V4MAPPED(v6srcp)) {
2367 			ip1dbg(("ip_bind_v6: "
2368 			    "src is mapped, but not the dst\n"));
2369 			goto bad_addr;
2370 		} else {
2371 			error = ip_bind_connected_v6(connp, mp, v6srcp,
2372 			    lport, v6dstp, ipp, fport, ire_requested,
2373 			    ipsec_policy_set, B_TRUE, verify_dst);
2374 			if (error != 0)
2375 				goto bad_addr;
2376 			connp->conn_pkt_isv6 = B_TRUE;
2377 		}
2378 	}
2379 	/* Update qinfo if v4/v6 changed */
2380 	if ((orig_pkt_isv6 != connp->conn_pkt_isv6) &&
2381 	    !(IPCL_IS_TCP(connp) || IPCL_IS_UDP(connp))) {
2382 		if (connp->conn_pkt_isv6)
2383 			ip_setqinfo(RD(q), IPV6_MINOR, B_TRUE);
2384 		else
2385 			ip_setqinfo(RD(q), IPV4_MINOR, B_TRUE);
2386 	}
2387 
2388 	/*
2389 	 * Pass the IPSEC headers size in ire_ipsec_overhead.
2390 	 * We can't do this in ip_bind_insert_ire because the policy
2391 	 * may not have been inherited at that point in time and hence
2392 	 * conn_out_enforce_policy may not be set.
2393 	 */
2394 	mp1 = mp->b_cont;
2395 	if (ire_requested && connp->conn_out_enforce_policy &&
2396 	    mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) {
2397 		ire_t *ire = (ire_t *)mp1->b_rptr;
2398 		ASSERT(MBLKL(mp1) >= sizeof (ire_t));
2399 		ire->ire_ipsec_overhead = (conn_ipsec_length(connp));
2400 	}
2401 
2402 	/* Send it home. */
2403 	mp->b_datap->db_type = M_PCPROTO;
2404 	tbr->PRIM_type = T_BIND_ACK;
2405 	return (mp);
2406 
2407 bad_addr:
2408 	if (error == EINPROGRESS)
2409 		return (NULL);
2410 	if (error > 0)
2411 		mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error);
2412 	else
2413 		mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0);
2414 	return (mp);
2415 }
2416 
2417 /*
2418  * Here address is verified to be a valid local address.
2419  * If the IRE_DB_REQ_TYPE mp is present, a multicast
2420  * address is also considered a valid local address.
2421  * In the case of a multicast address, however, the
2422  * upper protocol is expected to reset the src address
2423  * to 0 if it sees an ire with IN6_IS_ADDR_MULTICAST returned so that
2424  * no packets are emitted with multicast address as
2425  * source address.
2426  * The addresses valid for bind are:
2427  *	(1) - in6addr_any
2428  *	(2) - IP address of an UP interface
2429  *	(3) - IP address of a DOWN interface
2430  *	(4) - a multicast address. In this case
2431  *	the conn will only receive packets destined to
2432  *	the specified multicast address. Note: the
2433  *	application still has to issue an
2434  *	IPV6_JOIN_GROUP socket option.
2435  *
2436  * In all the above cases, the bound address must be valid in the current zone.
2437  * When the address is loopback or multicast, there might be many matching IREs
2438  * so bind has to look up based on the zone.
2439  */
2440 static int
2441 ip_bind_laddr_v6(conn_t *connp, mblk_t *mp, const in6_addr_t *v6src,
2442     uint16_t lport, boolean_t ire_requested, boolean_t ipsec_policy_set,
2443     boolean_t fanout_insert)
2444 {
2445 	int		error = 0;
2446 	ire_t		*src_ire = NULL;
2447 	ipif_t		*ipif = NULL;
2448 	mblk_t		*policy_mp;
2449 	zoneid_t	zoneid;
2450 
2451 	if (ipsec_policy_set)
2452 		policy_mp = mp->b_cont;
2453 
2454 	/*
2455 	 * If it was previously connected, conn_fully_bound would have
2456 	 * been set.
2457 	 */
2458 	connp->conn_fully_bound = B_FALSE;
2459 
2460 	zoneid = connp->conn_zoneid;
2461 
2462 	if (!IN6_IS_ADDR_UNSPECIFIED(v6src)) {
2463 		src_ire = ire_route_lookup_v6(v6src, 0, 0,
2464 		    0, NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY);
2465 		/*
2466 		 * If an address other than in6addr_any is requested,
2467 		 * we verify that it is a valid address for bind
2468 		 * Note: Following code is in if-else-if form for
2469 		 * readability compared to a condition check.
2470 		 */
2471 		ASSERT(src_ire == NULL || !(src_ire->ire_type & IRE_BROADCAST));
2472 		if (IRE_IS_LOCAL(src_ire)) {
2473 			/*
2474 			 * (2) Bind to address of local UP interface
2475 			 */
2476 			ipif = src_ire->ire_ipif;
2477 		} else if (IN6_IS_ADDR_MULTICAST(v6src)) {
2478 			ipif_t	*multi_ipif = NULL;
2479 			ire_t	*save_ire;
2480 			/*
2481 			 * (4) bind to multicast address.
2482 			 * Fake out the IRE returned to upper
2483 			 * layer to be a broadcast IRE in
2484 			 * ip_bind_insert_ire_v6().
2485 			 * Pass other information that matches
2486 			 * the ipif (e.g. the source address).
2487 			 * conn_multicast_ill is only used for
2488 			 * IPv6 packets
2489 			 */
2490 			mutex_enter(&connp->conn_lock);
2491 			if (connp->conn_multicast_ill != NULL) {
2492 				(void) ipif_lookup_zoneid(
2493 				    connp->conn_multicast_ill, zoneid, 0,
2494 				    &multi_ipif);
2495 			} else {
2496 				/*
2497 				 * Look for default like
2498 				 * ip_wput_v6
2499 				 */
2500 				multi_ipif = ipif_lookup_group_v6(
2501 				    &ipv6_unspecified_group, zoneid);
2502 			}
2503 			mutex_exit(&connp->conn_lock);
2504 			save_ire = src_ire;
2505 			src_ire = NULL;
2506 			if (multi_ipif == NULL || !ire_requested ||
2507 			    (src_ire = ipif_to_ire_v6(multi_ipif)) == NULL) {
2508 				src_ire = save_ire;
2509 				error = EADDRNOTAVAIL;
2510 			} else {
2511 				ASSERT(src_ire != NULL);
2512 				if (save_ire != NULL)
2513 					ire_refrele(save_ire);
2514 			}
2515 			if (multi_ipif != NULL)
2516 				ipif_refrele(multi_ipif);
2517 		} else {
2518 			*mp->b_wptr++ = (char)connp->conn_ulp;
2519 			ipif = ipif_lookup_addr_v6(v6src, NULL, zoneid,
2520 			    CONNP_TO_WQ(connp), mp, ip_wput_nondata, &error);
2521 			if (ipif == NULL) {
2522 				if (error == EINPROGRESS) {
2523 					if (src_ire != NULL)
2524 						ire_refrele(src_ire);
2525 					return (error);
2526 				}
2527 				/*
2528 				 * Not a valid address for bind
2529 				 */
2530 				error = EADDRNOTAVAIL;
2531 			} else {
2532 				ipif_refrele(ipif);
2533 			}
2534 			/*
2535 			 * Just to keep it consistent with the processing in
2536 			 * ip_bind_v6().
2537 			 */
2538 			mp->b_wptr--;
2539 		}
2540 
2541 		if (error != 0) {
2542 			/* Red Alert!  Attempting to be a bogon! */
2543 			if (ip_debug > 2) {
2544 				/* ip1dbg */
2545 				pr_addr_dbg("ip_bind_laddr_v6: bad src"
2546 				    " address %s\n", AF_INET6, v6src);
2547 			}
2548 			goto bad_addr;
2549 		}
2550 	}
2551 
2552 	/*
2553 	 * Allow setting new policies. For example, disconnects come
2554 	 * down as ipa_t bind. As we would have set conn_policy_cached
2555 	 * to B_TRUE before, we should set it to B_FALSE, so that policy
2556 	 * can change after the disconnect.
2557 	 */
2558 	connp->conn_policy_cached = B_FALSE;
2559 
2560 	/* If not fanout_insert this was just an address verification */
2561 	if (fanout_insert) {
2562 		/*
2563 		 * The addresses have been verified. Time to insert in
2564 		 * the correct fanout list.
2565 		 */
2566 		connp->conn_srcv6 = *v6src;
2567 		connp->conn_remv6 = ipv6_all_zeros;
2568 		connp->conn_lport = lport;
2569 		connp->conn_fport = 0;
2570 
2571 		/*
2572 		 * We need to make sure that the conn_recv is set to a non-null
2573 		 * value before we insert the conn_t into the classifier table.
2574 		 * This is to avoid a race with an incoming packet which does
2575 		 * an ipcl_classify().
2576 		 */
2577 		if (*mp->b_wptr == IPPROTO_TCP)
2578 			connp->conn_recv = tcp_conn_request;
2579 		error = ipcl_bind_insert_v6(connp, *mp->b_wptr, v6src, lport);
2580 	}
2581 	if (error == 0) {
2582 		if (ire_requested) {
2583 			if (!ip_bind_insert_ire_v6(mp, src_ire, v6src, NULL)) {
2584 				error = -1;
2585 				goto bad_addr;
2586 			}
2587 		} else if (ipsec_policy_set) {
2588 			if (!ip_bind_ipsec_policy_set(connp, policy_mp)) {
2589 				error = -1;
2590 				goto bad_addr;
2591 			}
2592 		}
2593 	} else if (connp->conn_ulp == IPPROTO_TCP) {
2594 		connp->conn_recv = tcp_input;
2595 	}
2596 bad_addr:
2597 	if (error != 0) {
2598 		if (connp->conn_anon_port) {
2599 			(void) tsol_mlp_anon(crgetzone(connp->conn_cred),
2600 			    connp->conn_mlp_type, connp->conn_ulp, ntohs(lport),
2601 			    B_FALSE);
2602 		}
2603 		connp->conn_mlp_type = mlptSingle;
2604 	}
2605 
2606 	if (src_ire != NULL)
2607 		ire_refrele(src_ire);
2608 
2609 	if (ipsec_policy_set) {
2610 		ASSERT(policy_mp != NULL);
2611 		freeb(policy_mp);
2612 		/*
2613 		 * As of now assume that nothing else accompanies
2614 		 * IPSEC_POLICY_SET.
2615 		 */
2616 		mp->b_cont = NULL;
2617 	}
2618 	return (error);
2619 }
2620 
2621 /* ARGSUSED */
2622 static void
2623 ip_bind_connected_resume_v6(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
2624     void *dummy_arg)
2625 {
2626 	conn_t	*connp = NULL;
2627 	t_scalar_t prim;
2628 
2629 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
2630 
2631 	if (CONN_Q(q))
2632 		connp = Q_TO_CONN(q);
2633 	ASSERT(connp != NULL);
2634 
2635 	prim = ((union T_primitives *)mp->b_rptr)->type;
2636 	ASSERT(prim == O_T_BIND_REQ || prim == T_BIND_REQ);
2637 
2638 	if (IPCL_IS_TCP(connp)) {
2639 		/* Pass sticky_ipp for scope_id and pktinfo */
2640 		mp = ip_bind_v6(q, mp, connp, &connp->conn_tcp->tcp_sticky_ipp);
2641 	} else {
2642 		/* For UDP and ICMP */
2643 		mp = ip_bind_v6(q, mp, connp, NULL);
2644 	}
2645 	if (mp != NULL) {
2646 		if (IPCL_IS_TCP(connp)) {
2647 			CONN_INC_REF(connp);
2648 			squeue_fill(connp->conn_sqp, mp, ip_resume_tcp_bind,
2649 			    connp, SQTAG_TCP_RPUTOTHER);
2650 		} else if (IPCL_IS_UDP(connp)) {
2651 			udp_resume_bind(connp, mp);
2652 		} else {
2653 			qreply(q, mp);
2654 			CONN_OPER_PENDING_DONE(connp);
2655 		}
2656 	}
2657 }
2658 
2659 /*
2660  * Verify that both the source and destination addresses
2661  * are valid.  If verify_dst, then destination address must also be reachable,
2662  * i.e. have a route.  Protocols like TCP want this.  Tunnels do not.
2663  * It takes ip6_pkt_t * as one of the arguments to determine correct
2664  * source address when IPV6_PKTINFO or scope_id is set along with a link-local
2665  * destination address. Note that parameter ipp is only useful for TCP connect
2666  * when scope_id is set or IPV6_PKTINFO option is set with an ifindex. For all
2667  * non-TCP cases, it is NULL and for all other tcp cases it is not useful.
2668  *
2669  */
2670 static int
2671 ip_bind_connected_v6(conn_t *connp, mblk_t *mp, in6_addr_t *v6src,
2672     uint16_t lport, const in6_addr_t *v6dst, ip6_pkt_t *ipp, uint16_t fport,
2673     boolean_t ire_requested, boolean_t ipsec_policy_set,
2674     boolean_t fanout_insert, boolean_t verify_dst)
2675 {
2676 	ire_t		*src_ire;
2677 	ire_t		*dst_ire;
2678 	int		error = 0;
2679 	int 		protocol;
2680 	mblk_t		*policy_mp;
2681 	ire_t		*sire = NULL;
2682 	ire_t		*md_dst_ire = NULL;
2683 	ill_t		*md_ill = NULL;
2684 	ill_t 		*dst_ill = NULL;
2685 	ipif_t		*src_ipif = NULL;
2686 	zoneid_t	zoneid;
2687 	boolean_t ill_held = B_FALSE;
2688 
2689 	src_ire = dst_ire = NULL;
2690 	/*
2691 	 * NOTE:  The protocol is beyond the wptr because that's how
2692 	 * the undocumented transport<-->IP T_BIND_REQ behavior works.
2693 	 */
2694 	protocol = *mp->b_wptr & 0xFF;
2695 
2696 	/*
2697 	 * If we never got a disconnect before, clear it now.
2698 	 */
2699 	connp->conn_fully_bound = B_FALSE;
2700 
2701 	if (ipsec_policy_set) {
2702 		policy_mp = mp->b_cont;
2703 	}
2704 
2705 	zoneid = connp->conn_zoneid;
2706 
2707 	if (IN6_IS_ADDR_MULTICAST(v6dst)) {
2708 		ipif_t *ipif;
2709 
2710 		/*
2711 		 * Use an "emulated" IRE_BROADCAST to tell the transport it
2712 		 * is a multicast.
2713 		 * Pass other information that matches
2714 		 * the ipif (e.g. the source address).
2715 		 *
2716 		 * conn_multicast_ill is only used for IPv6 packets
2717 		 */
2718 		mutex_enter(&connp->conn_lock);
2719 		if (connp->conn_multicast_ill != NULL) {
2720 			(void) ipif_lookup_zoneid(connp->conn_multicast_ill,
2721 			    zoneid, 0, &ipif);
2722 		} else {
2723 			/* Look for default like ip_wput_v6 */
2724 			ipif = ipif_lookup_group_v6(v6dst, zoneid);
2725 		}
2726 		mutex_exit(&connp->conn_lock);
2727 		if (ipif == NULL || !ire_requested ||
2728 		    (dst_ire = ipif_to_ire_v6(ipif)) == NULL) {
2729 			if (ipif != NULL)
2730 				ipif_refrele(ipif);
2731 			if (ip_debug > 2) {
2732 				/* ip1dbg */
2733 				pr_addr_dbg("ip_bind_connected_v6: bad "
2734 				    "connected multicast %s\n", AF_INET6,
2735 				    v6dst);
2736 			}
2737 			error = ENETUNREACH;
2738 			goto bad_addr;
2739 		}
2740 		if (ipif != NULL)
2741 			ipif_refrele(ipif);
2742 	} else {
2743 		dst_ire = ire_route_lookup_v6(v6dst, NULL, NULL, 0,
2744 		    NULL, &sire, zoneid, MBLK_GETLABEL(mp),
2745 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
2746 		    MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_SECATTR);
2747 		/*
2748 		 * We also prevent ire's with src address INADDR_ANY to
2749 		 * be used, which are created temporarily for
2750 		 * sending out packets from endpoints that have
2751 		 * conn_unspec_src set.
2752 		 */
2753 		if (dst_ire == NULL ||
2754 		    (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2755 		    IN6_IS_ADDR_UNSPECIFIED(&dst_ire->ire_src_addr_v6)) {
2756 			/*
2757 			 * When verifying destination reachability, we always
2758 			 * complain.
2759 			 *
2760 			 * When not verifying destination reachability but we
2761 			 * found an IRE, i.e. the destination is reachable,
2762 			 * then the other tests still apply and we complain.
2763 			 */
2764 			if (verify_dst || (dst_ire != NULL)) {
2765 				if (ip_debug > 2) {
2766 					/* ip1dbg */
2767 					pr_addr_dbg("ip_bind_connected_v6: bad"
2768 					    " connected dst %s\n", AF_INET6,
2769 					    v6dst);
2770 				}
2771 				if (dst_ire == NULL ||
2772 				    !(dst_ire->ire_type & IRE_HOST)) {
2773 					error = ENETUNREACH;
2774 				} else {
2775 					error = EHOSTUNREACH;
2776 				}
2777 				goto bad_addr;
2778 			}
2779 		}
2780 	}
2781 
2782 	/*
2783 	 * We now know that routing will allow us to reach the destination.
2784 	 * Check whether Trusted Solaris policy allows communication with this
2785 	 * host, and pretend that the destination is unreachable if not.
2786 	 *
2787 	 * This is never a problem for TCP, since that transport is known to
2788 	 * compute the label properly as part of the tcp_rput_other T_BIND_ACK
2789 	 * handling.  If the remote is unreachable, it will be detected at that
2790 	 * point, so there's no reason to check it here.
2791 	 *
2792 	 * Note that for sendto (and other datagram-oriented friends), this
2793 	 * check is done as part of the data path label computation instead.
2794 	 * The check here is just to make non-TCP connect() report the right
2795 	 * error.
2796 	 */
2797 	if (dst_ire != NULL && is_system_labeled() &&
2798 	    !IPCL_IS_TCP(connp) &&
2799 	    tsol_compute_label_v6(DB_CREDDEF(mp, connp->conn_cred), v6dst, NULL,
2800 	    connp->conn_mac_exempt) != 0) {
2801 		error = EHOSTUNREACH;
2802 		if (ip_debug > 2) {
2803 			pr_addr_dbg("ip_bind_connected: no label for dst %s\n",
2804 			    AF_INET6, v6dst);
2805 		}
2806 		goto bad_addr;
2807 	}
2808 
2809 	/*
2810 	 * If the app does a connect(), it means that it will most likely
2811 	 * send more than 1 packet to the destination.  It makes sense
2812 	 * to clear the temporary flag.
2813 	 */
2814 	if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE &&
2815 	    (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) {
2816 		irb_t *irb = dst_ire->ire_bucket;
2817 
2818 		rw_enter(&irb->irb_lock, RW_WRITER);
2819 		dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY;
2820 		irb->irb_tmp_ire_cnt--;
2821 		rw_exit(&irb->irb_lock);
2822 	}
2823 
2824 	ASSERT(dst_ire == NULL || dst_ire->ire_ipversion == IPV6_VERSION);
2825 
2826 	/*
2827 	 * See if we should notify ULP about MDT; we do this whether or not
2828 	 * ire_requested is TRUE, in order to handle active connects; MDT
2829 	 * eligibility tests for passive connects are handled separately
2830 	 * through tcp_adapt_ire().  We do this before the source address
2831 	 * selection, because dst_ire may change after a call to
2832 	 * ipif_select_source_v6().  This is a best-effort check, as the
2833 	 * packet for this connection may not actually go through
2834 	 * dst_ire->ire_stq, and the exact IRE can only be known after
2835 	 * calling ip_newroute_v6().  This is why we further check on the
2836 	 * IRE during Multidata packet transmission in tcp_multisend().
2837 	 */
2838 	if (ip_multidata_outbound && !ipsec_policy_set && dst_ire != NULL &&
2839 	    !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) &&
2840 	    (md_ill = ire_to_ill(dst_ire), md_ill != NULL) &&
2841 	    ILL_MDT_CAPABLE(md_ill)) {
2842 		md_dst_ire = dst_ire;
2843 		IRE_REFHOLD(md_dst_ire);
2844 	}
2845 
2846 	if (dst_ire != NULL &&
2847 	    dst_ire->ire_type == IRE_LOCAL &&
2848 	    dst_ire->ire_zoneid != zoneid &&
2849 	    dst_ire->ire_zoneid != ALL_ZONES) {
2850 		src_ire = ire_ftable_lookup_v6(v6dst, 0, 0, 0, NULL, NULL,
2851 		    zoneid, 0, NULL,
2852 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
2853 		    MATCH_IRE_RJ_BHOLE);
2854 		if (src_ire == NULL) {
2855 			error = EHOSTUNREACH;
2856 			goto bad_addr;
2857 		} else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2858 			if (!(src_ire->ire_type & IRE_HOST))
2859 				error = ENETUNREACH;
2860 			else
2861 				error = EHOSTUNREACH;
2862 			goto bad_addr;
2863 		}
2864 		if (IN6_IS_ADDR_UNSPECIFIED(v6src)) {
2865 			src_ipif = src_ire->ire_ipif;
2866 			ipif_refhold(src_ipif);
2867 			*v6src = src_ipif->ipif_v6lcl_addr;
2868 		}
2869 		ire_refrele(src_ire);
2870 		src_ire = NULL;
2871 	} else if (IN6_IS_ADDR_UNSPECIFIED(v6src) && dst_ire != NULL) {
2872 		if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
2873 			*v6src = sire->ire_src_addr_v6;
2874 			ire_refrele(dst_ire);
2875 			dst_ire = sire;
2876 			sire = NULL;
2877 		} else if (dst_ire->ire_type == IRE_CACHE &&
2878 		    (dst_ire->ire_flags & RTF_SETSRC)) {
2879 			ASSERT(dst_ire->ire_zoneid == zoneid ||
2880 			    dst_ire->ire_zoneid == ALL_ZONES);
2881 			*v6src = dst_ire->ire_src_addr_v6;
2882 		} else {
2883 			/*
2884 			 * Pick a source address so that a proper inbound load
2885 			 * spreading would happen. Use dst_ill specified by the
2886 			 * app. when socket option or scopeid is set.
2887 			 */
2888 			int  err;
2889 
2890 			if (ipp != NULL && ipp->ipp_ifindex != 0) {
2891 				uint_t	if_index;
2892 
2893 				/*
2894 				 * Scope id or IPV6_PKTINFO
2895 				 */
2896 
2897 				if_index = ipp->ipp_ifindex;
2898 				dst_ill = ill_lookup_on_ifindex(
2899 				    if_index, B_TRUE, NULL, NULL, NULL, NULL);
2900 				if (dst_ill == NULL) {
2901 					ip1dbg(("ip_bind_connected_v6:"
2902 					    " bad ifindex %d\n", if_index));
2903 					error = EADDRNOTAVAIL;
2904 					goto bad_addr;
2905 				}
2906 				ill_held = B_TRUE;
2907 			} else if (connp->conn_outgoing_ill != NULL) {
2908 				/*
2909 				 * For IPV6_BOUND_IF socket option,
2910 				 * conn_outgoing_ill should be set
2911 				 * already in TCP or UDP/ICMP.
2912 				 */
2913 				dst_ill = conn_get_held_ill(connp,
2914 				    &connp->conn_outgoing_ill, &err);
2915 				if (err == ILL_LOOKUP_FAILED) {
2916 					ip1dbg(("ip_bind_connected_v6:"
2917 					    "no ill for bound_if\n"));
2918 					error = EADDRNOTAVAIL;
2919 					goto bad_addr;
2920 				}
2921 				ill_held = B_TRUE;
2922 			} else if (dst_ire->ire_stq != NULL) {
2923 				/* No need to hold ill here */
2924 				dst_ill = (ill_t *)dst_ire->ire_stq->q_ptr;
2925 			} else {
2926 				/* No need to hold ill here */
2927 				dst_ill = dst_ire->ire_ipif->ipif_ill;
2928 			}
2929 			if (!ip6_asp_can_lookup()) {
2930 				*mp->b_wptr++ = (char)protocol;
2931 				ip6_asp_pending_op(CONNP_TO_WQ(connp), mp,
2932 				    ip_bind_connected_resume_v6);
2933 				error = EINPROGRESS;
2934 				goto refrele_and_quit;
2935 			}
2936 			src_ipif = ipif_select_source_v6(dst_ill, v6dst,
2937 			    RESTRICT_TO_NONE, connp->conn_src_preferences,
2938 			    zoneid);
2939 			ip6_asp_table_refrele();
2940 			if (src_ipif == NULL) {
2941 				pr_addr_dbg("ip_bind_connected_v6: "
2942 				    "no usable source address for "
2943 				    "connection to %s\n", AF_INET6, v6dst);
2944 				error = EADDRNOTAVAIL;
2945 				goto bad_addr;
2946 			}
2947 			*v6src = src_ipif->ipif_v6lcl_addr;
2948 		}
2949 	}
2950 
2951 	/*
2952 	 * We do ire_route_lookup_v6() here (and not an interface lookup)
2953 	 * as we assert that v6src should only come from an
2954 	 * UP interface for hard binding.
2955 	 */
2956 	src_ire = ire_route_lookup_v6(v6src, 0, 0, 0, NULL,
2957 	    NULL, zoneid, NULL, MATCH_IRE_ZONEONLY);
2958 
2959 	/* src_ire must be a local|loopback */
2960 	if (!IRE_IS_LOCAL(src_ire)) {
2961 		if (ip_debug > 2) {
2962 			/* ip1dbg */
2963 			pr_addr_dbg("ip_bind_connected_v6: bad "
2964 			    "connected src %s\n", AF_INET6, v6src);
2965 		}
2966 		error = EADDRNOTAVAIL;
2967 		goto bad_addr;
2968 	}
2969 
2970 	/*
2971 	 * If the source address is a loopback address, the
2972 	 * destination had best be local or multicast.
2973 	 * The transports that can't handle multicast will reject
2974 	 * those addresses.
2975 	 */
2976 	if (src_ire->ire_type == IRE_LOOPBACK &&
2977 	    !(IRE_IS_LOCAL(dst_ire) || IN6_IS_ADDR_MULTICAST(v6dst) ||
2978 	    IN6_IS_ADDR_V4MAPPED_CLASSD(v6dst))) {
2979 		ip1dbg(("ip_bind_connected_v6: bad connected loopback\n"));
2980 		error = -1;
2981 		goto bad_addr;
2982 	}
2983 	/*
2984 	 * Allow setting new policies. For example, disconnects come
2985 	 * down as ipa_t bind. As we would have set conn_policy_cached
2986 	 * to B_TRUE before, we should set it to B_FALSE, so that policy
2987 	 * can change after the disconnect.
2988 	 */
2989 	connp->conn_policy_cached = B_FALSE;
2990 
2991 	/*
2992 	 * The addresses have been verified. Initialize the conn
2993 	 * before calling the policy as they expect the conns
2994 	 * initialized.
2995 	 */
2996 	connp->conn_srcv6 = *v6src;
2997 	connp->conn_remv6 = *v6dst;
2998 	connp->conn_lport = lport;
2999 	connp->conn_fport = fport;
3000 
3001 	ASSERT(!(ipsec_policy_set && ire_requested));
3002 	if (ire_requested) {
3003 		iulp_t *ulp_info = NULL;
3004 
3005 		/*
3006 		 * Note that sire will not be NULL if this is an off-link
3007 		 * connection and there is not cache for that dest yet.
3008 		 *
3009 		 * XXX Because of an existing bug, if there are multiple
3010 		 * default routes, the IRE returned now may not be the actual
3011 		 * default route used (default routes are chosen in a
3012 		 * round robin fashion).  So if the metrics for different
3013 		 * default routes are different, we may return the wrong
3014 		 * metrics.  This will not be a problem if the existing
3015 		 * bug is fixed.
3016 		 */
3017 		if (sire != NULL)
3018 			ulp_info = &(sire->ire_uinfo);
3019 
3020 		if (!ip_bind_insert_ire_v6(mp, dst_ire, v6dst, ulp_info)) {
3021 			error = -1;
3022 			goto bad_addr;
3023 		}
3024 	} else if (ipsec_policy_set) {
3025 		if (!ip_bind_ipsec_policy_set(connp, policy_mp)) {
3026 			error = -1;
3027 			goto bad_addr;
3028 		}
3029 	}
3030 
3031 	/*
3032 	 * Cache IPsec policy in this conn.  If we have per-socket policy,
3033 	 * we'll cache that.  If we don't, we'll inherit global policy.
3034 	 *
3035 	 * We can't insert until the conn reflects the policy. Note that
3036 	 * conn_policy_cached is set by ipsec_conn_cache_policy() even for
3037 	 * connections where we don't have a policy. This is to prevent
3038 	 * global policy lookups in the inbound path.
3039 	 *
3040 	 * If we insert before we set conn_policy_cached,
3041 	 * CONN_INBOUND_POLICY_PRESENT_V6() check can still evaluate true
3042 	 * because global policy cound be non-empty. We normally call
3043 	 * ipsec_check_policy() for conn_policy_cached connections only if
3044 	 * conn_in_enforce_policy is set. But in this case,
3045 	 * conn_policy_cached can get set anytime since we made the
3046 	 * CONN_INBOUND_POLICY_PRESENT_V6() check and ipsec_check_policy()
3047 	 * is called, which will make the above assumption false.  Thus, we
3048 	 * need to insert after we set conn_policy_cached.
3049 	 */
3050 	if ((error = ipsec_conn_cache_policy(connp, B_FALSE)) != 0)
3051 		goto bad_addr;
3052 
3053 	/* If not fanout_insert this was just an address verification */
3054 	if (fanout_insert) {
3055 		/*
3056 		 * The addresses have been verified. Time to insert in
3057 		 * the correct fanout list.
3058 		 * We need to make sure that the conn_recv is set to a non-null
3059 		 * value before we insert the conn_t into the classifier table.
3060 		 * This is to avoid a race with an incoming packet which does
3061 		 * an ipcl_classify().
3062 		 */
3063 		if (protocol == IPPROTO_TCP)
3064 			connp->conn_recv = tcp_input;
3065 		error = ipcl_conn_insert_v6(connp, protocol, v6src, v6dst,
3066 		    connp->conn_ports,
3067 		    IPCL_IS_TCP(connp) ? connp->conn_tcp->tcp_bound_if : 0);
3068 	}
3069 	if (error == 0) {
3070 		connp->conn_fully_bound = B_TRUE;
3071 		/*
3072 		 * Our initial checks for MDT have passed; the IRE is not
3073 		 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to
3074 		 * be supporting MDT.  Pass the IRE, IPC and ILL into
3075 		 * ip_mdinfo_return(), which performs further checks
3076 		 * against them and upon success, returns the MDT info
3077 		 * mblk which we will attach to the bind acknowledgment.
3078 		 */
3079 		if (md_dst_ire != NULL) {
3080 			mblk_t *mdinfo_mp;
3081 
3082 			ASSERT(md_ill != NULL);
3083 			ASSERT(md_ill->ill_mdt_capab != NULL);
3084 			if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp,
3085 			    md_ill->ill_name, md_ill->ill_mdt_capab)) != NULL)
3086 				linkb(mp, mdinfo_mp);
3087 		}
3088 	}
3089 bad_addr:
3090 	if (ipsec_policy_set) {
3091 		ASSERT(policy_mp != NULL);
3092 		freeb(policy_mp);
3093 		/*
3094 		 * As of now assume that nothing else accompanies
3095 		 * IPSEC_POLICY_SET.
3096 		 */
3097 		mp->b_cont = NULL;
3098 	}
3099 refrele_and_quit:
3100 	if (src_ire != NULL)
3101 		IRE_REFRELE(src_ire);
3102 	if (dst_ire != NULL)
3103 		IRE_REFRELE(dst_ire);
3104 	if (sire != NULL)
3105 		IRE_REFRELE(sire);
3106 	if (src_ipif != NULL)
3107 		ipif_refrele(src_ipif);
3108 	if (md_dst_ire != NULL)
3109 		IRE_REFRELE(md_dst_ire);
3110 	if (ill_held && dst_ill != NULL)
3111 		ill_refrele(dst_ill);
3112 	return (error);
3113 }
3114 
3115 /*
3116  * Insert the ire in b_cont. Returns false if it fails (due to lack of space).
3117  * Makes the IRE be IRE_BROADCAST if dst is a multicast address.
3118  */
3119 static boolean_t
3120 ip_bind_insert_ire_v6(mblk_t *mp, ire_t *ire, const in6_addr_t *dst,
3121     iulp_t *ulp_info)
3122 {
3123 	mblk_t	*mp1;
3124 	ire_t	*ret_ire;
3125 
3126 	mp1 = mp->b_cont;
3127 	ASSERT(mp1 != NULL);
3128 
3129 	if (ire != NULL) {
3130 		/*
3131 		 * mp1 initialized above to IRE_DB_REQ_TYPE
3132 		 * appended mblk. Its <upper protocol>'s
3133 		 * job to make sure there is room.
3134 		 */
3135 		if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t))
3136 			return (B_FALSE);
3137 
3138 		mp1->b_datap->db_type = IRE_DB_TYPE;
3139 		mp1->b_wptr = mp1->b_rptr + sizeof (ire_t);
3140 		bcopy(ire, mp1->b_rptr, sizeof (ire_t));
3141 		ret_ire = (ire_t *)mp1->b_rptr;
3142 		if (IN6_IS_ADDR_MULTICAST(dst) ||
3143 		    IN6_IS_ADDR_V4MAPPED_CLASSD(dst)) {
3144 			ret_ire->ire_type = IRE_BROADCAST;
3145 			ret_ire->ire_addr_v6 = *dst;
3146 		}
3147 		if (ulp_info != NULL) {
3148 			bcopy(ulp_info, &(ret_ire->ire_uinfo),
3149 			    sizeof (iulp_t));
3150 		}
3151 		ret_ire->ire_mp = mp1;
3152 	} else {
3153 		/*
3154 		 * No IRE was found. Remove IRE mblk.
3155 		 */
3156 		mp->b_cont = mp1->b_cont;
3157 		freeb(mp1);
3158 	}
3159 	return (B_TRUE);
3160 }
3161 
3162 /*
3163  * Add an ip6i_t header to the front of the mblk.
3164  * Inline if possible else allocate a separate mblk containing only the ip6i_t.
3165  * Returns NULL if allocation fails (and frees original message).
3166  * Used in outgoing path when going through ip_newroute_*v6().
3167  * Used in incoming path to pass ifindex to transports.
3168  */
3169 mblk_t *
3170 ip_add_info_v6(mblk_t *mp, ill_t *ill, const in6_addr_t *dst)
3171 {
3172 	mblk_t *mp1;
3173 	ip6i_t *ip6i;
3174 	ip6_t *ip6h;
3175 
3176 	ip6h = (ip6_t *)mp->b_rptr;
3177 	ip6i = (ip6i_t *)(mp->b_rptr - sizeof (ip6i_t));
3178 	if ((uchar_t *)ip6i < mp->b_datap->db_base ||
3179 	    mp->b_datap->db_ref > 1) {
3180 		mp1 = allocb(sizeof (ip6i_t), BPRI_MED);
3181 		if (mp1 == NULL) {
3182 			freemsg(mp);
3183 			return (NULL);
3184 		}
3185 		mp1->b_wptr = mp1->b_rptr = mp1->b_datap->db_lim;
3186 		mp1->b_cont = mp;
3187 		mp = mp1;
3188 		ip6i = (ip6i_t *)(mp->b_rptr - sizeof (ip6i_t));
3189 	}
3190 	mp->b_rptr = (uchar_t *)ip6i;
3191 	ip6i->ip6i_vcf = ip6h->ip6_vcf;
3192 	ip6i->ip6i_nxt = IPPROTO_RAW;
3193 	if (ill != NULL) {
3194 		ip6i->ip6i_flags = IP6I_IFINDEX;
3195 		ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex;
3196 	} else {
3197 		ip6i->ip6i_flags = 0;
3198 	}
3199 	ip6i->ip6i_nexthop = *dst;
3200 	return (mp);
3201 }
3202 
3203 /*
3204  * Handle protocols with which IP is less intimate.  There
3205  * can be more than one stream bound to a particular
3206  * protocol.  When this is the case, normally each one gets a copy
3207  * of any incoming packets.
3208  * However, if the packet was tunneled and not multicast we only send to it
3209  * the first match.
3210  *
3211  * Zones notes:
3212  * Packets will be distributed to streams in all zones. This is really only
3213  * useful for ICMPv6 as only applications in the global zone can create raw
3214  * sockets for other protocols.
3215  */
3216 static void
3217 ip_fanout_proto_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, ill_t *ill,
3218     ill_t *inill, uint8_t nexthdr, uint_t nexthdr_offset, uint_t flags,
3219     boolean_t mctl_present, zoneid_t zoneid)
3220 {
3221 	queue_t	*rq;
3222 	mblk_t	*mp1, *first_mp1;
3223 	in6_addr_t dst = ip6h->ip6_dst;
3224 	in6_addr_t src = ip6h->ip6_src;
3225 	boolean_t one_only;
3226 	mblk_t *first_mp = mp;
3227 	boolean_t secure, shared_addr;
3228 	conn_t	*connp, *first_connp, *next_connp;
3229 	connf_t *connfp;
3230 
3231 	if (mctl_present) {
3232 		mp = first_mp->b_cont;
3233 		secure = ipsec_in_is_secure(first_mp);
3234 		ASSERT(mp != NULL);
3235 	} else {
3236 		secure = B_FALSE;
3237 	}
3238 
3239 	/*
3240 	 * If the packet was tunneled and not multicast we only send to it
3241 	 * the first match.
3242 	 */
3243 	one_only = ((nexthdr == IPPROTO_ENCAP || nexthdr == IPPROTO_IPV6) &&
3244 	    !IN6_IS_ADDR_MULTICAST(&dst));
3245 
3246 	shared_addr = (zoneid == ALL_ZONES);
3247 	if (shared_addr) {
3248 		/*
3249 		 * We don't allow multilevel ports for raw IP, so no need to
3250 		 * check for that here.
3251 		 */
3252 		zoneid = tsol_packet_to_zoneid(mp);
3253 	}
3254 
3255 	connfp = &ipcl_proto_fanout_v6[nexthdr];
3256 	mutex_enter(&connfp->connf_lock);
3257 	connp = connfp->connf_head;
3258 	for (connp = connfp->connf_head; connp != NULL;
3259 		connp = connp->conn_next) {
3260 		if (IPCL_PROTO_MATCH_V6(connp, nexthdr, ip6h, ill, flags,
3261 		    zoneid) &&
3262 		    (!is_system_labeled() ||
3263 		    tsol_receive_local(mp, &dst, IPV6_VERSION, shared_addr,
3264 		    connp)))
3265 			break;
3266 	}
3267 
3268 	if (connp == NULL || connp->conn_upq == NULL) {
3269 		/*
3270 		 * No one bound to this port.  Is
3271 		 * there a client that wants all
3272 		 * unclaimed datagrams?
3273 		 */
3274 		mutex_exit(&connfp->connf_lock);
3275 		if (ip_fanout_send_icmp_v6(q, first_mp, flags,
3276 		    ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER,
3277 		    nexthdr_offset, mctl_present, zoneid)) {
3278 			BUMP_MIB(ill->ill_ip6_mib, ipv6InUnknownProtos);
3279 		}
3280 
3281 		return;
3282 	}
3283 
3284 	CONN_INC_REF(connp);
3285 	first_connp = connp;
3286 
3287 	/*
3288 	 * XXX: Fix the multiple protocol listeners case. We should not
3289 	 * be walking the conn->next list here.
3290 	 */
3291 	if (one_only) {
3292 		/*
3293 		 * Only send message to one tunnel driver by immediately
3294 		 * terminating the loop.
3295 		 */
3296 		connp = NULL;
3297 	} else {
3298 		connp = connp->conn_next;
3299 
3300 	}
3301 	for (;;) {
3302 		while (connp != NULL) {
3303 			if (IPCL_PROTO_MATCH_V6(connp, nexthdr, ip6h, ill,
3304 			    flags, zoneid) &&
3305 			    (!is_system_labeled() ||
3306 			    tsol_receive_local(mp, &dst, IPV6_VERSION,
3307 			    shared_addr, connp)))
3308 				break;
3309 			connp = connp->conn_next;
3310 		}
3311 
3312 		/*
3313 		 * Just copy the data part alone. The mctl part is
3314 		 * needed just for verifying policy and it is never
3315 		 * sent up.
3316 		 */
3317 		if (connp == NULL || connp->conn_upq == NULL ||
3318 		    (((first_mp1 = dupmsg(first_mp)) == NULL) &&
3319 		    ((first_mp1 = ip_copymsg(first_mp)) == NULL))) {
3320 			/*
3321 			 * No more intested clients or memory
3322 			 * allocation failed
3323 			 */
3324 			connp = first_connp;
3325 			break;
3326 		}
3327 		mp1 = mctl_present ? first_mp1->b_cont : first_mp1;
3328 		CONN_INC_REF(connp);
3329 		mutex_exit(&connfp->connf_lock);
3330 		rq = connp->conn_rq;
3331 		/*
3332 		 * For link-local always add ifindex so that transport can set
3333 		 * sin6_scope_id. Avoid it for ICMP error fanout.
3334 		 */
3335 		if ((connp->conn_ipv6_recvpktinfo ||
3336 		    IN6_IS_ADDR_LINKLOCAL(&src)) &&
3337 		    (flags & IP_FF_IP6INFO)) {
3338 			/* Add header */
3339 			mp1 = ip_add_info_v6(mp1, inill, &dst);
3340 		}
3341 		if (mp1 == NULL) {
3342 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
3343 		} else if (!canputnext(rq)) {
3344 			if (flags & IP_FF_RAWIP) {
3345 				BUMP_MIB(ill->ill_ip6_mib, rawipInOverflows);
3346 			} else {
3347 				BUMP_MIB(ill->ill_icmp6_mib,
3348 				    ipv6IfIcmpInOverflows);
3349 			}
3350 
3351 			freemsg(mp1);
3352 		} else {
3353 			/*
3354 			 * Don't enforce here if we're a tunnel - let "tun" do
3355 			 * it instead.
3356 			 */
3357 			if (!IPCL_IS_IPTUN(connp) &&
3358 			    (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure)) {
3359 				first_mp1 = ipsec_check_inbound_policy
3360 				    (first_mp1, connp, NULL, ip6h,
3361 				    mctl_present);
3362 			}
3363 			if (first_mp1 != NULL) {
3364 				if (mctl_present)
3365 					freeb(first_mp1);
3366 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
3367 				putnext(rq, mp1);
3368 			}
3369 		}
3370 		mutex_enter(&connfp->connf_lock);
3371 		/* Follow the next pointer before releasing the conn. */
3372 		next_connp = connp->conn_next;
3373 		CONN_DEC_REF(connp);
3374 		connp = next_connp;
3375 	}
3376 
3377 	/* Last one.  Send it upstream. */
3378 	mutex_exit(&connfp->connf_lock);
3379 
3380 	/* Initiate IPPF processing */
3381 	if (IP6_IN_IPP(flags)) {
3382 		uint_t ifindex;
3383 
3384 		mutex_enter(&ill->ill_lock);
3385 		ifindex = ill->ill_phyint->phyint_ifindex;
3386 		mutex_exit(&ill->ill_lock);
3387 		ip_process(IPP_LOCAL_IN, &mp, ifindex);
3388 		if (mp == NULL) {
3389 			CONN_DEC_REF(connp);
3390 			if (mctl_present)
3391 				freeb(first_mp);
3392 			return;
3393 		}
3394 	}
3395 
3396 	/*
3397 	 * For link-local always add ifindex so that transport can set
3398 	 * sin6_scope_id. Avoid it for ICMP error fanout.
3399 	 */
3400 	if ((connp->conn_ipv6_recvpktinfo || IN6_IS_ADDR_LINKLOCAL(&src)) &&
3401 	    (flags & IP_FF_IP6INFO)) {
3402 		/* Add header */
3403 		mp = ip_add_info_v6(mp, inill, &dst);
3404 		if (mp == NULL) {
3405 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
3406 			CONN_DEC_REF(connp);
3407 			if (mctl_present)
3408 				freeb(first_mp);
3409 			return;
3410 		} else if (mctl_present) {
3411 			first_mp->b_cont = mp;
3412 		} else {
3413 			first_mp = mp;
3414 		}
3415 	}
3416 
3417 	rq = connp->conn_rq;
3418 	if (!canputnext(rq)) {
3419 		if (flags & IP_FF_RAWIP) {
3420 			BUMP_MIB(ill->ill_ip6_mib, rawipInOverflows);
3421 		} else {
3422 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
3423 		}
3424 
3425 		freemsg(first_mp);
3426 	} else {
3427 		if (IPCL_IS_IPTUN(connp)) {
3428 			/*
3429 			 * Tunneled packet.  We enforce policy in the tunnel
3430 			 * module itself.
3431 			 *
3432 			 * Send the WHOLE packet up (incl. IPSEC_IN) without
3433 			 * a policy check.
3434 			 */
3435 			putnext(rq, first_mp);
3436 			CONN_DEC_REF(connp);
3437 			return;
3438 		}
3439 		/*
3440 		 * Don't enforce here if we're a tunnel - let "tun" do
3441 		 * it instead.
3442 		 */
3443 		if (nexthdr != IPPROTO_ENCAP && nexthdr != IPPROTO_IPV6 &&
3444 		    (CONN_INBOUND_POLICY_PRESENT(connp) || secure)) {
3445 			first_mp = ipsec_check_inbound_policy(first_mp, connp,
3446 			    NULL, ip6h, mctl_present);
3447 			if (first_mp == NULL) {
3448 				CONN_DEC_REF(connp);
3449 				return;
3450 			}
3451 		}
3452 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
3453 		putnext(rq, mp);
3454 		if (mctl_present)
3455 			freeb(first_mp);
3456 	}
3457 	CONN_DEC_REF(connp);
3458 }
3459 
3460 /*
3461  * Send an ICMP error after patching up the packet appropriately.  Returns
3462  * non-zero if the appropriate MIB should be bumped; zero otherwise.
3463  */
3464 int
3465 ip_fanout_send_icmp_v6(queue_t *q, mblk_t *mp, uint_t flags,
3466     uint_t icmp_type, uint8_t icmp_code, uint_t nexthdr_offset,
3467     boolean_t mctl_present, zoneid_t zoneid)
3468 {
3469 	ip6_t *ip6h;
3470 	mblk_t *first_mp;
3471 	boolean_t secure;
3472 	unsigned char db_type;
3473 
3474 	first_mp = mp;
3475 	if (mctl_present) {
3476 		mp = mp->b_cont;
3477 		secure = ipsec_in_is_secure(first_mp);
3478 		ASSERT(mp != NULL);
3479 	} else {
3480 		/*
3481 		 * If this is an ICMP error being reported - which goes
3482 		 * up as M_CTLs, we need to convert them to M_DATA till
3483 		 * we finish checking with global policy because
3484 		 * ipsec_check_global_policy() assumes M_DATA as clear
3485 		 * and M_CTL as secure.
3486 		 */
3487 		db_type = mp->b_datap->db_type;
3488 		mp->b_datap->db_type = M_DATA;
3489 		secure = B_FALSE;
3490 	}
3491 	/*
3492 	 * We are generating an icmp error for some inbound packet.
3493 	 * Called from all ip_fanout_(udp, tcp, proto) functions.
3494 	 * Before we generate an error, check with global policy
3495 	 * to see whether this is allowed to enter the system. As
3496 	 * there is no "conn", we are checking with global policy.
3497 	 */
3498 	ip6h = (ip6_t *)mp->b_rptr;
3499 	if (secure || ipsec_inbound_v6_policy_present) {
3500 		first_mp = ipsec_check_global_policy(first_mp, NULL,
3501 		    NULL, ip6h, mctl_present);
3502 		if (first_mp == NULL)
3503 			return (0);
3504 	}
3505 
3506 	if (!mctl_present)
3507 		mp->b_datap->db_type = db_type;
3508 
3509 	if (flags & IP_FF_SEND_ICMP) {
3510 		if (flags & IP_FF_HDR_COMPLETE) {
3511 			if (ip_hdr_complete_v6(ip6h, zoneid)) {
3512 				freemsg(first_mp);
3513 				return (1);
3514 			}
3515 		}
3516 		switch (icmp_type) {
3517 		case ICMP6_DST_UNREACH:
3518 			icmp_unreachable_v6(WR(q), first_mp, icmp_code,
3519 			    B_FALSE, B_FALSE, zoneid);
3520 			break;
3521 		case ICMP6_PARAM_PROB:
3522 			icmp_param_problem_v6(WR(q), first_mp, icmp_code,
3523 			    nexthdr_offset, B_FALSE, B_FALSE, zoneid);
3524 			break;
3525 		default:
3526 #ifdef DEBUG
3527 			panic("ip_fanout_send_icmp_v6: wrong type");
3528 			/*NOTREACHED*/
3529 #else
3530 			freemsg(first_mp);
3531 			break;
3532 #endif
3533 		}
3534 	} else {
3535 		freemsg(first_mp);
3536 		return (0);
3537 	}
3538 
3539 	return (1);
3540 }
3541 
3542 
3543 /*
3544  * Fanout for TCP packets
3545  * The caller puts <fport, lport> in the ports parameter.
3546  */
3547 static void
3548 ip_fanout_tcp_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, ill_t *ill, ill_t *inill,
3549     uint_t flags, uint_t hdr_len, boolean_t mctl_present, zoneid_t zoneid)
3550 {
3551 	mblk_t  	*first_mp;
3552 	boolean_t 	secure;
3553 	conn_t		*connp;
3554 	tcph_t		*tcph;
3555 	boolean_t	syn_present = B_FALSE;
3556 
3557 	first_mp = mp;
3558 	if (mctl_present) {
3559 		mp = first_mp->b_cont;
3560 		secure = ipsec_in_is_secure(first_mp);
3561 		ASSERT(mp != NULL);
3562 	} else {
3563 		secure = B_FALSE;
3564 	}
3565 
3566 	connp = ipcl_classify_v6(mp, IPPROTO_TCP, hdr_len, zoneid);
3567 
3568 	if (connp == NULL ||
3569 	    !conn_wantpacket_v6(connp, ill, ip6h, flags, zoneid)) {
3570 		/*
3571 		 * No hard-bound match. Send Reset.
3572 		 */
3573 		dblk_t *dp = mp->b_datap;
3574 		uint32_t ill_index;
3575 
3576 		ASSERT((dp->db_struioflag & STRUIO_IP) == 0);
3577 
3578 		/* Initiate IPPf processing, if needed. */
3579 		if (IPP_ENABLED(IPP_LOCAL_IN) && (flags & IP6_NO_IPPOLICY)) {
3580 			ill_index = ill->ill_phyint->phyint_ifindex;
3581 			ip_process(IPP_LOCAL_IN, &first_mp, ill_index);
3582 			if (first_mp == NULL) {
3583 				if (connp != NULL)
3584 					CONN_DEC_REF(connp);
3585 				return;
3586 			}
3587 		}
3588 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
3589 		tcp_xmit_listeners_reset(first_mp, hdr_len, zoneid);
3590 		if (connp != NULL)
3591 			CONN_DEC_REF(connp);
3592 		return;
3593 	}
3594 
3595 	tcph = (tcph_t *)&mp->b_rptr[hdr_len];
3596 	if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) {
3597 		if (connp->conn_flags & IPCL_TCP) {
3598 			squeue_t *sqp;
3599 
3600 			/*
3601 			 * For fused tcp loopback, assign the eager's
3602 			 * squeue to be that of the active connect's.
3603 			 */
3604 			if ((flags & IP_FF_LOOPBACK) && do_tcp_fusion &&
3605 			    !CONN_INBOUND_POLICY_PRESENT_V6(connp) && !secure &&
3606 			    !IP6_IN_IPP(flags)) {
3607 				ASSERT(Q_TO_CONN(q) != NULL);
3608 				sqp = Q_TO_CONN(q)->conn_sqp;
3609 			} else {
3610 				sqp = IP_SQUEUE_GET(lbolt);
3611 			}
3612 
3613 			mp->b_datap->db_struioflag |= STRUIO_EAGER;
3614 			DB_CKSUMSTART(mp) = (intptr_t)sqp;
3615 
3616 			/*
3617 			 * db_cksumstuff is unused in the incoming
3618 			 * path; Thus store the ifindex here. It will
3619 			 * be cleared in tcp_conn_create_v6().
3620 			 */
3621 			DB_CKSUMSTUFF(mp) =
3622 			    (intptr_t)ill->ill_phyint->phyint_ifindex;
3623 			syn_present = B_TRUE;
3624 		}
3625 	}
3626 
3627 	if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) {
3628 		uint_t	flags = (unsigned int)tcph->th_flags[0] & 0xFF;
3629 		if ((flags & TH_RST) || (flags & TH_URG)) {
3630 			CONN_DEC_REF(connp);
3631 			freemsg(first_mp);
3632 			return;
3633 		}
3634 		if (flags & TH_ACK) {
3635 			tcp_xmit_listeners_reset(first_mp, hdr_len, zoneid);
3636 			CONN_DEC_REF(connp);
3637 			return;
3638 		}
3639 
3640 		CONN_DEC_REF(connp);
3641 		freemsg(first_mp);
3642 		return;
3643 	}
3644 
3645 	if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) {
3646 		first_mp = ipsec_check_inbound_policy(first_mp, connp,
3647 		    NULL, ip6h, mctl_present);
3648 		if (first_mp == NULL) {
3649 			CONN_DEC_REF(connp);
3650 			return;
3651 		}
3652 		if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) {
3653 			ASSERT(syn_present);
3654 			if (mctl_present) {
3655 				ASSERT(first_mp != mp);
3656 				first_mp->b_datap->db_struioflag |=
3657 				    STRUIO_POLICY;
3658 			} else {
3659 				ASSERT(first_mp == mp);
3660 				mp->b_datap->db_struioflag &=
3661 				    ~STRUIO_EAGER;
3662 				mp->b_datap->db_struioflag |=
3663 				    STRUIO_POLICY;
3664 			}
3665 		} else {
3666 			/*
3667 			 * Discard first_mp early since we're dealing with a
3668 			 * fully-connected conn_t and tcp doesn't do policy in
3669 			 * this case. Also, if someone is bound to IPPROTO_TCP
3670 			 * over raw IP, they don't expect to see a M_CTL.
3671 			 */
3672 			if (mctl_present) {
3673 				freeb(first_mp);
3674 				mctl_present = B_FALSE;
3675 			}
3676 			first_mp = mp;
3677 		}
3678 	}
3679 
3680 	/* Initiate IPPF processing */
3681 	if (IP6_IN_IPP(flags)) {
3682 		uint_t	ifindex;
3683 
3684 		mutex_enter(&ill->ill_lock);
3685 		ifindex = ill->ill_phyint->phyint_ifindex;
3686 		mutex_exit(&ill->ill_lock);
3687 		ip_process(IPP_LOCAL_IN, &mp, ifindex);
3688 		if (mp == NULL) {
3689 			CONN_DEC_REF(connp);
3690 			if (mctl_present) {
3691 				freeb(first_mp);
3692 			}
3693 			return;
3694 		} else if (mctl_present) {
3695 			/*
3696 			 * ip_add_info_v6 might return a new mp.
3697 			 */
3698 			ASSERT(first_mp != mp);
3699 			first_mp->b_cont = mp;
3700 		} else {
3701 			first_mp = mp;
3702 		}
3703 	}
3704 
3705 	/*
3706 	 * For link-local always add ifindex so that TCP can bind to that
3707 	 * interface. Avoid it for ICMP error fanout.
3708 	 */
3709 	if (!syn_present && ((connp->conn_ipv6_recvpktinfo ||
3710 	    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) &&
3711 	    (flags & IP_FF_IP6INFO))) {
3712 		/* Add header */
3713 		mp = ip_add_info_v6(mp, inill, &ip6h->ip6_dst);
3714 		if (mp == NULL) {
3715 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
3716 			CONN_DEC_REF(connp);
3717 			if (mctl_present)
3718 				freeb(first_mp);
3719 			return;
3720 		} else if (mctl_present) {
3721 			ASSERT(first_mp != mp);
3722 			first_mp->b_cont = mp;
3723 		} else {
3724 			first_mp = mp;
3725 		}
3726 	}
3727 
3728 	BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
3729 	if (IPCL_IS_TCP(connp)) {
3730 		(*ip_input_proc)(connp->conn_sqp, first_mp,
3731 		    connp->conn_recv, connp, SQTAG_IP6_TCP_INPUT);
3732 	} else {
3733 		putnext(connp->conn_rq, first_mp);
3734 		CONN_DEC_REF(connp);
3735 	}
3736 }
3737 
3738 /*
3739  * Fanout for UDP packets.
3740  * The caller puts <fport, lport> in the ports parameter.
3741  * ire_type must be IRE_BROADCAST for multicast and broadcast packets.
3742  *
3743  * If SO_REUSEADDR is set all multicast and broadcast packets
3744  * will be delivered to all streams bound to the same port.
3745  *
3746  * Zones notes:
3747  * Multicast packets will be distributed to streams in all zones.
3748  */
3749 static void
3750 ip_fanout_udp_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, uint32_t ports,
3751     ill_t *ill, ill_t *inill, uint_t flags, boolean_t mctl_present,
3752     zoneid_t zoneid)
3753 {
3754 	uint32_t	dstport, srcport;
3755 	in6_addr_t	dst;
3756 	mblk_t		*first_mp;
3757 	boolean_t	secure;
3758 	conn_t		*connp;
3759 	connf_t		*connfp;
3760 	conn_t		*first_conn;
3761 	conn_t 		*next_conn;
3762 	mblk_t		*mp1, *first_mp1;
3763 	in6_addr_t	src;
3764 	boolean_t	shared_addr;
3765 
3766 	first_mp = mp;
3767 	if (mctl_present) {
3768 		mp = first_mp->b_cont;
3769 		secure = ipsec_in_is_secure(first_mp);
3770 		ASSERT(mp != NULL);
3771 	} else {
3772 		secure = B_FALSE;
3773 	}
3774 
3775 	/* Extract ports in net byte order */
3776 	dstport = htons(ntohl(ports) & 0xFFFF);
3777 	srcport = htons(ntohl(ports) >> 16);
3778 	dst = ip6h->ip6_dst;
3779 	src = ip6h->ip6_src;
3780 
3781 	shared_addr = (zoneid == ALL_ZONES);
3782 	if (shared_addr) {
3783 		zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport);
3784 		/*
3785 		 * If no shared MLP is found, tsol_mlp_findzone returns
3786 		 * ALL_ZONES.  In that case, we assume it's SLP, and
3787 		 * search for the zone based on the packet label.
3788 		 * That will also return ALL_ZONES on failure, but
3789 		 * we never allow conn_zoneid to be set to ALL_ZONES.
3790 		 */
3791 		if (zoneid == ALL_ZONES)
3792 			zoneid = tsol_packet_to_zoneid(mp);
3793 	}
3794 
3795 	/* Attempt to find a client stream based on destination port. */
3796 	connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)];
3797 	mutex_enter(&connfp->connf_lock);
3798 	connp = connfp->connf_head;
3799 	if (!IN6_IS_ADDR_MULTICAST(&dst)) {
3800 		/*
3801 		 * Not multicast. Send to the one (first) client we find.
3802 		 */
3803 		while (connp != NULL) {
3804 			if (IPCL_UDP_MATCH_V6(connp, dstport, dst, srcport,
3805 			    src) && IPCL_ZONE_MATCH(connp, zoneid) &&
3806 			    conn_wantpacket_v6(connp, ill, ip6h,
3807 			    flags, zoneid)) {
3808 				break;
3809 			}
3810 			connp = connp->conn_next;
3811 		}
3812 		if (connp == NULL || connp->conn_upq == NULL)
3813 			goto notfound;
3814 
3815 		if (is_system_labeled() &&
3816 		    !tsol_receive_local(mp, &dst, IPV6_VERSION, shared_addr,
3817 		    connp))
3818 			goto notfound;
3819 
3820 		/* Found a client */
3821 		CONN_INC_REF(connp);
3822 		mutex_exit(&connfp->connf_lock);
3823 
3824 		if (CONN_UDP_FLOWCTLD(connp)) {
3825 			freemsg(first_mp);
3826 			CONN_DEC_REF(connp);
3827 			return;
3828 		}
3829 		if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) {
3830 			first_mp = ipsec_check_inbound_policy(first_mp,
3831 			    connp, NULL, ip6h, mctl_present);
3832 			if (first_mp == NULL) {
3833 				CONN_DEC_REF(connp);
3834 				return;
3835 			}
3836 		}
3837 		/* Initiate IPPF processing */
3838 		if (IP6_IN_IPP(flags)) {
3839 			uint_t	ifindex;
3840 
3841 			mutex_enter(&ill->ill_lock);
3842 			ifindex = ill->ill_phyint->phyint_ifindex;
3843 			mutex_exit(&ill->ill_lock);
3844 			ip_process(IPP_LOCAL_IN, &mp, ifindex);
3845 			if (mp == NULL) {
3846 				CONN_DEC_REF(connp);
3847 				if (mctl_present)
3848 					freeb(first_mp);
3849 				return;
3850 			}
3851 		}
3852 		/*
3853 		 * For link-local always add ifindex so that
3854 		 * transport can set sin6_scope_id. Avoid it for
3855 		 * ICMP error fanout.
3856 		 */
3857 		if ((connp->conn_ipv6_recvpktinfo ||
3858 		    IN6_IS_ADDR_LINKLOCAL(&src)) &&
3859 		    (flags & IP_FF_IP6INFO)) {
3860 				/* Add header */
3861 			mp = ip_add_info_v6(mp, inill, &dst);
3862 			if (mp == NULL) {
3863 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
3864 				CONN_DEC_REF(connp);
3865 				if (mctl_present)
3866 					freeb(first_mp);
3867 				return;
3868 			} else if (mctl_present) {
3869 				first_mp->b_cont = mp;
3870 			} else {
3871 				first_mp = mp;
3872 			}
3873 		}
3874 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
3875 
3876 		/* Send it upstream */
3877 		CONN_UDP_RECV(connp, mp);
3878 
3879 		IP6_STAT(ip6_udp_fannorm);
3880 		CONN_DEC_REF(connp);
3881 		if (mctl_present)
3882 			freeb(first_mp);
3883 		return;
3884 	}
3885 
3886 	while (connp != NULL) {
3887 		if ((IPCL_UDP_MATCH_V6(connp, dstport, dst, srcport, src)) &&
3888 		    conn_wantpacket_v6(connp, ill, ip6h, flags, zoneid) &&
3889 		    (!is_system_labeled() ||
3890 		    tsol_receive_local(mp, &dst, IPV6_VERSION, shared_addr,
3891 		    connp)))
3892 			break;
3893 		connp = connp->conn_next;
3894 	}
3895 
3896 	if (connp == NULL || connp->conn_upq == NULL)
3897 		goto notfound;
3898 
3899 	first_conn = connp;
3900 
3901 	CONN_INC_REF(connp);
3902 	connp = connp->conn_next;
3903 	for (;;) {
3904 		while (connp != NULL) {
3905 			if (IPCL_UDP_MATCH_V6(connp, dstport, dst, srcport,
3906 			    src) && conn_wantpacket_v6(connp, ill, ip6h,
3907 			    flags, zoneid) &&
3908 			    (!is_system_labeled() ||
3909 			    tsol_receive_local(mp, &dst, IPV6_VERSION,
3910 			    shared_addr, connp)))
3911 				break;
3912 			connp = connp->conn_next;
3913 		}
3914 		/*
3915 		 * Just copy the data part alone. The mctl part is
3916 		 * needed just for verifying policy and it is never
3917 		 * sent up.
3918 		 */
3919 		if (connp == NULL ||
3920 		    (((first_mp1 = dupmsg(first_mp)) == NULL) &&
3921 			((first_mp1 = ip_copymsg(first_mp))
3922 			    == NULL))) {
3923 			/*
3924 			 * No more interested clients or memory
3925 			 * allocation failed
3926 			 */
3927 			connp = first_conn;
3928 			break;
3929 		}
3930 		mp1 = mctl_present ? first_mp1->b_cont : first_mp1;
3931 		CONN_INC_REF(connp);
3932 		mutex_exit(&connfp->connf_lock);
3933 		/*
3934 		 * For link-local always add ifindex so that transport
3935 		 * can set sin6_scope_id. Avoid it for ICMP error
3936 		 * fanout.
3937 		 */
3938 		if ((connp->conn_ipv6_recvpktinfo ||
3939 		    IN6_IS_ADDR_LINKLOCAL(&src)) &&
3940 		    (flags & IP_FF_IP6INFO)) {
3941 			/* Add header */
3942 			mp1 = ip_add_info_v6(mp1, inill, &dst);
3943 		}
3944 		/* mp1 could have changed */
3945 		if (mctl_present)
3946 			first_mp1->b_cont = mp1;
3947 		else
3948 			first_mp1 = mp1;
3949 		if (mp1 == NULL) {
3950 			if (mctl_present)
3951 				freeb(first_mp1);
3952 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
3953 			goto next_one;
3954 		}
3955 		if (CONN_UDP_FLOWCTLD(connp)) {
3956 			BUMP_MIB(ill->ill_ip6_mib, udpInOverflows);
3957 			freemsg(first_mp1);
3958 			goto next_one;
3959 		}
3960 
3961 		if (CONN_INBOUND_POLICY_PRESENT_V6(connp) ||
3962 		    secure) {
3963 			first_mp1 = ipsec_check_inbound_policy
3964 			    (first_mp1, connp, NULL, ip6h,
3965 			    mctl_present);
3966 		}
3967 		if (first_mp1 != NULL) {
3968 			if (mctl_present)
3969 				freeb(first_mp1);
3970 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
3971 
3972 			/* Send it upstream */
3973 			CONN_UDP_RECV(connp, mp1);
3974 		}
3975 next_one:
3976 		mutex_enter(&connfp->connf_lock);
3977 		/* Follow the next pointer before releasing the conn. */
3978 		next_conn = connp->conn_next;
3979 		IP6_STAT(ip6_udp_fanmb);
3980 		CONN_DEC_REF(connp);
3981 		connp = next_conn;
3982 	}
3983 
3984 	/* Last one.  Send it upstream. */
3985 	mutex_exit(&connfp->connf_lock);
3986 
3987 	/* Initiate IPPF processing */
3988 	if (IP6_IN_IPP(flags)) {
3989 		uint_t	ifindex;
3990 
3991 		mutex_enter(&ill->ill_lock);
3992 		ifindex = ill->ill_phyint->phyint_ifindex;
3993 		mutex_exit(&ill->ill_lock);
3994 		ip_process(IPP_LOCAL_IN, &mp, ifindex);
3995 		if (mp == NULL) {
3996 			CONN_DEC_REF(connp);
3997 			if (mctl_present) {
3998 				freeb(first_mp);
3999 			}
4000 			return;
4001 		}
4002 	}
4003 
4004 	/*
4005 	 * For link-local always add ifindex so that transport can set
4006 	 * sin6_scope_id. Avoid it for ICMP error fanout.
4007 	 */
4008 	if ((connp->conn_ipv6_recvpktinfo ||
4009 	    IN6_IS_ADDR_LINKLOCAL(&src)) && (flags & IP_FF_IP6INFO)) {
4010 		/* Add header */
4011 		mp = ip_add_info_v6(mp, inill, &dst);
4012 		if (mp == NULL) {
4013 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
4014 			CONN_DEC_REF(connp);
4015 			if (mctl_present)
4016 				freeb(first_mp);
4017 			return;
4018 		} else if (mctl_present) {
4019 			first_mp->b_cont = mp;
4020 		} else {
4021 			first_mp = mp;
4022 		}
4023 	}
4024 	if (CONN_UDP_FLOWCTLD(connp)) {
4025 		BUMP_MIB(ill->ill_ip6_mib, udpInOverflows);
4026 		freemsg(mp);
4027 	} else {
4028 		if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) {
4029 			first_mp = ipsec_check_inbound_policy(first_mp,
4030 			    connp, NULL, ip6h, mctl_present);
4031 			if (first_mp == NULL) {
4032 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
4033 				CONN_DEC_REF(connp);
4034 				return;
4035 			}
4036 		}
4037 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
4038 
4039 		/* Send it upstream */
4040 		CONN_UDP_RECV(connp, mp);
4041 	}
4042 	IP6_STAT(ip6_udp_fanmb);
4043 	CONN_DEC_REF(connp);
4044 	if (mctl_present)
4045 		freeb(first_mp);
4046 	return;
4047 
4048 notfound:
4049 	mutex_exit(&connfp->connf_lock);
4050 	/*
4051 	 * No one bound to this port.  Is
4052 	 * there a client that wants all
4053 	 * unclaimed datagrams?
4054 	 */
4055 	if (ipcl_proto_fanout_v6[IPPROTO_UDP].connf_head != NULL) {
4056 		ip_fanout_proto_v6(q, first_mp, ip6h, ill, inill, IPPROTO_UDP,
4057 		    0, flags | IP_FF_RAWIP | IP_FF_IP6INFO, mctl_present,
4058 		    zoneid);
4059 	} else {
4060 		if (ip_fanout_send_icmp_v6(q, first_mp, flags,
4061 		    ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0,
4062 		    mctl_present, zoneid)) {
4063 			BUMP_MIB(&ip_mib, udpNoPorts);
4064 		}
4065 	}
4066 }
4067 
4068 /*
4069  * int ip_find_hdr_v6()
4070  *
4071  * This routine is used by the upper layer protocols and the IP tunnel
4072  * module to:
4073  * - Set extension header pointers to appropriate locations
4074  * - Determine IPv6 header length and return it
4075  * - Return a pointer to the last nexthdr value
4076  *
4077  * The caller must initialize ipp_fields.
4078  *
4079  * NOTE: If multiple extension headers of the same type are present,
4080  * ip_find_hdr_v6() will set the respective extension header pointers
4081  * to the first one that it encounters in the IPv6 header.  It also
4082  * skips fragment headers.  This routine deals with malformed packets
4083  * of various sorts in which case the returned length is up to the
4084  * malformed part.
4085  */
4086 int
4087 ip_find_hdr_v6(mblk_t *mp, ip6_t *ip6h, ip6_pkt_t *ipp, uint8_t *nexthdrp)
4088 {
4089 	uint_t	length, ehdrlen;
4090 	uint8_t nexthdr;
4091 	uint8_t *whereptr, *endptr;
4092 	ip6_dest_t *tmpdstopts;
4093 	ip6_rthdr_t *tmprthdr;
4094 	ip6_hbh_t *tmphopopts;
4095 	ip6_frag_t *tmpfraghdr;
4096 
4097 	length = IPV6_HDR_LEN;
4098 	whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */
4099 	endptr = mp->b_wptr;
4100 
4101 	nexthdr = ip6h->ip6_nxt;
4102 	while (whereptr < endptr) {
4103 		/* Is there enough left for len + nexthdr? */
4104 		if (whereptr + MIN_EHDR_LEN > endptr)
4105 			goto done;
4106 
4107 		switch (nexthdr) {
4108 		case IPPROTO_HOPOPTS:
4109 			tmphopopts = (ip6_hbh_t *)whereptr;
4110 			ehdrlen = 8 * (tmphopopts->ip6h_len + 1);
4111 			if ((uchar_t *)tmphopopts +  ehdrlen > endptr)
4112 				goto done;
4113 			nexthdr = tmphopopts->ip6h_nxt;
4114 			/* return only 1st hbh */
4115 			if (!(ipp->ipp_fields & IPPF_HOPOPTS)) {
4116 				ipp->ipp_fields |= IPPF_HOPOPTS;
4117 				ipp->ipp_hopopts = tmphopopts;
4118 				ipp->ipp_hopoptslen = ehdrlen;
4119 			}
4120 			break;
4121 		case IPPROTO_DSTOPTS:
4122 			tmpdstopts = (ip6_dest_t *)whereptr;
4123 			ehdrlen = 8 * (tmpdstopts->ip6d_len + 1);
4124 			if ((uchar_t *)tmpdstopts +  ehdrlen > endptr)
4125 				goto done;
4126 			nexthdr = tmpdstopts->ip6d_nxt;
4127 			/*
4128 			 * ipp_dstopts is set to the destination header after a
4129 			 * routing header.
4130 			 * Assume it is a post-rthdr destination header
4131 			 * and adjust when we find an rthdr.
4132 			 */
4133 			if (!(ipp->ipp_fields & IPPF_DSTOPTS)) {
4134 				ipp->ipp_fields |= IPPF_DSTOPTS;
4135 				ipp->ipp_dstopts = tmpdstopts;
4136 				ipp->ipp_dstoptslen = ehdrlen;
4137 			}
4138 			break;
4139 		case IPPROTO_ROUTING:
4140 			tmprthdr = (ip6_rthdr_t *)whereptr;
4141 			ehdrlen = 8 * (tmprthdr->ip6r_len + 1);
4142 			if ((uchar_t *)tmprthdr +  ehdrlen > endptr)
4143 				goto done;
4144 			nexthdr = tmprthdr->ip6r_nxt;
4145 			/* return only 1st rthdr */
4146 			if (!(ipp->ipp_fields & IPPF_RTHDR)) {
4147 				ipp->ipp_fields |= IPPF_RTHDR;
4148 				ipp->ipp_rthdr = tmprthdr;
4149 				ipp->ipp_rthdrlen = ehdrlen;
4150 			}
4151 			/*
4152 			 * Make any destination header we've seen be a
4153 			 * pre-rthdr destination header.
4154 			 */
4155 			if (ipp->ipp_fields & IPPF_DSTOPTS) {
4156 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
4157 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
4158 				ipp->ipp_rtdstopts = ipp->ipp_dstopts;
4159 				ipp->ipp_dstopts = NULL;
4160 				ipp->ipp_rtdstoptslen = ipp->ipp_dstoptslen;
4161 				ipp->ipp_dstoptslen = 0;
4162 			}
4163 			break;
4164 		case IPPROTO_FRAGMENT:
4165 			tmpfraghdr = (ip6_frag_t *)whereptr;
4166 			ehdrlen = sizeof (ip6_frag_t);
4167 			if ((uchar_t *)tmpfraghdr + ehdrlen > endptr)
4168 				goto done;
4169 			nexthdr = tmpfraghdr->ip6f_nxt;
4170 			if (!(ipp->ipp_fields & IPPF_FRAGHDR)) {
4171 				ipp->ipp_fields |= IPPF_FRAGHDR;
4172 				ipp->ipp_fraghdr = tmpfraghdr;
4173 				ipp->ipp_fraghdrlen = ehdrlen;
4174 			}
4175 			break;
4176 		case IPPROTO_NONE:
4177 		default:
4178 			goto done;
4179 		}
4180 		length += ehdrlen;
4181 		whereptr += ehdrlen;
4182 	}
4183 done:
4184 	if (nexthdrp != NULL)
4185 		*nexthdrp = nexthdr;
4186 	return (length);
4187 }
4188 
4189 int
4190 ip_hdr_complete_v6(ip6_t *ip6h, zoneid_t zoneid)
4191 {
4192 	ire_t *ire;
4193 
4194 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
4195 		ire = ire_lookup_local_v6(zoneid);
4196 		if (ire == NULL) {
4197 			ip1dbg(("ip_hdr_complete_v6: no source IRE\n"));
4198 			return (1);
4199 		}
4200 		ip6h->ip6_src = ire->ire_addr_v6;
4201 		ire_refrele(ire);
4202 	}
4203 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
4204 	ip6h->ip6_hops = ipv6_def_hops;
4205 	return (0);
4206 }
4207 
4208 /*
4209  * Try to determine where and what are the IPv6 header length and
4210  * pointer to nexthdr value for the upper layer protocol (or an
4211  * unknown next hdr).
4212  *
4213  * Parameters returns a pointer to the nexthdr value;
4214  * Must handle malformed packets of various sorts.
4215  * Function returns failure for malformed cases.
4216  */
4217 boolean_t
4218 ip_hdr_length_nexthdr_v6(mblk_t *mp, ip6_t *ip6h, uint16_t *hdr_length_ptr,
4219     uint8_t **nexthdrpp)
4220 {
4221 	uint16_t length;
4222 	uint_t	ehdrlen;
4223 	uint8_t	*nexthdrp;
4224 	uint8_t *whereptr;
4225 	uint8_t *endptr;
4226 	ip6_dest_t *desthdr;
4227 	ip6_rthdr_t *rthdr;
4228 	ip6_frag_t *fraghdr;
4229 
4230 	ASSERT(IPH_HDR_VERSION(ip6h) == IPV6_VERSION);
4231 	length = IPV6_HDR_LEN;
4232 	whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */
4233 	endptr = mp->b_wptr;
4234 
4235 	nexthdrp = &ip6h->ip6_nxt;
4236 	while (whereptr < endptr) {
4237 		/* Is there enough left for len + nexthdr? */
4238 		if (whereptr + MIN_EHDR_LEN > endptr)
4239 			break;
4240 
4241 		switch (*nexthdrp) {
4242 		case IPPROTO_HOPOPTS:
4243 		case IPPROTO_DSTOPTS:
4244 			/* Assumes the headers are identical for hbh and dst */
4245 			desthdr = (ip6_dest_t *)whereptr;
4246 			ehdrlen = 8 * (desthdr->ip6d_len + 1);
4247 			if ((uchar_t *)desthdr +  ehdrlen > endptr)
4248 				return (B_FALSE);
4249 			nexthdrp = &desthdr->ip6d_nxt;
4250 			break;
4251 		case IPPROTO_ROUTING:
4252 			rthdr = (ip6_rthdr_t *)whereptr;
4253 			ehdrlen =  8 * (rthdr->ip6r_len + 1);
4254 			if ((uchar_t *)rthdr +  ehdrlen > endptr)
4255 				return (B_FALSE);
4256 			nexthdrp = &rthdr->ip6r_nxt;
4257 			break;
4258 		case IPPROTO_FRAGMENT:
4259 			fraghdr = (ip6_frag_t *)whereptr;
4260 			ehdrlen = sizeof (ip6_frag_t);
4261 			if ((uchar_t *)&fraghdr[1] > endptr)
4262 				return (B_FALSE);
4263 			nexthdrp = &fraghdr->ip6f_nxt;
4264 			break;
4265 		case IPPROTO_NONE:
4266 			/* No next header means we're finished */
4267 		default:
4268 			*hdr_length_ptr = length;
4269 			*nexthdrpp = nexthdrp;
4270 			return (B_TRUE);
4271 		}
4272 		length += ehdrlen;
4273 		whereptr += ehdrlen;
4274 		*hdr_length_ptr = length;
4275 		*nexthdrpp = nexthdrp;
4276 	}
4277 	switch (*nexthdrp) {
4278 	case IPPROTO_HOPOPTS:
4279 	case IPPROTO_DSTOPTS:
4280 	case IPPROTO_ROUTING:
4281 	case IPPROTO_FRAGMENT:
4282 		/*
4283 		 * If any know extension headers are still to be processed,
4284 		 * the packet's malformed (or at least all the IP header(s) are
4285 		 * not in the same mblk - and that should never happen.
4286 		 */
4287 		return (B_FALSE);
4288 
4289 	default:
4290 		/*
4291 		 * If we get here, we know that all of the IP headers were in
4292 		 * the same mblk, even if the ULP header is in the next mblk.
4293 		 */
4294 		*hdr_length_ptr = length;
4295 		*nexthdrpp = nexthdrp;
4296 		return (B_TRUE);
4297 	}
4298 }
4299 
4300 /*
4301  * Return the length of the IPv6 related headers (including extension headers)
4302  * Returns a length even if the packet is malformed.
4303  */
4304 int
4305 ip_hdr_length_v6(mblk_t *mp, ip6_t *ip6h)
4306 {
4307 	uint16_t hdr_len;
4308 	uint8_t	*nexthdrp;
4309 
4310 	(void) ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_len, &nexthdrp);
4311 	return (hdr_len);
4312 }
4313 
4314 /*
4315  * Select an ill for the packet by considering load spreading across
4316  * a different ill in the group if dst_ill is part of some group.
4317  */
4318 static ill_t *
4319 ip_newroute_get_dst_ill_v6(ill_t *dst_ill)
4320 {
4321 	ill_t *ill;
4322 
4323 	/*
4324 	 * We schedule irrespective of whether the source address is
4325 	 * INADDR_UNSPECIED or not.
4326 	 */
4327 	ill = illgrp_scheduler(dst_ill);
4328 	if (ill == NULL)
4329 		return (NULL);
4330 
4331 	/*
4332 	 * For groups with names ip_sioctl_groupname ensures that all
4333 	 * ills are of same type. For groups without names, ifgrp_insert
4334 	 * ensures this.
4335 	 */
4336 	ASSERT(dst_ill->ill_type == ill->ill_type);
4337 
4338 	return (ill);
4339 }
4340 
4341 /*
4342  * IPv6 -
4343  * ip_newroute_v6 is called by ip_rput_data_v6 or ip_wput_v6 whenever we need
4344  * to send out a packet to a destination address for which we do not have
4345  * specific routing information.
4346  *
4347  * Handle non-multicast packets. If ill is non-NULL the match is done
4348  * for that ill.
4349  *
4350  * When a specific ill is specified (using IPV6_PKTINFO,
4351  * IPV6_MULTICAST_IF, or IPV6_BOUND_IF) we will only match
4352  * on routing entries (ftable and ctable) that have a matching
4353  * ire->ire_ipif->ipif_ill. Thus this can only be used
4354  * for destinations that are on-link for the specific ill
4355  * and that can appear on multiple links. Thus it is useful
4356  * for multicast destinations, link-local destinations, and
4357  * at some point perhaps for site-local destinations (if the
4358  * node sits at a site boundary).
4359  * We create the cache entries in the regular ctable since
4360  * it can not "confuse" things for other destinations.
4361  * table.
4362  *
4363  * When ill is part of a ill group, we subject the packets
4364  * to load spreading even if the ill is specified by the
4365  * means described above. We disable only for IPV6_BOUND_PIF
4366  * and for the cases where IP6I_ATTACH_IF is set i.e NS/NA/
4367  * Echo replies to link-local destinations have IP6I_ATTACH_IF
4368  * set.
4369  *
4370  * NOTE : These are the scopes of some of the variables that point at IRE,
4371  *	  which needs to be followed while making any future modifications
4372  *	  to avoid memory leaks.
4373  *
4374  *	- ire and sire are the entries looked up initially by
4375  *	  ire_ftable_lookup_v6.
4376  *	- ipif_ire is used to hold the interface ire associated with
4377  *	  the new cache ire. But it's scope is limited, so we always REFRELE
4378  *	  it before branching out to error paths.
4379  *	- save_ire is initialized before ire_create, so that ire returned
4380  *	  by ire_create will not over-write the ire. We REFRELE save_ire
4381  *	  before breaking out of the switch.
4382  *
4383  *	Thus on failures, we have to REFRELE only ire and sire, if they
4384  *	are not NULL.
4385  *
4386  *	v6srcp may be used in the future. Currently unused.
4387  */
4388 /* ARGSUSED */
4389 void
4390 ip_newroute_v6(queue_t *q, mblk_t *mp, const in6_addr_t *v6dstp,
4391     const in6_addr_t *v6srcp, ill_t *ill, zoneid_t zoneid)
4392 {
4393 	in6_addr_t	v6gw;
4394 	in6_addr_t	dst;
4395 	ire_t		*ire = NULL;
4396 	ipif_t		*src_ipif = NULL;
4397 	ill_t		*dst_ill = NULL;
4398 	ire_t		*sire = NULL;
4399 	ire_t		*save_ire;
4400 	mblk_t		*dlureq_mp;
4401 	ip6_t		*ip6h;
4402 	int		err = 0;
4403 	mblk_t		*first_mp;
4404 	ipsec_out_t	*io;
4405 	ill_t		*attach_ill = NULL;
4406 	ushort_t	ire_marks = 0;
4407 	int		match_flags;
4408 	boolean_t	ip6i_present;
4409 	ire_t		*first_sire = NULL;
4410 	mblk_t		*copy_mp = NULL;
4411 	mblk_t		*xmit_mp = NULL;
4412 	in6_addr_t	save_dst;
4413 	uint32_t	multirt_flags =
4414 	    MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP;
4415 	boolean_t	multirt_is_resolvable;
4416 	boolean_t	multirt_resolve_next;
4417 	boolean_t	need_rele = B_FALSE;
4418 	boolean_t	do_attach_ill = B_FALSE;
4419 	boolean_t	ip6_asp_table_held = B_FALSE;
4420 	tsol_ire_gw_secattr_t *attrp = NULL;
4421 	tsol_gcgrp_t	*gcgrp = NULL;
4422 	tsol_gcgrp_addr_t ga;
4423 
4424 	ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp));
4425 
4426 	first_mp = mp;
4427 	if (mp->b_datap->db_type == M_CTL) {
4428 		mp = mp->b_cont;
4429 		io = (ipsec_out_t *)first_mp->b_rptr;
4430 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
4431 	} else {
4432 		io = NULL;
4433 	}
4434 
4435 	/*
4436 	 * If this end point is bound to IPIF_NOFAILOVER, set bnf_ill and
4437 	 * bind_to_nofailover B_TRUE. We can't use conn to determine as it
4438 	 * could be NULL.
4439 	 *
4440 	 * This information can appear either in an ip6i_t or an IPSEC_OUT
4441 	 * message.
4442 	 */
4443 	ip6h = (ip6_t *)mp->b_rptr;
4444 	ip6i_present = (ip6h->ip6_nxt == IPPROTO_RAW);
4445 	if (ip6i_present || (io != NULL && io->ipsec_out_attach_if)) {
4446 		if (!ip6i_present ||
4447 		    ((ip6i_t *)ip6h)->ip6i_flags & IP6I_ATTACH_IF) {
4448 			attach_ill = ip_grab_attach_ill(ill, first_mp,
4449 			    (ip6i_present ? ((ip6i_t *)ip6h)->ip6i_ifindex :
4450 				io->ipsec_out_ill_index), B_TRUE);
4451 			/* Failure case frees things for us. */
4452 			if (attach_ill == NULL)
4453 				return;
4454 
4455 			/*
4456 			 * Check if we need an ire that will not be
4457 			 * looked up by anybody else i.e. HIDDEN.
4458 			 */
4459 			if (ill_is_probeonly(attach_ill))
4460 				ire_marks = IRE_MARK_HIDDEN;
4461 		}
4462 	}
4463 
4464 	if (IN6_IS_ADDR_LOOPBACK(v6dstp)) {
4465 		ip1dbg(("ip_newroute_v6: dst with loopback addr\n"));
4466 		goto icmp_err_ret;
4467 	} else if ((v6srcp != NULL) && IN6_IS_ADDR_LOOPBACK(v6srcp)) {
4468 		ip1dbg(("ip_newroute_v6: src with loopback addr\n"));
4469 		goto icmp_err_ret;
4470 	}
4471 
4472 	/*
4473 	 * If this IRE is created for forwarding or it is not for
4474 	 * TCP traffic, mark it as temporary.
4475 	 *
4476 	 * Is it sufficient just to check the next header??
4477 	 */
4478 	if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ip6h->ip6_nxt))
4479 		ire_marks |= IRE_MARK_TEMPORARY;
4480 
4481 	/*
4482 	 * Get what we can from ire_ftable_lookup_v6 which will follow an IRE
4483 	 * chain until it gets the most specific information available.
4484 	 * For example, we know that there is no IRE_CACHE for this dest,
4485 	 * but there may be an IRE_OFFSUBNET which specifies a gateway.
4486 	 * ire_ftable_lookup_v6 will look up the gateway, etc.
4487 	 */
4488 
4489 	if (ill == NULL) {
4490 		match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4491 		    MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_SECATTR;
4492 		ire = ire_ftable_lookup_v6(v6dstp, 0, 0, 0,
4493 		    NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp),
4494 		    match_flags);
4495 		/*
4496 		 * ire_add_then_send -> ip_newroute_v6 in the CGTP case passes
4497 		 * in a NULL ill, but the packet could be a neighbor
4498 		 * solicitation/advertisment and could have a valid attach_ill.
4499 		 */
4500 		if (attach_ill != NULL)
4501 			ill_refrele(attach_ill);
4502 	} else {
4503 		if (attach_ill != NULL) {
4504 			/*
4505 			 * attach_ill is set only for communicating with
4506 			 * on-link hosts. So, don't look for DEFAULT.
4507 			 * ip_wput_v6 passes the right ill in this case and
4508 			 * hence we can assert.
4509 			 */
4510 			ASSERT(ill == attach_ill);
4511 			ill_refrele(attach_ill);
4512 			do_attach_ill = B_TRUE;
4513 			match_flags = MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL;
4514 		} else {
4515 			match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4516 			    MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL_GROUP;
4517 		}
4518 		match_flags |= MATCH_IRE_PARENT | MATCH_IRE_SECATTR;
4519 		ire = ire_ftable_lookup_v6(v6dstp, NULL, NULL, 0, ill->ill_ipif,
4520 		    &sire, zoneid, 0, MBLK_GETLABEL(mp), match_flags);
4521 	}
4522 
4523 	ip3dbg(("ip_newroute_v6: ire_ftable_lookup_v6() "
4524 	    "returned ire %p, sire %p\n", (void *)ire, (void *)sire));
4525 
4526 	if (zoneid == ALL_ZONES && ire != NULL) {
4527 		/*
4528 		 * In the forwarding case, we can use a route from any zone
4529 		 * since we won't change the source address. We can easily
4530 		 * assert that the source address is already set when there's no
4531 		 * ip6_info header - otherwise we'd have to call pullupmsg().
4532 		 */
4533 		ASSERT(ip6i_present ||
4534 		    !IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src));
4535 		zoneid = ire->ire_zoneid;
4536 	}
4537 
4538 	/*
4539 	 * We enter a loop that will be run only once in most cases.
4540 	 * The loop is re-entered in the case where the destination
4541 	 * can be reached through multiple RTF_MULTIRT-flagged routes.
4542 	 * The intention is to compute multiple routes to a single
4543 	 * destination in a single ip_newroute_v6 call.
4544 	 * The information is contained in sire->ire_flags.
4545 	 */
4546 	do {
4547 		multirt_resolve_next = B_FALSE;
4548 
4549 		if (dst_ill != NULL) {
4550 			ill_refrele(dst_ill);
4551 			dst_ill = NULL;
4552 		}
4553 		if (src_ipif != NULL) {
4554 			ipif_refrele(src_ipif);
4555 			src_ipif = NULL;
4556 		}
4557 		if ((sire != NULL) && sire->ire_flags & RTF_MULTIRT) {
4558 			ip3dbg(("ip_newroute_v6: starting new resolution "
4559 			    "with first_mp %p, tag %d\n",
4560 			    (void *)first_mp, MULTIRT_DEBUG_TAGGED(first_mp)));
4561 
4562 			/*
4563 			 * We check if there are trailing unresolved routes for
4564 			 * the destination contained in sire.
4565 			 */
4566 			multirt_is_resolvable = ire_multirt_lookup_v6(&ire,
4567 			    &sire, multirt_flags, MBLK_GETLABEL(mp));
4568 
4569 			ip3dbg(("ip_newroute_v6: multirt_is_resolvable %d, "
4570 			    "ire %p, sire %p\n",
4571 			    multirt_is_resolvable, (void *)ire, (void *)sire));
4572 
4573 			if (!multirt_is_resolvable) {
4574 				/*
4575 				 * No more multirt routes to resolve; give up
4576 				 * (all routes resolved or no more resolvable
4577 				 * routes).
4578 				 */
4579 				if (ire != NULL) {
4580 					ire_refrele(ire);
4581 					ire = NULL;
4582 				}
4583 			} else {
4584 				ASSERT(sire != NULL);
4585 				ASSERT(ire != NULL);
4586 				/*
4587 				 * We simply use first_sire as a flag that
4588 				 * indicates if a resolvable multirt route has
4589 				 * already been found during the preceding
4590 				 * loops. If it is not the case, we may have
4591 				 * to send an ICMP error to report that the
4592 				 * destination is unreachable. We do not
4593 				 * IRE_REFHOLD first_sire.
4594 				 */
4595 				if (first_sire == NULL) {
4596 					first_sire = sire;
4597 				}
4598 			}
4599 		}
4600 		if ((ire == NULL) || (ire == sire)) {
4601 			/*
4602 			 * either ire == NULL (the destination cannot be
4603 			 * resolved) or ire == sire (the gateway cannot be
4604 			 * resolved). At this point, there are no more routes
4605 			 * to resolve for the destination, thus we exit.
4606 			 */
4607 			if (ip_debug > 3) {
4608 				/* ip2dbg */
4609 				pr_addr_dbg("ip_newroute_v6: "
4610 				    "can't resolve %s\n", AF_INET6, v6dstp);
4611 			}
4612 			ip3dbg(("ip_newroute_v6: "
4613 			    "ire %p, sire %p, first_sire %p\n",
4614 			    (void *)ire, (void *)sire, (void *)first_sire));
4615 
4616 			if (sire != NULL) {
4617 				ire_refrele(sire);
4618 				sire = NULL;
4619 			}
4620 
4621 			if (first_sire != NULL) {
4622 				/*
4623 				 * At least one multirt route has been found
4624 				 * in the same ip_newroute() call; there is no
4625 				 * need to report an ICMP error.
4626 				 * first_sire was not IRE_REFHOLDed.
4627 				 */
4628 				MULTIRT_DEBUG_UNTAG(first_mp);
4629 				freemsg(first_mp);
4630 				return;
4631 			}
4632 			ip_rts_change_v6(RTM_MISS, v6dstp, 0, 0, 0, 0, 0, 0,
4633 			    RTA_DST);
4634 			goto icmp_err_ret;
4635 		}
4636 
4637 		ASSERT(ire->ire_ipversion == IPV6_VERSION);
4638 
4639 		/*
4640 		 * Verify that the returned IRE does not have either the
4641 		 * RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
4642 		 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
4643 		 */
4644 		if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
4645 		    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0)
4646 			goto icmp_err_ret;
4647 
4648 		/*
4649 		 * Increment the ire_ob_pkt_count field for ire if it is an
4650 		 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
4651 		 * increment the same for the parent IRE, sire, if it is some
4652 		 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST
4653 		 * and HOST_REDIRECT).
4654 		 */
4655 		if ((ire->ire_type & IRE_INTERFACE) != 0) {
4656 			UPDATE_OB_PKT_COUNT(ire);
4657 			ire->ire_last_used_time = lbolt;
4658 		}
4659 
4660 		if (sire != NULL) {
4661 			mutex_enter(&sire->ire_lock);
4662 			v6gw = sire->ire_gateway_addr_v6;
4663 			mutex_exit(&sire->ire_lock);
4664 			ASSERT((sire->ire_type & (IRE_CACHETABLE |
4665 			    IRE_INTERFACE)) == 0);
4666 			UPDATE_OB_PKT_COUNT(sire);
4667 			sire->ire_last_used_time = lbolt;
4668 		} else {
4669 			v6gw = ipv6_all_zeros;
4670 		}
4671 
4672 		/*
4673 		 * We have a route to reach the destination.
4674 		 *
4675 		 * 1) If the interface is part of ill group, try to get a new
4676 		 *    ill taking load spreading into account.
4677 		 *
4678 		 * 2) After selecting the ill, get a source address that might
4679 		 *    create good inbound load spreading and that matches the
4680 		 *    right scope. ipif_select_source_v6 does this for us.
4681 		 *
4682 		 * If the application specified the ill (ifindex), we still
4683 		 * load spread. Only if the packets needs to go out specifically
4684 		 * on a given ill e.g. bind to IPIF_NOFAILOVER address,
4685 		 * IPV6_BOUND_PIF we don't try to use a different ill for load
4686 		 * spreading.
4687 		 */
4688 		if (!do_attach_ill) {
4689 			/*
4690 			 * If the interface belongs to an interface group,
4691 			 * make sure the next possible interface in the group
4692 			 * is used.  This encourages load spreading among
4693 			 * peers in an interface group. However, in the case
4694 			 * of multirouting, load spreading is not used, as we
4695 			 * actually want to replicate outgoing packets through
4696 			 * particular interfaces.
4697 			 *
4698 			 * Note: While we pick a dst_ill we are really only
4699 			 * interested in the ill for load spreading.
4700 			 * The source ipif is determined by source address
4701 			 * selection below.
4702 			 */
4703 			if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
4704 				dst_ill = ire->ire_ipif->ipif_ill;
4705 				/* For uniformity do a refhold */
4706 				ill_refhold(dst_ill);
4707 			} else {
4708 				/*
4709 				 * If we are here trying to create an IRE_CACHE
4710 				 * for an offlink destination and have the
4711 				 * IRE_CACHE for the next hop and the latter is
4712 				 * using virtual IP source address selection i.e
4713 				 * it's ire->ire_ipif is pointing to a virtual
4714 				 * network interface (vni) then
4715 				 * ip_newroute_get_dst_ll() will return the vni
4716 				 * interface as the dst_ill. Since the vni is
4717 				 * virtual i.e not associated with any physical
4718 				 * interface, it cannot be the dst_ill, hence
4719 				 * in such a case call ip_newroute_get_dst_ll()
4720 				 * with the stq_ill instead of the ire_ipif ILL.
4721 				 * The function returns a refheld ill.
4722 				 */
4723 				if ((ire->ire_type == IRE_CACHE) &&
4724 				    IS_VNI(ire->ire_ipif->ipif_ill))
4725 					dst_ill = ip_newroute_get_dst_ill_v6(
4726 						ire->ire_stq->q_ptr);
4727 				else
4728 					dst_ill = ip_newroute_get_dst_ill_v6(
4729 						ire->ire_ipif->ipif_ill);
4730 			}
4731 			if (dst_ill == NULL) {
4732 				if (ip_debug > 2) {
4733 					pr_addr_dbg("ip_newroute_v6 : no dst "
4734 					    "ill for dst %s\n",
4735 					    AF_INET6, v6dstp);
4736 				}
4737 				goto icmp_err_ret;
4738 			} else if (dst_ill->ill_group == NULL && ill != NULL &&
4739 			    dst_ill != ill) {
4740 				/*
4741 				 * If "ill" is not part of any group, we should
4742 				 * have found a route matching "ill" as we
4743 				 * called ire_ftable_lookup_v6 with
4744 				 * MATCH_IRE_ILL_GROUP.
4745 				 * Rather than asserting when there is a
4746 				 * mismatch, we just drop the packet.
4747 				 */
4748 				ip0dbg(("ip_newroute_v6: BOUND_IF failed : "
4749 				    "dst_ill %s ill %s\n",
4750 				    dst_ill->ill_name,
4751 				    ill->ill_name));
4752 				goto icmp_err_ret;
4753 			}
4754 		} else {
4755 			dst_ill = ire->ire_ipif->ipif_ill;
4756 			/* For uniformity do refhold */
4757 			ill_refhold(dst_ill);
4758 			/*
4759 			 * We should have found a route matching ill as we
4760 			 * called ire_ftable_lookup_v6 with MATCH_IRE_ILL.
4761 			 * Rather than asserting, while there is a mismatch,
4762 			 * we just drop the packet.
4763 			 */
4764 			if (dst_ill != ill) {
4765 				ip0dbg(("ip_newroute_v6: Packet dropped as "
4766 				    "IP6I_ATTACH_IF ill is %s, "
4767 				    "ire->ire_ipif->ipif_ill is %s\n",
4768 				    ill->ill_name,
4769 				    dst_ill->ill_name));
4770 				goto icmp_err_ret;
4771 			}
4772 		}
4773 		/*
4774 		 * Pick a source address which matches the scope of the
4775 		 * destination address.
4776 		 * For RTF_SETSRC routes, the source address is imposed by the
4777 		 * parent ire (sire).
4778 		 */
4779 		ASSERT(src_ipif == NULL);
4780 		if (ire->ire_type == IRE_IF_RESOLVER &&
4781 		    !IN6_IS_ADDR_UNSPECIFIED(&v6gw) &&
4782 		    ip6_asp_can_lookup()) {
4783 			/*
4784 			 * The ire cache entry we're adding is for the
4785 			 * gateway itself.  The source address in this case
4786 			 * is relative to the gateway's address.
4787 			 */
4788 			ip6_asp_table_held = B_TRUE;
4789 			src_ipif = ipif_select_source_v6(dst_ill, &v6gw,
4790 			    RESTRICT_TO_GROUP, IPV6_PREFER_SRC_DEFAULT, zoneid);
4791 			if (src_ipif != NULL)
4792 				ire_marks |= IRE_MARK_USESRC_CHECK;
4793 		} else {
4794 			if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
4795 				/*
4796 				 * Check that the ipif matching the requested
4797 				 * source address still exists.
4798 				 */
4799 				src_ipif = ipif_lookup_addr_v6(
4800 				    &sire->ire_src_addr_v6, NULL, zoneid,
4801 					NULL, NULL, NULL, NULL);
4802 			}
4803 			if (src_ipif == NULL && ip6_asp_can_lookup()) {
4804 				uint_t restrict_ill = RESTRICT_TO_NONE;
4805 
4806 				if (ip6i_present && ((ip6i_t *)ip6h)->ip6i_flags
4807 				    & IP6I_ATTACH_IF)
4808 					restrict_ill = RESTRICT_TO_ILL;
4809 				ip6_asp_table_held = B_TRUE;
4810 				src_ipif = ipif_select_source_v6(dst_ill,
4811 				    v6dstp, restrict_ill,
4812 				    IPV6_PREFER_SRC_DEFAULT, zoneid);
4813 				if (src_ipif != NULL)
4814 					ire_marks |= IRE_MARK_USESRC_CHECK;
4815 			}
4816 		}
4817 
4818 		if (src_ipif == NULL) {
4819 			if (ip_debug > 2) {
4820 				/* ip1dbg */
4821 				pr_addr_dbg("ip_newroute_v6: no src for "
4822 				    "dst %s\n, ", AF_INET6, v6dstp);
4823 				printf("ip_newroute_v6: interface name %s\n",
4824 				    dst_ill->ill_name);
4825 			}
4826 			goto icmp_err_ret;
4827 		}
4828 
4829 		if (ip_debug > 3) {
4830 			/* ip2dbg */
4831 			pr_addr_dbg("ip_newroute_v6: first hop %s\n",
4832 			    AF_INET6, &v6gw);
4833 		}
4834 		ip2dbg(("\tire type %s (%d)\n",
4835 		    ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type));
4836 
4837 		/*
4838 		 * At this point in ip_newroute_v6(), ire is either the
4839 		 * IRE_CACHE of the next-hop gateway for an off-subnet
4840 		 * destination or an IRE_INTERFACE type that should be used
4841 		 * to resolve an on-subnet destination or an on-subnet
4842 		 * next-hop gateway.
4843 		 *
4844 		 * In the IRE_CACHE case, we have the following :
4845 		 *
4846 		 * 1) src_ipif - used for getting a source address.
4847 		 *
4848 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
4849 		 *    means packets using this IRE_CACHE will go out on dst_ill.
4850 		 *
4851 		 * 3) The IRE sire will point to the prefix that is the longest
4852 		 *    matching route for the destination. These prefix types
4853 		 *    include IRE_DEFAULT, IRE_PREFIX, IRE_HOST.
4854 		 *
4855 		 *    The newly created IRE_CACHE entry for the off-subnet
4856 		 *    destination is tied to both the prefix route and the
4857 		 *    interface route used to resolve the next-hop gateway
4858 		 *    via the ire_phandle and ire_ihandle fields, respectively.
4859 		 *
4860 		 * In the IRE_INTERFACE case, we have the following :
4861 		 *
4862 		 * 1) src_ipif - used for getting a source address.
4863 		 *
4864 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
4865 		 *    means packets using the IRE_CACHE that we will build
4866 		 *    here will go out on dst_ill.
4867 		 *
4868 		 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
4869 		 *    to be created will only be tied to the IRE_INTERFACE that
4870 		 *    was derived from the ire_ihandle field.
4871 		 *
4872 		 *    If sire is non-NULL, it means the destination is off-link
4873 		 *    and we will first create the IRE_CACHE for the gateway.
4874 		 *    Next time through ip_newroute_v6, we will create the
4875 		 *    IRE_CACHE for the final destination as described above.
4876 		 */
4877 		save_ire = ire;
4878 		switch (ire->ire_type) {
4879 		case IRE_CACHE: {
4880 			ire_t	*ipif_ire;
4881 
4882 			ASSERT(sire != NULL);
4883 			if (IN6_IS_ADDR_UNSPECIFIED(&v6gw)) {
4884 				mutex_enter(&ire->ire_lock);
4885 				v6gw = ire->ire_gateway_addr_v6;
4886 				mutex_exit(&ire->ire_lock);
4887 			}
4888 			/*
4889 			 * We need 3 ire's to create a new cache ire for an
4890 			 * off-link destination from the cache ire of the
4891 			 * gateway.
4892 			 *
4893 			 *	1. The prefix ire 'sire'
4894 			 *	2. The cache ire of the gateway 'ire'
4895 			 *	3. The interface ire 'ipif_ire'
4896 			 *
4897 			 * We have (1) and (2). We lookup (3) below.
4898 			 *
4899 			 * If there is no interface route to the gateway,
4900 			 * it is a race condition, where we found the cache
4901 			 * but the inteface route has been deleted.
4902 			 */
4903 			ipif_ire = ire_ihandle_lookup_offlink_v6(ire, sire);
4904 			if (ipif_ire == NULL) {
4905 				ip1dbg(("ip_newroute_v6:"
4906 				    "ire_ihandle_lookup_offlink_v6 failed\n"));
4907 				goto icmp_err_ret;
4908 			}
4909 			/*
4910 			 * Assume DL_UNITDATA_REQ is same for all physical
4911 			 * interfaces in the ifgrp.  If it isn't, this code will
4912 			 * have to be seriously rewhacked to allow the
4913 			 * fastpath probing (such that I cache the link
4914 			 * header in the IRE_CACHE) to work over ifgrps.
4915 			 * We have what we need to build an IRE_CACHE.
4916 			 */
4917 			/*
4918 			 * Note: the new ire inherits RTF_SETSRC
4919 			 * and RTF_MULTIRT to propagate these flags from prefix
4920 			 * to cache.
4921 			 */
4922 
4923 			/*
4924 			 * Check cached gateway IRE for any security
4925 			 * attributes; if found, associate the gateway
4926 			 * credentials group to the destination IRE.
4927 			 */
4928 			if ((attrp = save_ire->ire_gw_secattr) != NULL) {
4929 				mutex_enter(&attrp->igsa_lock);
4930 				if ((gcgrp = attrp->igsa_gcgrp) != NULL)
4931 					GCGRP_REFHOLD(gcgrp);
4932 				mutex_exit(&attrp->igsa_lock);
4933 			}
4934 
4935 			ire = ire_create_v6(
4936 				v6dstp,			/* dest address */
4937 				&ipv6_all_ones,		/* mask */
4938 				&src_ipif->ipif_v6src_addr, /* source address */
4939 				&v6gw,			/* gateway address */
4940 				&save_ire->ire_max_frag,
4941 				NULL,			/* Fast Path header */
4942 				dst_ill->ill_rq,	/* recv-from queue */
4943 				dst_ill->ill_wq,	/* send-to queue */
4944 				IRE_CACHE,
4945 				NULL,
4946 				src_ipif,
4947 				&sire->ire_mask_v6,	/* Parent mask */
4948 				sire->ire_phandle,	/* Parent handle */
4949 				ipif_ire->ire_ihandle,	/* Interface handle */
4950 				sire->ire_flags &	/* flags if any */
4951 				    (RTF_SETSRC | RTF_MULTIRT),
4952 				&(sire->ire_uinfo),
4953 				NULL,
4954 				gcgrp);
4955 
4956 			if (ire == NULL) {
4957 				if (gcgrp != NULL) {
4958 					GCGRP_REFRELE(gcgrp);
4959 					gcgrp = NULL;
4960 				}
4961 				ire_refrele(save_ire);
4962 				ire_refrele(ipif_ire);
4963 				break;
4964 			}
4965 
4966 			/* reference now held by IRE */
4967 			gcgrp = NULL;
4968 
4969 			ire->ire_marks |= ire_marks;
4970 
4971 			/*
4972 			 * Prevent sire and ipif_ire from getting deleted. The
4973 			 * newly created ire is tied to both of them via the
4974 			 * phandle and ihandle respectively.
4975 			 */
4976 			IRB_REFHOLD(sire->ire_bucket);
4977 			/* Has it been removed already ? */
4978 			if (sire->ire_marks & IRE_MARK_CONDEMNED) {
4979 				IRB_REFRELE(sire->ire_bucket);
4980 				ire_refrele(ipif_ire);
4981 				ire_refrele(save_ire);
4982 				break;
4983 			}
4984 
4985 			IRB_REFHOLD(ipif_ire->ire_bucket);
4986 			/* Has it been removed already ? */
4987 			if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) {
4988 				IRB_REFRELE(ipif_ire->ire_bucket);
4989 				IRB_REFRELE(sire->ire_bucket);
4990 				ire_refrele(ipif_ire);
4991 				ire_refrele(save_ire);
4992 				break;
4993 			}
4994 
4995 			xmit_mp = first_mp;
4996 			if (ire->ire_flags & RTF_MULTIRT) {
4997 				copy_mp = copymsg(first_mp);
4998 				if (copy_mp != NULL) {
4999 					xmit_mp = copy_mp;
5000 					MULTIRT_DEBUG_TAG(first_mp);
5001 				}
5002 			}
5003 			ire_add_then_send(q, ire, xmit_mp);
5004 			if (ip6_asp_table_held) {
5005 				ip6_asp_table_refrele();
5006 				ip6_asp_table_held = B_FALSE;
5007 			}
5008 			ire_refrele(save_ire);
5009 
5010 			/* Assert that sire is not deleted yet. */
5011 			ASSERT(sire->ire_ptpn != NULL);
5012 			IRB_REFRELE(sire->ire_bucket);
5013 
5014 			/* Assert that ipif_ire is not deleted yet. */
5015 			ASSERT(ipif_ire->ire_ptpn != NULL);
5016 			IRB_REFRELE(ipif_ire->ire_bucket);
5017 			ire_refrele(ipif_ire);
5018 
5019 			if (copy_mp != NULL) {
5020 				/*
5021 				 * Search for the next unresolved
5022 				 * multirt route.
5023 				 */
5024 				copy_mp = NULL;
5025 				ipif_ire = NULL;
5026 				ire = NULL;
5027 				/* re-enter the loop */
5028 				multirt_resolve_next = B_TRUE;
5029 				continue;
5030 			}
5031 			ire_refrele(sire);
5032 			ill_refrele(dst_ill);
5033 			ipif_refrele(src_ipif);
5034 			return;
5035 		}
5036 		case IRE_IF_NORESOLVER:
5037 			/*
5038 			 * We have what we need to build an IRE_CACHE.
5039 			 *
5040 			 * Create a new dlureq_mp with the IPv6 gateway
5041 			 * address in destination address in the DLPI hdr
5042 			 * if the physical length is exactly 16 bytes.
5043 			 */
5044 			if (dst_ill->ill_phys_addr_length == IPV6_ADDR_LEN) {
5045 				const in6_addr_t *addr;
5046 
5047 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw))
5048 					addr = &v6gw;
5049 				else
5050 					addr = v6dstp;
5051 
5052 				dlureq_mp = ill_dlur_gen((uchar_t *)addr,
5053 				    dst_ill->ill_phys_addr_length,
5054 				    dst_ill->ill_sap,
5055 				    dst_ill->ill_sap_length);
5056 			} else {
5057 				/*
5058 				 * handle the Gated case, where we create
5059 				 * a NORESOLVER route for loopback.
5060 				 */
5061 				if (dst_ill->ill_net_type != IRE_IF_NORESOLVER)
5062 					break;
5063 				dlureq_mp = ill_dlur_gen(NULL,
5064 				    dst_ill->ill_phys_addr_length,
5065 				    dst_ill->ill_sap,
5066 				    dst_ill->ill_sap_length);
5067 			}
5068 			if (dlureq_mp == NULL)
5069 				break;
5070 			/*
5071 			 * TSol note: We are creating the ire cache for the
5072 			 * destination 'dst'. If 'dst' is offlink, going
5073 			 * through the first hop 'gw', the security attributes
5074 			 * of 'dst' must be set to point to the gateway
5075 			 * credentials of gateway 'gw'. If 'dst' is onlink, it
5076 			 * is possible that 'dst' is a potential gateway that is
5077 			 * referenced by some route that has some security
5078 			 * attributes. Thus in the former case, we need to do a
5079 			 * gcgrp_lookup of 'gw' while in the latter case we
5080 			 * need to do gcgrp_lookup of 'dst' itself.
5081 			 */
5082 			ga.ga_af = AF_INET6;
5083 			if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw))
5084 				ga.ga_addr = v6gw;
5085 			else
5086 				ga.ga_addr = *v6dstp;
5087 			gcgrp = gcgrp_lookup(&ga, B_FALSE);
5088 
5089 			/*
5090 			 * Note: the new ire inherits sire flags RTF_SETSRC
5091 			 * and RTF_MULTIRT to propagate those rules from prefix
5092 			 * to cache.
5093 			 */
5094 			ire = ire_create_v6(
5095 				v6dstp,			/* dest address */
5096 				&ipv6_all_ones,		/* mask */
5097 				&src_ipif->ipif_v6src_addr, /* source address */
5098 				&v6gw,			/* gateway address */
5099 				&save_ire->ire_max_frag,
5100 				NULL,			/* Fast Path header */
5101 				dst_ill->ill_rq,	/* recv-from queue */
5102 				dst_ill->ill_wq,	/* send-to queue */
5103 				IRE_CACHE,
5104 				dlureq_mp,
5105 				src_ipif,
5106 				&save_ire->ire_mask_v6,	/* Parent mask */
5107 				(sire != NULL) ?	/* Parent handle */
5108 				    sire->ire_phandle : 0,
5109 				save_ire->ire_ihandle,	/* Interface handle */
5110 				(sire != NULL) ?	/* flags if any */
5111 				    sire->ire_flags &
5112 				    (RTF_SETSRC | RTF_MULTIRT) : 0,
5113 				&(save_ire->ire_uinfo),
5114 				NULL,
5115 				gcgrp);
5116 
5117 			freeb(dlureq_mp);
5118 
5119 			if (ire == NULL) {
5120 				if (gcgrp != NULL) {
5121 					GCGRP_REFRELE(gcgrp);
5122 					gcgrp = NULL;
5123 				}
5124 				ire_refrele(save_ire);
5125 				break;
5126 			}
5127 
5128 			/* reference now held by IRE */
5129 			gcgrp = NULL;
5130 
5131 			ire->ire_marks |= ire_marks;
5132 
5133 			if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw))
5134 				dst = v6gw;
5135 			else
5136 				dst = *v6dstp;
5137 			err = ndp_noresolver(dst_ill, &dst);
5138 			if (err != 0) {
5139 				ire_refrele(save_ire);
5140 				break;
5141 			}
5142 
5143 			/* Prevent save_ire from getting deleted */
5144 			IRB_REFHOLD(save_ire->ire_bucket);
5145 			/* Has it been removed already ? */
5146 			if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
5147 				IRB_REFRELE(save_ire->ire_bucket);
5148 				ire_refrele(save_ire);
5149 				break;
5150 			}
5151 
5152 			xmit_mp = first_mp;
5153 			/*
5154 			 * In case of MULTIRT, a copy of the current packet
5155 			 * to send is made to further re-enter the
5156 			 * loop and attempt another route resolution
5157 			 */
5158 			if ((sire != NULL) && sire->ire_flags & RTF_MULTIRT) {
5159 				copy_mp = copymsg(first_mp);
5160 				if (copy_mp != NULL) {
5161 					xmit_mp = copy_mp;
5162 					MULTIRT_DEBUG_TAG(first_mp);
5163 				}
5164 			}
5165 			ire_add_then_send(q, ire, xmit_mp);
5166 			if (ip6_asp_table_held) {
5167 				ip6_asp_table_refrele();
5168 				ip6_asp_table_held = B_FALSE;
5169 			}
5170 
5171 			/* Assert that it is not deleted yet. */
5172 			ASSERT(save_ire->ire_ptpn != NULL);
5173 			IRB_REFRELE(save_ire->ire_bucket);
5174 			ire_refrele(save_ire);
5175 
5176 			if (copy_mp != NULL) {
5177 				/*
5178 				 * If we found a (no)resolver, we ignore any
5179 				 * trailing top priority IRE_CACHE in
5180 				 * further loops. This ensures that we do not
5181 				 * omit any (no)resolver despite the priority
5182 				 * in this call.
5183 				 * IRE_CACHE, if any, will be processed
5184 				 * by another thread entering ip_newroute(),
5185 				 * (on resolver response, for example).
5186 				 * We use this to force multiple parallel
5187 				 * resolution as soon as a packet needs to be
5188 				 * sent. The result is, after one packet
5189 				 * emission all reachable routes are generally
5190 				 * resolved.
5191 				 * Otherwise, complete resolution of MULTIRT
5192 				 * routes would require several emissions as
5193 				 * side effect.
5194 				 */
5195 				multirt_flags &= ~MULTIRT_CACHEGW;
5196 
5197 				/*
5198 				 * Search for the next unresolved multirt
5199 				 * route.
5200 				 */
5201 				copy_mp = NULL;
5202 				save_ire = NULL;
5203 				ire = NULL;
5204 				/* re-enter the loop */
5205 				multirt_resolve_next = B_TRUE;
5206 				continue;
5207 			}
5208 
5209 			/* Don't need sire anymore */
5210 			if (sire != NULL)
5211 				ire_refrele(sire);
5212 			ill_refrele(dst_ill);
5213 			ipif_refrele(src_ipif);
5214 			return;
5215 
5216 		case IRE_IF_RESOLVER:
5217 			/*
5218 			 * We can't build an IRE_CACHE yet, but at least we
5219 			 * found a resolver that can help.
5220 			 */
5221 			dst = *v6dstp;
5222 
5223 			/*
5224 			 * To be at this point in the code with a non-zero gw
5225 			 * means that dst is reachable through a gateway that
5226 			 * we have never resolved.  By changing dst to the gw
5227 			 * addr we resolve the gateway first.  When
5228 			 * ire_add_then_send() tries to put the IP dg to dst,
5229 			 * it will reenter ip_newroute() at which time we will
5230 			 * find the IRE_CACHE for the gw and create another
5231 			 * IRE_CACHE above (for dst itself).
5232 			 */
5233 			if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw)) {
5234 				save_dst = dst;
5235 				dst = v6gw;
5236 				v6gw = ipv6_all_zeros;
5237 			}
5238 			if (dst_ill->ill_flags & ILLF_XRESOLV) {
5239 				/*
5240 				 * Ask the external resolver to do its thing.
5241 				 * Make an mblk chain in the following form:
5242 				 * ARQ_REQ_MBLK-->IRE_MBLK-->packet
5243 				 */
5244 				mblk_t		*ire_mp;
5245 				mblk_t		*areq_mp;
5246 				areq_t		*areq;
5247 				in6_addr_t	*addrp;
5248 
5249 				ip1dbg(("ip_newroute_v6:ILLF_XRESOLV\n"));
5250 				if (ip6_asp_table_held) {
5251 					ip6_asp_table_refrele();
5252 					ip6_asp_table_held = B_FALSE;
5253 				}
5254 				ire = ire_create_mp_v6(
5255 					&dst,		/* dest address */
5256 					&ipv6_all_ones,	/* mask */
5257 					&src_ipif->ipif_v6src_addr,
5258 							/* source address */
5259 					&v6gw,		/* gateway address */
5260 					NULL,		/* Fast Path header */
5261 					dst_ill->ill_rq, /* recv-from queue */
5262 					dst_ill->ill_wq, /* send-to queue */
5263 					IRE_CACHE,
5264 					NULL,
5265 					src_ipif,
5266 					&save_ire->ire_mask_v6,
5267 							/* Parent mask */
5268 					0,
5269 					save_ire->ire_ihandle,
5270 							/* Interface handle */
5271 					0,		/* flags if any */
5272 					&(save_ire->ire_uinfo),
5273 					NULL,
5274 					NULL);
5275 
5276 				ire_refrele(save_ire);
5277 				if (ire == NULL) {
5278 					ip1dbg(("ip_newroute_v6:"
5279 					    "ire is NULL\n"));
5280 					break;
5281 				}
5282 
5283 				if ((sire != NULL) &&
5284 				    (sire->ire_flags & RTF_MULTIRT)) {
5285 					/*
5286 					 * processing a copy of the packet to
5287 					 * send for further resolution loops
5288 					 */
5289 					copy_mp = copymsg(first_mp);
5290 					if (copy_mp != NULL)
5291 						MULTIRT_DEBUG_TAG(copy_mp);
5292 				}
5293 				ire->ire_marks |= ire_marks;
5294 				ire_mp = ire->ire_mp;
5295 				/*
5296 				 * Now create or find an nce for this interface.
5297 				 * The hw addr will need to to be set from
5298 				 * the reply to the AR_ENTRY_QUERY that
5299 				 * we're about to send. This will be done in
5300 				 * ire_add_v6().
5301 				 */
5302 				err = ndp_resolver(dst_ill, &dst, mp, zoneid);
5303 				switch (err) {
5304 				case 0:
5305 					/*
5306 					 * New cache entry created.
5307 					 * Break, then ask the external
5308 					 * resolver.
5309 					 */
5310 					break;
5311 				case EINPROGRESS:
5312 					/*
5313 					 * Resolution in progress;
5314 					 * packet has been queued by
5315 					 * ndp_resolver().
5316 					 */
5317 					ire_delete(ire);
5318 					ire = NULL;
5319 					/*
5320 					 * Check if another multirt
5321 					 * route must be resolved.
5322 					 */
5323 					if (copy_mp != NULL) {
5324 						/*
5325 						 * If we found a resolver, we
5326 						 * ignore any trailing top
5327 						 * priority IRE_CACHE in
5328 						 * further loops. The reason is
5329 						 * the same as for noresolver.
5330 						 */
5331 						multirt_flags &=
5332 						    ~MULTIRT_CACHEGW;
5333 						/*
5334 						 * Search for the next
5335 						 * unresolved multirt route.
5336 						 */
5337 						first_mp = copy_mp;
5338 						copy_mp = NULL;
5339 						mp = first_mp;
5340 						if (mp->b_datap->db_type ==
5341 						    M_CTL) {
5342 							mp = mp->b_cont;
5343 						}
5344 						ASSERT(sire != NULL);
5345 						dst = save_dst;
5346 						/*
5347 						 * re-enter the loop
5348 						 */
5349 						multirt_resolve_next =
5350 						    B_TRUE;
5351 						continue;
5352 					}
5353 
5354 					if (sire != NULL)
5355 						ire_refrele(sire);
5356 					ill_refrele(dst_ill);
5357 					ipif_refrele(src_ipif);
5358 					return;
5359 				default:
5360 					/*
5361 					 * Transient error; packet will be
5362 					 * freed.
5363 					 */
5364 					ire_delete(ire);
5365 					ire = NULL;
5366 					break;
5367 				}
5368 				if (err != 0)
5369 					break;
5370 				/*
5371 				 * Now set up the AR_ENTRY_QUERY and send it.
5372 				 */
5373 				areq_mp = ill_arp_alloc(dst_ill,
5374 				    (uchar_t *)&ipv6_areq_template,
5375 				    (caddr_t)&dst);
5376 				if (areq_mp == NULL) {
5377 					ip1dbg(("ip_newroute_v6:"
5378 					    "areq_mp is NULL\n"));
5379 					freemsg(ire_mp);
5380 					break;
5381 				}
5382 				areq = (areq_t *)areq_mp->b_rptr;
5383 				addrp = (in6_addr_t *)((char *)areq +
5384 				    areq->areq_target_addr_offset);
5385 				*addrp = dst;
5386 				addrp = (in6_addr_t *)((char *)areq +
5387 				    areq->areq_sender_addr_offset);
5388 				*addrp = src_ipif->ipif_v6src_addr;
5389 				/*
5390 				 * link the chain, then send up to the resolver.
5391 				 */
5392 				linkb(areq_mp, ire_mp);
5393 				linkb(areq_mp, mp);
5394 				ip1dbg(("ip_newroute_v6:"
5395 				    "putnext to resolver\n"));
5396 				putnext(dst_ill->ill_rq, areq_mp);
5397 				/*
5398 				 * Check if another multirt route
5399 				 * must be resolved.
5400 				 */
5401 				ire = NULL;
5402 				if (copy_mp != NULL) {
5403 					/*
5404 					 * If we find a resolver, we ignore any
5405 					 * trailing top priority IRE_CACHE in
5406 					 * further loops. The reason is the
5407 					 * same as for noresolver.
5408 					 */
5409 					multirt_flags &= ~MULTIRT_CACHEGW;
5410 					/*
5411 					 * Search for the next unresolved
5412 					 * multirt route.
5413 					 */
5414 					first_mp = copy_mp;
5415 					copy_mp = NULL;
5416 					mp = first_mp;
5417 					if (mp->b_datap->db_type == M_CTL) {
5418 						mp = mp->b_cont;
5419 					}
5420 					ASSERT(sire != NULL);
5421 					dst = save_dst;
5422 					/*
5423 					 * re-enter the loop
5424 					 */
5425 					multirt_resolve_next = B_TRUE;
5426 					continue;
5427 				}
5428 
5429 				if (sire != NULL)
5430 					ire_refrele(sire);
5431 				ill_refrele(dst_ill);
5432 				ipif_refrele(src_ipif);
5433 				return;
5434 			}
5435 			/*
5436 			 * Non-external resolver case.
5437 			 *
5438 			 * TSol note: Please see the note above the
5439 			 * IRE_IF_NORESOLVER case.
5440 			 */
5441 			ga.ga_af = AF_INET6;
5442 			ga.ga_addr = dst;
5443 			gcgrp = gcgrp_lookup(&ga, B_FALSE);
5444 
5445 			ire = ire_create_v6(
5446 				&dst,			/* dest address */
5447 				&ipv6_all_ones,		/* mask */
5448 				&src_ipif->ipif_v6src_addr, /* source address */
5449 				&v6gw,			/* gateway address */
5450 				&save_ire->ire_max_frag,
5451 				NULL,			/* Fast Path header */
5452 				dst_ill->ill_rq,	/* recv-from queue */
5453 				dst_ill->ill_wq,	/* send-to queue */
5454 				IRE_CACHE,
5455 				NULL,
5456 				src_ipif,
5457 				&save_ire->ire_mask_v6,	/* Parent mask */
5458 				0,
5459 				save_ire->ire_ihandle,	/* Interface handle */
5460 				0,			/* flags if any */
5461 				&(save_ire->ire_uinfo),
5462 				NULL,
5463 				gcgrp);
5464 
5465 			if (ire == NULL) {
5466 				if (gcgrp != NULL) {
5467 					GCGRP_REFRELE(gcgrp);
5468 					gcgrp = NULL;
5469 				}
5470 				ire_refrele(save_ire);
5471 				break;
5472 			}
5473 
5474 			/* reference now held by IRE */
5475 			gcgrp = NULL;
5476 
5477 			if ((sire != NULL) &&
5478 			    (sire->ire_flags & RTF_MULTIRT)) {
5479 				copy_mp = copymsg(first_mp);
5480 				if (copy_mp != NULL)
5481 					MULTIRT_DEBUG_TAG(copy_mp);
5482 			}
5483 
5484 			ire->ire_marks |= ire_marks;
5485 			err = ndp_resolver(dst_ill, &dst, first_mp, zoneid);
5486 			switch (err) {
5487 			case 0:
5488 				/* Prevent save_ire from getting deleted */
5489 				IRB_REFHOLD(save_ire->ire_bucket);
5490 				/* Has it been removed already ? */
5491 				if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
5492 					IRB_REFRELE(save_ire->ire_bucket);
5493 					ire_refrele(save_ire);
5494 					break;
5495 				}
5496 
5497 				/*
5498 				 * We have a resolved cache entry,
5499 				 * add in the IRE.
5500 				 */
5501 				ire_add_then_send(q, ire, first_mp);
5502 				if (ip6_asp_table_held) {
5503 					ip6_asp_table_refrele();
5504 					ip6_asp_table_held = B_FALSE;
5505 				}
5506 
5507 				/* Assert that it is not deleted yet. */
5508 				ASSERT(save_ire->ire_ptpn != NULL);
5509 				IRB_REFRELE(save_ire->ire_bucket);
5510 				ire_refrele(save_ire);
5511 				/*
5512 				 * Check if another multirt route
5513 				 * must be resolved.
5514 				 */
5515 				ire = NULL;
5516 				if (copy_mp != NULL) {
5517 					/*
5518 					 * If we find a resolver, we ignore any
5519 					 * trailing top priority IRE_CACHE in
5520 					 * further loops. The reason is the
5521 					 * same as for noresolver.
5522 					 */
5523 					multirt_flags &= ~MULTIRT_CACHEGW;
5524 					/*
5525 					 * Search for the next unresolved
5526 					 * multirt route.
5527 					 */
5528 					first_mp = copy_mp;
5529 					copy_mp = NULL;
5530 					mp = first_mp;
5531 					if (mp->b_datap->db_type == M_CTL) {
5532 						mp = mp->b_cont;
5533 					}
5534 					ASSERT(sire != NULL);
5535 					dst = save_dst;
5536 					/*
5537 					 * re-enter the loop
5538 					 */
5539 					multirt_resolve_next = B_TRUE;
5540 					continue;
5541 				}
5542 
5543 				if (sire != NULL)
5544 					ire_refrele(sire);
5545 				ill_refrele(dst_ill);
5546 				ipif_refrele(src_ipif);
5547 				return;
5548 
5549 			case EINPROGRESS:
5550 				/*
5551 				 * mp was consumed - presumably queued.
5552 				 * No need for ire, presumably resolution is
5553 				 * in progress, and ire will be added when the
5554 				 * address is resolved.
5555 				 */
5556 				if (ip6_asp_table_held) {
5557 					ip6_asp_table_refrele();
5558 					ip6_asp_table_held = B_FALSE;
5559 				}
5560 				ASSERT(ire->ire_nce == NULL);
5561 				ire_delete(ire);
5562 				ire_refrele(save_ire);
5563 				/*
5564 				 * Check if another multirt route
5565 				 * must be resolved.
5566 				 */
5567 				ire = NULL;
5568 				if (copy_mp != NULL) {
5569 					/*
5570 					 * If we find a resolver, we ignore any
5571 					 * trailing top priority IRE_CACHE in
5572 					 * further loops. The reason is the
5573 					 * same as for noresolver.
5574 					 */
5575 					multirt_flags &= ~MULTIRT_CACHEGW;
5576 					/*
5577 					 * Search for the next unresolved
5578 					 * multirt route.
5579 					 */
5580 					first_mp = copy_mp;
5581 					copy_mp = NULL;
5582 					mp = first_mp;
5583 					if (mp->b_datap->db_type == M_CTL) {
5584 						mp = mp->b_cont;
5585 					}
5586 					ASSERT(sire != NULL);
5587 					dst = save_dst;
5588 					/*
5589 					 * re-enter the loop
5590 					 */
5591 					multirt_resolve_next = B_TRUE;
5592 					continue;
5593 				}
5594 				if (sire != NULL)
5595 					ire_refrele(sire);
5596 				ill_refrele(dst_ill);
5597 				ipif_refrele(src_ipif);
5598 				return;
5599 			default:
5600 				/* Some transient error */
5601 				ASSERT(ire->ire_nce == NULL);
5602 				ire_refrele(save_ire);
5603 				break;
5604 			}
5605 			break;
5606 		default:
5607 			break;
5608 		}
5609 		if (ip6_asp_table_held) {
5610 			ip6_asp_table_refrele();
5611 			ip6_asp_table_held = B_FALSE;
5612 		}
5613 	} while (multirt_resolve_next);
5614 
5615 err_ret:
5616 	ip1dbg(("ip_newroute_v6: dropped\n"));
5617 	if (src_ipif != NULL)
5618 		ipif_refrele(src_ipif);
5619 	if (dst_ill != NULL) {
5620 		need_rele = B_TRUE;
5621 		ill = dst_ill;
5622 	}
5623 	if (ill != NULL) {
5624 		if (mp->b_prev != NULL) {
5625 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
5626 		} else {
5627 			BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards);
5628 		}
5629 
5630 		if (need_rele)
5631 			ill_refrele(ill);
5632 	} else {
5633 		if (mp->b_prev != NULL) {
5634 			BUMP_MIB(&ip6_mib, ipv6InDiscards);
5635 		} else {
5636 			BUMP_MIB(&ip6_mib, ipv6OutDiscards);
5637 		}
5638 	}
5639 	/* Did this packet originate externally? */
5640 	if (mp->b_prev) {
5641 		mp->b_next = NULL;
5642 		mp->b_prev = NULL;
5643 	}
5644 	if (copy_mp != NULL) {
5645 		MULTIRT_DEBUG_UNTAG(copy_mp);
5646 		freemsg(copy_mp);
5647 	}
5648 	MULTIRT_DEBUG_UNTAG(first_mp);
5649 	freemsg(first_mp);
5650 	if (ire != NULL)
5651 		ire_refrele(ire);
5652 	if (sire != NULL)
5653 		ire_refrele(sire);
5654 	return;
5655 
5656 icmp_err_ret:
5657 	if (ip6_asp_table_held)
5658 		ip6_asp_table_refrele();
5659 	if (src_ipif != NULL)
5660 		ipif_refrele(src_ipif);
5661 	if (dst_ill != NULL) {
5662 		need_rele = B_TRUE;
5663 		ill = dst_ill;
5664 	}
5665 	ip1dbg(("ip_newroute_v6: no route\n"));
5666 	if (sire != NULL)
5667 		ire_refrele(sire);
5668 	/*
5669 	 * We need to set sire to NULL to avoid double freeing if we
5670 	 * ever goto err_ret from below.
5671 	 */
5672 	sire = NULL;
5673 	ip6h = (ip6_t *)mp->b_rptr;
5674 	/* Skip ip6i_t header if present */
5675 	if (ip6h->ip6_nxt == IPPROTO_RAW) {
5676 		/* Make sure the IPv6 header is present */
5677 		if ((mp->b_wptr - (uchar_t *)ip6h) <
5678 		    sizeof (ip6i_t) + IPV6_HDR_LEN) {
5679 			if (!pullupmsg(mp, sizeof (ip6i_t) + IPV6_HDR_LEN)) {
5680 				ip1dbg(("ip_newroute_v6: pullupmsg failed\n"));
5681 				goto err_ret;
5682 			}
5683 		}
5684 		mp->b_rptr += sizeof (ip6i_t);
5685 		ip6h = (ip6_t *)mp->b_rptr;
5686 	}
5687 	/* Did this packet originate externally? */
5688 	if (mp->b_prev) {
5689 		if (ill != NULL) {
5690 			BUMP_MIB(ill->ill_ip6_mib, ipv6InNoRoutes);
5691 		} else {
5692 			BUMP_MIB(&ip6_mib, ipv6InNoRoutes);
5693 		}
5694 		mp->b_next = NULL;
5695 		mp->b_prev = NULL;
5696 		q = WR(q);
5697 	} else {
5698 		if (ill != NULL) {
5699 			BUMP_MIB(ill->ill_ip6_mib, ipv6OutNoRoutes);
5700 		} else {
5701 			BUMP_MIB(&ip6_mib, ipv6OutNoRoutes);
5702 		}
5703 		if (ip_hdr_complete_v6(ip6h, zoneid)) {
5704 			/* Failed */
5705 			if (copy_mp != NULL) {
5706 				MULTIRT_DEBUG_UNTAG(copy_mp);
5707 				freemsg(copy_mp);
5708 			}
5709 			MULTIRT_DEBUG_UNTAG(first_mp);
5710 			freemsg(first_mp);
5711 			if (ire != NULL)
5712 				ire_refrele(ire);
5713 			if (need_rele)
5714 				ill_refrele(ill);
5715 			return;
5716 		}
5717 	}
5718 
5719 	if (need_rele)
5720 		ill_refrele(ill);
5721 
5722 	/*
5723 	 * At this point we will have ire only if RTF_BLACKHOLE
5724 	 * or RTF_REJECT flags are set on the IRE. It will not
5725 	 * generate ICMP6_DST_UNREACH_NOROUTE if RTF_BLACKHOLE is set.
5726 	 */
5727 	if (ire != NULL) {
5728 		if (ire->ire_flags & RTF_BLACKHOLE) {
5729 			ire_refrele(ire);
5730 			if (copy_mp != NULL) {
5731 				MULTIRT_DEBUG_UNTAG(copy_mp);
5732 				freemsg(copy_mp);
5733 			}
5734 			MULTIRT_DEBUG_UNTAG(first_mp);
5735 			freemsg(first_mp);
5736 			return;
5737 		}
5738 		ire_refrele(ire);
5739 	}
5740 	if (ip_debug > 3) {
5741 		/* ip2dbg */
5742 		pr_addr_dbg("ip_newroute_v6: no route to %s\n",
5743 		    AF_INET6, v6dstp);
5744 	}
5745 	icmp_unreachable_v6(WR(q), first_mp, ICMP6_DST_UNREACH_NOROUTE,
5746 	    B_FALSE, B_FALSE, zoneid);
5747 }
5748 
5749 /*
5750  * ip_newroute_ipif_v6 is called by ip_wput_v6 and ip_wput_ipsec_out_v6 whenever
5751  * we need to send out a packet to a destination address for which we do not
5752  * have specific routing information. It is only used for multicast packets.
5753  *
5754  * If unspec_src we allow creating an IRE with source address zero.
5755  * ire_send_v6() will delete it after the packet is sent.
5756  */
5757 void
5758 ip_newroute_ipif_v6(queue_t *q, mblk_t *mp, ipif_t *ipif,
5759     in6_addr_t v6dst, int unspec_src, zoneid_t zoneid)
5760 {
5761 	ire_t	*ire = NULL;
5762 	ipif_t	*src_ipif = NULL;
5763 	int	err = 0;
5764 	ill_t	*dst_ill = NULL;
5765 	ire_t	*save_ire;
5766 	ushort_t ire_marks = 0;
5767 	ipsec_out_t *io;
5768 	ill_t *attach_ill = NULL;
5769 	ill_t *ill;
5770 	ip6_t *ip6h;
5771 	mblk_t *first_mp;
5772 	boolean_t ip6i_present;
5773 	ire_t *fire = NULL;
5774 	mblk_t  *copy_mp = NULL;
5775 	boolean_t multirt_resolve_next;
5776 	in6_addr_t *v6dstp = &v6dst;
5777 	boolean_t ipif_held = B_FALSE;
5778 	boolean_t ill_held = B_FALSE;
5779 	boolean_t ip6_asp_table_held = B_FALSE;
5780 
5781 	/*
5782 	 * This loop is run only once in most cases.
5783 	 * We loop to resolve further routes only when the destination
5784 	 * can be reached through multiple RTF_MULTIRT-flagged ires.
5785 	 */
5786 	do {
5787 		multirt_resolve_next = B_FALSE;
5788 		if (dst_ill != NULL) {
5789 			ill_refrele(dst_ill);
5790 			dst_ill = NULL;
5791 		}
5792 
5793 		if (src_ipif != NULL) {
5794 			ipif_refrele(src_ipif);
5795 			src_ipif = NULL;
5796 		}
5797 		ASSERT(ipif != NULL);
5798 		ill = ipif->ipif_ill;
5799 
5800 		ASSERT(!IN6_IS_ADDR_V4MAPPED(v6dstp));
5801 		if (ip_debug > 2) {
5802 			/* ip1dbg */
5803 			pr_addr_dbg("ip_newroute_ipif_v6: v6dst %s\n",
5804 			    AF_INET6, v6dstp);
5805 			printf("ip_newroute_ipif_v6: if %s, v6 %d\n",
5806 			    ill->ill_name, ipif->ipif_isv6);
5807 		}
5808 
5809 		first_mp = mp;
5810 		if (mp->b_datap->db_type == M_CTL) {
5811 			mp = mp->b_cont;
5812 			io = (ipsec_out_t *)first_mp->b_rptr;
5813 			ASSERT(io->ipsec_out_type == IPSEC_OUT);
5814 		} else {
5815 			io = NULL;
5816 		}
5817 
5818 		/*
5819 		 * If the interface is a pt-pt interface we look for an
5820 		 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the
5821 		 * local_address and the pt-pt destination address.
5822 		 * Otherwise we just match the local address.
5823 		 */
5824 		if (!(ill->ill_flags & ILLF_MULTICAST)) {
5825 			goto err_ret;
5826 		}
5827 		/*
5828 		 * If this end point is bound to IPIF_NOFAILOVER, set bnf_ill
5829 		 * and bind_to_nofailover B_TRUE. We can't use conn to determine
5830 		 * as it could be NULL.
5831 		 *
5832 		 * This information can appear either in an ip6i_t or an
5833 		 * IPSEC_OUT message.
5834 		 */
5835 		ip6h = (ip6_t *)mp->b_rptr;
5836 		ip6i_present = (ip6h->ip6_nxt == IPPROTO_RAW);
5837 		if (ip6i_present || (io != NULL && io->ipsec_out_attach_if)) {
5838 			if (!ip6i_present ||
5839 			    ((ip6i_t *)ip6h)->ip6i_flags & IP6I_ATTACH_IF) {
5840 				attach_ill = ip_grab_attach_ill(ill, first_mp,
5841 				    (ip6i_present ?
5842 					((ip6i_t *)ip6h)->ip6i_ifindex :
5843 					io->ipsec_out_ill_index), B_TRUE);
5844 				/* Failure case frees things for us. */
5845 				if (attach_ill == NULL)
5846 					return;
5847 
5848 				/*
5849 				 * Check if we need an ire that will not be
5850 				 * looked up by anybody else i.e. HIDDEN.
5851 				 */
5852 				if (ill_is_probeonly(attach_ill))
5853 					ire_marks = IRE_MARK_HIDDEN;
5854 			}
5855 		}
5856 
5857 		/*
5858 		 * We check if an IRE_OFFSUBNET for the addr that goes through
5859 		 * ipif exists. We need it to determine if the RTF_SETSRC and/or
5860 		 * RTF_MULTIRT flags must be honored.
5861 		 */
5862 		fire = ipif_lookup_multi_ire_v6(ipif, v6dstp);
5863 		ip2dbg(("ip_newroute_ipif_v6: "
5864 			"ipif_lookup_multi_ire_v6("
5865 			"ipif %p, dst %08x) = fire %p\n",
5866 			(void *)ipif, ntohl(V4_PART_OF_V6((*v6dstp))),
5867 			(void *)fire));
5868 
5869 		/*
5870 		 * If the application specified the ill (ifindex), we still
5871 		 * load spread. Only if the packets needs to go out specifically
5872 		 * on a given ill e.g. binding to IPIF_NOFAILOVER address or
5873 		 * IPV6_BOUND_PIF, or there is a parent ire entry that specified
5874 		 * multirouting, then we don't try to use a different ill for
5875 		 * load spreading.
5876 		 */
5877 		if (attach_ill == NULL) {
5878 			/*
5879 			 * If the interface belongs to an interface group,
5880 			 * make sure the next possible interface in the group
5881 			 * is used.  This encourages load spreading among peers
5882 			 * in an interface group.
5883 			 *
5884 			 * Note: While we pick a dst_ill we are really only
5885 			 * interested in the ill for load spreading. The source
5886 			 * ipif is determined by source address selection below.
5887 			 */
5888 			if ((fire != NULL) && (fire->ire_flags & RTF_MULTIRT)) {
5889 				dst_ill = ipif->ipif_ill;
5890 				/* For uniformity do a refhold */
5891 				ill_refhold(dst_ill);
5892 			} else {
5893 				/* refheld by ip_newroute_get_dst_ill_v6 */
5894 				dst_ill =
5895 				    ip_newroute_get_dst_ill_v6(ipif->ipif_ill);
5896 			}
5897 			if (dst_ill == NULL) {
5898 				if (ip_debug > 2) {
5899 					pr_addr_dbg("ip_newroute_ipif_v6: "
5900 					    "no dst ill for dst %s\n",
5901 					    AF_INET6, v6dstp);
5902 				}
5903 				goto err_ret;
5904 			}
5905 		} else {
5906 			dst_ill = ipif->ipif_ill;
5907 			/*
5908 			 * ip_wput_v6 passes the right ipif for IPIF_NOFAILOVER
5909 			 * and IPV6_BOUND_PIF case.
5910 			 */
5911 			ASSERT(dst_ill == attach_ill);
5912 			/* attach_ill is already refheld */
5913 		}
5914 		/*
5915 		 * Pick a source address which matches the scope of the
5916 		 * destination address.
5917 		 * For RTF_SETSRC routes, the source address is imposed by the
5918 		 * parent ire (fire).
5919 		 */
5920 		ASSERT(src_ipif == NULL);
5921 		if ((fire != NULL) && (fire->ire_flags & RTF_SETSRC)) {
5922 			/*
5923 			 * Check that the ipif matching the requested source
5924 			 * address still exists.
5925 			 */
5926 			src_ipif =
5927 			    ipif_lookup_addr_v6(&fire->ire_src_addr_v6,
5928 				NULL, zoneid, NULL, NULL, NULL, NULL);
5929 		}
5930 		if (src_ipif == NULL && ip6_asp_can_lookup()) {
5931 			ip6_asp_table_held = B_TRUE;
5932 			src_ipif = ipif_select_source_v6(dst_ill, v6dstp,
5933 			    RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT, zoneid);
5934 		}
5935 
5936 		if (src_ipif == NULL) {
5937 			if (!unspec_src) {
5938 				if (ip_debug > 2) {
5939 					/* ip1dbg */
5940 					pr_addr_dbg("ip_newroute_ipif_v6: "
5941 					    "no src for dst %s\n,",
5942 					    AF_INET6, v6dstp);
5943 					printf(" through interface %s\n",
5944 					    dst_ill->ill_name);
5945 				}
5946 				goto err_ret;
5947 			}
5948 			src_ipif = ipif;
5949 			ipif_refhold(src_ipif);
5950 		}
5951 		ire = ipif_to_ire_v6(ipif);
5952 		if (ire == NULL) {
5953 			if (ip_debug > 2) {
5954 				/* ip1dbg */
5955 				pr_addr_dbg("ip_newroute_ipif_v6: v6src %s\n",
5956 				    AF_INET6, &ipif->ipif_v6lcl_addr);
5957 				printf("ip_newroute_ipif_v6: "
5958 				    "if %s\n", dst_ill->ill_name);
5959 			}
5960 			goto err_ret;
5961 		}
5962 		if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
5963 			goto err_ret;
5964 
5965 		ASSERT(ire->ire_ipversion == IPV6_VERSION);
5966 
5967 		ip1dbg(("ip_newroute_ipif_v6: interface type %s (%d),",
5968 		    ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type));
5969 		if (ip_debug > 2) {
5970 			/* ip1dbg */
5971 			pr_addr_dbg(" address %s\n",
5972 			    AF_INET6, &ire->ire_src_addr_v6);
5973 		}
5974 		save_ire = ire;
5975 		ip2dbg(("ip_newroute_ipif: ire %p, ipif %p\n",
5976 			(void *)ire, (void *)ipif));
5977 
5978 		if ((fire != NULL) && (fire->ire_flags & RTF_MULTIRT)) {
5979 			/*
5980 			 * an IRE_OFFSUBET was looked up
5981 			 * on that interface.
5982 			 * this ire has RTF_MULTIRT flag,
5983 			 * so the resolution loop
5984 			 * will be re-entered to resolve
5985 			 * additional routes on other
5986 			 * interfaces. For that purpose,
5987 			 * a copy of the packet is
5988 			 * made at this point.
5989 			 */
5990 			fire->ire_last_used_time = lbolt;
5991 			copy_mp = copymsg(first_mp);
5992 			if (copy_mp) {
5993 				MULTIRT_DEBUG_TAG(copy_mp);
5994 			}
5995 		}
5996 
5997 		ASSERT((attach_ill == NULL) || (dst_ill == attach_ill));
5998 		switch (ire->ire_type) {
5999 		case IRE_IF_NORESOLVER: {
6000 			/* We have what we need to build an IRE_CACHE. */
6001 			mblk_t	*dlureq_mp;
6002 
6003 			/*
6004 			 * Create a new dlureq_mp with the
6005 			 * IPv6 gateway address in destination address in the
6006 			 * DLPI hdr if the physical length is exactly 16 bytes.
6007 			 */
6008 			ASSERT(dst_ill->ill_isv6);
6009 			if (dst_ill->ill_phys_addr_length == IPV6_ADDR_LEN) {
6010 				dlureq_mp = ill_dlur_gen((uchar_t *)v6dstp,
6011 				    dst_ill->ill_phys_addr_length,
6012 				    dst_ill->ill_sap,
6013 				    dst_ill->ill_sap_length);
6014 			} else {
6015 				/*
6016 				 * handle the Gated case, where we create
6017 				 * a NORESOLVER route for loopback.
6018 				 */
6019 				if (dst_ill->ill_net_type != IRE_IF_NORESOLVER)
6020 					break;
6021 				dlureq_mp = ill_dlur_gen(NULL,
6022 				    dst_ill->ill_phys_addr_length,
6023 				    dst_ill->ill_sap,
6024 				    dst_ill->ill_sap_length);
6025 			}
6026 
6027 			if (dlureq_mp == NULL)
6028 				break;
6029 			/*
6030 			 * The newly created ire will inherit the flags of the
6031 			 * parent ire, if any.
6032 			 */
6033 			ire = ire_create_v6(
6034 				v6dstp,			/* dest address */
6035 				&ipv6_all_ones,		/* mask */
6036 				&src_ipif->ipif_v6src_addr, /* source address */
6037 				NULL,			/* gateway address */
6038 				&save_ire->ire_max_frag,
6039 				NULL,			/* Fast Path header */
6040 				dst_ill->ill_rq,	/* recv-from queue */
6041 				dst_ill->ill_wq,	/* send-to queue */
6042 				IRE_CACHE,
6043 				dlureq_mp,
6044 				src_ipif,
6045 				NULL,
6046 				(fire != NULL) ?	/* Parent handle */
6047 				    fire->ire_phandle : 0,
6048 				save_ire->ire_ihandle,	/* Interface handle */
6049 				(fire != NULL) ?
6050 				(fire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) :
6051 				0,
6052 				&ire_uinfo_null,
6053 				NULL,
6054 				NULL);
6055 
6056 			freeb(dlureq_mp);
6057 
6058 			if (ire == NULL) {
6059 				ire_refrele(save_ire);
6060 				break;
6061 			}
6062 
6063 			ire->ire_marks |= ire_marks;
6064 
6065 			err = ndp_noresolver(dst_ill, v6dstp);
6066 			if (err != 0) {
6067 				ire_refrele(save_ire);
6068 				break;
6069 			}
6070 
6071 			/* Prevent save_ire from getting deleted */
6072 			IRB_REFHOLD(save_ire->ire_bucket);
6073 			/* Has it been removed already ? */
6074 			if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
6075 				IRB_REFRELE(save_ire->ire_bucket);
6076 				ire_refrele(save_ire);
6077 				break;
6078 			}
6079 
6080 			ire_add_then_send(q, ire, first_mp);
6081 			if (ip6_asp_table_held) {
6082 				ip6_asp_table_refrele();
6083 				ip6_asp_table_held = B_FALSE;
6084 			}
6085 
6086 			/* Assert that it is not deleted yet. */
6087 			ASSERT(save_ire->ire_ptpn != NULL);
6088 			IRB_REFRELE(save_ire->ire_bucket);
6089 			ire_refrele(save_ire);
6090 			if (fire != NULL) {
6091 				ire_refrele(fire);
6092 				fire = NULL;
6093 			}
6094 
6095 			/*
6096 			 * The resolution loop is re-entered if we
6097 			 * actually are in a multirouting case.
6098 			 */
6099 			if (copy_mp != NULL) {
6100 				boolean_t need_resolve =
6101 				    ire_multirt_need_resolve_v6(v6dstp,
6102 					MBLK_GETLABEL(copy_mp));
6103 				if (!need_resolve) {
6104 					MULTIRT_DEBUG_UNTAG(copy_mp);
6105 					freemsg(copy_mp);
6106 					copy_mp = NULL;
6107 				} else {
6108 					/*
6109 					 * ipif_lookup_group_v6() calls
6110 					 * ire_lookup_multi_v6() that uses
6111 					 * ire_ftable_lookup_v6() to find
6112 					 * an IRE_INTERFACE for the group.
6113 					 * In the multirt case,
6114 					 * ire_lookup_multi_v6() then invokes
6115 					 * ire_multirt_lookup_v6() to find
6116 					 * the next resolvable ire.
6117 					 * As a result, we obtain a new
6118 					 * interface, derived from the
6119 					 * next ire.
6120 					 */
6121 					if (ipif_held) {
6122 						ipif_refrele(ipif);
6123 						ipif_held = B_FALSE;
6124 					}
6125 					ipif = ipif_lookup_group_v6(v6dstp,
6126 					    zoneid);
6127 					ip2dbg(("ip_newroute_ipif: "
6128 						"multirt dst %08x, ipif %p\n",
6129 						ntohl(V4_PART_OF_V6((*v6dstp))),
6130 						(void *)ipif));
6131 					if (ipif != NULL) {
6132 						ipif_held = B_TRUE;
6133 						mp = copy_mp;
6134 						copy_mp = NULL;
6135 						multirt_resolve_next =
6136 						    B_TRUE;
6137 						continue;
6138 					} else {
6139 						freemsg(copy_mp);
6140 					}
6141 				}
6142 			}
6143 			ill_refrele(dst_ill);
6144 			if (ipif_held) {
6145 				ipif_refrele(ipif);
6146 				ipif_held = B_FALSE;
6147 			}
6148 			if (src_ipif != NULL)
6149 				ipif_refrele(src_ipif);
6150 			return;
6151 		}
6152 		case IRE_IF_RESOLVER: {
6153 
6154 			ASSERT(dst_ill->ill_isv6);
6155 
6156 			/*
6157 			 * We obtain a partial IRE_CACHE which we will pass
6158 			 * along with the resolver query.  When the response
6159 			 * comes back it will be there ready for us to add.
6160 			 */
6161 			/*
6162 			 * the newly created ire will inherit the flags of the
6163 			 * parent ire, if any.
6164 			 */
6165 			ire = ire_create_v6(
6166 				v6dstp,			/* dest address */
6167 				&ipv6_all_ones,		/* mask */
6168 				&src_ipif->ipif_v6src_addr, /* source address */
6169 				NULL,			/* gateway address */
6170 				&save_ire->ire_max_frag,
6171 				NULL,			/* Fast Path header */
6172 				dst_ill->ill_rq,	/* recv-from queue */
6173 				dst_ill->ill_wq,	/* send-to queue */
6174 				IRE_CACHE,
6175 				NULL,
6176 				src_ipif,
6177 				NULL,
6178 				(fire != NULL) ?	/* Parent handle */
6179 				    fire->ire_phandle : 0,
6180 				save_ire->ire_ihandle,	/* Interface handle */
6181 				(fire != NULL) ?
6182 				(fire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) :
6183 				0,
6184 				&ire_uinfo_null,
6185 				NULL,
6186 				NULL);
6187 
6188 			if (ire == NULL) {
6189 				ire_refrele(save_ire);
6190 				break;
6191 			}
6192 
6193 			ire->ire_marks |= ire_marks;
6194 
6195 			/* Resolve and add ire to the ctable */
6196 			err = ndp_resolver(dst_ill, v6dstp, first_mp, zoneid);
6197 			switch (err) {
6198 			case 0:
6199 				/* Prevent save_ire from getting deleted */
6200 				IRB_REFHOLD(save_ire->ire_bucket);
6201 				/* Has it been removed already ? */
6202 				if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
6203 					IRB_REFRELE(save_ire->ire_bucket);
6204 					ire_refrele(save_ire);
6205 					break;
6206 				}
6207 				/*
6208 				 * We have a resolved cache entry,
6209 				 * add in the IRE.
6210 				 */
6211 				ire_add_then_send(q, ire, first_mp);
6212 				if (ip6_asp_table_held) {
6213 					ip6_asp_table_refrele();
6214 					ip6_asp_table_held = B_FALSE;
6215 				}
6216 
6217 				/* Assert that it is not deleted yet. */
6218 				ASSERT(save_ire->ire_ptpn != NULL);
6219 				IRB_REFRELE(save_ire->ire_bucket);
6220 				ire_refrele(save_ire);
6221 				if (fire != NULL) {
6222 					ire_refrele(fire);
6223 					fire = NULL;
6224 				}
6225 
6226 				/*
6227 				 * The resolution loop is re-entered if we
6228 				 * actually are in a multirouting case.
6229 				 */
6230 				if (copy_mp != NULL) {
6231 					boolean_t need_resolve =
6232 					    ire_multirt_need_resolve_v6(v6dstp,
6233 						MBLK_GETLABEL(copy_mp));
6234 					if (!need_resolve) {
6235 						MULTIRT_DEBUG_UNTAG(copy_mp);
6236 						freemsg(copy_mp);
6237 						copy_mp = NULL;
6238 					} else {
6239 						/*
6240 						 * ipif_lookup_group_v6() calls
6241 						 * ire_lookup_multi_v6() that
6242 						 * uses ire_ftable_lookup_v6()
6243 						 * to find an IRE_INTERFACE for
6244 						 * the group. In the multirt
6245 						 * case, ire_lookup_multi_v6()
6246 						 * then invokes
6247 						 * ire_multirt_lookup_v6() to
6248 						 * find the next resolvable ire.
6249 						 * As a result, we obtain a new
6250 						 * interface, derived from the
6251 						 * next ire.
6252 						 */
6253 						if (ipif_held) {
6254 							ipif_refrele(ipif);
6255 							ipif_held = B_FALSE;
6256 						}
6257 						ipif = ipif_lookup_group_v6(
6258 						    v6dstp, zoneid);
6259 						ip2dbg(("ip_newroute_ipif: "
6260 						    "multirt dst %08x, "
6261 						    "ipif %p\n",
6262 						    ntohl(V4_PART_OF_V6(
6263 							(*v6dstp))),
6264 						    (void *)ipif));
6265 						if (ipif != NULL) {
6266 							ipif_held = B_TRUE;
6267 							mp = copy_mp;
6268 							copy_mp = NULL;
6269 							multirt_resolve_next =
6270 							    B_TRUE;
6271 							continue;
6272 						} else {
6273 							freemsg(copy_mp);
6274 						}
6275 					}
6276 				}
6277 				ill_refrele(dst_ill);
6278 				if (ipif_held) {
6279 					ipif_refrele(ipif);
6280 					ipif_held = B_FALSE;
6281 				}
6282 				if (src_ipif != NULL)
6283 					ipif_refrele(src_ipif);
6284 				return;
6285 
6286 			case EINPROGRESS:
6287 				/*
6288 				 * mp was consumed - presumably queued.
6289 				 * No need for ire, presumably resolution is
6290 				 * in progress, and ire will be added when the
6291 				 * address is resolved.
6292 				 */
6293 				if (ip6_asp_table_held) {
6294 					ip6_asp_table_refrele();
6295 					ip6_asp_table_held = B_FALSE;
6296 				}
6297 				ire_delete(ire);
6298 				ire_refrele(save_ire);
6299 				if (fire != NULL) {
6300 					ire_refrele(fire);
6301 					fire = NULL;
6302 				}
6303 
6304 				/*
6305 				 * The resolution loop is re-entered if we
6306 				 * actually are in a multirouting case.
6307 				 */
6308 				if (copy_mp != NULL) {
6309 					boolean_t need_resolve =
6310 					    ire_multirt_need_resolve_v6(v6dstp,
6311 						MBLK_GETLABEL(copy_mp));
6312 					if (!need_resolve) {
6313 						MULTIRT_DEBUG_UNTAG(copy_mp);
6314 						freemsg(copy_mp);
6315 						copy_mp = NULL;
6316 					} else {
6317 						/*
6318 						 * ipif_lookup_group_v6() calls
6319 						 * ire_lookup_multi_v6() that
6320 						 * uses ire_ftable_lookup_v6()
6321 						 * to find an IRE_INTERFACE for
6322 						 * the group. In the multirt
6323 						 * case, ire_lookup_multi_v6()
6324 						 * then invokes
6325 						 * ire_multirt_lookup_v6() to
6326 						 * find the next resolvable ire.
6327 						 * As a result, we obtain a new
6328 						 * interface, derived from the
6329 						 * next ire.
6330 						 */
6331 						if (ipif_held) {
6332 							ipif_refrele(ipif);
6333 							ipif_held = B_FALSE;
6334 						}
6335 						ipif = ipif_lookup_group_v6(
6336 						    v6dstp, zoneid);
6337 						ip2dbg(("ip_newroute_ipif: "
6338 						    "multirt dst %08x, "
6339 						    "ipif %p\n",
6340 						    ntohl(V4_PART_OF_V6(
6341 							(*v6dstp))),
6342 						    (void *)ipif));
6343 						if (ipif != NULL) {
6344 							ipif_held = B_TRUE;
6345 							mp = copy_mp;
6346 							copy_mp = NULL;
6347 							multirt_resolve_next =
6348 							    B_TRUE;
6349 							continue;
6350 						} else {
6351 							freemsg(copy_mp);
6352 						}
6353 					}
6354 				}
6355 				ill_refrele(dst_ill);
6356 				if (ipif_held) {
6357 					ipif_refrele(ipif);
6358 					ipif_held = B_FALSE;
6359 				}
6360 				if (src_ipif != NULL)
6361 					ipif_refrele(src_ipif);
6362 				return;
6363 			default:
6364 				/* Some transient error */
6365 				ire_refrele(save_ire);
6366 				break;
6367 			}
6368 			break;
6369 		}
6370 		default:
6371 			break;
6372 		}
6373 		if (ip6_asp_table_held) {
6374 			ip6_asp_table_refrele();
6375 			ip6_asp_table_held = B_FALSE;
6376 		}
6377 	} while (multirt_resolve_next);
6378 
6379 err_ret:
6380 	if (ip6_asp_table_held)
6381 		ip6_asp_table_refrele();
6382 	if (ire != NULL)
6383 		ire_refrele(ire);
6384 	if (fire != NULL)
6385 		ire_refrele(fire);
6386 	if (ipif != NULL && ipif_held)
6387 		ipif_refrele(ipif);
6388 	if (src_ipif != NULL)
6389 		ipif_refrele(src_ipif);
6390 	/* Multicast - no point in trying to generate ICMP error */
6391 	ASSERT((attach_ill == NULL) || (dst_ill == attach_ill));
6392 	if (dst_ill != NULL) {
6393 		ill = dst_ill;
6394 		ill_held = B_TRUE;
6395 	}
6396 	if (mp->b_prev || mp->b_next) {
6397 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
6398 	} else {
6399 		BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards);
6400 	}
6401 	ip1dbg(("ip_newroute_ipif_v6: dropped\n"));
6402 	mp->b_next = NULL;
6403 	mp->b_prev = NULL;
6404 	freemsg(first_mp);
6405 	if (ill_held)
6406 		ill_refrele(ill);
6407 }
6408 
6409 /*
6410  * Parse and process any hop-by-hop or destination options.
6411  *
6412  * Assumes that q is an ill read queue so that ICMP errors for link-local
6413  * destinations are sent out the correct interface.
6414  *
6415  * Returns -1 if there was an error and mp has been consumed.
6416  * Returns 0 if no special action is needed.
6417  * Returns 1 if the packet contained a router alert option for this node
6418  * which is verified to be "interesting/known" for our implementation.
6419  *
6420  * XXX Note: In future as more hbh or dest options are defined,
6421  * it may be better to have different routines for hbh and dest
6422  * options as opt_type fields other than IP6OPT_PAD1 and IP6OPT_PADN
6423  * may have same value in different namespaces. Or is it same namespace ??
6424  * Current code checks for each opt_type (other than pads) if it is in
6425  * the expected  nexthdr (hbh or dest)
6426  */
6427 static int
6428 ip_process_options_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h,
6429     uint8_t *optptr, uint_t optlen, uint8_t hdr_type)
6430 {
6431 	uint8_t opt_type;
6432 	uint_t optused;
6433 	int ret = 0;
6434 	mblk_t *first_mp;
6435 	const char *errtype;
6436 	zoneid_t zoneid;
6437 	ill_t *ill = q->q_ptr;
6438 
6439 	first_mp = mp;
6440 	if (mp->b_datap->db_type == M_CTL) {
6441 		mp = mp->b_cont;
6442 	}
6443 
6444 	while (optlen != 0) {
6445 		opt_type = *optptr;
6446 		if (opt_type == IP6OPT_PAD1) {
6447 			optused = 1;
6448 		} else {
6449 			if (optlen < 2)
6450 				goto bad_opt;
6451 			errtype = "malformed";
6452 			if (opt_type == ip6opt_ls) {
6453 				optused = 2 + optptr[1];
6454 				if (optused > optlen)
6455 					goto bad_opt;
6456 			} else switch (opt_type) {
6457 			case IP6OPT_PADN:
6458 				/*
6459 				 * Note:We don't verify that (N-2) pad octets
6460 				 * are zero as required by spec. Adhere to
6461 				 * "be liberal in what you accept..." part of
6462 				 * implementation philosophy (RFC791,RFC1122)
6463 				 */
6464 				optused = 2 + optptr[1];
6465 				if (optused > optlen)
6466 					goto bad_opt;
6467 				break;
6468 
6469 			case IP6OPT_JUMBO:
6470 				if (hdr_type != IPPROTO_HOPOPTS)
6471 					goto opt_error;
6472 				goto opt_error; /* XXX Not implemented! */
6473 
6474 			case IP6OPT_ROUTER_ALERT: {
6475 				struct ip6_opt_router *or;
6476 
6477 				if (hdr_type != IPPROTO_HOPOPTS)
6478 					goto opt_error;
6479 				optused = 2 + optptr[1];
6480 				if (optused > optlen)
6481 					goto bad_opt;
6482 				or = (struct ip6_opt_router *)optptr;
6483 				/* Check total length and alignment */
6484 				if (optused != sizeof (*or) ||
6485 				    ((uintptr_t)or->ip6or_value & 0x1) != 0)
6486 					goto opt_error;
6487 				/* Check value */
6488 				switch (*((uint16_t *)or->ip6or_value)) {
6489 				case IP6_ALERT_MLD:
6490 				case IP6_ALERT_RSVP:
6491 					ret = 1;
6492 				}
6493 				break;
6494 			}
6495 			case IP6OPT_HOME_ADDRESS: {
6496 				/*
6497 				 * Minimal support for the home address option
6498 				 * (which is required by all IPv6 nodes).
6499 				 * Implement by just swapping the home address
6500 				 * and source address.
6501 				 * XXX Note: this has IPsec implications since
6502 				 * AH needs to take this into account.
6503 				 * Also, when IPsec is used we need to ensure
6504 				 * that this is only processed once
6505 				 * in the received packet (to avoid swapping
6506 				 * back and forth).
6507 				 * NOTE:This option processing is considered
6508 				 * to be unsafe and prone to a denial of
6509 				 * service attack.
6510 				 * The current processing is not safe even with
6511 				 * IPsec secured IP packets. Since the home
6512 				 * address option processing requirement still
6513 				 * is in the IETF draft and in the process of
6514 				 * being redefined for its usage, it has been
6515 				 * decided to turn off the option by default.
6516 				 * If this section of code needs to be executed,
6517 				 * ndd variable ip6_ignore_home_address_opt
6518 				 * should be set to 0 at the user's own risk.
6519 				 */
6520 				struct ip6_opt_home_address *oh;
6521 				in6_addr_t tmp;
6522 
6523 				if (ipv6_ignore_home_address_opt)
6524 					goto opt_error;
6525 
6526 				if (hdr_type != IPPROTO_DSTOPTS)
6527 					goto opt_error;
6528 				optused = 2 + optptr[1];
6529 				if (optused > optlen)
6530 					goto bad_opt;
6531 
6532 				/*
6533 				 * We did this dest. opt the first time
6534 				 * around (i.e. before AH processing).
6535 				 * If we've done AH... stop now.
6536 				 */
6537 				if (first_mp != mp) {
6538 					ipsec_in_t *ii;
6539 
6540 					ii = (ipsec_in_t *)first_mp->b_rptr;
6541 					if (ii->ipsec_in_ah_sa != NULL)
6542 						break;
6543 				}
6544 
6545 				oh = (struct ip6_opt_home_address *)optptr;
6546 				/* Check total length and alignment */
6547 				if (optused < sizeof (*oh) ||
6548 				    ((uintptr_t)oh->ip6oh_addr & 0x7) != 0)
6549 					goto opt_error;
6550 				/* Swap ip6_src and the home address */
6551 				tmp = ip6h->ip6_src;
6552 				/* XXX Note: only 8 byte alignment option */
6553 				ip6h->ip6_src = *(in6_addr_t *)oh->ip6oh_addr;
6554 				*(in6_addr_t *)oh->ip6oh_addr = tmp;
6555 				break;
6556 			}
6557 
6558 			case IP6OPT_TUNNEL_LIMIT:
6559 				if (hdr_type != IPPROTO_DSTOPTS) {
6560 					goto opt_error;
6561 				}
6562 				optused = 2 + optptr[1];
6563 				if (optused > optlen) {
6564 					goto bad_opt;
6565 				}
6566 				if (optused != 3) {
6567 					goto opt_error;
6568 				}
6569 				break;
6570 
6571 			default:
6572 				errtype = "unknown";
6573 				/* FALLTHROUGH */
6574 			opt_error:
6575 				/* Determine which zone should send error */
6576 				zoneid = ipif_lookup_addr_zoneid_v6(
6577 				    &ip6h->ip6_dst, ill);
6578 				switch (IP6OPT_TYPE(opt_type)) {
6579 				case IP6OPT_TYPE_SKIP:
6580 					optused = 2 + optptr[1];
6581 					if (optused > optlen)
6582 						goto bad_opt;
6583 					ip1dbg(("ip_process_options_v6: %s "
6584 					    "opt 0x%x skipped\n",
6585 					    errtype, opt_type));
6586 					break;
6587 				case IP6OPT_TYPE_DISCARD:
6588 					ip1dbg(("ip_process_options_v6: %s "
6589 					    "opt 0x%x; packet dropped\n",
6590 					    errtype, opt_type));
6591 					freemsg(first_mp);
6592 					return (-1);
6593 				case IP6OPT_TYPE_ICMP:
6594 					if (zoneid == ALL_ZONES) {
6595 						freemsg(first_mp);
6596 						return (-1);
6597 					}
6598 					icmp_param_problem_v6(WR(q), first_mp,
6599 					    ICMP6_PARAMPROB_OPTION,
6600 					    (uint32_t)(optptr -
6601 					    (uint8_t *)ip6h),
6602 					    B_FALSE, B_FALSE, zoneid);
6603 					return (-1);
6604 				case IP6OPT_TYPE_FORCEICMP:
6605 					if (zoneid == ALL_ZONES) {
6606 						freemsg(first_mp);
6607 						return (-1);
6608 					}
6609 					icmp_param_problem_v6(WR(q), first_mp,
6610 					    ICMP6_PARAMPROB_OPTION,
6611 					    (uint32_t)(optptr -
6612 					    (uint8_t *)ip6h),
6613 					    B_FALSE, B_TRUE, zoneid);
6614 					return (-1);
6615 				default:
6616 					ASSERT(0);
6617 				}
6618 			}
6619 		}
6620 		optlen -= optused;
6621 		optptr += optused;
6622 	}
6623 	return (ret);
6624 
6625 bad_opt:
6626 	/* Determine which zone should send error */
6627 	zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst, ill);
6628 	if (zoneid == ALL_ZONES) {
6629 		freemsg(first_mp);
6630 	} else {
6631 		icmp_param_problem_v6(WR(q), first_mp, ICMP6_PARAMPROB_OPTION,
6632 		    (uint32_t)(optptr - (uint8_t *)ip6h),
6633 		    B_FALSE, B_FALSE, zoneid);
6634 	}
6635 	return (-1);
6636 }
6637 
6638 /*
6639  * Process a routing header that is not yet empty.
6640  * Only handles type 0 routing headers.
6641  */
6642 static void
6643 ip_process_rthdr(queue_t *q, mblk_t *mp, ip6_t *ip6h, ip6_rthdr_t *rth,
6644     ill_t *ill, uint_t flags, mblk_t *hada_mp, mblk_t *dl_mp)
6645 {
6646 	ip6_rthdr0_t *rthdr;
6647 	uint_t ehdrlen;
6648 	uint_t numaddr;
6649 	in6_addr_t *addrptr;
6650 	in6_addr_t tmp;
6651 
6652 	ASSERT(rth->ip6r_segleft != 0);
6653 
6654 	if (!ipv6_forward_src_routed) {
6655 		/* XXX Check for source routed out same interface? */
6656 		BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
6657 		BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors);
6658 		freemsg(hada_mp);
6659 		freemsg(mp);
6660 		return;
6661 	}
6662 
6663 	if (rth->ip6r_type != 0) {
6664 		if (hada_mp != NULL)
6665 			goto hada_drop;
6666 		/* Sent by forwarding path, and router is global zone */
6667 		icmp_param_problem_v6(WR(q), mp,
6668 		    ICMP6_PARAMPROB_HEADER,
6669 		    (uint32_t)((uchar_t *)&rth->ip6r_type - (uchar_t *)ip6h),
6670 		    B_FALSE, B_FALSE, GLOBAL_ZONEID);
6671 		return;
6672 	}
6673 	rthdr = (ip6_rthdr0_t *)rth;
6674 	ehdrlen = 8 * (rthdr->ip6r0_len + 1);
6675 	ASSERT(mp->b_rptr + ehdrlen <= mp->b_wptr);
6676 	addrptr = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
6677 	/* rthdr->ip6r0_len is twice the number of addresses in the header */
6678 	if (rthdr->ip6r0_len & 0x1) {
6679 		/* An odd length is impossible */
6680 		if (hada_mp != NULL)
6681 			goto hada_drop;
6682 		/* Sent by forwarding path, and router is global zone */
6683 		icmp_param_problem_v6(WR(q), mp,
6684 		    ICMP6_PARAMPROB_HEADER,
6685 		    (uint32_t)((uchar_t *)&rthdr->ip6r0_len - (uchar_t *)ip6h),
6686 		    B_FALSE, B_FALSE, GLOBAL_ZONEID);
6687 		return;
6688 	}
6689 	numaddr = rthdr->ip6r0_len / 2;
6690 	if (rthdr->ip6r0_segleft > numaddr) {
6691 		/* segleft exceeds number of addresses in routing header */
6692 		if (hada_mp != NULL)
6693 			goto hada_drop;
6694 		/* Sent by forwarding path, and router is global zone */
6695 		icmp_param_problem_v6(WR(q), mp,
6696 		    ICMP6_PARAMPROB_HEADER,
6697 		    (uint32_t)((uchar_t *)&rthdr->ip6r0_segleft -
6698 			(uchar_t *)ip6h),
6699 		    B_FALSE, B_FALSE, GLOBAL_ZONEID);
6700 		return;
6701 	}
6702 	addrptr += (numaddr - rthdr->ip6r0_segleft);
6703 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) ||
6704 	    IN6_IS_ADDR_MULTICAST(addrptr)) {
6705 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
6706 		freemsg(hada_mp);
6707 		freemsg(mp);
6708 		return;
6709 	}
6710 	/* Swap */
6711 	tmp = *addrptr;
6712 	*addrptr = ip6h->ip6_dst;
6713 	ip6h->ip6_dst = tmp;
6714 	rthdr->ip6r0_segleft--;
6715 	/* Don't allow any mapped addresses - ip_wput_v6 can't handle them */
6716 	if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
6717 		if (hada_mp != NULL)
6718 			goto hada_drop;
6719 		/* Sent by forwarding path, and router is global zone */
6720 		icmp_unreachable_v6(WR(q), mp, ICMP6_DST_UNREACH_NOROUTE,
6721 		    B_FALSE, B_FALSE, GLOBAL_ZONEID);
6722 		return;
6723 	}
6724 	if (ip_check_v6_mblk(mp, ill) == 0) {
6725 		ip6h = (ip6_t *)mp->b_rptr;
6726 		ip_rput_data_v6(q, ill, mp, ip6h, flags, hada_mp, dl_mp);
6727 	}
6728 	return;
6729 hada_drop:
6730 	/* IPsec kstats: bean counter? */
6731 	freemsg(hada_mp);
6732 	freemsg(mp);
6733 }
6734 
6735 /*
6736  * Read side put procedure for IPv6 module.
6737  */
6738 void
6739 ip_rput_v6(queue_t *q, mblk_t *mp)
6740 {
6741 	mblk_t		*first_mp;
6742 	mblk_t		*hada_mp = NULL;
6743 	ip6_t		*ip6h;
6744 	boolean_t	ll_multicast = B_FALSE;
6745 	boolean_t	mctl_present = B_FALSE;
6746 	ill_t		*ill;
6747 	struct iocblk	*iocp;
6748 	uint_t 		flags = 0;
6749 	mblk_t		*dl_mp;
6750 
6751 	ill = (ill_t *)q->q_ptr;
6752 	if (ill->ill_state_flags & ILL_CONDEMNED) {
6753 		union DL_primitives *dl;
6754 
6755 		dl = (union DL_primitives *)mp->b_rptr;
6756 		/*
6757 		 * Things are opening or closing - only accept DLPI
6758 		 * ack messages. If the stream is closing and ip_wsrv
6759 		 * has completed, ip_close is out of the qwait, but has
6760 		 * not yet completed qprocsoff. Don't proceed any further
6761 		 * because the ill has been cleaned up and things hanging
6762 		 * off the ill have been freed.
6763 		 */
6764 		if ((mp->b_datap->db_type != M_PCPROTO) ||
6765 		    (dl->dl_primitive == DL_UNITDATA_IND)) {
6766 			inet_freemsg(mp);
6767 			return;
6768 		}
6769 	}
6770 
6771 	dl_mp = NULL;
6772 	switch (mp->b_datap->db_type) {
6773 	case M_DATA: {
6774 		int hlen;
6775 		uchar_t *ucp;
6776 		struct ether_header *eh;
6777 		dl_unitdata_ind_t *dui;
6778 
6779 		/*
6780 		 * This is a work-around for CR 6451644, a bug in Nemo.  It
6781 		 * should be removed when that problem is fixed.
6782 		 */
6783 		if (ill->ill_mactype == DL_ETHER &&
6784 		    (hlen = MBLKHEAD(mp)) >= sizeof (struct ether_header) &&
6785 		    (ucp = mp->b_rptr)[-1] == (IP6_DL_SAP & 0xFF) &&
6786 		    ucp[-2] == (IP6_DL_SAP >> 8)) {
6787 			if (hlen >= sizeof (struct ether_vlan_header) &&
6788 			    ucp[-5] == 0 && ucp[-6] == 0x81)
6789 				ucp -= sizeof (struct ether_vlan_header);
6790 			else
6791 				ucp -= sizeof (struct ether_header);
6792 			/*
6793 			 * If it's a group address, then fabricate a
6794 			 * DL_UNITDATA_IND message.
6795 			 */
6796 			if ((ll_multicast = (ucp[0] & 1)) != 0 &&
6797 			    (dl_mp = allocb(DL_UNITDATA_IND_SIZE + 16,
6798 			    BPRI_HI)) != NULL) {
6799 				eh = (struct ether_header *)ucp;
6800 				dui = (dl_unitdata_ind_t *)dl_mp->b_rptr;
6801 				DB_TYPE(dl_mp) = M_PROTO;
6802 				dl_mp->b_wptr = (uchar_t *)(dui + 1) + 16;
6803 				dui->dl_primitive = DL_UNITDATA_IND;
6804 				dui->dl_dest_addr_length = 8;
6805 				dui->dl_dest_addr_offset = DL_UNITDATA_IND_SIZE;
6806 				dui->dl_src_addr_length = 8;
6807 				dui->dl_src_addr_offset = DL_UNITDATA_IND_SIZE +
6808 				    8;
6809 				dui->dl_group_address = 1;
6810 				ucp = (uchar_t *)(dui + 1);
6811 				if (ill->ill_sap_length > 0)
6812 					ucp += ill->ill_sap_length;
6813 				bcopy(&eh->ether_dhost, ucp, 6);
6814 				bcopy(&eh->ether_shost, ucp + 8, 6);
6815 				ucp = (uchar_t *)(dui + 1);
6816 				if (ill->ill_sap_length < 0)
6817 					ucp += 8 + ill->ill_sap_length;
6818 				bcopy(&eh->ether_type, ucp, 2);
6819 				bcopy(&eh->ether_type, ucp + 8, 2);
6820 			}
6821 		}
6822 		break;
6823 	}
6824 
6825 	case M_PROTO:
6826 	case M_PCPROTO:
6827 		if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
6828 		    DL_UNITDATA_IND) {
6829 			/* Go handle anything other than data elsewhere. */
6830 			ip_rput_dlpi(q, mp);
6831 			return;
6832 		}
6833 #define	dlur	((dl_unitdata_ind_t *)mp->b_rptr)
6834 		ll_multicast = dlur->dl_group_address;
6835 #undef	dlur
6836 		/* Save the DLPI header. */
6837 		dl_mp = mp;
6838 		mp = mp->b_cont;
6839 		dl_mp->b_cont = NULL;
6840 		break;
6841 	case M_BREAK:
6842 		panic("ip_rput_v6: got an M_BREAK");
6843 		/*NOTREACHED*/
6844 	case M_IOCACK:
6845 		iocp = (struct iocblk *)mp->b_rptr;
6846 		switch (iocp->ioc_cmd) {
6847 		case DL_IOC_HDR_INFO:
6848 			ill = (ill_t *)q->q_ptr;
6849 			ill_fastpath_ack(ill, mp);
6850 			return;
6851 		case SIOCSTUNPARAM:
6852 		case SIOCGTUNPARAM:
6853 		case OSIOCSTUNPARAM:
6854 		case OSIOCGTUNPARAM:
6855 			/* Go through qwriter */
6856 			break;
6857 		default:
6858 			putnext(q, mp);
6859 			return;
6860 		}
6861 		/* FALLTHRU */
6862 	case M_ERROR:
6863 	case M_HANGUP:
6864 		mutex_enter(&ill->ill_lock);
6865 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6866 			mutex_exit(&ill->ill_lock);
6867 			freemsg(mp);
6868 			return;
6869 		}
6870 		ill_refhold_locked(ill);
6871 		mutex_exit(&ill->ill_lock);
6872 		qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, B_FALSE);
6873 		return;
6874 	case M_CTL:
6875 		if ((MBLKL(mp) > sizeof (int)) &&
6876 		    ((da_ipsec_t *)mp->b_rptr)->da_type == IPHADA_M_CTL) {
6877 			ASSERT(MBLKL(mp) >= sizeof (da_ipsec_t));
6878 			mctl_present = B_TRUE;
6879 			break;
6880 		}
6881 		putnext(q, mp);
6882 		return;
6883 	case M_IOCNAK:
6884 		iocp = (struct iocblk *)mp->b_rptr;
6885 		switch (iocp->ioc_cmd) {
6886 		case DL_IOC_HDR_INFO:
6887 		case SIOCSTUNPARAM:
6888 		case SIOCGTUNPARAM:
6889 		case OSIOCSTUNPARAM:
6890 		case OSIOCGTUNPARAM:
6891 			mutex_enter(&ill->ill_lock);
6892 			if (ill->ill_state_flags & ILL_CONDEMNED) {
6893 				mutex_exit(&ill->ill_lock);
6894 				freemsg(mp);
6895 				return;
6896 			}
6897 			ill_refhold_locked(ill);
6898 			mutex_exit(&ill->ill_lock);
6899 			qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP,
6900 			    B_FALSE);
6901 			return;
6902 		default:
6903 			break;
6904 		}
6905 		/* FALLTHRU */
6906 	default:
6907 		putnext(q, mp);
6908 		return;
6909 	}
6910 
6911 	BUMP_MIB(ill->ill_ip6_mib, ipv6InReceives);
6912 	/*
6913 	 * if db_ref > 1 then copymsg and free original. Packet may be
6914 	 * changed and do not want other entity who has a reference to this
6915 	 * message to trip over the changes. This is a blind change because
6916 	 * trying to catch all places that might change packet is too
6917 	 * difficult (since it may be a module above this one).
6918 	 */
6919 	if (mp->b_datap->db_ref > 1) {
6920 		mblk_t  *mp1;
6921 
6922 		mp1 = copymsg(mp);
6923 		freemsg(mp);
6924 		if (mp1 == NULL) {
6925 			first_mp = NULL;
6926 			goto discard;
6927 		}
6928 		mp = mp1;
6929 	}
6930 	first_mp = mp;
6931 	if (mctl_present) {
6932 		hada_mp = first_mp;
6933 		mp = first_mp->b_cont;
6934 	}
6935 
6936 	if (ip_check_v6_mblk(mp, ill) == -1)
6937 		return;
6938 
6939 	ip6h = (ip6_t *)mp->b_rptr;
6940 
6941 	DTRACE_PROBE4(ip6__physical__in__start,
6942 	    ill_t *, ill, ill_t *, NULL,
6943 	    ip6_t *, ip6h, mblk_t *, first_mp);
6944 
6945 	FW_HOOKS6(ip6_physical_in_event, ipv6firewall_physical_in,
6946 	    ill, NULL, ip6h, first_mp, mp);
6947 
6948 	DTRACE_PROBE1(ip6__physical__in__end, mblk_t *, first_mp);
6949 
6950 	if (first_mp == NULL)
6951 		return;
6952 
6953 	if ((ip6h->ip6_vcf & IPV6_VERS_AND_FLOW_MASK) ==
6954 	    IPV6_DEFAULT_VERS_AND_FLOW) {
6955 		/*
6956 		 * It may be a bit too expensive to do this mapped address
6957 		 * check here, but in the interest of robustness, it seems
6958 		 * like the correct place.
6959 		 * TODO: Avoid this check for e.g. connected TCP sockets
6960 		 */
6961 		if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_src)) {
6962 			ip1dbg(("ip_rput_v6: pkt with mapped src addr\n"));
6963 			goto discard;
6964 		}
6965 
6966 		if (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src)) {
6967 			ip1dbg(("ip_rput_v6: pkt with loopback src"));
6968 			goto discard;
6969 		} else if (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst)) {
6970 			ip1dbg(("ip_rput_v6: pkt with loopback dst"));
6971 			goto discard;
6972 		}
6973 
6974 		flags |= (ll_multicast ? IP6_IN_LLMCAST : 0);
6975 		ip_rput_data_v6(q, ill, mp, ip6h, flags, hada_mp, dl_mp);
6976 	} else {
6977 		BUMP_MIB(ill->ill_ip6_mib, ipv6InIPv4);
6978 		goto discard;
6979 	}
6980 	freemsg(dl_mp);
6981 	return;
6982 
6983 discard:
6984 	if (dl_mp != NULL)
6985 		freeb(dl_mp);
6986 	freemsg(first_mp);
6987 	BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
6988 }
6989 
6990 /*
6991  * Walk through the IPv6 packet in mp and see if there's an AH header
6992  * in it.  See if the AH header needs to get done before other headers in
6993  * the packet.  (Worker function for ipsec_early_ah_v6().)
6994  */
6995 #define	IPSEC_HDR_DONT_PROCESS	0
6996 #define	IPSEC_HDR_PROCESS	1
6997 #define	IPSEC_MEMORY_ERROR	2
6998 static int
6999 ipsec_needs_processing_v6(mblk_t *mp, uint8_t *nexthdr)
7000 {
7001 	uint_t	length;
7002 	uint_t	ehdrlen;
7003 	uint8_t *whereptr;
7004 	uint8_t *endptr;
7005 	uint8_t *nexthdrp;
7006 	ip6_dest_t *desthdr;
7007 	ip6_rthdr_t *rthdr;
7008 	ip6_t	*ip6h;
7009 
7010 	/*
7011 	 * For now just pullup everything.  In general, the less pullups,
7012 	 * the better, but there's so much squirrelling through anyway,
7013 	 * it's just easier this way.
7014 	 */
7015 	if (!pullupmsg(mp, -1)) {
7016 		return (IPSEC_MEMORY_ERROR);
7017 	}
7018 
7019 	ip6h = (ip6_t *)mp->b_rptr;
7020 	length = IPV6_HDR_LEN;
7021 	whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */
7022 	endptr = mp->b_wptr;
7023 
7024 	/*
7025 	 * We can't just use the argument nexthdr in the place
7026 	 * of nexthdrp becaue we don't dereference nexthdrp
7027 	 * till we confirm whether it is a valid address.
7028 	 */
7029 	nexthdrp = &ip6h->ip6_nxt;
7030 	while (whereptr < endptr) {
7031 		/* Is there enough left for len + nexthdr? */
7032 		if (whereptr + MIN_EHDR_LEN > endptr)
7033 			return (IPSEC_MEMORY_ERROR);
7034 
7035 		switch (*nexthdrp) {
7036 		case IPPROTO_HOPOPTS:
7037 		case IPPROTO_DSTOPTS:
7038 			/* Assumes the headers are identical for hbh and dst */
7039 			desthdr = (ip6_dest_t *)whereptr;
7040 			ehdrlen = 8 * (desthdr->ip6d_len + 1);
7041 			if ((uchar_t *)desthdr +  ehdrlen > endptr)
7042 				return (IPSEC_MEMORY_ERROR);
7043 			/*
7044 			 * Return DONT_PROCESS because of potential Mobile IPv6
7045 			 * cruft for destination options.
7046 			 */
7047 			if (*nexthdrp == IPPROTO_DSTOPTS)
7048 				return (IPSEC_HDR_DONT_PROCESS);
7049 			nexthdrp = &desthdr->ip6d_nxt;
7050 			break;
7051 		case IPPROTO_ROUTING:
7052 			rthdr = (ip6_rthdr_t *)whereptr;
7053 
7054 			/*
7055 			 * If there's more hops left on the routing header,
7056 			 * return now with DON'T PROCESS.
7057 			 */
7058 			if (rthdr->ip6r_segleft > 0)
7059 				return (IPSEC_HDR_DONT_PROCESS);
7060 
7061 			ehdrlen =  8 * (rthdr->ip6r_len + 1);
7062 			if ((uchar_t *)rthdr +  ehdrlen > endptr)
7063 				return (IPSEC_MEMORY_ERROR);
7064 			nexthdrp = &rthdr->ip6r_nxt;
7065 			break;
7066 		case IPPROTO_FRAGMENT:
7067 			/* Wait for reassembly */
7068 			return (IPSEC_HDR_DONT_PROCESS);
7069 		case IPPROTO_AH:
7070 			*nexthdr = IPPROTO_AH;
7071 			return (IPSEC_HDR_PROCESS);
7072 		case IPPROTO_NONE:
7073 			/* No next header means we're finished */
7074 		default:
7075 			return (IPSEC_HDR_DONT_PROCESS);
7076 		}
7077 		length += ehdrlen;
7078 		whereptr += ehdrlen;
7079 	}
7080 	panic("ipsec_needs_processing_v6");
7081 	/*NOTREACHED*/
7082 }
7083 
7084 /*
7085  * Path for AH if options are present. If this is the first time we are
7086  * sending a datagram to AH, allocate a IPSEC_IN message and prepend it.
7087  * Otherwise, just fanout.  Return value answers the boolean question:
7088  * "Did I consume the mblk you sent me?"
7089  *
7090  * Sometimes AH needs to be done before other IPv6 headers for security
7091  * reasons.  This function (and its ipsec_needs_processing_v6() above)
7092  * indicates if that is so, and fans out to the appropriate IPsec protocol
7093  * for the datagram passed in.
7094  */
7095 static boolean_t
7096 ipsec_early_ah_v6(queue_t *q, mblk_t *first_mp, boolean_t mctl_present,
7097     ill_t *ill, ire_t *ire, mblk_t *hada_mp, zoneid_t zoneid)
7098 {
7099 	mblk_t *mp;
7100 	uint8_t nexthdr;
7101 	ipsec_in_t *ii = NULL;
7102 	ah_t *ah;
7103 	ipsec_status_t ipsec_rc;
7104 
7105 	ASSERT((hada_mp == NULL) || (!mctl_present));
7106 
7107 	switch (ipsec_needs_processing_v6(
7108 	    (mctl_present ? first_mp->b_cont : first_mp), &nexthdr)) {
7109 	case IPSEC_MEMORY_ERROR:
7110 		BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7111 		freemsg(hada_mp);
7112 		freemsg(first_mp);
7113 		return (B_TRUE);
7114 	case IPSEC_HDR_DONT_PROCESS:
7115 		return (B_FALSE);
7116 	}
7117 
7118 	/* Default means send it to AH! */
7119 	ASSERT(nexthdr == IPPROTO_AH);
7120 	if (!mctl_present) {
7121 		mp = first_mp;
7122 		if ((first_mp = ipsec_in_alloc(B_FALSE)) == NULL) {
7123 			ip1dbg(("ipsec_early_ah_v6: IPSEC_IN "
7124 			    "allocation failure.\n"));
7125 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7126 			freemsg(hada_mp);
7127 			freemsg(mp);
7128 			return (B_TRUE);
7129 		}
7130 		/*
7131 		 * Store the ill_index so that when we come back
7132 		 * from IPSEC we ride on the same queue.
7133 		 */
7134 		ii = (ipsec_in_t *)first_mp->b_rptr;
7135 		ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex;
7136 		ii->ipsec_in_rill_index = ii->ipsec_in_ill_index;
7137 		first_mp->b_cont = mp;
7138 	}
7139 	/*
7140 	 * Cache hardware acceleration info.
7141 	 */
7142 	if (hada_mp != NULL) {
7143 		ASSERT(ii != NULL);
7144 		IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_early_ah_v6: "
7145 		    "caching data attr.\n"));
7146 		ii->ipsec_in_accelerated = B_TRUE;
7147 		ii->ipsec_in_da = hada_mp;
7148 	}
7149 
7150 	if (!ipsec_loaded()) {
7151 		ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, zoneid);
7152 		return (B_TRUE);
7153 	}
7154 
7155 	ah = ipsec_inbound_ah_sa(first_mp);
7156 	if (ah == NULL)
7157 		return (B_TRUE);
7158 	ASSERT(ii->ipsec_in_ah_sa != NULL);
7159 	ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL);
7160 	ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(first_mp, ah);
7161 
7162 	switch (ipsec_rc) {
7163 	case IPSEC_STATUS_SUCCESS:
7164 		/* we're done with IPsec processing, send it up */
7165 		ip_fanout_proto_again(first_mp, ill, ill, ire);
7166 		break;
7167 	case IPSEC_STATUS_FAILED:
7168 		BUMP_MIB(&ip6_mib, ipv6InDiscards);
7169 		break;
7170 	case IPSEC_STATUS_PENDING:
7171 		/* no action needed */
7172 		break;
7173 	}
7174 	return (B_TRUE);
7175 }
7176 
7177 /*
7178  * Validate the IPv6 mblk for alignment.
7179  */
7180 int
7181 ip_check_v6_mblk(mblk_t *mp, ill_t *ill)
7182 {
7183 	int pkt_len, ip6_len;
7184 	ip6_t *ip6h = (ip6_t *)mp->b_rptr;
7185 
7186 	/* check for alignment and full IPv6 header */
7187 	if (!OK_32PTR((uchar_t *)ip6h) ||
7188 	    (mp->b_wptr - (uchar_t *)ip6h) < IPV6_HDR_LEN) {
7189 		if (!pullupmsg(mp, IPV6_HDR_LEN)) {
7190 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7191 			ip1dbg(("ip_rput_v6: pullupmsg failed\n"));
7192 			freemsg(mp);
7193 			return (-1);
7194 		}
7195 		ip6h = (ip6_t *)mp->b_rptr;
7196 	}
7197 
7198 	ASSERT(OK_32PTR((uchar_t *)ip6h) &&
7199 	    (mp->b_wptr - (uchar_t *)ip6h) >= IPV6_HDR_LEN);
7200 
7201 	if (mp->b_cont == NULL)
7202 		pkt_len = mp->b_wptr - mp->b_rptr;
7203 	else
7204 		pkt_len = msgdsize(mp);
7205 	ip6_len = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
7206 
7207 	/*
7208 	 * Check for bogus (too short packet) and packet which
7209 	 * was padded by the link layer.
7210 	 */
7211 	if (ip6_len != pkt_len) {
7212 		ssize_t diff;
7213 
7214 		if (ip6_len > pkt_len) {
7215 			ip1dbg(("ip_rput_data_v6: packet too short %d %d\n",
7216 			    ip6_len, pkt_len));
7217 			BUMP_MIB(ill->ill_ip6_mib, ipv6InTruncatedPkts);
7218 			freemsg(mp);
7219 			return (-1);
7220 		}
7221 		diff = (ssize_t)(pkt_len - ip6_len);
7222 
7223 		if (!adjmsg(mp, -diff)) {
7224 			ip1dbg(("ip_rput_data_v6: adjmsg failed\n"));
7225 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7226 			freemsg(mp);
7227 			return (-1);
7228 		}
7229 	}
7230 	return (0);
7231 }
7232 
7233 /*
7234  * ip_rput_data_v6 -- received IPv6 packets in M_DATA messages show up here.
7235  * ip_rput_v6 has already verified alignment, the min length, the version,
7236  * and db_ref = 1.
7237  *
7238  * The ill passed in (the arg named inill) is the ill that the packet
7239  * actually arrived on.  We need to remember this when saving the
7240  * input interface index into potential IPV6_PKTINFO data in
7241  * ip_add_info_v6().
7242  *
7243  * This routine doesn't free dl_mp; that's the caller's responsibility on
7244  * return.  (Note that the callers are complex enough that there's no tail
7245  * recursion here anyway.)
7246  */
7247 void
7248 ip_rput_data_v6(queue_t *q, ill_t *inill, mblk_t *mp, ip6_t *ip6h,
7249     uint_t flags, mblk_t *hada_mp, mblk_t *dl_mp)
7250 {
7251 	ire_t		*ire = NULL;
7252 	queue_t		*rq;
7253 	ill_t		*ill = inill;
7254 	ill_t		*outill;
7255 	ipif_t		*ipif;
7256 	uint8_t		*whereptr;
7257 	uint8_t		nexthdr;
7258 	uint16_t	remlen;
7259 	uint_t		prev_nexthdr_offset;
7260 	uint_t		used;
7261 	size_t		pkt_len;
7262 	uint16_t	ip6_len;
7263 	uint_t		hdr_len;
7264 	boolean_t	mctl_present;
7265 	mblk_t		*first_mp;
7266 	mblk_t		*first_mp1;
7267 	boolean_t	no_forward;
7268 	ip6_hbh_t	*hbhhdr;
7269 	boolean_t	ll_multicast = (flags & IP6_IN_LLMCAST);
7270 	conn_t		*connp;
7271 	ilm_t		*ilm;
7272 	uint32_t	ports;
7273 	uint_t		ipif_id = 0;
7274 	zoneid_t	zoneid = GLOBAL_ZONEID;
7275 	uint16_t	hck_flags, reass_hck_flags;
7276 	uint32_t	reass_sum;
7277 	boolean_t	cksum_err;
7278 	mblk_t		*mp1;
7279 
7280 	EXTRACT_PKT_MP(mp, first_mp, mctl_present);
7281 
7282 	if (hada_mp != NULL) {
7283 		/*
7284 		 * It's an IPsec accelerated packet.
7285 		 * Keep a pointer to the data attributes around until
7286 		 * we allocate the ipsecinfo structure.
7287 		 */
7288 		IPSECHW_DEBUG(IPSECHW_PKT,
7289 		    ("ip_rput_data_v6: inbound HW accelerated IPsec pkt\n"));
7290 		hada_mp->b_cont = NULL;
7291 		/*
7292 		 * Since it is accelerated, it came directly from
7293 		 * the ill.
7294 		 */
7295 		ASSERT(mctl_present == B_FALSE);
7296 		ASSERT(mp->b_datap->db_type != M_CTL);
7297 	}
7298 
7299 	ip6h = (ip6_t *)mp->b_rptr;
7300 	ip6_len = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
7301 	pkt_len = ip6_len;
7302 
7303 	if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum)
7304 		hck_flags = DB_CKSUMFLAGS(mp);
7305 	else
7306 		hck_flags = 0;
7307 
7308 	/* Clear checksum flags in case we need to forward */
7309 	DB_CKSUMFLAGS(mp) = 0;
7310 	reass_sum = reass_hck_flags = 0;
7311 
7312 	nexthdr = ip6h->ip6_nxt;
7313 
7314 	prev_nexthdr_offset = (uint_t)((uchar_t *)&ip6h->ip6_nxt -
7315 	    (uchar_t *)ip6h);
7316 	whereptr = (uint8_t *)&ip6h[1];
7317 	remlen = pkt_len - IPV6_HDR_LEN;	/* Track how much is left */
7318 
7319 	/* Process hop by hop header options */
7320 	if (nexthdr == IPPROTO_HOPOPTS) {
7321 		uint_t ehdrlen;
7322 		uint8_t *optptr;
7323 
7324 		if (remlen < MIN_EHDR_LEN)
7325 			goto pkt_too_short;
7326 		if (mp->b_cont != NULL &&
7327 		    whereptr + MIN_EHDR_LEN > mp->b_wptr) {
7328 			if (!pullupmsg(mp, IPV6_HDR_LEN + MIN_EHDR_LEN)) {
7329 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7330 				freemsg(hada_mp);
7331 				freemsg(first_mp);
7332 				return;
7333 			}
7334 			ip6h = (ip6_t *)mp->b_rptr;
7335 			whereptr = (uint8_t *)ip6h + pkt_len - remlen;
7336 		}
7337 		hbhhdr = (ip6_hbh_t *)whereptr;
7338 		nexthdr = hbhhdr->ip6h_nxt;
7339 		prev_nexthdr_offset = (uint_t)(whereptr - (uint8_t *)ip6h);
7340 		ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
7341 
7342 		if (remlen < ehdrlen)
7343 			goto pkt_too_short;
7344 		if (mp->b_cont != NULL &&
7345 		    whereptr + ehdrlen > mp->b_wptr) {
7346 			if (!pullupmsg(mp, IPV6_HDR_LEN + ehdrlen)) {
7347 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7348 				freemsg(hada_mp);
7349 				freemsg(first_mp);
7350 				return;
7351 			}
7352 			ip6h = (ip6_t *)mp->b_rptr;
7353 			whereptr = (uint8_t *)ip6h + pkt_len - remlen;
7354 			hbhhdr = (ip6_hbh_t *)whereptr;
7355 		}
7356 
7357 		optptr = whereptr + 2;
7358 		whereptr += ehdrlen;
7359 		remlen -= ehdrlen;
7360 		switch (ip_process_options_v6(q, first_mp, ip6h, optptr,
7361 		    ehdrlen - 2, IPPROTO_HOPOPTS)) {
7362 		case -1:
7363 			/*
7364 			 * Packet has been consumed and any
7365 			 * needed ICMP messages sent.
7366 			 */
7367 			BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors);
7368 			freemsg(hada_mp);
7369 			return;
7370 		case 0:
7371 			/* no action needed */
7372 			break;
7373 		case 1:
7374 			/* Known router alert */
7375 			goto ipv6forus;
7376 		}
7377 	}
7378 
7379 	/*
7380 	 * Attach any necessary label information to this packet.
7381 	 */
7382 	if (is_system_labeled() && !tsol_get_pkt_label(mp, IPV6_VERSION)) {
7383 		if (ip6opt_ls != 0)
7384 			ip0dbg(("tsol_get_pkt_label v6 failed\n"));
7385 		BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors);
7386 		freemsg(hada_mp);
7387 		freemsg(first_mp);
7388 		return;
7389 	}
7390 
7391 	/*
7392 	 * On incoming v6 multicast packets we will bypass the ire table,
7393 	 * and assume that the read queue corresponds to the targetted
7394 	 * interface.
7395 	 *
7396 	 * The effect of this is the same as the IPv4 original code, but is
7397 	 * much cleaner I think.  See ip_rput for how that was done.
7398 	 */
7399 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
7400 		BUMP_MIB(ill->ill_ip6_mib, ipv6InMcastPkts);
7401 		/*
7402 		 * XXX TODO Give to mrouted to for multicast forwarding.
7403 		 */
7404 		ILM_WALKER_HOLD(ill);
7405 		ilm = ilm_lookup_ill_v6(ill, &ip6h->ip6_dst, ALL_ZONES);
7406 		ILM_WALKER_RELE(ill);
7407 		if (ilm == NULL) {
7408 			if (ip_debug > 3) {
7409 				/* ip2dbg */
7410 				pr_addr_dbg("ip_rput_data_v6: got mcast packet"
7411 				    "  which is not for us: %s\n", AF_INET6,
7412 				    &ip6h->ip6_dst);
7413 			}
7414 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7415 			freemsg(hada_mp);
7416 			freemsg(first_mp);
7417 			return;
7418 		}
7419 		if (ip_debug > 3) {
7420 			/* ip2dbg */
7421 			pr_addr_dbg("ip_rput_data_v6: multicast for us: %s\n",
7422 			    AF_INET6, &ip6h->ip6_dst);
7423 		}
7424 		rq = ill->ill_rq;
7425 		zoneid = GLOBAL_ZONEID;
7426 		goto ipv6forus;
7427 	}
7428 
7429 	ipif = ill->ill_ipif;
7430 
7431 	/*
7432 	 * If a packet was received on an interface that is a 6to4 tunnel,
7433 	 * incoming IPv6 packets, with a 6to4 addressed IPv6 destination, must
7434 	 * be checked to have a 6to4 prefix (2002:V4ADDR::/48) that is equal to
7435 	 * the 6to4 prefix of the address configured on the receiving interface.
7436 	 * Otherwise, the packet was delivered to this interface in error and
7437 	 * the packet must be dropped.
7438 	 */
7439 	if ((ill->ill_is_6to4tun) && IN6_IS_ADDR_6TO4(&ip6h->ip6_dst)) {
7440 
7441 		if (!IN6_ARE_6TO4_PREFIX_EQUAL(&ipif->ipif_v6lcl_addr,
7442 		    &ip6h->ip6_dst)) {
7443 			if (ip_debug > 2) {
7444 				/* ip1dbg */
7445 				pr_addr_dbg("ip_rput_data_v6: received 6to4 "
7446 				    "addressed packet which is not for us: "
7447 				    "%s\n", AF_INET6, &ip6h->ip6_dst);
7448 			}
7449 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
7450 			freemsg(first_mp);
7451 			return;
7452 		}
7453 	}
7454 
7455 	/*
7456 	 * Find an ire that matches destination. For link-local addresses
7457 	 * we have to match the ill.
7458 	 * TBD for site local addresses.
7459 	 */
7460 	if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst)) {
7461 		ire = ire_ctable_lookup_v6(&ip6h->ip6_dst, NULL,
7462 		    IRE_CACHE|IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL,
7463 		    MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP);
7464 	} else {
7465 		ire = ire_cache_lookup_v6(&ip6h->ip6_dst, ALL_ZONES,
7466 		    MBLK_GETLABEL(mp));
7467 	}
7468 	if (ire == NULL) {
7469 		/*
7470 		 * No matching IRE found.  Mark this packet as having
7471 		 * originated externally.
7472 		 */
7473 		if (!(ill->ill_flags & ILLF_ROUTER) || ll_multicast) {
7474 			BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
7475 			if (!(ill->ill_flags & ILLF_ROUTER))
7476 				BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors);
7477 			freemsg(hada_mp);
7478 			freemsg(first_mp);
7479 			return;
7480 		}
7481 		if (ip6h->ip6_hops <= 1) {
7482 			if (hada_mp != NULL)
7483 				goto hada_drop;
7484 			/* Sent by forwarding path, and router is global zone */
7485 			icmp_time_exceeded_v6(WR(q), first_mp,
7486 			    ICMP6_TIME_EXCEED_TRANSIT, ll_multicast, B_FALSE,
7487 			    GLOBAL_ZONEID);
7488 			return;
7489 		}
7490 		/*
7491 		 * Per RFC 3513 section 2.5.2, we must not forward packets with
7492 		 * an unspecified source address.
7493 		 */
7494 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
7495 			BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
7496 			freemsg(hada_mp);
7497 			freemsg(first_mp);
7498 			return;
7499 		}
7500 		mp->b_prev = (mblk_t *)(uintptr_t)
7501 		    ill->ill_phyint->phyint_ifindex;
7502 		ip_newroute_v6(q, mp, &ip6h->ip6_dst, &ip6h->ip6_src,
7503 		    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ? ill : NULL,
7504 		    ALL_ZONES);
7505 		return;
7506 	}
7507 	ipif_id = ire->ire_ipif->ipif_seqid;
7508 	/* we have a matching IRE */
7509 	if (ire->ire_stq != NULL) {
7510 		ill_group_t *ill_group;
7511 		ill_group_t *ire_group;
7512 
7513 		/*
7514 		 * To be quicker, we may wish not to chase pointers
7515 		 * (ire->ire_ipif->ipif_ill...) and instead store the
7516 		 * forwarding policy in the ire.  An unfortunate side-
7517 		 * effect of this would be requiring an ire flush whenever
7518 		 * the ILLF_ROUTER flag changes.  For now, chase pointers
7519 		 * once and store in the boolean no_forward.
7520 		 *
7521 		 * This appears twice to keep it out of the non-forwarding,
7522 		 * yes-it's-for-us-on-the-right-interface case.
7523 		 */
7524 		no_forward = ((ill->ill_flags &
7525 		    ire->ire_ipif->ipif_ill->ill_flags & ILLF_ROUTER) == 0);
7526 
7527 
7528 		ASSERT(first_mp == mp);
7529 		/*
7530 		 * This ire has a send-to queue - forward the packet.
7531 		 */
7532 		if (no_forward || ll_multicast || (hada_mp != NULL)) {
7533 			freemsg(hada_mp);
7534 			BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
7535 			if (no_forward)
7536 				BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors);
7537 			freemsg(mp);
7538 			ire_refrele(ire);
7539 			return;
7540 		}
7541 		if (ip6h->ip6_hops <= 1) {
7542 			ip1dbg(("ip_rput_data_v6: hop limit expired.\n"));
7543 			/* Sent by forwarding path, and router is global zone */
7544 			icmp_time_exceeded_v6(WR(q), mp,
7545 			    ICMP6_TIME_EXCEED_TRANSIT, ll_multicast, B_FALSE,
7546 			    GLOBAL_ZONEID);
7547 			ire_refrele(ire);
7548 			return;
7549 		}
7550 		/*
7551 		 * Per RFC 3513 section 2.5.2, we must not forward packets with
7552 		 * an unspecified source address.
7553 		 */
7554 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
7555 			BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
7556 			freemsg(mp);
7557 			ire_refrele(ire);
7558 			return;
7559 		}
7560 
7561 		if (is_system_labeled()) {
7562 			mblk_t *mp1;
7563 
7564 			if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) {
7565 				BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
7566 				freemsg(mp);
7567 				ire_refrele(ire);
7568 				return;
7569 			}
7570 			/* Size may have changed */
7571 			mp = mp1;
7572 			ip6h = (ip6_t *)mp->b_rptr;
7573 			pkt_len = msgdsize(mp);
7574 		}
7575 
7576 		if (pkt_len > ire->ire_max_frag) {
7577 			BUMP_MIB(ill->ill_ip6_mib, ipv6InTooBigErrors);
7578 			/* Sent by forwarding path, and router is global zone */
7579 			icmp_pkt2big_v6(WR(q), mp, ire->ire_max_frag,
7580 			    ll_multicast, B_TRUE, GLOBAL_ZONEID);
7581 			ire_refrele(ire);
7582 			return;
7583 		}
7584 
7585 		/*
7586 		 * Check to see if we're forwarding the packet to a
7587 		 * different link from which it came.  If so, check the
7588 		 * source and destination addresses since routers must not
7589 		 * forward any packets with link-local source or
7590 		 * destination addresses to other links.  Otherwise (if
7591 		 * we're forwarding onto the same link), conditionally send
7592 		 * a redirect message.
7593 		 */
7594 		ill_group = ill->ill_group;
7595 		ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group;
7596 		if (ire->ire_rfq != q && (ill_group == NULL ||
7597 		    ill_group != ire_group)) {
7598 			if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ||
7599 			    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) {
7600 				BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors);
7601 				freemsg(mp);
7602 				ire_refrele(ire);
7603 				return;
7604 			}
7605 			/* TBD add site-local check at site boundary? */
7606 		} else if (ipv6_send_redirects) {
7607 			in6_addr_t	*v6targ;
7608 			in6_addr_t	gw_addr_v6;
7609 			ire_t		*src_ire_v6 = NULL;
7610 
7611 			/*
7612 			 * Don't send a redirect when forwarding a source
7613 			 * routed packet.
7614 			 */
7615 			if (ip_source_routed_v6(ip6h, mp))
7616 				goto forward;
7617 
7618 			mutex_enter(&ire->ire_lock);
7619 			gw_addr_v6 = ire->ire_gateway_addr_v6;
7620 			mutex_exit(&ire->ire_lock);
7621 			if (!IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) {
7622 				v6targ = &gw_addr_v6;
7623 				/*
7624 				 * We won't send redirects to a router
7625 				 * that doesn't have a link local
7626 				 * address, but will forward.
7627 				 */
7628 				if (!IN6_IS_ADDR_LINKLOCAL(v6targ)) {
7629 					BUMP_MIB(ill->ill_ip6_mib,
7630 					    ipv6InAddrErrors);
7631 					goto forward;
7632 				}
7633 			} else {
7634 				v6targ = &ip6h->ip6_dst;
7635 			}
7636 
7637 			src_ire_v6 = ire_ftable_lookup_v6(&ip6h->ip6_src,
7638 			    NULL, NULL, IRE_INTERFACE, ire->ire_ipif, NULL,
7639 			    ALL_ZONES, 0, NULL,
7640 			    MATCH_IRE_IPIF | MATCH_IRE_TYPE);
7641 
7642 			if (src_ire_v6 != NULL) {
7643 				/*
7644 				 * The source is directly connected.
7645 				 */
7646 				mp1 = copymsg(mp);
7647 				if (mp1 != NULL) {
7648 					icmp_send_redirect_v6(WR(q),
7649 					    mp1, v6targ, &ip6h->ip6_dst,
7650 					    ill, B_FALSE);
7651 				}
7652 				ire_refrele(src_ire_v6);
7653 			}
7654 		}
7655 
7656 forward:
7657 		/* Hoplimit verified above */
7658 		ip6h->ip6_hops--;
7659 
7660 		outill = ire->ire_ipif->ipif_ill;
7661 
7662 		DTRACE_PROBE4(ip6__forwarding__start,
7663 		    ill_t *, inill, ill_t *, outill,
7664 		    ip6_t *, ip6h, mblk_t *, mp);
7665 
7666 		FW_HOOKS6(ip6_forwarding_event, ipv6firewall_forwarding,
7667 		    inill, outill, ip6h, mp, mp);
7668 
7669 		DTRACE_PROBE1(ip6__forwarding__end, mblk_t *, mp);
7670 
7671 		if (mp != NULL) {
7672 			UPDATE_IB_PKT_COUNT(ire);
7673 			ire->ire_last_used_time = lbolt;
7674 			BUMP_MIB(ill->ill_ip6_mib, ipv6OutForwDatagrams);
7675 			ip_xmit_v6(mp, ire, 0, NULL, B_FALSE, NULL);
7676 		}
7677 		IRE_REFRELE(ire);
7678 		return;
7679 	}
7680 	rq = ire->ire_rfq;
7681 
7682 	/*
7683 	 * Need to put on correct queue for reassembly to find it.
7684 	 * No need to use put() since reassembly has its own locks.
7685 	 * Note: multicast packets and packets destined to addresses
7686 	 * assigned to loopback (ire_rfq is NULL) will be reassembled on
7687 	 * the arriving ill.
7688 	 */
7689 	if (rq != q) {
7690 		boolean_t check_multi = B_TRUE;
7691 		ill_group_t *ill_group = NULL;
7692 		ill_group_t *ire_group = NULL;
7693 		ill_t	*ire_ill = NULL;
7694 		uint_t	ill_ifindex = ill->ill_usesrc_ifindex;
7695 
7696 		/*
7697 		 * To be quicker, we may wish not to chase pointers
7698 		 * (ire->ire_ipif->ipif_ill...) and instead store the
7699 		 * forwarding policy in the ire.  An unfortunate side-
7700 		 * effect of this would be requiring an ire flush whenever
7701 		 * the ILLF_ROUTER flag changes.  For now, chase pointers
7702 		 * once and store in the boolean no_forward.
7703 		 */
7704 		no_forward = ((ill->ill_flags &
7705 		    ire->ire_ipif->ipif_ill->ill_flags & ILLF_ROUTER) == 0);
7706 
7707 		ill_group = ill->ill_group;
7708 		if (rq != NULL) {
7709 			ire_ill = (ill_t *)(rq->q_ptr);
7710 			ire_group = ire_ill->ill_group;
7711 		}
7712 
7713 		/*
7714 		 * If it's part of the same IPMP group, or if it's a legal
7715 		 * address on the 'usesrc' interface, then bypass strict
7716 		 * checks.
7717 		 */
7718 		if (ill_group != NULL && ill_group == ire_group) {
7719 			check_multi = B_FALSE;
7720 		} else if (ill_ifindex != 0 && ire_ill != NULL &&
7721 		    ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7722 			check_multi = B_FALSE;
7723 		}
7724 
7725 		ASSERT(!IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst));
7726 		if (check_multi && ipv6_strict_dst_multihoming && no_forward) {
7727 			/*
7728 			 * This packet came in on an interface other than the
7729 			 * one associated with the destination address
7730 			 * and we are strict about matches.
7731 			 *
7732 			 * As long as the ills belong to the same group,
7733 			 * we don't consider them to arriving on the wrong
7734 			 * interface. Thus, when the switch is doing inbound
7735 			 * load spreading, we won't drop packets when we
7736 			 * are doing strict multihoming checks.
7737 			 */
7738 			BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits);
7739 			freemsg(hada_mp);
7740 			freemsg(first_mp);
7741 			ire_refrele(ire);
7742 			return;
7743 		}
7744 
7745 		if (rq != NULL)
7746 			q = rq;
7747 
7748 		ill = (ill_t *)q->q_ptr;
7749 		ASSERT(ill);
7750 	}
7751 
7752 	zoneid = ire->ire_zoneid;
7753 	UPDATE_IB_PKT_COUNT(ire);
7754 	ire->ire_last_used_time = lbolt;
7755 	/* Don't use the ire after this point. */
7756 	ire_refrele(ire);
7757 ipv6forus:
7758 	/*
7759 	 * Looks like this packet is for us one way or another.
7760 	 * This is where we'll process destination headers etc.
7761 	 */
7762 	for (; ; ) {
7763 		switch (nexthdr) {
7764 		case IPPROTO_TCP: {
7765 			uint16_t	*up;
7766 			uint32_t	sum;
7767 			int		offset;
7768 
7769 			hdr_len = pkt_len - remlen;
7770 
7771 			if (hada_mp != NULL) {
7772 				ip0dbg(("tcp hada drop\n"));
7773 				goto hada_drop;
7774 			}
7775 
7776 
7777 			/* TCP needs all of the TCP header */
7778 			if (remlen < TCP_MIN_HEADER_LENGTH)
7779 				goto pkt_too_short;
7780 			if (mp->b_cont != NULL &&
7781 			    whereptr + TCP_MIN_HEADER_LENGTH > mp->b_wptr) {
7782 				if (!pullupmsg(mp,
7783 				    hdr_len + TCP_MIN_HEADER_LENGTH)) {
7784 					BUMP_MIB(ill->ill_ip6_mib,
7785 					    ipv6InDiscards);
7786 					freemsg(first_mp);
7787 					return;
7788 				}
7789 				hck_flags = 0;
7790 				ip6h = (ip6_t *)mp->b_rptr;
7791 				whereptr = (uint8_t *)ip6h + hdr_len;
7792 			}
7793 			/*
7794 			 * Extract the offset field from the TCP header.
7795 			 */
7796 			offset = ((uchar_t *)ip6h)[hdr_len + 12] >> 4;
7797 			if (offset != 5) {
7798 				if (offset < 5) {
7799 					ip1dbg(("ip_rput_data_v6: short "
7800 					    "TCP data offset"));
7801 					BUMP_MIB(ill->ill_ip6_mib,
7802 					    ipv6InDiscards);
7803 					freemsg(first_mp);
7804 					return;
7805 				}
7806 				/*
7807 				 * There must be TCP options.
7808 				 * Make sure we can grab them.
7809 				 */
7810 				offset <<= 2;
7811 				if (remlen < offset)
7812 					goto pkt_too_short;
7813 				if (mp->b_cont != NULL &&
7814 				    whereptr + offset > mp->b_wptr) {
7815 					if (!pullupmsg(mp,
7816 					    hdr_len + offset)) {
7817 						BUMP_MIB(ill->ill_ip6_mib,
7818 						    ipv6InDiscards);
7819 						freemsg(first_mp);
7820 						return;
7821 					}
7822 					hck_flags = 0;
7823 					ip6h = (ip6_t *)mp->b_rptr;
7824 					whereptr = (uint8_t *)ip6h + hdr_len;
7825 				}
7826 			}
7827 
7828 			up = (uint16_t *)&ip6h->ip6_src;
7829 			/*
7830 			 * TCP checksum calculation.  First sum up the
7831 			 * pseudo-header fields:
7832 			 *  -	Source IPv6 address
7833 			 *  -	Destination IPv6 address
7834 			 *  -	TCP payload length
7835 			 *  -	TCP protocol ID
7836 			 */
7837 			sum = htons(IPPROTO_TCP + remlen) +
7838 			    up[0] + up[1] + up[2] + up[3] +
7839 			    up[4] + up[5] + up[6] + up[7] +
7840 			    up[8] + up[9] + up[10] + up[11] +
7841 			    up[12] + up[13] + up[14] + up[15];
7842 
7843 			/* Fold initial sum */
7844 			sum = (sum & 0xffff) + (sum >> 16);
7845 
7846 			mp1 = mp->b_cont;
7847 
7848 			if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0)
7849 				IP6_STAT(ip6_in_sw_cksum);
7850 
7851 			IP_CKSUM_RECV(hck_flags, sum, (uchar_t *)
7852 			    ((uchar_t *)mp->b_rptr + DB_CKSUMSTART(mp)),
7853 			    (int32_t)(whereptr - (uchar_t *)mp->b_rptr),
7854 			    mp, mp1, cksum_err);
7855 
7856 			if (cksum_err) {
7857 				BUMP_MIB(&ip_mib, tcpInErrs);
7858 
7859 				if (hck_flags & HCK_FULLCKSUM)
7860 					IP6_STAT(ip6_tcp_in_full_hw_cksum_err);
7861 				else if (hck_flags & HCK_PARTIALCKSUM)
7862 					IP6_STAT(ip6_tcp_in_part_hw_cksum_err);
7863 				else
7864 					IP6_STAT(ip6_tcp_in_sw_cksum_err);
7865 
7866 				freemsg(first_mp);
7867 				return;
7868 			}
7869 tcp_fanout:
7870 			ip_fanout_tcp_v6(q, first_mp, ip6h, ill, inill,
7871 			    (flags|IP_FF_SEND_ICMP|IP_FF_SYN_ADDIRE|
7872 			    IP_FF_IP6INFO), hdr_len, mctl_present, zoneid);
7873 			return;
7874 		}
7875 		case IPPROTO_SCTP:
7876 		{
7877 			sctp_hdr_t *sctph;
7878 			uint32_t calcsum, pktsum;
7879 			uint_t hdr_len = pkt_len - remlen;
7880 
7881 			/* SCTP needs all of the SCTP header */
7882 			if (remlen < sizeof (*sctph)) {
7883 				goto pkt_too_short;
7884 			}
7885 			if (whereptr + sizeof (*sctph) > mp->b_wptr) {
7886 				ASSERT(mp->b_cont != NULL);
7887 				if (!pullupmsg(mp, hdr_len + sizeof (*sctph))) {
7888 					BUMP_MIB(ill->ill_ip6_mib,
7889 					    ipv6InDiscards);
7890 					freemsg(mp);
7891 					return;
7892 				}
7893 				ip6h = (ip6_t *)mp->b_rptr;
7894 				whereptr = (uint8_t *)ip6h + hdr_len;
7895 			}
7896 
7897 			sctph = (sctp_hdr_t *)(mp->b_rptr + hdr_len);
7898 			/* checksum */
7899 			pktsum = sctph->sh_chksum;
7900 			sctph->sh_chksum = 0;
7901 			calcsum = sctp_cksum(mp, hdr_len);
7902 			if (calcsum != pktsum) {
7903 				BUMP_MIB(&sctp_mib, sctpChecksumError);
7904 				freemsg(mp);
7905 				return;
7906 			}
7907 			sctph->sh_chksum = pktsum;
7908 			ports = *(uint32_t *)(mp->b_rptr + hdr_len);
7909 			if ((connp = sctp_fanout(&ip6h->ip6_src, &ip6h->ip6_dst,
7910 			    ports, ipif_id, zoneid, mp)) == NULL) {
7911 				ip_fanout_sctp_raw(first_mp, ill,
7912 				    (ipha_t *)ip6h, B_FALSE, ports,
7913 				    mctl_present,
7914 				    (flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO),
7915 				    B_TRUE, ipif_id, zoneid);
7916 				return;
7917 			}
7918 			BUMP_MIB(&ip_mib, ipInDelivers);
7919 			sctp_input(connp, (ipha_t *)ip6h, mp, first_mp, ill,
7920 			    B_FALSE, mctl_present);
7921 			return;
7922 		}
7923 		case IPPROTO_UDP: {
7924 			uint16_t	*up;
7925 			uint32_t	sum;
7926 
7927 			hdr_len = pkt_len - remlen;
7928 
7929 			if (hada_mp != NULL) {
7930 				ip0dbg(("udp hada drop\n"));
7931 				goto hada_drop;
7932 			}
7933 
7934 			/* Verify that at least the ports are present */
7935 			if (remlen < UDPH_SIZE)
7936 				goto pkt_too_short;
7937 			if (mp->b_cont != NULL &&
7938 			    whereptr + UDPH_SIZE > mp->b_wptr) {
7939 				if (!pullupmsg(mp, hdr_len + UDPH_SIZE)) {
7940 					BUMP_MIB(ill->ill_ip6_mib,
7941 					    ipv6InDiscards);
7942 					freemsg(first_mp);
7943 					return;
7944 				}
7945 				hck_flags = 0;
7946 				ip6h = (ip6_t *)mp->b_rptr;
7947 				whereptr = (uint8_t *)ip6h + hdr_len;
7948 			}
7949 
7950 			/*
7951 			 *  Before going through the regular checksum
7952 			 *  calculation, make sure the received checksum
7953 			 *  is non-zero. RFC 2460 says, a 0x0000 checksum
7954 			 *  in a UDP packet (within IPv6 packet) is invalid
7955 			 *  and should be replaced by 0xffff. This makes
7956 			 *  sense as regular checksum calculation will
7957 			 *  pass for both the cases i.e. 0x0000 and 0xffff.
7958 			 *  Removing one of the case makes error detection
7959 			 *  stronger.
7960 			 */
7961 
7962 			if (((udpha_t *)whereptr)->uha_checksum == 0) {
7963 				/* 0x0000 checksum is invalid */
7964 				ip1dbg(("ip_rput_data_v6: Invalid UDP "
7965 				    "checksum value 0x0000\n"));
7966 				BUMP_MIB(ill->ill_ip6_mib, udpInCksumErrs);
7967 				freemsg(first_mp);
7968 				return;
7969 			}
7970 
7971 			up = (uint16_t *)&ip6h->ip6_src;
7972 
7973 			/*
7974 			 * UDP checksum calculation.  First sum up the
7975 			 * pseudo-header fields:
7976 			 *  -	Source IPv6 address
7977 			 *  -	Destination IPv6 address
7978 			 *  -	UDP payload length
7979 			 *  -	UDP protocol ID
7980 			 */
7981 
7982 			sum = htons(IPPROTO_UDP + remlen) +
7983 			    up[0] + up[1] + up[2] + up[3] +
7984 			    up[4] + up[5] + up[6] + up[7] +
7985 			    up[8] + up[9] + up[10] + up[11] +
7986 			    up[12] + up[13] + up[14] + up[15];
7987 
7988 			/* Fold initial sum */
7989 			sum = (sum & 0xffff) + (sum >> 16);
7990 
7991 			if (reass_hck_flags != 0) {
7992 				hck_flags = reass_hck_flags;
7993 
7994 				IP_CKSUM_RECV_REASS(hck_flags,
7995 				    (int32_t)(whereptr - (uchar_t *)mp->b_rptr),
7996 				    sum, reass_sum, cksum_err);
7997 			} else {
7998 				mp1 = mp->b_cont;
7999 
8000 				IP_CKSUM_RECV(hck_flags, sum, (uchar_t *)
8001 				    ((uchar_t *)mp->b_rptr + DB_CKSUMSTART(mp)),
8002 				    (int32_t)(whereptr - (uchar_t *)mp->b_rptr),
8003 				    mp, mp1, cksum_err);
8004 			}
8005 
8006 			if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0)
8007 				IP6_STAT(ip6_in_sw_cksum);
8008 
8009 			if (cksum_err) {
8010 				BUMP_MIB(ill->ill_ip6_mib, udpInCksumErrs);
8011 
8012 				if (hck_flags & HCK_FULLCKSUM)
8013 					IP6_STAT(ip6_udp_in_full_hw_cksum_err);
8014 				else if (hck_flags & HCK_PARTIALCKSUM)
8015 					IP6_STAT(ip6_udp_in_part_hw_cksum_err);
8016 				else
8017 					IP6_STAT(ip6_udp_in_sw_cksum_err);
8018 
8019 				freemsg(first_mp);
8020 				return;
8021 			}
8022 			goto udp_fanout;
8023 		}
8024 		case IPPROTO_ICMPV6: {
8025 			uint16_t	*up;
8026 			uint32_t	sum;
8027 			uint_t		hdr_len = pkt_len - remlen;
8028 
8029 			if (hada_mp != NULL) {
8030 				ip0dbg(("icmp hada drop\n"));
8031 				goto hada_drop;
8032 			}
8033 
8034 			up = (uint16_t *)&ip6h->ip6_src;
8035 			sum = htons(IPPROTO_ICMPV6 + remlen) +
8036 			    up[0] + up[1] + up[2] + up[3] +
8037 			    up[4] + up[5] + up[6] + up[7] +
8038 			    up[8] + up[9] + up[10] + up[11] +
8039 			    up[12] + up[13] + up[14] + up[15];
8040 			sum = (sum & 0xffff) + (sum >> 16);
8041 			sum = IP_CSUM(mp, hdr_len, sum);
8042 			if (sum != 0) {
8043 				/* IPv6 ICMP checksum failed */
8044 				ip1dbg(("ip_rput_data_v6: ICMPv6 checksum "
8045 				    "failed %x\n",
8046 				    sum));
8047 				BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInMsgs);
8048 				BUMP_MIB(ill->ill_icmp6_mib,
8049 				    ipv6IfIcmpInErrors);
8050 				freemsg(first_mp);
8051 				return;
8052 			}
8053 
8054 		icmp_fanout:
8055 			/* Check variable for testing applications */
8056 			if (ipv6_drop_inbound_icmpv6) {
8057 				freemsg(first_mp);
8058 				return;
8059 			}
8060 			/*
8061 			 * Assume that there is always at least one conn for
8062 			 * ICMPv6 (in.ndpd) i.e. don't optimize the case
8063 			 * where there is no conn.
8064 			 */
8065 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
8066 				ASSERT(!(ill->ill_phyint->phyint_flags &
8067 				    PHYI_LOOPBACK));
8068 				/*
8069 				 * In the multicast case, applications may have
8070 				 * joined the group from different zones, so we
8071 				 * need to deliver the packet to each of them.
8072 				 * Loop through the multicast memberships
8073 				 * structures (ilm) on the receive ill and send
8074 				 * a copy of the packet up each matching one.
8075 				 */
8076 				ILM_WALKER_HOLD(ill);
8077 				for (ilm = ill->ill_ilm; ilm != NULL;
8078 				    ilm = ilm->ilm_next) {
8079 					if (ilm->ilm_flags & ILM_DELETED)
8080 						continue;
8081 					if (!IN6_ARE_ADDR_EQUAL(
8082 					    &ilm->ilm_v6addr, &ip6h->ip6_dst))
8083 						continue;
8084 					if (!ipif_lookup_zoneid(ill,
8085 					    ilm->ilm_zoneid, IPIF_UP, NULL))
8086 						continue;
8087 
8088 					first_mp1 = ip_copymsg(first_mp);
8089 					if (first_mp1 == NULL)
8090 						continue;
8091 					icmp_inbound_v6(q, first_mp1, ill,
8092 					    hdr_len, mctl_present, 0,
8093 					    ilm->ilm_zoneid, dl_mp);
8094 				}
8095 				ILM_WALKER_RELE(ill);
8096 			} else {
8097 				first_mp1 = ip_copymsg(first_mp);
8098 				if (first_mp1 != NULL)
8099 					icmp_inbound_v6(q, first_mp1, ill,
8100 					    hdr_len, mctl_present, 0, zoneid,
8101 					    dl_mp);
8102 			}
8103 		}
8104 			/* FALLTHRU */
8105 		default: {
8106 			/*
8107 			 * Handle protocols with which IPv6 is less intimate.
8108 			 */
8109 			uint_t proto_flags = IP_FF_RAWIP|IP_FF_IP6INFO;
8110 
8111 			if (hada_mp != NULL) {
8112 				ip0dbg(("default hada drop\n"));
8113 				goto hada_drop;
8114 			}
8115 
8116 			/*
8117 			 * Enable sending ICMP for "Unknown" nexthdr
8118 			 * case. i.e. where we did not FALLTHRU from
8119 			 * IPPROTO_ICMPV6 processing case above.
8120 			 * If we did FALLTHRU, then the packet has already been
8121 			 * processed for IPPF, don't process it again in
8122 			 * ip_fanout_proto_v6; set IP6_NO_IPPOLICY in the
8123 			 * flags
8124 			 */
8125 			if (nexthdr != IPPROTO_ICMPV6)
8126 				proto_flags |= IP_FF_SEND_ICMP;
8127 			else
8128 				proto_flags |= IP6_NO_IPPOLICY;
8129 
8130 			ip_fanout_proto_v6(q, first_mp, ip6h, ill, inill,
8131 			    nexthdr, prev_nexthdr_offset, (flags|proto_flags),
8132 			    mctl_present, zoneid);
8133 			return;
8134 		}
8135 
8136 		case IPPROTO_DSTOPTS: {
8137 			uint_t ehdrlen;
8138 			uint8_t *optptr;
8139 			ip6_dest_t *desthdr;
8140 
8141 			/* Check if AH is present. */
8142 			if (ipsec_early_ah_v6(q, first_mp, mctl_present, ill,
8143 			    ire, hada_mp, zoneid)) {
8144 				ip0dbg(("dst early hada drop\n"));
8145 				return;
8146 			}
8147 
8148 			/*
8149 			 * Reinitialize pointers, as ipsec_early_ah_v6() does
8150 			 * complete pullups.  We don't have to do more pullups
8151 			 * as a result.
8152 			 */
8153 			whereptr = (uint8_t *)((uintptr_t)mp->b_rptr +
8154 			    (uintptr_t)(whereptr - ((uint8_t *)ip6h)));
8155 			ip6h = (ip6_t *)mp->b_rptr;
8156 
8157 			if (remlen < MIN_EHDR_LEN)
8158 				goto pkt_too_short;
8159 
8160 			desthdr = (ip6_dest_t *)whereptr;
8161 			nexthdr = desthdr->ip6d_nxt;
8162 			prev_nexthdr_offset = (uint_t)(whereptr -
8163 			    (uint8_t *)ip6h);
8164 			ehdrlen = 8 * (desthdr->ip6d_len + 1);
8165 			if (remlen < ehdrlen)
8166 				goto pkt_too_short;
8167 			optptr = whereptr + 2;
8168 			/*
8169 			 * Note: XXX This code does not seem to make
8170 			 * distinction between Destination Options Header
8171 			 * being before/after Routing Header which can
8172 			 * happen if we are at the end of source route.
8173 			 * This may become significant in future.
8174 			 * (No real significant Destination Options are
8175 			 * defined/implemented yet ).
8176 			 */
8177 			switch (ip_process_options_v6(q, first_mp, ip6h, optptr,
8178 			    ehdrlen - 2, IPPROTO_DSTOPTS)) {
8179 			case -1:
8180 				/*
8181 				 * Packet has been consumed and any needed
8182 				 * ICMP errors sent.
8183 				 */
8184 				BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors);
8185 				freemsg(hada_mp);
8186 				return;
8187 			case 0:
8188 				/* No action needed  continue */
8189 				break;
8190 			case 1:
8191 				/*
8192 				 * Unnexpected return value
8193 				 * (Router alert is a Hop-by-Hop option)
8194 				 */
8195 #ifdef DEBUG
8196 				panic("ip_rput_data_v6: router "
8197 				    "alert hbh opt indication in dest opt");
8198 				/*NOTREACHED*/
8199 #else
8200 				freemsg(hada_mp);
8201 				freemsg(first_mp);
8202 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
8203 				return;
8204 #endif
8205 			}
8206 			used = ehdrlen;
8207 			break;
8208 		}
8209 		case IPPROTO_FRAGMENT: {
8210 			ip6_frag_t *fraghdr;
8211 			size_t no_frag_hdr_len;
8212 
8213 			if (hada_mp != NULL) {
8214 				ip0dbg(("frag hada drop\n"));
8215 				goto hada_drop;
8216 			}
8217 
8218 			ASSERT(first_mp == mp);
8219 			if (remlen < sizeof (ip6_frag_t))
8220 				goto pkt_too_short;
8221 
8222 			if (mp->b_cont != NULL &&
8223 			    whereptr + sizeof (ip6_frag_t) > mp->b_wptr) {
8224 				if (!pullupmsg(mp,
8225 				    pkt_len - remlen + sizeof (ip6_frag_t))) {
8226 					BUMP_MIB(ill->ill_ip6_mib,
8227 					    ipv6InDiscards);
8228 					freemsg(mp);
8229 					return;
8230 				}
8231 				hck_flags = 0;
8232 				ip6h = (ip6_t *)mp->b_rptr;
8233 				whereptr = (uint8_t *)ip6h + pkt_len - remlen;
8234 			}
8235 
8236 			fraghdr = (ip6_frag_t *)whereptr;
8237 			used = (uint_t)sizeof (ip6_frag_t);
8238 			BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmReqds);
8239 
8240 			/*
8241 			 * Invoke the CGTP (multirouting) filtering module to
8242 			 * process the incoming packet. Packets identified as
8243 			 * duplicates must be discarded. Filtering is active
8244 			 * only if the the ip_cgtp_filter ndd variable is
8245 			 * non-zero.
8246 			 */
8247 			if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL)) {
8248 				int cgtp_flt_pkt =
8249 				    ip_cgtp_filter_ops->cfo_filter_v6(
8250 				    inill->ill_rq, ip6h, fraghdr);
8251 				if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) {
8252 					freemsg(mp);
8253 					return;
8254 				}
8255 			}
8256 
8257 			/* Restore the flags */
8258 			DB_CKSUMFLAGS(mp) = hck_flags;
8259 
8260 			mp = ip_rput_frag_v6(q, mp, ip6h, fraghdr,
8261 			    remlen - used, &prev_nexthdr_offset,
8262 			    &reass_sum, &reass_hck_flags);
8263 			if (mp == NULL) {
8264 				/* Reassembly is still pending */
8265 				return;
8266 			}
8267 			/* The first mblk are the headers before the frag hdr */
8268 			BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmOKs);
8269 
8270 			first_mp = mp;	/* mp has most likely changed! */
8271 			no_frag_hdr_len = mp->b_wptr - mp->b_rptr;
8272 			ip6h = (ip6_t *)mp->b_rptr;
8273 			nexthdr = ((char *)ip6h)[prev_nexthdr_offset];
8274 			whereptr = mp->b_rptr + no_frag_hdr_len;
8275 			remlen = ntohs(ip6h->ip6_plen)  +
8276 			    (uint16_t)(IPV6_HDR_LEN - no_frag_hdr_len);
8277 			pkt_len = msgdsize(mp);
8278 			used = 0;
8279 			break;
8280 		}
8281 		case IPPROTO_HOPOPTS:
8282 			if (hada_mp != NULL) {
8283 				ip0dbg(("hop hada drop\n"));
8284 				goto hada_drop;
8285 			}
8286 			/*
8287 			 * Illegal header sequence.
8288 			 * (Hop-by-hop headers are processed above
8289 			 *  and required to immediately follow IPv6 header)
8290 			 */
8291 			icmp_param_problem_v6(WR(q), first_mp,
8292 			    ICMP6_PARAMPROB_NEXTHEADER,
8293 			    prev_nexthdr_offset,
8294 			    B_FALSE, B_FALSE, zoneid);
8295 			return;
8296 
8297 		case IPPROTO_ROUTING: {
8298 			uint_t ehdrlen;
8299 			ip6_rthdr_t *rthdr;
8300 
8301 			/* Check if AH is present. */
8302 			if (ipsec_early_ah_v6(q, first_mp, mctl_present, ill,
8303 			    ire, hada_mp, zoneid)) {
8304 				ip0dbg(("routing hada drop\n"));
8305 				return;
8306 			}
8307 
8308 			/*
8309 			 * Reinitialize pointers, as ipsec_early_ah_v6() does
8310 			 * complete pullups.  We don't have to do more pullups
8311 			 * as a result.
8312 			 */
8313 			whereptr = (uint8_t *)((uintptr_t)mp->b_rptr +
8314 			    (uintptr_t)(whereptr - ((uint8_t *)ip6h)));
8315 			ip6h = (ip6_t *)mp->b_rptr;
8316 
8317 			if (remlen < MIN_EHDR_LEN)
8318 				goto pkt_too_short;
8319 			rthdr = (ip6_rthdr_t *)whereptr;
8320 			nexthdr = rthdr->ip6r_nxt;
8321 			prev_nexthdr_offset = (uint_t)(whereptr -
8322 			    (uint8_t *)ip6h);
8323 			ehdrlen = 8 * (rthdr->ip6r_len + 1);
8324 			if (remlen < ehdrlen)
8325 				goto pkt_too_short;
8326 			if (rthdr->ip6r_segleft != 0) {
8327 				/* Not end of source route */
8328 				if (ll_multicast) {
8329 					BUMP_MIB(ill->ill_ip6_mib,
8330 					    ipv6ForwProhibits);
8331 					freemsg(hada_mp);
8332 					freemsg(mp);
8333 					return;
8334 				}
8335 				ip_process_rthdr(q, mp, ip6h, rthdr, ill,
8336 				    flags, hada_mp, dl_mp);
8337 				return;
8338 			}
8339 			used = ehdrlen;
8340 			break;
8341 		}
8342 		case IPPROTO_AH:
8343 		case IPPROTO_ESP: {
8344 			/*
8345 			 * Fast path for AH/ESP. If this is the first time
8346 			 * we are sending a datagram to AH/ESP, allocate
8347 			 * a IPSEC_IN message and prepend it. Otherwise,
8348 			 * just fanout.
8349 			 */
8350 
8351 			ipsec_in_t *ii;
8352 			int ipsec_rc;
8353 
8354 			if (!mctl_present) {
8355 				ASSERT(first_mp == mp);
8356 				if ((first_mp = ipsec_in_alloc(B_FALSE)) ==
8357 				    NULL) {
8358 					ip1dbg(("ip_rput_data_v6: IPSEC_IN "
8359 					    "allocation failure.\n"));
8360 					BUMP_MIB(ill->ill_ip6_mib,
8361 					    ipv6InDiscards);
8362 					freemsg(mp);
8363 					return;
8364 				}
8365 				/*
8366 				 * Store the ill_index so that when we come back
8367 				 * from IPSEC we ride on the same queue.
8368 				 */
8369 				ii = (ipsec_in_t *)first_mp->b_rptr;
8370 				ii->ipsec_in_ill_index =
8371 				    ill->ill_phyint->phyint_ifindex;
8372 				ii->ipsec_in_rill_index =
8373 				    ii->ipsec_in_ill_index;
8374 				first_mp->b_cont = mp;
8375 				/*
8376 				 * Cache hardware acceleration info.
8377 				 */
8378 				if (hada_mp != NULL) {
8379 					IPSECHW_DEBUG(IPSECHW_PKT,
8380 					    ("ip_rput_data_v6: "
8381 						"caching data attr.\n"));
8382 					ii->ipsec_in_accelerated = B_TRUE;
8383 					ii->ipsec_in_da = hada_mp;
8384 					hada_mp = NULL;
8385 				}
8386 			} else {
8387 				ii = (ipsec_in_t *)first_mp->b_rptr;
8388 			}
8389 
8390 			if (!ipsec_loaded()) {
8391 				ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP,
8392 				    ire->ire_zoneid);
8393 				return;
8394 			}
8395 
8396 			/* select inbound SA and have IPsec process the pkt */
8397 			if (nexthdr == IPPROTO_ESP) {
8398 				esph_t *esph = ipsec_inbound_esp_sa(first_mp);
8399 				if (esph == NULL)
8400 					return;
8401 				ASSERT(ii->ipsec_in_esp_sa != NULL);
8402 				ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func !=
8403 				    NULL);
8404 				ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(
8405 				    first_mp, esph);
8406 			} else {
8407 				ah_t *ah = ipsec_inbound_ah_sa(first_mp);
8408 				if (ah == NULL)
8409 					return;
8410 				ASSERT(ii->ipsec_in_ah_sa != NULL);
8411 				ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func !=
8412 				    NULL);
8413 				ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(
8414 				    first_mp, ah);
8415 			}
8416 
8417 			switch (ipsec_rc) {
8418 			case IPSEC_STATUS_SUCCESS:
8419 				break;
8420 			case IPSEC_STATUS_FAILED:
8421 				BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
8422 				/* FALLTHRU */
8423 			case IPSEC_STATUS_PENDING:
8424 				return;
8425 			}
8426 			/* we're done with IPsec processing, send it up */
8427 			ip_fanout_proto_again(first_mp, ill, inill, ire);
8428 			return;
8429 		}
8430 		case IPPROTO_NONE:
8431 			/* All processing is done. Count as "delivered". */
8432 			freemsg(hada_mp);
8433 			freemsg(first_mp);
8434 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
8435 			return;
8436 		}
8437 		whereptr += used;
8438 		ASSERT(remlen >= used);
8439 		remlen -= used;
8440 	}
8441 	/* NOTREACHED */
8442 
8443 pkt_too_short:
8444 	ip1dbg(("ip_rput_data_v6: packet too short %d %lu %d\n",
8445 	    ip6_len, pkt_len, remlen));
8446 	BUMP_MIB(ill->ill_ip6_mib, ipv6InTruncatedPkts);
8447 	freemsg(hada_mp);
8448 	freemsg(first_mp);
8449 	return;
8450 udp_fanout:
8451 	if (mctl_present || IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
8452 		connp = NULL;
8453 	} else {
8454 		connp = ipcl_classify_v6(mp, IPPROTO_UDP, hdr_len, zoneid);
8455 		if ((connp != NULL) && (connp->conn_upq == NULL)) {
8456 			CONN_DEC_REF(connp);
8457 			connp = NULL;
8458 		}
8459 	}
8460 
8461 	if (connp == NULL) {
8462 		uint32_t	ports;
8463 
8464 		ports = *(uint32_t *)(mp->b_rptr + hdr_len +
8465 		    UDP_PORTS_OFFSET);
8466 		IP6_STAT(ip6_udp_slow_path);
8467 		ip_fanout_udp_v6(q, first_mp, ip6h, ports, ill, inill,
8468 		    (flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO), mctl_present,
8469 		    zoneid);
8470 		return;
8471 	}
8472 
8473 	if (CONN_UDP_FLOWCTLD(connp)) {
8474 		freemsg(first_mp);
8475 		BUMP_MIB(ill->ill_ip6_mib, udpInOverflows);
8476 		CONN_DEC_REF(connp);
8477 		return;
8478 	}
8479 
8480 	/* Initiate IPPF processing */
8481 	if (IP6_IN_IPP(flags)) {
8482 		ip_process(IPP_LOCAL_IN, &mp, ill->ill_phyint->phyint_ifindex);
8483 		if (mp == NULL) {
8484 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
8485 			CONN_DEC_REF(connp);
8486 			return;
8487 		}
8488 	}
8489 
8490 	if (connp->conn_ipv6_recvpktinfo ||
8491 	    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) {
8492 		mp = ip_add_info_v6(mp, inill, &ip6h->ip6_dst);
8493 		if (mp == NULL) {
8494 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
8495 			CONN_DEC_REF(connp);
8496 			return;
8497 		}
8498 	}
8499 
8500 	IP6_STAT(ip6_udp_fast_path);
8501 	BUMP_MIB(ill->ill_ip6_mib, ipv6InReceives);
8502 	BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers);
8503 
8504 	/* Send it upstream */
8505 	CONN_UDP_RECV(connp, mp);
8506 
8507 	CONN_DEC_REF(connp);
8508 	freemsg(hada_mp);
8509 	return;
8510 
8511 hada_drop:
8512 	ip1dbg(("ip_rput_data_v6: malformed accelerated packet\n"));
8513 	/* IPsec kstats: bump counter here */
8514 	freemsg(hada_mp);
8515 	freemsg(first_mp);
8516 }
8517 
8518 /*
8519  * Reassemble fragment.
8520  * When it returns a completed message the first mblk will only contain
8521  * the headers prior to the fragment header.
8522  *
8523  * prev_nexthdr_offset is an offset indication of where the nexthdr field is
8524  * of the preceding header.  This is needed to patch the previous header's
8525  * nexthdr field when reassembly completes.
8526  */
8527 static mblk_t *
8528 ip_rput_frag_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h,
8529     ip6_frag_t *fraghdr, uint_t remlen, uint_t *prev_nexthdr_offset,
8530     uint32_t *cksum_val, uint16_t *cksum_flags)
8531 {
8532 	ill_t		*ill = (ill_t *)q->q_ptr;
8533 	uint32_t	ident = ntohl(fraghdr->ip6f_ident);
8534 	uint16_t	offset;
8535 	boolean_t	more_frags;
8536 	uint8_t		nexthdr = fraghdr->ip6f_nxt;
8537 	in6_addr_t	*v6dst_ptr;
8538 	in6_addr_t	*v6src_ptr;
8539 	uint_t		end;
8540 	uint_t		hdr_length;
8541 	size_t		count;
8542 	ipf_t		*ipf;
8543 	ipf_t		**ipfp;
8544 	ipfb_t		*ipfb;
8545 	mblk_t		*mp1;
8546 	uint8_t		ecn_info = 0;
8547 	size_t		msg_len;
8548 	mblk_t		*tail_mp;
8549 	mblk_t		*t_mp;
8550 	boolean_t	pruned = B_FALSE;
8551 	uint32_t	sum_val;
8552 	uint16_t	sum_flags;
8553 
8554 
8555 	if (cksum_val != NULL)
8556 		*cksum_val = 0;
8557 	if (cksum_flags != NULL)
8558 		*cksum_flags = 0;
8559 
8560 	/*
8561 	 * We utilize hardware computed checksum info only for UDP since
8562 	 * IP fragmentation is a normal occurence for the protocol.  In
8563 	 * addition, checksum offload support for IP fragments carrying
8564 	 * UDP payload is commonly implemented across network adapters.
8565 	 */
8566 	ASSERT(ill != NULL);
8567 	if (nexthdr == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) &&
8568 	    (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
8569 		mblk_t *mp1 = mp->b_cont;
8570 		int32_t len;
8571 
8572 		/* Record checksum information from the packet */
8573 		sum_val = (uint32_t)DB_CKSUM16(mp);
8574 		sum_flags = DB_CKSUMFLAGS(mp);
8575 
8576 		/* fragmented payload offset from beginning of mblk */
8577 		offset = (uint16_t)((uchar_t *)&fraghdr[1] - mp->b_rptr);
8578 
8579 		if ((sum_flags & HCK_PARTIALCKSUM) &&
8580 		    (mp1 == NULL || mp1->b_cont == NULL) &&
8581 		    offset >= (uint16_t)DB_CKSUMSTART(mp) &&
8582 		    ((len = offset - (uint16_t)DB_CKSUMSTART(mp)) & 1) == 0) {
8583 			uint32_t adj;
8584 			/*
8585 			 * Partial checksum has been calculated by hardware
8586 			 * and attached to the packet; in addition, any
8587 			 * prepended extraneous data is even byte aligned.
8588 			 * If any such data exists, we adjust the checksum;
8589 			 * this would also handle any postpended data.
8590 			 */
8591 			IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
8592 			    mp, mp1, len, adj);
8593 
8594 			/* One's complement subtract extraneous checksum */
8595 			if (adj >= sum_val)
8596 				sum_val = ~(adj - sum_val) & 0xFFFF;
8597 			else
8598 				sum_val -= adj;
8599 		}
8600 	} else {
8601 		sum_val = 0;
8602 		sum_flags = 0;
8603 	}
8604 
8605 	/* Clear hardware checksumming flag */
8606 	DB_CKSUMFLAGS(mp) = 0;
8607 
8608 	/*
8609 	 * Note: Fragment offset in header is in 8-octet units.
8610 	 * Clearing least significant 3 bits not only extracts
8611 	 * it but also gets it in units of octets.
8612 	 */
8613 	offset = ntohs(fraghdr->ip6f_offlg) & ~7;
8614 	more_frags = (fraghdr->ip6f_offlg & IP6F_MORE_FRAG);
8615 
8616 	/*
8617 	 * Is the more frags flag on and the payload length not a multiple
8618 	 * of eight?
8619 	 */
8620 	if (more_frags && (ntohs(ip6h->ip6_plen) & 7)) {
8621 		zoneid_t zoneid;
8622 
8623 		BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors);
8624 		zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst, ill);
8625 		if (zoneid == ALL_ZONES) {
8626 			freemsg(mp);
8627 			return (NULL);
8628 		}
8629 		icmp_param_problem_v6(WR(q), mp, ICMP6_PARAMPROB_HEADER,
8630 		    (uint32_t)((char *)&ip6h->ip6_plen -
8631 		    (char *)ip6h), B_FALSE, B_FALSE, zoneid);
8632 		return (NULL);
8633 	}
8634 
8635 	v6src_ptr = &ip6h->ip6_src;
8636 	v6dst_ptr = &ip6h->ip6_dst;
8637 	end = remlen;
8638 
8639 	hdr_length = (uint_t)((char *)&fraghdr[1] - (char *)ip6h);
8640 	end += offset;
8641 
8642 	/*
8643 	 * Would fragment cause reassembled packet to have a payload length
8644 	 * greater than IP_MAXPACKET - the max payload size?
8645 	 */
8646 	if (end > IP_MAXPACKET) {
8647 		zoneid_t	zoneid;
8648 
8649 		BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors);
8650 		zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst, ill);
8651 		if (zoneid == ALL_ZONES) {
8652 			freemsg(mp);
8653 			return (NULL);
8654 		}
8655 		icmp_param_problem_v6(WR(q), mp, ICMP6_PARAMPROB_HEADER,
8656 		    (uint32_t)((char *)&fraghdr->ip6f_offlg -
8657 		    (char *)ip6h), B_FALSE, B_FALSE, zoneid);
8658 		return (NULL);
8659 	}
8660 
8661 	/*
8662 	 * This packet just has one fragment. Reassembly not
8663 	 * needed.
8664 	 */
8665 	if (!more_frags && offset == 0) {
8666 		goto reass_done;
8667 	}
8668 
8669 	/*
8670 	 * Drop the fragmented as early as possible, if
8671 	 * we don't have resource(s) to re-assemble.
8672 	 */
8673 	if (ip_reass_queue_bytes == 0) {
8674 		freemsg(mp);
8675 		return (NULL);
8676 	}
8677 
8678 	/* Record the ECN field info. */
8679 	ecn_info = (uint8_t)(ntohl(ip6h->ip6_vcf & htonl(~0xFFCFFFFF)) >> 20);
8680 	/*
8681 	 * If this is not the first fragment, dump the unfragmentable
8682 	 * portion of the packet.
8683 	 */
8684 	if (offset)
8685 		mp->b_rptr = (uchar_t *)&fraghdr[1];
8686 
8687 	/*
8688 	 * Fragmentation reassembly.  Each ILL has a hash table for
8689 	 * queueing packets undergoing reassembly for all IPIFs
8690 	 * associated with the ILL.  The hash is based on the packet
8691 	 * IP ident field.  The ILL frag hash table was allocated
8692 	 * as a timer block at the time the ILL was created.  Whenever
8693 	 * there is anything on the reassembly queue, the timer will
8694 	 * be running.
8695 	 */
8696 	msg_len = MBLKSIZE(mp);
8697 	tail_mp = mp;
8698 	while (tail_mp->b_cont != NULL) {
8699 		tail_mp = tail_mp->b_cont;
8700 		msg_len += MBLKSIZE(tail_mp);
8701 	}
8702 	/*
8703 	 * If the reassembly list for this ILL will get too big
8704 	 * prune it.
8705 	 */
8706 
8707 	if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
8708 	    ip_reass_queue_bytes) {
8709 		ill_frag_prune(ill, (ip_reass_queue_bytes < msg_len) ? 0
8710 		    : (ip_reass_queue_bytes - msg_len));
8711 		pruned = B_TRUE;
8712 	}
8713 
8714 	ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH_V6(*v6src_ptr, ident)];
8715 	mutex_enter(&ipfb->ipfb_lock);
8716 
8717 	ipfp = &ipfb->ipfb_ipf;
8718 	/* Try to find an existing fragment queue for this packet. */
8719 	for (;;) {
8720 		ipf = ipfp[0];
8721 		if (ipf) {
8722 			/*
8723 			 * It has to match on ident, source address, and
8724 			 * dest address.
8725 			 */
8726 			if (ipf->ipf_ident == ident &&
8727 			    IN6_ARE_ADDR_EQUAL(&ipf->ipf_v6src, v6src_ptr) &&
8728 			    IN6_ARE_ADDR_EQUAL(&ipf->ipf_v6dst, v6dst_ptr)) {
8729 
8730 				/*
8731 				 * If we have received too many
8732 				 * duplicate fragments for this packet
8733 				 * free it.
8734 				 */
8735 				if (ipf->ipf_num_dups > ip_max_frag_dups) {
8736 					ill_frag_free_pkts(ill, ipfb, ipf, 1);
8737 					freemsg(mp);
8738 					mutex_exit(&ipfb->ipfb_lock);
8739 					return (NULL);
8740 				}
8741 
8742 				break;
8743 			}
8744 			ipfp = &ipf->ipf_hash_next;
8745 			continue;
8746 		}
8747 
8748 
8749 		/*
8750 		 * If we pruned the list, do we want to store this new
8751 		 * fragment?. We apply an optimization here based on the
8752 		 * fact that most fragments will be received in order.
8753 		 * So if the offset of this incoming fragment is zero,
8754 		 * it is the first fragment of a new packet. We will
8755 		 * keep it.  Otherwise drop the fragment, as we have
8756 		 * probably pruned the packet already (since the
8757 		 * packet cannot be found).
8758 		 */
8759 
8760 		if (pruned && offset != 0) {
8761 			mutex_exit(&ipfb->ipfb_lock);
8762 			freemsg(mp);
8763 			return (NULL);
8764 		}
8765 
8766 		/* New guy.  Allocate a frag message. */
8767 		mp1 = allocb(sizeof (*ipf), BPRI_MED);
8768 		if (!mp1) {
8769 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
8770 			freemsg(mp);
8771 	partial_reass_done:
8772 			mutex_exit(&ipfb->ipfb_lock);
8773 			return (NULL);
8774 		}
8775 
8776 		if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS)  {
8777 			/*
8778 			 * Too many fragmented packets in this hash bucket.
8779 			 * Free the oldest.
8780 			 */
8781 			ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
8782 		}
8783 
8784 		mp1->b_cont = mp;
8785 
8786 		/* Initialize the fragment header. */
8787 		ipf = (ipf_t *)mp1->b_rptr;
8788 		ipf->ipf_mp = mp1;
8789 		ipf->ipf_ptphn = ipfp;
8790 		ipfp[0] = ipf;
8791 		ipf->ipf_hash_next = NULL;
8792 		ipf->ipf_ident = ident;
8793 		ipf->ipf_v6src = *v6src_ptr;
8794 		ipf->ipf_v6dst = *v6dst_ptr;
8795 		/* Record reassembly start time. */
8796 		ipf->ipf_timestamp = gethrestime_sec();
8797 		/* Record ipf generation and account for frag header */
8798 		ipf->ipf_gen = ill->ill_ipf_gen++;
8799 		ipf->ipf_count = MBLKSIZE(mp1);
8800 		ipf->ipf_protocol = nexthdr;
8801 		ipf->ipf_nf_hdr_len = 0;
8802 		ipf->ipf_prev_nexthdr_offset = 0;
8803 		ipf->ipf_last_frag_seen = B_FALSE;
8804 		ipf->ipf_ecn = ecn_info;
8805 		ipf->ipf_num_dups = 0;
8806 		ipfb->ipfb_frag_pkts++;
8807 		ipf->ipf_checksum = 0;
8808 		ipf->ipf_checksum_flags = 0;
8809 
8810 		/* Store checksum value in fragment header */
8811 		if (sum_flags != 0) {
8812 			sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8813 			sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8814 			ipf->ipf_checksum = sum_val;
8815 			ipf->ipf_checksum_flags = sum_flags;
8816 		}
8817 
8818 		/*
8819 		 * We handle reassembly two ways.  In the easy case,
8820 		 * where all the fragments show up in order, we do
8821 		 * minimal bookkeeping, and just clip new pieces on
8822 		 * the end.  If we ever see a hole, then we go off
8823 		 * to ip_reassemble which has to mark the pieces and
8824 		 * keep track of the number of holes, etc.  Obviously,
8825 		 * the point of having both mechanisms is so we can
8826 		 * handle the easy case as efficiently as possible.
8827 		 */
8828 		if (offset == 0) {
8829 			/* Easy case, in-order reassembly so far. */
8830 			/* Update the byte count */
8831 			ipf->ipf_count += msg_len;
8832 			ipf->ipf_tail_mp = tail_mp;
8833 			/*
8834 			 * Keep track of next expected offset in
8835 			 * ipf_end.
8836 			 */
8837 			ipf->ipf_end = end;
8838 			ipf->ipf_nf_hdr_len = hdr_length;
8839 			ipf->ipf_prev_nexthdr_offset = *prev_nexthdr_offset;
8840 		} else {
8841 			/* Hard case, hole at the beginning. */
8842 			ipf->ipf_tail_mp = NULL;
8843 			/*
8844 			 * ipf_end == 0 means that we have given up
8845 			 * on easy reassembly.
8846 			 */
8847 			ipf->ipf_end = 0;
8848 
8849 			/* Forget checksum offload from now on */
8850 			ipf->ipf_checksum_flags = 0;
8851 
8852 			/*
8853 			 * ipf_hole_cnt is set by ip_reassemble.
8854 			 * ipf_count is updated by ip_reassemble.
8855 			 * No need to check for return value here
8856 			 * as we don't expect reassembly to complete or
8857 			 * fail for the first fragment itself.
8858 			 */
8859 			(void) ip_reassemble(mp, ipf, offset, more_frags, ill,
8860 			    msg_len);
8861 		}
8862 		/* Update per ipfb and ill byte counts */
8863 		ipfb->ipfb_count += ipf->ipf_count;
8864 		ASSERT(ipfb->ipfb_count > 0);	/* Wraparound */
8865 		ill->ill_frag_count += ipf->ipf_count;
8866 		ASSERT(ill->ill_frag_count > 0);	/* Wraparound */
8867 		/* If the frag timer wasn't already going, start it. */
8868 		mutex_enter(&ill->ill_lock);
8869 		ill_frag_timer_start(ill);
8870 		mutex_exit(&ill->ill_lock);
8871 		goto partial_reass_done;
8872 	}
8873 
8874 	/*
8875 	 * If the packet's flag has changed (it could be coming up
8876 	 * from an interface different than the previous, therefore
8877 	 * possibly different checksum capability), then forget about
8878 	 * any stored checksum states.  Otherwise add the value to
8879 	 * the existing one stored in the fragment header.
8880 	 */
8881 	if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
8882 		sum_val += ipf->ipf_checksum;
8883 		sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8884 		sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8885 		ipf->ipf_checksum = sum_val;
8886 	} else if (ipf->ipf_checksum_flags != 0) {
8887 		/* Forget checksum offload from now on */
8888 		ipf->ipf_checksum_flags = 0;
8889 	}
8890 
8891 	/*
8892 	 * We have a new piece of a datagram which is already being
8893 	 * reassembled.  Update the ECN info if all IP fragments
8894 	 * are ECN capable.  If there is one which is not, clear
8895 	 * all the info.  If there is at least one which has CE
8896 	 * code point, IP needs to report that up to transport.
8897 	 */
8898 	if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
8899 		if (ecn_info == IPH_ECN_CE)
8900 			ipf->ipf_ecn = IPH_ECN_CE;
8901 	} else {
8902 		ipf->ipf_ecn = IPH_ECN_NECT;
8903 	}
8904 
8905 	if (offset && ipf->ipf_end == offset) {
8906 		/* The new fragment fits at the end */
8907 		ipf->ipf_tail_mp->b_cont = mp;
8908 		/* Update the byte count */
8909 		ipf->ipf_count += msg_len;
8910 		/* Update per ipfb and ill byte counts */
8911 		ipfb->ipfb_count += msg_len;
8912 		ASSERT(ipfb->ipfb_count > 0);	/* Wraparound */
8913 		ill->ill_frag_count += msg_len;
8914 		ASSERT(ill->ill_frag_count > 0);	/* Wraparound */
8915 		if (more_frags) {
8916 			/* More to come. */
8917 			ipf->ipf_end = end;
8918 			ipf->ipf_tail_mp = tail_mp;
8919 			goto partial_reass_done;
8920 		}
8921 	} else {
8922 		/*
8923 		 * Go do the hard cases.
8924 		 * Call ip_reassemble().
8925 		 */
8926 		int ret;
8927 
8928 		if (offset == 0) {
8929 			if (ipf->ipf_prev_nexthdr_offset == 0) {
8930 				ipf->ipf_nf_hdr_len = hdr_length;
8931 				ipf->ipf_prev_nexthdr_offset =
8932 				    *prev_nexthdr_offset;
8933 			}
8934 		}
8935 		/* Save current byte count */
8936 		count = ipf->ipf_count;
8937 		ret = ip_reassemble(mp, ipf, offset, more_frags, ill, msg_len);
8938 
8939 		/* Count of bytes added and subtracted (freeb()ed) */
8940 		count = ipf->ipf_count - count;
8941 		if (count) {
8942 			/* Update per ipfb and ill byte counts */
8943 			ipfb->ipfb_count += count;
8944 			ASSERT(ipfb->ipfb_count > 0);	/* Wraparound */
8945 			ill->ill_frag_count += count;
8946 			ASSERT(ill->ill_frag_count > 0); /* Wraparound */
8947 		}
8948 		if (ret == IP_REASS_PARTIAL) {
8949 			goto partial_reass_done;
8950 		} else if (ret == IP_REASS_FAILED) {
8951 			/* Reassembly failed. Free up all resources */
8952 			ill_frag_free_pkts(ill, ipfb, ipf, 1);
8953 			for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
8954 				IP_REASS_SET_START(t_mp, 0);
8955 				IP_REASS_SET_END(t_mp, 0);
8956 			}
8957 			freemsg(mp);
8958 			goto partial_reass_done;
8959 		}
8960 
8961 		/* We will reach here iff 'ret' is IP_REASS_COMPLETE */
8962 	}
8963 	/*
8964 	 * We have completed reassembly.  Unhook the frag header from
8965 	 * the reassembly list.
8966 	 *
8967 	 * Grab the unfragmentable header length next header value out
8968 	 * of the first fragment
8969 	 */
8970 	ASSERT(ipf->ipf_nf_hdr_len != 0);
8971 	hdr_length = ipf->ipf_nf_hdr_len;
8972 
8973 	/*
8974 	 * Before we free the frag header, record the ECN info
8975 	 * to report back to the transport.
8976 	 */
8977 	ecn_info = ipf->ipf_ecn;
8978 
8979 	/*
8980 	 * Store the nextheader field in the header preceding the fragment
8981 	 * header
8982 	 */
8983 	nexthdr = ipf->ipf_protocol;
8984 	*prev_nexthdr_offset = ipf->ipf_prev_nexthdr_offset;
8985 	ipfp = ipf->ipf_ptphn;
8986 
8987 	/* We need to supply these to caller */
8988 	if ((sum_flags = ipf->ipf_checksum_flags) != 0)
8989 		sum_val = ipf->ipf_checksum;
8990 	else
8991 		sum_val = 0;
8992 
8993 	mp1 = ipf->ipf_mp;
8994 	count = ipf->ipf_count;
8995 	ipf = ipf->ipf_hash_next;
8996 	if (ipf)
8997 		ipf->ipf_ptphn = ipfp;
8998 	ipfp[0] = ipf;
8999 	ill->ill_frag_count -= count;
9000 	ASSERT(ipfb->ipfb_count >= count);
9001 	ipfb->ipfb_count -= count;
9002 	ipfb->ipfb_frag_pkts--;
9003 	mutex_exit(&ipfb->ipfb_lock);
9004 	/* Ditch the frag header. */
9005 	mp = mp1->b_cont;
9006 	freeb(mp1);
9007 
9008 	/*
9009 	 * Make sure the packet is good by doing some sanity
9010 	 * check. If bad we can silentely drop the packet.
9011 	 */
9012 reass_done:
9013 	if (hdr_length < sizeof (ip6_frag_t)) {
9014 		BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors);
9015 		ip1dbg(("ip_rput_frag_v6: bad packet\n"));
9016 		freemsg(mp);
9017 		return (NULL);
9018 	}
9019 
9020 	/*
9021 	 * Remove the fragment header from the initial header by
9022 	 * splitting the mblk into the non-fragmentable header and
9023 	 * everthing after the fragment extension header.  This has the
9024 	 * side effect of putting all the headers that need destination
9025 	 * processing into the b_cont block-- on return this fact is
9026 	 * used in order to avoid having to look at the extensions
9027 	 * already processed.
9028 	 *
9029 	 * Note that this code assumes that the unfragmentable portion
9030 	 * of the header is in the first mblk and increments
9031 	 * the read pointer past it.  If this assumption is broken
9032 	 * this code fails badly.
9033 	 */
9034 	if (mp->b_rptr + hdr_length != mp->b_wptr) {
9035 		mblk_t *nmp;
9036 
9037 		if (!(nmp = dupb(mp))) {
9038 			BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards);
9039 			ip1dbg(("ip_rput_frag_v6: dupb failed\n"));
9040 			freemsg(mp);
9041 			return (NULL);
9042 		}
9043 		nmp->b_cont = mp->b_cont;
9044 		mp->b_cont = nmp;
9045 		nmp->b_rptr += hdr_length;
9046 	}
9047 	mp->b_wptr = mp->b_rptr + hdr_length - sizeof (ip6_frag_t);
9048 
9049 	ip6h = (ip6_t *)mp->b_rptr;
9050 	((char *)ip6h)[*prev_nexthdr_offset] = nexthdr;
9051 
9052 	/* Restore original IP length in header. */
9053 	ip6h->ip6_plen = htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN));
9054 	/* Record the ECN info. */
9055 	ip6h->ip6_vcf &= htonl(0xFFCFFFFF);
9056 	ip6h->ip6_vcf |= htonl(ecn_info << 20);
9057 
9058 	/* Reassembly is successful; return checksum information if needed */
9059 	if (cksum_val != NULL)
9060 		*cksum_val = sum_val;
9061 	if (cksum_flags != NULL)
9062 		*cksum_flags = sum_flags;
9063 
9064 	return (mp);
9065 }
9066 
9067 /*
9068  * Walk through the options to see if there is a routing header.
9069  * If present get the destination which is the last address of
9070  * the option.
9071  */
9072 in6_addr_t
9073 ip_get_dst_v6(ip6_t *ip6h, boolean_t *is_fragment)
9074 {
9075 	uint8_t nexthdr;
9076 	uint8_t *whereptr;
9077 	ip6_hbh_t *hbhhdr;
9078 	ip6_dest_t *dsthdr;
9079 	ip6_rthdr0_t *rthdr;
9080 	ip6_frag_t *fraghdr;
9081 	int ehdrlen;
9082 	int left;
9083 	in6_addr_t *ap, rv;
9084 
9085 	if (is_fragment != NULL)
9086 		*is_fragment = B_FALSE;
9087 
9088 	rv = ip6h->ip6_dst;
9089 
9090 	nexthdr = ip6h->ip6_nxt;
9091 	whereptr = (uint8_t *)&ip6h[1];
9092 	for (;;) {
9093 
9094 		ASSERT(nexthdr != IPPROTO_RAW);
9095 		switch (nexthdr) {
9096 		case IPPROTO_HOPOPTS:
9097 			hbhhdr = (ip6_hbh_t *)whereptr;
9098 			nexthdr = hbhhdr->ip6h_nxt;
9099 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
9100 			break;
9101 		case IPPROTO_DSTOPTS:
9102 			dsthdr = (ip6_dest_t *)whereptr;
9103 			nexthdr = dsthdr->ip6d_nxt;
9104 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
9105 			break;
9106 		case IPPROTO_ROUTING:
9107 			rthdr = (ip6_rthdr0_t *)whereptr;
9108 			nexthdr = rthdr->ip6r0_nxt;
9109 			ehdrlen = 8 * (rthdr->ip6r0_len + 1);
9110 
9111 			left = rthdr->ip6r0_segleft;
9112 			ap = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
9113 			rv = *(ap + left - 1);
9114 			/*
9115 			 * If the caller doesn't care whether the packet
9116 			 * is a fragment or not, we can stop here since
9117 			 * we have our destination.
9118 			 */
9119 			if (is_fragment == NULL)
9120 				goto done;
9121 			break;
9122 		case IPPROTO_FRAGMENT:
9123 			fraghdr = (ip6_frag_t *)whereptr;
9124 			nexthdr = fraghdr->ip6f_nxt;
9125 			ehdrlen = sizeof (ip6_frag_t);
9126 			if (is_fragment != NULL)
9127 				*is_fragment = B_TRUE;
9128 			goto done;
9129 		default :
9130 			goto done;
9131 		}
9132 		whereptr += ehdrlen;
9133 	}
9134 
9135 done:
9136 	return (rv);
9137 }
9138 
9139 /*
9140  * ip_source_routed_v6:
9141  * This function is called by redirect code in ip_rput_data_v6 to
9142  * know whether this packet is source routed through this node i.e
9143  * whether this node (router) is part of the journey. This
9144  * function is called under two cases :
9145  *
9146  * case 1 : Routing header was processed by this node and
9147  *	    ip_process_rthdr replaced ip6_dst with the next hop
9148  *          and we are forwarding the packet to the next hop.
9149  *
9150  * case 2 : Routing header was not processed by this node and we
9151  *	    are just forwarding the packet.
9152  *
9153  * For case (1) we don't want to send redirects. For case(2) we
9154  * want to send redirects.
9155  */
9156 static boolean_t
9157 ip_source_routed_v6(ip6_t *ip6h, mblk_t *mp)
9158 {
9159 	uint8_t		nexthdr;
9160 	in6_addr_t	*addrptr;
9161 	ip6_rthdr0_t	*rthdr;
9162 	uint8_t		numaddr;
9163 	ip6_hbh_t	*hbhhdr;
9164 	uint_t		ehdrlen;
9165 	uint8_t		*byteptr;
9166 
9167 	ip2dbg(("ip_source_routed_v6\n"));
9168 	nexthdr = ip6h->ip6_nxt;
9169 	ehdrlen = IPV6_HDR_LEN;
9170 
9171 	/* if a routing hdr is preceeded by HOPOPT or DSTOPT */
9172 	while (nexthdr == IPPROTO_HOPOPTS ||
9173 	    nexthdr == IPPROTO_DSTOPTS) {
9174 		byteptr = (uint8_t *)ip6h + ehdrlen;
9175 		/*
9176 		 * Check if we have already processed
9177 		 * packets or we are just a forwarding
9178 		 * router which only pulled up msgs up
9179 		 * to IPV6HDR and  one HBH ext header
9180 		 */
9181 		if (byteptr + MIN_EHDR_LEN > mp->b_wptr) {
9182 			ip2dbg(("ip_source_routed_v6: Extension"
9183 			    " headers not processed\n"));
9184 			return (B_FALSE);
9185 		}
9186 		hbhhdr = (ip6_hbh_t *)byteptr;
9187 		nexthdr = hbhhdr->ip6h_nxt;
9188 		ehdrlen = ehdrlen + 8 * (hbhhdr->ip6h_len + 1);
9189 	}
9190 	switch (nexthdr) {
9191 	case IPPROTO_ROUTING:
9192 		byteptr = (uint8_t *)ip6h + ehdrlen;
9193 		/*
9194 		 * If for some reason, we haven't pulled up
9195 		 * the routing hdr data mblk, then we must
9196 		 * not have processed it at all. So for sure
9197 		 * we are not part of the source routed journey.
9198 		 */
9199 		if (byteptr + MIN_EHDR_LEN > mp->b_wptr) {
9200 			ip2dbg(("ip_source_routed_v6: Routing"
9201 			    " header not processed\n"));
9202 			return (B_FALSE);
9203 		}
9204 		rthdr = (ip6_rthdr0_t *)byteptr;
9205 		/*
9206 		 * Either we are an intermediate router or the
9207 		 * last hop before destination and we have
9208 		 * already processed the routing header.
9209 		 * If segment_left is greater than or equal to zero,
9210 		 * then we must be the (numaddr - segleft) entry
9211 		 * of the routing header. Although ip6r0_segleft
9212 		 * is a unit8_t variable, we still check for zero
9213 		 * or greater value, if in case the data type
9214 		 * is changed someday in future.
9215 		 */
9216 		if (rthdr->ip6r0_segleft > 0 ||
9217 		    rthdr->ip6r0_segleft == 0) {
9218 			ire_t 	*ire = NULL;
9219 
9220 			numaddr = rthdr->ip6r0_len / 2;
9221 			addrptr = (in6_addr_t *)((char *)rthdr +
9222 			    sizeof (*rthdr));
9223 			addrptr += (numaddr - (rthdr->ip6r0_segleft + 1));
9224 			if (addrptr != NULL) {
9225 				ire = ire_ctable_lookup_v6(addrptr, NULL,
9226 				    IRE_LOCAL, NULL, ALL_ZONES, NULL,
9227 				    MATCH_IRE_TYPE);
9228 				if (ire != NULL) {
9229 					ire_refrele(ire);
9230 					return (B_TRUE);
9231 				}
9232 				ip1dbg(("ip_source_routed_v6: No ire found\n"));
9233 			}
9234 		}
9235 	/* FALLTHRU */
9236 	default:
9237 		ip2dbg(("ip_source_routed_v6: Not source routed here\n"));
9238 		return (B_FALSE);
9239 	}
9240 }
9241 
9242 /*
9243  * ip_wput_v6 -- Packets sent down from transport modules show up here.
9244  * Assumes that the following set of headers appear in the first
9245  * mblk:
9246  *	ip6i_t (if present) CAN also appear as a separate mblk.
9247  *	ip6_t
9248  *	Any extension headers
9249  *	TCP/UDP/SCTP header (if present)
9250  * The routine can handle an ICMPv6 header that is not in the first mblk.
9251  *
9252  * The order to determine the outgoing interface is as follows:
9253  * 1. IPV6_BOUND_PIF is set, use that ill (conn_outgoing_pill)
9254  * 2. If conn_nofailover_ill is set then use that ill.
9255  * 3. If an ip6i_t with IP6I_IFINDEX set then use that ill.
9256  * 4. If q is an ill queue and (link local or multicast destination) then
9257  *    use that ill.
9258  * 5. If IPV6_BOUND_IF has been set use that ill.
9259  * 6. For multicast: if IPV6_MULTICAST_IF has been set use it. Otherwise
9260  *    look for the best IRE match for the unspecified group to determine
9261  *    the ill.
9262  * 7. For unicast: Just do an IRE lookup for the best match.
9263  *
9264  * arg2 is always a queue_t *.
9265  * When that queue is an ill_t (i.e. q_next != NULL), then arg must be
9266  * the zoneid.
9267  * When that queue is not an ill_t, then arg must be a conn_t pointer.
9268  */
9269 void
9270 ip_output_v6(void *arg, mblk_t *mp, void *arg2, int caller)
9271 {
9272 	conn_t		*connp = NULL;
9273 	queue_t		*q = (queue_t *)arg2;
9274 	ire_t		*ire = NULL;
9275 	ire_t		*sctp_ire = NULL;
9276 	ip6_t		*ip6h;
9277 	in6_addr_t	*v6dstp;
9278 	ill_t		*ill = NULL;
9279 	ipif_t		*ipif;
9280 	ip6i_t		*ip6i;
9281 	int		cksum_request;	/* -1 => normal. */
9282 			/* 1 => Skip TCP/UDP/SCTP checksum */
9283 			/* Otherwise contains insert offset for checksum */
9284 	int		unspec_src;
9285 	boolean_t	do_outrequests;	/* Increment OutRequests? */
9286 	mib2_ipv6IfStatsEntry_t	*mibptr;
9287 	int 		match_flags = MATCH_IRE_ILL_GROUP;
9288 	boolean_t	attach_if = B_FALSE;
9289 	mblk_t		*first_mp;
9290 	boolean_t	mctl_present;
9291 	ipsec_out_t	*io;
9292 	boolean_t	drop_if_delayed = B_FALSE;
9293 	boolean_t	multirt_need_resolve = B_FALSE;
9294 	mblk_t		*copy_mp = NULL;
9295 	int		err;
9296 	int		ip6i_flags = 0;
9297 	zoneid_t	zoneid;
9298 	ill_t		*saved_ill = NULL;
9299 	boolean_t	conn_lock_held;
9300 	boolean_t	need_decref = B_FALSE;
9301 
9302 	/*
9303 	 * Highest bit in version field is Reachability Confirmation bit
9304 	 * used by NUD in ip_xmit_v6().
9305 	 */
9306 #ifdef	_BIG_ENDIAN
9307 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
9308 #else
9309 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
9310 #endif
9311 
9312 	/*
9313 	 * M_CTL comes from 6 places
9314 	 *
9315 	 * 1) TCP sends down IPSEC_OUT(M_CTL) for detached connections
9316 	 *    both V4 and V6 datagrams.
9317 	 *
9318 	 * 2) AH/ESP sends down M_CTL after doing their job with both
9319 	 *    V4 and V6 datagrams.
9320 	 *
9321 	 * 3) NDP callbacks when nce is resolved and IPSEC_OUT has been
9322 	 *    attached.
9323 	 *
9324 	 * 4) Notifications from an external resolver (for XRESOLV ifs)
9325 	 *
9326 	 * 5) AH/ESP send down IPSEC_CTL(M_CTL) to be relayed to hardware for
9327 	 *    IPsec hardware acceleration support.
9328 	 *
9329 	 * 6) TUN_HELLO.
9330 	 *
9331 	 * We need to handle (1)'s IPv6 case and (3) here.  For the
9332 	 * IPv4 case in (1), and (2), IPSEC processing has already
9333 	 * started. The code in ip_wput() already knows how to handle
9334 	 * continuing IPSEC processing (for IPv4 and IPv6).  All other
9335 	 * M_CTLs (including case (4)) are passed on to ip_wput_nondata()
9336 	 * for handling.
9337 	 */
9338 	first_mp = mp;
9339 	mctl_present = B_FALSE;
9340 	io = NULL;
9341 
9342 	/* Multidata transmit? */
9343 	if (DB_TYPE(mp) == M_MULTIDATA) {
9344 		/*
9345 		 * We should never get here, since all Multidata messages
9346 		 * originating from tcp should have been directed over to
9347 		 * tcp_multisend() in the first place.
9348 		 */
9349 		BUMP_MIB(&ip6_mib, ipv6OutDiscards);
9350 		freemsg(mp);
9351 		return;
9352 	} else if (DB_TYPE(mp) == M_CTL) {
9353 		uint32_t mctltype = 0;
9354 		uint32_t mlen = MBLKL(first_mp);
9355 
9356 		mp = mp->b_cont;
9357 		mctl_present = B_TRUE;
9358 		io = (ipsec_out_t *)first_mp->b_rptr;
9359 
9360 		/*
9361 		 * Validate this M_CTL message.  The only three types of
9362 		 * M_CTL messages we expect to see in this code path are
9363 		 * ipsec_out_t or ipsec_in_t structures (allocated as
9364 		 * ipsec_info_t unions), or ipsec_ctl_t structures.
9365 		 * The ipsec_out_type and ipsec_in_type overlap in the two
9366 		 * data structures, and they are either set to IPSEC_OUT
9367 		 * or IPSEC_IN depending on which data structure it is.
9368 		 * ipsec_ctl_t is an IPSEC_CTL.
9369 		 *
9370 		 * All other M_CTL messages are sent to ip_wput_nondata()
9371 		 * for handling.
9372 		 */
9373 		if (mlen >= sizeof (io->ipsec_out_type))
9374 			mctltype = io->ipsec_out_type;
9375 
9376 		if ((mlen == sizeof (ipsec_ctl_t)) &&
9377 		    (mctltype == IPSEC_CTL)) {
9378 			ip_output(arg, first_mp, arg2, caller);
9379 			return;
9380 		}
9381 
9382 		if ((mlen < sizeof (ipsec_info_t)) ||
9383 		    (mctltype != IPSEC_OUT && mctltype != IPSEC_IN) ||
9384 		    mp == NULL) {
9385 			ip_wput_nondata(NULL, q, first_mp, NULL);
9386 			return;
9387 		}
9388 		/* NDP callbacks have q_next non-NULL.  That's case #3. */
9389 		if (q->q_next == NULL) {
9390 			ip6h = (ip6_t *)mp->b_rptr;
9391 			/*
9392 			 * For a freshly-generated TCP dgram that needs IPV6
9393 			 * processing, don't call ip_wput immediately. We can
9394 			 * tell this by the ipsec_out_proc_begin. In-progress
9395 			 * IPSEC_OUT messages have proc_begin set to TRUE,
9396 			 * and we want to send all IPSEC_IN messages to
9397 			 * ip_wput() for IPsec processing or finishing.
9398 			 */
9399 			if (mctltype == IPSEC_IN ||
9400 			    IPVER(ip6h) != IPV6_VERSION ||
9401 			    io->ipsec_out_proc_begin) {
9402 				mibptr = &ip6_mib;
9403 				goto notv6;
9404 			}
9405 		}
9406 	} else if (DB_TYPE(mp) != M_DATA) {
9407 		ip_wput_nondata(NULL, q, mp, NULL);
9408 		return;
9409 	}
9410 
9411 	ip6h = (ip6_t *)mp->b_rptr;
9412 
9413 	if (IPVER(ip6h) != IPV6_VERSION) {
9414 		mibptr = &ip6_mib;
9415 		goto notv6;
9416 	}
9417 
9418 	if (q->q_next != NULL) {
9419 		ill = (ill_t *)q->q_ptr;
9420 		/*
9421 		 * We don't know if this ill will be used for IPv6
9422 		 * until the ILLF_IPV6 flag is set via SIOCSLIFNAME.
9423 		 * ipif_set_values() sets the ill_isv6 flag to true if
9424 		 * ILLF_IPV6 is set.  If the ill_isv6 flag isn't true,
9425 		 * just drop the packet.
9426 		 */
9427 		if (!ill->ill_isv6) {
9428 			ip1dbg(("ip_wput_v6: Received an IPv6 packet before "
9429 			    "ILLF_IPV6 was set\n"));
9430 			freemsg(first_mp);
9431 			return;
9432 		}
9433 		/* For uniformity do a refhold */
9434 		mutex_enter(&ill->ill_lock);
9435 		if (!ILL_CAN_LOOKUP(ill)) {
9436 			mutex_exit(&ill->ill_lock);
9437 			freemsg(first_mp);
9438 			return;
9439 		}
9440 		ill_refhold_locked(ill);
9441 		mutex_exit(&ill->ill_lock);
9442 		mibptr = ill->ill_ip6_mib;
9443 		/*
9444 		 * ill_ip6_mib is allocated by ipif_set_values() when
9445 		 * ill_isv6 is set.  Thus if ill_isv6 is true,
9446 		 * ill_ip6_mib had better not be NULL.
9447 		 */
9448 		ASSERT(mibptr != NULL);
9449 		unspec_src = 0;
9450 		BUMP_MIB(mibptr, ipv6OutRequests);
9451 		do_outrequests = B_FALSE;
9452 		zoneid = (zoneid_t)(uintptr_t)arg;
9453 	} else {
9454 		connp = (conn_t *)arg;
9455 		ASSERT(connp != NULL);
9456 		zoneid = connp->conn_zoneid;
9457 
9458 		/* is queue flow controlled? */
9459 		if ((q->q_first || connp->conn_draining) &&
9460 		    (caller == IP_WPUT)) {
9461 			/*
9462 			 * 1) TCP sends down M_CTL for detached connections.
9463 			 * 2) AH/ESP sends down M_CTL.
9464 			 *
9465 			 * We don't flow control either of the above. Only
9466 			 * UDP and others are flow controlled for which we
9467 			 * can't have a M_CTL.
9468 			 */
9469 			ASSERT(first_mp == mp);
9470 			(void) putq(q, mp);
9471 			return;
9472 		}
9473 		mibptr = &ip6_mib;
9474 		unspec_src = connp->conn_unspec_src;
9475 		do_outrequests = B_TRUE;
9476 		if (mp->b_flag & MSGHASREF) {
9477 			mp->b_flag &= ~MSGHASREF;
9478 			ASSERT(connp->conn_ulp == IPPROTO_SCTP);
9479 			SCTP_EXTRACT_IPINFO(mp, sctp_ire);
9480 			need_decref = B_TRUE;
9481 		}
9482 
9483 		/*
9484 		 * If there is a policy, try to attach an ipsec_out in
9485 		 * the front. At the end, first_mp either points to a
9486 		 * M_DATA message or IPSEC_OUT message linked to a
9487 		 * M_DATA message. We have to do it now as we might
9488 		 * lose the "conn" if we go through ip_newroute.
9489 		 */
9490 		if (!mctl_present &&
9491 		    (connp->conn_out_enforce_policy ||
9492 		    connp->conn_latch != NULL)) {
9493 			ASSERT(first_mp == mp);
9494 			/* XXX Any better way to get the protocol fast ? */
9495 			if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL,
9496 			    connp->conn_ulp)) == NULL)) {
9497 				if (need_decref)
9498 					CONN_DEC_REF(connp);
9499 				return;
9500 			} else {
9501 				ASSERT(mp->b_datap->db_type == M_CTL);
9502 				first_mp = mp;
9503 				mp = mp->b_cont;
9504 				mctl_present = B_TRUE;
9505 				io = (ipsec_out_t *)first_mp->b_rptr;
9506 			}
9507 		}
9508 	}
9509 
9510 	/* check for alignment and full IPv6 header */
9511 	if (!OK_32PTR((uchar_t *)ip6h) ||
9512 	    (mp->b_wptr - (uchar_t *)ip6h) < IPV6_HDR_LEN) {
9513 		ip0dbg(("ip_wput_v6: bad alignment or length\n"));
9514 		if (do_outrequests)
9515 			BUMP_MIB(mibptr, ipv6OutRequests);
9516 		BUMP_MIB(mibptr, ipv6OutDiscards);
9517 		freemsg(first_mp);
9518 		if (ill != NULL)
9519 			ill_refrele(ill);
9520 		if (need_decref)
9521 			CONN_DEC_REF(connp);
9522 		return;
9523 	}
9524 	v6dstp = &ip6h->ip6_dst;
9525 	cksum_request = -1;
9526 	ip6i = NULL;
9527 
9528 	/*
9529 	 * Once neighbor discovery has completed, ndp_process() will provide
9530 	 * locally generated packets for which processing can be reattempted.
9531 	 * In these cases, connp is NULL and the original zone is part of a
9532 	 * prepended ipsec_out_t.
9533 	 */
9534 	if (io != NULL) {
9535 		/*
9536 		 * When coming from icmp_input_v6, the zoneid might not match
9537 		 * for the loopback case, because inside icmp_input_v6 the
9538 		 * queue_t is a conn queue from the sending side.
9539 		 */
9540 		zoneid = io->ipsec_out_zoneid;
9541 		ASSERT(zoneid != ALL_ZONES);
9542 	}
9543 
9544 	if (ip6h->ip6_nxt == IPPROTO_RAW) {
9545 		/*
9546 		 * This is an ip6i_t header followed by an ip6_hdr.
9547 		 * Check which fields are set.
9548 		 *
9549 		 * When the packet comes from a transport we should have
9550 		 * all needed headers in the first mblk. However, when
9551 		 * going through ip_newroute*_v6 the ip6i might be in
9552 		 * a separate mblk when we return here. In that case
9553 		 * we pullup everything to ensure that extension and transport
9554 		 * headers "stay" in the first mblk.
9555 		 */
9556 		ip6i = (ip6i_t *)ip6h;
9557 		ip6i_flags = ip6i->ip6i_flags;
9558 
9559 		ASSERT((mp->b_wptr - (uchar_t *)ip6i) == sizeof (ip6i_t) ||
9560 		    ((mp->b_wptr - (uchar_t *)ip6i) >=
9561 		    sizeof (ip6i_t) + IPV6_HDR_LEN));
9562 
9563 		if ((mp->b_wptr - (uchar_t *)ip6i) == sizeof (ip6i_t)) {
9564 			if (!pullupmsg(mp, -1)) {
9565 				ip1dbg(("ip_wput_v6: pullupmsg failed\n"));
9566 				if (do_outrequests)
9567 					BUMP_MIB(mibptr, ipv6OutRequests);
9568 				BUMP_MIB(mibptr, ipv6OutDiscards);
9569 				freemsg(first_mp);
9570 				if (ill != NULL)
9571 					ill_refrele(ill);
9572 				if (need_decref)
9573 					CONN_DEC_REF(connp);
9574 				return;
9575 			}
9576 			ip6h = (ip6_t *)mp->b_rptr;
9577 			v6dstp = &ip6h->ip6_dst;
9578 			ip6i = (ip6i_t *)ip6h;
9579 		}
9580 		ip6h = (ip6_t *)&ip6i[1];
9581 
9582 		/*
9583 		 * Advance rptr past the ip6i_t to get ready for
9584 		 * transmitting the packet. However, if the packet gets
9585 		 * passed to ip_newroute*_v6 then rptr is moved back so
9586 		 * that the ip6i_t header can be inspected when the
9587 		 * packet comes back here after passing through
9588 		 * ire_add_then_send.
9589 		 */
9590 		mp->b_rptr = (uchar_t *)ip6h;
9591 
9592 		/*
9593 		 * IP6I_ATTACH_IF is set in this function when we had a
9594 		 * conn and it was either bound to the IPFF_NOFAILOVER address
9595 		 * or IPV6_BOUND_PIF was set. These options override other
9596 		 * options that set the ifindex. We come here with
9597 		 * IP6I_ATTACH_IF set when we can't find the ire and
9598 		 * ip_newroute_v6 is feeding the packet for second time.
9599 		 */
9600 		if ((ip6i->ip6i_flags & IP6I_IFINDEX) ||
9601 		    (ip6i->ip6i_flags & IP6I_ATTACH_IF)) {
9602 			ASSERT(ip6i->ip6i_ifindex != 0);
9603 			if (ill != NULL)
9604 				ill_refrele(ill);
9605 			ill = ill_lookup_on_ifindex(ip6i->ip6i_ifindex, 1,
9606 			    NULL, NULL, NULL, NULL);
9607 			if (ill == NULL) {
9608 				if (do_outrequests)
9609 					BUMP_MIB(mibptr, ipv6OutRequests);
9610 				BUMP_MIB(mibptr, ipv6OutDiscards);
9611 				ip1dbg(("ip_wput_v6: bad ifindex %d\n",
9612 				    ip6i->ip6i_ifindex));
9613 				if (need_decref)
9614 					CONN_DEC_REF(connp);
9615 				freemsg(first_mp);
9616 				return;
9617 			}
9618 			mibptr = ill->ill_ip6_mib;
9619 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
9620 				/*
9621 				 * Preserve the index so that when we return
9622 				 * from IPSEC processing, we know where to
9623 				 * send the packet.
9624 				 */
9625 				if (mctl_present) {
9626 					ASSERT(io != NULL);
9627 					io->ipsec_out_ill_index =
9628 					    ip6i->ip6i_ifindex;
9629 				}
9630 			}
9631 			if (ip6i->ip6i_flags & IP6I_ATTACH_IF) {
9632 				/*
9633 				 * This is a multipathing probe packet that has
9634 				 * been delayed in ND resolution. Drop the
9635 				 * packet for the reasons mentioned in
9636 				 * nce_queue_mp()
9637 				 */
9638 				if ((ip6i->ip6i_flags & IP6I_DROP_IFDELAYED) &&
9639 				    (ip6i->ip6i_flags & IP6I_ND_DELAYED)) {
9640 					freemsg(first_mp);
9641 					ill_refrele(ill);
9642 					if (need_decref)
9643 						CONN_DEC_REF(connp);
9644 					return;
9645 				}
9646 			}
9647 		}
9648 		if (ip6i->ip6i_flags & IP6I_VERIFY_SRC) {
9649 			cred_t *cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q));
9650 
9651 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src));
9652 			if (secpolicy_net_rawaccess(cr) != 0) {
9653 				ire = ire_route_lookup_v6(&ip6h->ip6_src,
9654 				    0, 0, (IRE_LOCAL|IRE_LOOPBACK), NULL,
9655 				    NULL, zoneid, NULL,
9656 				    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9657 				if (ire == NULL) {
9658 					if (do_outrequests)
9659 						BUMP_MIB(mibptr,
9660 						    ipv6OutRequests);
9661 					BUMP_MIB(mibptr, ipv6OutDiscards);
9662 					ip1dbg(("ip_wput_v6: bad source "
9663 					    "addr\n"));
9664 					freemsg(first_mp);
9665 					if (ill != NULL)
9666 						ill_refrele(ill);
9667 					if (need_decref)
9668 						CONN_DEC_REF(connp);
9669 					return;
9670 				}
9671 				ire_refrele(ire);
9672 			}
9673 			/* No need to verify again when using ip_newroute */
9674 			ip6i->ip6i_flags &= ~IP6I_VERIFY_SRC;
9675 		}
9676 		if (!(ip6i->ip6i_flags & IP6I_NEXTHOP)) {
9677 			/*
9678 			 * Make sure they match since ip_newroute*_v6 etc might
9679 			 * (unknown to them) inspect ip6i_nexthop when
9680 			 * they think they access ip6_dst.
9681 			 */
9682 			ip6i->ip6i_nexthop = ip6h->ip6_dst;
9683 		}
9684 		if (ip6i->ip6i_flags & IP6I_NO_ULP_CKSUM)
9685 			cksum_request = 1;
9686 		if (ip6i->ip6i_flags & IP6I_RAW_CHECKSUM)
9687 			cksum_request = ip6i->ip6i_checksum_off;
9688 		if (ip6i->ip6i_flags & IP6I_UNSPEC_SRC)
9689 			unspec_src = 1;
9690 
9691 		if (do_outrequests && ill != NULL) {
9692 			BUMP_MIB(mibptr, ipv6OutRequests);
9693 			do_outrequests = B_FALSE;
9694 		}
9695 		/*
9696 		 * Store ip6i_t info that we need after we come back
9697 		 * from IPSEC processing.
9698 		 */
9699 		if (mctl_present) {
9700 			ASSERT(io != NULL);
9701 			io->ipsec_out_unspec_src = unspec_src;
9702 		}
9703 	}
9704 	if (connp != NULL && connp->conn_dontroute)
9705 		ip6h->ip6_hops = 1;
9706 
9707 	if (IN6_IS_ADDR_MULTICAST(v6dstp))
9708 		goto ipv6multicast;
9709 
9710 	/* 1. IPV6_BOUND_PIF takes precedence over all the ifindex settings. */
9711 	if (connp != NULL && connp->conn_outgoing_pill != NULL) {
9712 		ill_t	*conn_outgoing_pill;
9713 
9714 		conn_outgoing_pill = conn_get_held_ill(connp,
9715 		    &connp->conn_outgoing_pill, &err);
9716 		if (err == ILL_LOOKUP_FAILED) {
9717 			if (ill != NULL)
9718 				ill_refrele(ill);
9719 			if (need_decref)
9720 				CONN_DEC_REF(connp);
9721 			freemsg(first_mp);
9722 			return;
9723 		}
9724 		if (conn_outgoing_pill != NULL) {
9725 			if (ill != NULL)
9726 				ill_refrele(ill);
9727 			ill = conn_outgoing_pill;
9728 			attach_if = B_TRUE;
9729 			match_flags = MATCH_IRE_ILL;
9730 			mibptr = ill->ill_ip6_mib;
9731 
9732 			/*
9733 			 * Check if we need an ire that will not be
9734 			 * looked up by anybody else i.e. HIDDEN.
9735 			 */
9736 			if (ill_is_probeonly(ill))
9737 				match_flags |= MATCH_IRE_MARK_HIDDEN;
9738 			goto send_from_ill;
9739 		}
9740 	}
9741 
9742 	/* 2. If ipc_nofailover_ill is set then use that ill. */
9743 	if (connp != NULL && connp->conn_nofailover_ill != NULL) {
9744 		ill_t	*conn_nofailover_ill;
9745 
9746 		conn_nofailover_ill = conn_get_held_ill(connp,
9747 		    &connp->conn_nofailover_ill, &err);
9748 		if (err == ILL_LOOKUP_FAILED) {
9749 			if (ill != NULL)
9750 				ill_refrele(ill);
9751 			if (need_decref)
9752 				CONN_DEC_REF(connp);
9753 			freemsg(first_mp);
9754 			return;
9755 		}
9756 		if (conn_nofailover_ill != NULL) {
9757 			if (ill != NULL)
9758 				ill_refrele(ill);
9759 			ill = conn_nofailover_ill;
9760 			attach_if = B_TRUE;
9761 			/*
9762 			 * Assumes that ipc_nofailover_ill is used only for
9763 			 * multipathing probe packets. These packets are better
9764 			 * dropped, if they are delayed in ND resolution, for
9765 			 * the reasons described in nce_queue_mp().
9766 			 * IP6I_DROP_IFDELAYED will be set later on in this
9767 			 * function for this packet.
9768 			 */
9769 			drop_if_delayed = B_TRUE;
9770 			match_flags = MATCH_IRE_ILL;
9771 			mibptr = ill->ill_ip6_mib;
9772 
9773 			/*
9774 			 * Check if we need an ire that will not be
9775 			 * looked up by anybody else i.e. HIDDEN.
9776 			 */
9777 			if (ill_is_probeonly(ill))
9778 				match_flags |= MATCH_IRE_MARK_HIDDEN;
9779 			goto send_from_ill;
9780 		}
9781 	}
9782 
9783 	/*
9784 	 * Redo 1. If we did not find an IRE_CACHE the first time, we should
9785 	 * have an ip6i_t with IP6I_ATTACH_IF if IPV6_BOUND_PIF or
9786 	 * bind to the IPIF_NOFAILOVER address was used on this endpoint.
9787 	 */
9788 	if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_ATTACH_IF)) {
9789 		ASSERT(ip6i->ip6i_ifindex != 0);
9790 		attach_if = B_TRUE;
9791 		ASSERT(ill != NULL);
9792 		match_flags = MATCH_IRE_ILL;
9793 
9794 		/*
9795 		 * Check if we need an ire that will not be
9796 		 * looked up by anybody else i.e. HIDDEN.
9797 		 */
9798 		if (ill_is_probeonly(ill))
9799 			match_flags |= MATCH_IRE_MARK_HIDDEN;
9800 		goto send_from_ill;
9801 	}
9802 
9803 	/* 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. */
9804 	if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_IFINDEX)) {
9805 		ASSERT(ill != NULL);
9806 		goto send_from_ill;
9807 	}
9808 
9809 	/*
9810 	 * 4. If q is an ill queue and (link local or multicast destination)
9811 	 *    then use that ill.
9812 	 */
9813 	if (ill != NULL && IN6_IS_ADDR_LINKLOCAL(v6dstp)) {
9814 		goto send_from_ill;
9815 	}
9816 
9817 	/* 5. If IPV6_BOUND_IF has been set use that ill. */
9818 	if (connp != NULL && connp->conn_outgoing_ill != NULL) {
9819 		ill_t	*conn_outgoing_ill;
9820 
9821 		conn_outgoing_ill = conn_get_held_ill(connp,
9822 		    &connp->conn_outgoing_ill, &err);
9823 		if (err == ILL_LOOKUP_FAILED) {
9824 			if (ill != NULL)
9825 				ill_refrele(ill);
9826 			if (need_decref)
9827 				CONN_DEC_REF(connp);
9828 			freemsg(first_mp);
9829 			return;
9830 		}
9831 		if (ill != NULL)
9832 			ill_refrele(ill);
9833 		ill = conn_outgoing_ill;
9834 		mibptr = ill->ill_ip6_mib;
9835 		goto send_from_ill;
9836 	}
9837 
9838 	/*
9839 	 * 6. For unicast: Just do an IRE lookup for the best match.
9840 	 * If we get here for a link-local address it is rather random
9841 	 * what interface we pick on a multihomed host.
9842 	 * *If* there is an IRE_CACHE (and the link-local address
9843 	 * isn't duplicated on multi links) this will find the IRE_CACHE.
9844 	 * Otherwise it will use one of the matching IRE_INTERFACE routes
9845 	 * for the link-local prefix. Hence, applications
9846 	 * *should* be encouraged to specify an outgoing interface when sending
9847 	 * to a link local address.
9848 	 */
9849 	if (connp == NULL || (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) &&
9850 	    !connp->conn_fully_bound)) {
9851 		/*
9852 		 * We cache IRE_CACHEs to avoid lookups. We don't do
9853 		 * this for the tcp global queue and listen end point
9854 		 * as it does not really have a real destination to
9855 		 * talk to.
9856 		 */
9857 		ire = ire_cache_lookup_v6(v6dstp, zoneid, MBLK_GETLABEL(mp));
9858 	} else {
9859 		/*
9860 		 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't
9861 		 * grab a lock here to check for CONDEMNED as it is okay
9862 		 * to send a packet or two with the IRE_CACHE that is going
9863 		 * away.
9864 		 */
9865 		mutex_enter(&connp->conn_lock);
9866 		ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache;
9867 		if (ire != NULL &&
9868 		    IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6dstp) &&
9869 		    !(ire->ire_marks & IRE_MARK_CONDEMNED)) {
9870 
9871 			IRE_REFHOLD(ire);
9872 			mutex_exit(&connp->conn_lock);
9873 
9874 		} else {
9875 			boolean_t cached = B_FALSE;
9876 
9877 			connp->conn_ire_cache = NULL;
9878 			mutex_exit(&connp->conn_lock);
9879 			/* Release the old ire */
9880 			if (ire != NULL && sctp_ire == NULL)
9881 				IRE_REFRELE_NOTR(ire);
9882 
9883 			ire = ire_cache_lookup_v6(v6dstp, zoneid,
9884 			    MBLK_GETLABEL(mp));
9885 			if (ire != NULL) {
9886 				IRE_REFHOLD_NOTR(ire);
9887 
9888 				mutex_enter(&connp->conn_lock);
9889 				if (!(connp->conn_state_flags & CONN_CLOSING) &&
9890 				    (connp->conn_ire_cache == NULL)) {
9891 					rw_enter(&ire->ire_bucket->irb_lock,
9892 					    RW_READER);
9893 					if (!(ire->ire_marks &
9894 					    IRE_MARK_CONDEMNED)) {
9895 						connp->conn_ire_cache = ire;
9896 						cached = B_TRUE;
9897 					}
9898 					rw_exit(&ire->ire_bucket->irb_lock);
9899 				}
9900 				mutex_exit(&connp->conn_lock);
9901 
9902 				/*
9903 				 * We can continue to use the ire but since it
9904 				 * was not cached, we should drop the extra
9905 				 * reference.
9906 				 */
9907 				if (!cached)
9908 					IRE_REFRELE_NOTR(ire);
9909 			}
9910 		}
9911 	}
9912 
9913 	if (ire != NULL) {
9914 		if (do_outrequests) {
9915 			/* Handle IRE_LOCAL's that might appear here */
9916 			if (ire->ire_type == IRE_CACHE) {
9917 				mibptr = ((ill_t *)ire->ire_stq->q_ptr)->
9918 				    ill_ip6_mib;
9919 			} else {
9920 				mibptr = ire->ire_ipif->ipif_ill->ill_ip6_mib;
9921 			}
9922 			BUMP_MIB(mibptr, ipv6OutRequests);
9923 		}
9924 		ASSERT(!attach_if);
9925 
9926 		/*
9927 		 * Check if the ire has the RTF_MULTIRT flag, inherited
9928 		 * from an IRE_OFFSUBNET ire entry in ip_newroute().
9929 		 */
9930 		if (ire->ire_flags & RTF_MULTIRT) {
9931 			/*
9932 			 * Force hop limit of multirouted packets if required.
9933 			 * The hop limit of such packets is bounded by the
9934 			 * ip_multirt_ttl ndd variable.
9935 			 * NDP packets must have a hop limit of 255; don't
9936 			 * change the hop limit in that case.
9937 			 */
9938 			if ((ip_multirt_ttl > 0) &&
9939 			    (ip6h->ip6_hops > ip_multirt_ttl) &&
9940 			    (ip6h->ip6_hops != IPV6_MAX_HOPS)) {
9941 				if (ip_debug > 3) {
9942 					ip2dbg(("ip_wput_v6: forcing multirt "
9943 					    "hop limit to %d (was %d) ",
9944 					    ip_multirt_ttl, ip6h->ip6_hops));
9945 					pr_addr_dbg("v6dst %s\n", AF_INET6,
9946 					    &ire->ire_addr_v6);
9947 				}
9948 				ip6h->ip6_hops = ip_multirt_ttl;
9949 			}
9950 
9951 			/*
9952 			 * We look at this point if there are pending
9953 			 * unresolved routes. ire_multirt_need_resolve_v6()
9954 			 * checks in O(n) that all IRE_OFFSUBNET ire
9955 			 * entries for the packet's destination and
9956 			 * flagged RTF_MULTIRT are currently resolved.
9957 			 * If some remain unresolved, we do a copy
9958 			 * of the current message. It will be used
9959 			 * to initiate additional route resolutions.
9960 			 */
9961 			multirt_need_resolve =
9962 			    ire_multirt_need_resolve_v6(&ire->ire_addr_v6,
9963 				MBLK_GETLABEL(first_mp));
9964 			ip2dbg(("ip_wput_v6: ire %p, "
9965 			    "multirt_need_resolve %d, first_mp %p\n",
9966 			    (void *)ire, multirt_need_resolve,
9967 			    (void *)first_mp));
9968 			if (multirt_need_resolve) {
9969 				copy_mp = copymsg(first_mp);
9970 				if (copy_mp != NULL) {
9971 					MULTIRT_DEBUG_TAG(copy_mp);
9972 				}
9973 			}
9974 		}
9975 		ip_wput_ire_v6(q, first_mp, ire, unspec_src, cksum_request,
9976 		    connp, caller, 0, ip6i_flags, zoneid);
9977 		if (need_decref) {
9978 			CONN_DEC_REF(connp);
9979 			connp = NULL;
9980 		}
9981 		IRE_REFRELE(ire);
9982 
9983 		/*
9984 		 * Try to resolve another multiroute if
9985 		 * ire_multirt_need_resolve_v6() deemed it necessary.
9986 		 * copy_mp will be consumed (sent or freed) by
9987 		 * ip_newroute_v6().
9988 		 */
9989 		if (copy_mp != NULL) {
9990 			if (mctl_present) {
9991 				ip6h = (ip6_t *)copy_mp->b_cont->b_rptr;
9992 			} else {
9993 				ip6h = (ip6_t *)copy_mp->b_rptr;
9994 			}
9995 			ip_newroute_v6(q, copy_mp, &ip6h->ip6_dst,
9996 			    &ip6h->ip6_src, NULL, zoneid);
9997 		}
9998 		if (ill != NULL)
9999 			ill_refrele(ill);
10000 		return;
10001 	}
10002 
10003 	/*
10004 	 * No full IRE for this destination.  Send it to
10005 	 * ip_newroute_v6 to see if anything else matches.
10006 	 * Mark this packet as having originated on this
10007 	 * machine.
10008 	 * Update rptr if there was an ip6i_t header.
10009 	 */
10010 	mp->b_prev = NULL;
10011 	mp->b_next = NULL;
10012 	if (ip6i != NULL)
10013 		mp->b_rptr -= sizeof (ip6i_t);
10014 
10015 	if (unspec_src) {
10016 		if (ip6i == NULL) {
10017 			/*
10018 			 * Add ip6i_t header to carry unspec_src
10019 			 * until the packet comes back in ip_wput_v6.
10020 			 */
10021 			mp = ip_add_info_v6(mp, NULL, v6dstp);
10022 			if (mp == NULL) {
10023 				if (do_outrequests)
10024 					BUMP_MIB(mibptr, ipv6OutRequests);
10025 				BUMP_MIB(mibptr, ipv6OutDiscards);
10026 				if (mctl_present)
10027 					freeb(first_mp);
10028 				if (ill != NULL)
10029 					ill_refrele(ill);
10030 				if (need_decref)
10031 					CONN_DEC_REF(connp);
10032 				return;
10033 			}
10034 			ip6i = (ip6i_t *)mp->b_rptr;
10035 
10036 			if (mctl_present) {
10037 				ASSERT(first_mp != mp);
10038 				first_mp->b_cont = mp;
10039 			} else {
10040 				first_mp = mp;
10041 			}
10042 
10043 			if ((mp->b_wptr - (uchar_t *)ip6i) ==
10044 			    sizeof (ip6i_t)) {
10045 				/*
10046 				 * ndp_resolver called from ip_newroute_v6
10047 				 * expects pulled up message.
10048 				 */
10049 				if (!pullupmsg(mp, -1)) {
10050 					ip1dbg(("ip_wput_v6: pullupmsg"
10051 					    " failed\n"));
10052 					if (do_outrequests) {
10053 						BUMP_MIB(mibptr,
10054 						    ipv6OutRequests);
10055 					}
10056 					BUMP_MIB(mibptr, ipv6OutDiscards);
10057 					freemsg(first_mp);
10058 					if (ill != NULL)
10059 						ill_refrele(ill);
10060 					if (need_decref)
10061 						CONN_DEC_REF(connp);
10062 					return;
10063 				}
10064 				ip6i = (ip6i_t *)mp->b_rptr;
10065 			}
10066 			ip6h = (ip6_t *)&ip6i[1];
10067 			v6dstp = &ip6h->ip6_dst;
10068 		}
10069 		ip6i->ip6i_flags |= IP6I_UNSPEC_SRC;
10070 		if (mctl_present) {
10071 			ASSERT(io != NULL);
10072 			io->ipsec_out_unspec_src = unspec_src;
10073 		}
10074 	}
10075 	if (do_outrequests)
10076 		BUMP_MIB(mibptr, ipv6OutRequests);
10077 	if (need_decref)
10078 		CONN_DEC_REF(connp);
10079 	ip_newroute_v6(q, first_mp, v6dstp, &ip6h->ip6_src, NULL, zoneid);
10080 	if (ill != NULL)
10081 		ill_refrele(ill);
10082 	return;
10083 
10084 
10085 	/*
10086 	 * Handle multicast packets with or without an conn.
10087 	 * Assumes that the transports set ip6_hops taking
10088 	 * IPV6_MULTICAST_HOPS (and the other ways to set the hoplimit)
10089 	 * into account.
10090 	 */
10091 ipv6multicast:
10092 	ip2dbg(("ip_wput_v6: multicast\n"));
10093 
10094 	/*
10095 	 * 1. IPV6_BOUND_PIF takes precedence over all the ifindex settings
10096 	 * 2. If conn_nofailover_ill is set then use that ill.
10097 	 *
10098 	 * Hold the conn_lock till we refhold the ill of interest that is
10099 	 * pointed to from the conn. Since we cannot do an ill/ipif_refrele
10100 	 * while holding any locks, postpone the refrele until after the
10101 	 * conn_lock is dropped.
10102 	 */
10103 	if (connp != NULL) {
10104 		mutex_enter(&connp->conn_lock);
10105 		conn_lock_held = B_TRUE;
10106 	} else {
10107 		conn_lock_held = B_FALSE;
10108 	}
10109 	if (connp != NULL && connp->conn_outgoing_pill != NULL) {
10110 		err = ill_check_and_refhold(connp->conn_outgoing_pill);
10111 		if (err == ILL_LOOKUP_FAILED) {
10112 			ip1dbg(("ip_output_v6: multicast"
10113 			    " conn_outgoing_pill no ipif\n"));
10114 multicast_discard:
10115 			ASSERT(saved_ill == NULL);
10116 			if (conn_lock_held)
10117 				mutex_exit(&connp->conn_lock);
10118 			if (ill != NULL)
10119 				ill_refrele(ill);
10120 			freemsg(first_mp);
10121 			if (do_outrequests)
10122 				BUMP_MIB(mibptr, ipv6OutDiscards);
10123 			if (need_decref)
10124 				CONN_DEC_REF(connp);
10125 			return;
10126 		}
10127 		saved_ill = ill;
10128 		ill = connp->conn_outgoing_pill;
10129 		attach_if = B_TRUE;
10130 		match_flags = MATCH_IRE_ILL;
10131 		mibptr = ill->ill_ip6_mib;
10132 
10133 		/*
10134 		 * Check if we need an ire that will not be
10135 		 * looked up by anybody else i.e. HIDDEN.
10136 		 */
10137 		if (ill_is_probeonly(ill))
10138 			match_flags |= MATCH_IRE_MARK_HIDDEN;
10139 	} else if (connp != NULL && connp->conn_nofailover_ill != NULL) {
10140 		err = ill_check_and_refhold(connp->conn_nofailover_ill);
10141 		if (err == ILL_LOOKUP_FAILED) {
10142 			ip1dbg(("ip_output_v6: multicast"
10143 			    " conn_nofailover_ill no ipif\n"));
10144 			goto multicast_discard;
10145 		}
10146 		saved_ill = ill;
10147 		ill = connp->conn_nofailover_ill;
10148 		attach_if = B_TRUE;
10149 		match_flags = MATCH_IRE_ILL;
10150 
10151 		/*
10152 		 * Check if we need an ire that will not be
10153 		 * looked up by anybody else i.e. HIDDEN.
10154 		 */
10155 		if (ill_is_probeonly(ill))
10156 			match_flags |= MATCH_IRE_MARK_HIDDEN;
10157 	} else if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_ATTACH_IF)) {
10158 		/*
10159 		 * Redo 1. If we did not find an IRE_CACHE the first time,
10160 		 * we should have an ip6i_t with IP6I_ATTACH_IF if
10161 		 * IPV6_BOUND_PIF or bind to the IPIF_NOFAILOVER address was
10162 		 * used on this endpoint.
10163 		 */
10164 		ASSERT(ip6i->ip6i_ifindex != 0);
10165 		attach_if = B_TRUE;
10166 		ASSERT(ill != NULL);
10167 		match_flags = MATCH_IRE_ILL;
10168 
10169 		/*
10170 		 * Check if we need an ire that will not be
10171 		 * looked up by anybody else i.e. HIDDEN.
10172 		 */
10173 		if (ill_is_probeonly(ill))
10174 			match_flags |= MATCH_IRE_MARK_HIDDEN;
10175 	} else if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_IFINDEX)) {
10176 		/* 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. */
10177 
10178 		ASSERT(ill != NULL);
10179 	} else if (ill != NULL) {
10180 		/*
10181 		 * 4. If q is an ill queue and (link local or multicast
10182 		 * destination) then use that ill.
10183 		 * We don't need the ipif initialization here.
10184 		 * This useless assert below is just to prevent lint from
10185 		 * reporting a null body if statement.
10186 		 */
10187 		ASSERT(ill != NULL);
10188 	} else if (connp != NULL) {
10189 		/*
10190 		 * 5. If IPV6_BOUND_IF has been set use that ill.
10191 		 *
10192 		 * 6. For multicast: if IPV6_MULTICAST_IF has been set use it.
10193 		 * Otherwise look for the best IRE match for the unspecified
10194 		 * group to determine the ill.
10195 		 *
10196 		 * conn_multicast_ill is used for only IPv6 packets.
10197 		 * conn_multicast_ipif is used for only IPv4 packets.
10198 		 * Thus a PF_INET6 socket send both IPv4 and IPv6
10199 		 * multicast packets using different IP*_MULTICAST_IF
10200 		 * interfaces.
10201 		 */
10202 		if (connp->conn_outgoing_ill != NULL) {
10203 			err = ill_check_and_refhold(connp->conn_outgoing_ill);
10204 			if (err == ILL_LOOKUP_FAILED) {
10205 				ip1dbg(("ip_output_v6: multicast"
10206 				    " conn_outgoing_ill no ipif\n"));
10207 				goto multicast_discard;
10208 			}
10209 			ill = connp->conn_outgoing_ill;
10210 		} else if (connp->conn_multicast_ill != NULL) {
10211 			err = ill_check_and_refhold(connp->conn_multicast_ill);
10212 			if (err == ILL_LOOKUP_FAILED) {
10213 				ip1dbg(("ip_output_v6: multicast"
10214 				    " conn_multicast_ill no ipif\n"));
10215 				goto multicast_discard;
10216 			}
10217 			ill = connp->conn_multicast_ill;
10218 		} else {
10219 			mutex_exit(&connp->conn_lock);
10220 			conn_lock_held = B_FALSE;
10221 			ipif = ipif_lookup_group_v6(v6dstp, zoneid);
10222 			if (ipif == NULL) {
10223 				ip1dbg(("ip_output_v6: multicast no ipif\n"));
10224 				goto multicast_discard;
10225 			}
10226 			/*
10227 			 * We have a ref to this ipif, so we can safely
10228 			 * access ipif_ill.
10229 			 */
10230 			ill = ipif->ipif_ill;
10231 			mutex_enter(&ill->ill_lock);
10232 			if (!ILL_CAN_LOOKUP(ill)) {
10233 				mutex_exit(&ill->ill_lock);
10234 				ipif_refrele(ipif);
10235 				ill = NULL;
10236 				ip1dbg(("ip_output_v6: multicast no ipif\n"));
10237 				goto multicast_discard;
10238 			}
10239 			ill_refhold_locked(ill);
10240 			mutex_exit(&ill->ill_lock);
10241 			ipif_refrele(ipif);
10242 			/*
10243 			 * Save binding until IPV6_MULTICAST_IF
10244 			 * changes it
10245 			 */
10246 			mutex_enter(&connp->conn_lock);
10247 			connp->conn_multicast_ill = ill;
10248 			connp->conn_orig_multicast_ifindex =
10249 			    ill->ill_phyint->phyint_ifindex;
10250 			mutex_exit(&connp->conn_lock);
10251 		}
10252 	}
10253 	if (conn_lock_held)
10254 		mutex_exit(&connp->conn_lock);
10255 
10256 	if (saved_ill != NULL)
10257 		ill_refrele(saved_ill);
10258 
10259 	ASSERT(ill != NULL);
10260 	/*
10261 	 * For multicast loopback interfaces replace the multicast address
10262 	 * with a unicast address for the ire lookup.
10263 	 */
10264 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)
10265 		v6dstp = &ill->ill_ipif->ipif_v6lcl_addr;
10266 
10267 	mibptr = ill->ill_ip6_mib;
10268 	if (do_outrequests) {
10269 		BUMP_MIB(mibptr, ipv6OutRequests);
10270 		do_outrequests = B_FALSE;
10271 	}
10272 	BUMP_MIB(mibptr, ipv6OutMcastPkts);
10273 
10274 	/*
10275 	 * As we may lose the conn by the time we reach ip_wput_ire_v6
10276 	 * we copy conn_multicast_loop and conn_dontroute on to an
10277 	 * ipsec_out. In case if this datagram goes out secure,
10278 	 * we need the ill_index also. Copy that also into the
10279 	 * ipsec_out.
10280 	 */
10281 	if (mctl_present) {
10282 		io = (ipsec_out_t *)first_mp->b_rptr;
10283 		ASSERT(first_mp->b_datap->db_type == M_CTL);
10284 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
10285 	} else {
10286 		ASSERT(mp == first_mp);
10287 		if ((first_mp = ipsec_alloc_ipsec_out()) == NULL) {
10288 			BUMP_MIB(mibptr, ipv6OutDiscards);
10289 			freemsg(mp);
10290 			if (ill != NULL)
10291 				ill_refrele(ill);
10292 			if (need_decref)
10293 				CONN_DEC_REF(connp);
10294 			return;
10295 		}
10296 		io = (ipsec_out_t *)first_mp->b_rptr;
10297 		/* This is not a secure packet */
10298 		io->ipsec_out_secure = B_FALSE;
10299 		io->ipsec_out_use_global_policy = B_TRUE;
10300 		io->ipsec_out_zoneid =
10301 		    (zoneid != ALL_ZONES ? zoneid : GLOBAL_ZONEID);
10302 		first_mp->b_cont = mp;
10303 		mctl_present = B_TRUE;
10304 	}
10305 	io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex;
10306 	io->ipsec_out_unspec_src = unspec_src;
10307 	if (connp != NULL)
10308 		io->ipsec_out_dontroute = connp->conn_dontroute;
10309 
10310 send_from_ill:
10311 	ASSERT(ill != NULL);
10312 	ASSERT(mibptr == ill->ill_ip6_mib);
10313 	if (do_outrequests) {
10314 		BUMP_MIB(mibptr, ipv6OutRequests);
10315 		do_outrequests = B_FALSE;
10316 	}
10317 
10318 	if (io != NULL)
10319 		io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex;
10320 
10321 	/*
10322 	 * When a specific ill is specified (using IPV6_PKTINFO,
10323 	 * IPV6_MULTICAST_IF, or IPV6_BOUND_IF) we will only match
10324 	 * on routing entries (ftable and ctable) that have a matching
10325 	 * ire->ire_ipif->ipif_ill. Thus this can only be used
10326 	 * for destinations that are on-link for the specific ill
10327 	 * and that can appear on multiple links. Thus it is useful
10328 	 * for multicast destinations, link-local destinations, and
10329 	 * at some point perhaps for site-local destinations (if the
10330 	 * node sits at a site boundary).
10331 	 * We create the cache entries in the regular ctable since
10332 	 * it can not "confuse" things for other destinations.
10333 	 * table.
10334 	 *
10335 	 * NOTE : conn_ire_cache is not used for caching ire_ctable_lookups.
10336 	 *	  It is used only when ire_cache_lookup is used above.
10337 	 */
10338 	ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ill->ill_ipif,
10339 	    zoneid, MBLK_GETLABEL(mp), match_flags);
10340 	if (ire != NULL) {
10341 		/*
10342 		 * Check if the ire has the RTF_MULTIRT flag, inherited
10343 		 * from an IRE_OFFSUBNET ire entry in ip_newroute().
10344 		 */
10345 		if (ire->ire_flags & RTF_MULTIRT) {
10346 			/*
10347 			 * Force hop limit of multirouted packets if required.
10348 			 * The hop limit of such packets is bounded by the
10349 			 * ip_multirt_ttl ndd variable.
10350 			 * NDP packets must have a hop limit of 255; don't
10351 			 * change the hop limit in that case.
10352 			 */
10353 			if ((ip_multirt_ttl > 0) &&
10354 			    (ip6h->ip6_hops > ip_multirt_ttl) &&
10355 			    (ip6h->ip6_hops != IPV6_MAX_HOPS)) {
10356 				if (ip_debug > 3) {
10357 					ip2dbg(("ip_wput_v6: forcing multirt "
10358 					    "hop limit to %d (was %d) ",
10359 					    ip_multirt_ttl, ip6h->ip6_hops));
10360 					pr_addr_dbg("v6dst %s\n", AF_INET6,
10361 					    &ire->ire_addr_v6);
10362 				}
10363 				ip6h->ip6_hops = ip_multirt_ttl;
10364 			}
10365 
10366 			/*
10367 			 * We look at this point if there are pending
10368 			 * unresolved routes. ire_multirt_need_resolve_v6()
10369 			 * checks in O(n) that all IRE_OFFSUBNET ire
10370 			 * entries for the packet's destination and
10371 			 * flagged RTF_MULTIRT are currently resolved.
10372 			 * If some remain unresolved, we make a copy
10373 			 * of the current message. It will be used
10374 			 * to initiate additional route resolutions.
10375 			 */
10376 			multirt_need_resolve =
10377 			    ire_multirt_need_resolve_v6(&ire->ire_addr_v6,
10378 				MBLK_GETLABEL(first_mp));
10379 			ip2dbg(("ip_wput_v6[send_from_ill]: ire %p, "
10380 			    "multirt_need_resolve %d, first_mp %p\n",
10381 			    (void *)ire, multirt_need_resolve,
10382 			    (void *)first_mp));
10383 			if (multirt_need_resolve) {
10384 				copy_mp = copymsg(first_mp);
10385 				if (copy_mp != NULL) {
10386 					MULTIRT_DEBUG_TAG(copy_mp);
10387 				}
10388 			}
10389 		}
10390 
10391 		ip1dbg(("ip_wput_v6: send on %s, ire = %p, ill index = %d\n",
10392 		    ill->ill_name, (void *)ire,
10393 		    ill->ill_phyint->phyint_ifindex));
10394 		ip_wput_ire_v6(q, first_mp, ire, unspec_src, cksum_request,
10395 		    connp, caller,
10396 		    (attach_if ? ill->ill_phyint->phyint_ifindex : 0),
10397 		    ip6i_flags, zoneid);
10398 		ire_refrele(ire);
10399 		if (need_decref) {
10400 			CONN_DEC_REF(connp);
10401 			connp = NULL;
10402 		}
10403 
10404 		/*
10405 		 * Try to resolve another multiroute if
10406 		 * ire_multirt_need_resolve_v6() deemed it necessary.
10407 		 * copy_mp will be consumed (sent or freed) by
10408 		 * ip_newroute_[ipif_]v6().
10409 		 */
10410 		if (copy_mp != NULL) {
10411 			if (mctl_present) {
10412 				ip6h = (ip6_t *)copy_mp->b_cont->b_rptr;
10413 			} else {
10414 				ip6h = (ip6_t *)copy_mp->b_rptr;
10415 			}
10416 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
10417 				ipif = ipif_lookup_group_v6(&ip6h->ip6_dst,
10418 				    zoneid);
10419 				if (ipif == NULL) {
10420 					ip1dbg(("ip_wput_v6: No ipif for "
10421 					    "multicast\n"));
10422 					MULTIRT_DEBUG_UNTAG(copy_mp);
10423 					freemsg(copy_mp);
10424 					return;
10425 				}
10426 				ip_newroute_ipif_v6(q, copy_mp, ipif,
10427 				    ip6h->ip6_dst, unspec_src, zoneid);
10428 				ipif_refrele(ipif);
10429 			} else {
10430 				ip_newroute_v6(q, copy_mp, &ip6h->ip6_dst,
10431 				    &ip6h->ip6_src, ill, zoneid);
10432 			}
10433 		}
10434 		ill_refrele(ill);
10435 		return;
10436 	}
10437 	if (need_decref) {
10438 		CONN_DEC_REF(connp);
10439 		connp = NULL;
10440 	}
10441 
10442 	/* Update rptr if there was an ip6i_t header. */
10443 	if (ip6i != NULL)
10444 		mp->b_rptr -= sizeof (ip6i_t);
10445 	if (unspec_src || attach_if) {
10446 		if (ip6i == NULL) {
10447 			/*
10448 			 * Add ip6i_t header to carry unspec_src
10449 			 * or attach_if until the packet comes back in
10450 			 * ip_wput_v6.
10451 			 */
10452 			if (mctl_present) {
10453 				first_mp->b_cont =
10454 				    ip_add_info_v6(mp, NULL, v6dstp);
10455 				mp = first_mp->b_cont;
10456 				if (mp == NULL)
10457 					freeb(first_mp);
10458 			} else {
10459 				first_mp = mp = ip_add_info_v6(mp, NULL,
10460 				    v6dstp);
10461 			}
10462 			if (mp == NULL) {
10463 				BUMP_MIB(mibptr, ipv6OutDiscards);
10464 				ill_refrele(ill);
10465 				return;
10466 			}
10467 			ip6i = (ip6i_t *)mp->b_rptr;
10468 			if ((mp->b_wptr - (uchar_t *)ip6i) ==
10469 			    sizeof (ip6i_t)) {
10470 				/*
10471 				 * ndp_resolver called from ip_newroute_v6
10472 				 * expects a pulled up message.
10473 				 */
10474 				if (!pullupmsg(mp, -1)) {
10475 					ip1dbg(("ip_wput_v6: pullupmsg"
10476 					    " failed\n"));
10477 					BUMP_MIB(mibptr, ipv6OutDiscards);
10478 					freemsg(first_mp);
10479 					return;
10480 				}
10481 				ip6i = (ip6i_t *)mp->b_rptr;
10482 			}
10483 			ip6h = (ip6_t *)&ip6i[1];
10484 			v6dstp = &ip6h->ip6_dst;
10485 		}
10486 		if (unspec_src)
10487 			ip6i->ip6i_flags |= IP6I_UNSPEC_SRC;
10488 		if (attach_if) {
10489 			/*
10490 			 * Bind to nofailover/BOUND_PIF overrides ifindex.
10491 			 */
10492 			ip6i->ip6i_flags |= IP6I_ATTACH_IF;
10493 			ip6i->ip6i_flags &= ~IP6I_IFINDEX;
10494 			ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex;
10495 			if (drop_if_delayed) {
10496 				/* This is a multipathing probe packet */
10497 				ip6i->ip6i_flags |= IP6I_DROP_IFDELAYED;
10498 			}
10499 		}
10500 		if (mctl_present) {
10501 			ASSERT(io != NULL);
10502 			io->ipsec_out_unspec_src = unspec_src;
10503 		}
10504 	}
10505 	if (IN6_IS_ADDR_MULTICAST(v6dstp)) {
10506 		ip_newroute_ipif_v6(q, first_mp, ill->ill_ipif, *v6dstp,
10507 		    unspec_src, zoneid);
10508 	} else {
10509 		ip_newroute_v6(q, first_mp, v6dstp, &ip6h->ip6_src, ill,
10510 		    zoneid);
10511 	}
10512 	ill_refrele(ill);
10513 	return;
10514 
10515 notv6:
10516 	/*
10517 	 * XXX implement a IPv4 and IPv6 packet counter per conn and
10518 	 * switch when ratio exceeds e.g. 10:1
10519 	 */
10520 	if (q->q_next == NULL) {
10521 		connp = Q_TO_CONN(q);
10522 
10523 		if (IPCL_IS_TCP(connp)) {
10524 			/* change conn_send for the tcp_v4_connections */
10525 			connp->conn_send = ip_output;
10526 		} else if (connp->conn_ulp == IPPROTO_SCTP) {
10527 			/* The 'q' is the default SCTP queue */
10528 			connp = (conn_t *)arg;
10529 		} else {
10530 			ip_setqinfo(RD(q), IPV4_MINOR, B_TRUE);
10531 		}
10532 	}
10533 	BUMP_MIB(mibptr, ipv6OutIPv4);
10534 	(void) ip_output(arg, first_mp, arg2, caller);
10535 	if (ill != NULL)
10536 		ill_refrele(ill);
10537 }
10538 
10539 /*
10540  * If this is a conn_t queue, then we pass in the conn. This includes the
10541  * zoneid.
10542  * Otherwise, this is a message for an ill_t queue,
10543  * in which case we use the global zoneid since those are all part of
10544  * the global zone.
10545  */
10546 static void
10547 ip_wput_v6(queue_t *q, mblk_t *mp)
10548 {
10549 	if (CONN_Q(q))
10550 		ip_output_v6(Q_TO_CONN(q), mp, q, IP_WPUT);
10551 	else
10552 		ip_output_v6(GLOBAL_ZONEID, mp, q, IP_WPUT);
10553 }
10554 
10555 static void
10556 ipsec_out_attach_if(ipsec_out_t *io, int attach_index)
10557 {
10558 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
10559 	io->ipsec_out_attach_if = B_TRUE;
10560 	io->ipsec_out_ill_index = attach_index;
10561 }
10562 
10563 /*
10564  * NULL send-to queue - packet is to be delivered locally.
10565  */
10566 void
10567 ip_wput_local_v6(queue_t *q, ill_t *ill, ip6_t *ip6h, mblk_t *first_mp,
10568     ire_t *ire, int fanout_flags)
10569 {
10570 	uint32_t	ports;
10571 	mblk_t		*mp = first_mp, *first_mp1;
10572 	boolean_t	mctl_present;
10573 	uint8_t		nexthdr;
10574 	uint16_t	hdr_length;
10575 	ipsec_out_t	*io;
10576 	mib2_ipv6IfStatsEntry_t	*mibptr;
10577 	ilm_t		*ilm;
10578 	uint_t	nexthdr_offset;
10579 
10580 	if (DB_TYPE(mp) == M_CTL) {
10581 		io = (ipsec_out_t *)mp->b_rptr;
10582 		if (!io->ipsec_out_secure) {
10583 			mp = mp->b_cont;
10584 			freeb(first_mp);
10585 			first_mp = mp;
10586 			mctl_present = B_FALSE;
10587 		} else {
10588 			mctl_present = B_TRUE;
10589 			mp = first_mp->b_cont;
10590 			ipsec_out_to_in(first_mp);
10591 		}
10592 	} else {
10593 		mctl_present = B_FALSE;
10594 	}
10595 
10596 	DTRACE_PROBE4(ip6__loopback__in__start,
10597 	    ill_t *, ill, ill_t *, NULL,
10598 	    ip6_t *, ip6h, mblk_t *, first_mp);
10599 
10600 	FW_HOOKS6(ip6_loopback_in_event, ipv6firewall_loopback_in,
10601 	    ill, NULL, ip6h, first_mp, mp);
10602 
10603 	DTRACE_PROBE1(ip6__loopback__in__end, mblk_t *, first_mp);
10604 
10605 	if (first_mp == NULL)
10606 		return;
10607 
10608 	nexthdr = ip6h->ip6_nxt;
10609 	mibptr = ill->ill_ip6_mib;
10610 
10611 	/* Fastpath */
10612 	switch (nexthdr) {
10613 	case IPPROTO_TCP:
10614 	case IPPROTO_UDP:
10615 	case IPPROTO_ICMPV6:
10616 	case IPPROTO_SCTP:
10617 		hdr_length = IPV6_HDR_LEN;
10618 		nexthdr_offset = (uint_t)((uchar_t *)&ip6h->ip6_nxt -
10619 		    (uchar_t *)ip6h);
10620 		break;
10621 	default: {
10622 		uint8_t	*nexthdrp;
10623 
10624 		if (!ip_hdr_length_nexthdr_v6(mp, ip6h,
10625 		    &hdr_length, &nexthdrp)) {
10626 			/* Malformed packet */
10627 			BUMP_MIB(mibptr, ipv6OutDiscards);
10628 			freemsg(first_mp);
10629 			return;
10630 		}
10631 		nexthdr = *nexthdrp;
10632 		nexthdr_offset = nexthdrp - (uint8_t *)ip6h;
10633 		break;
10634 	}
10635 	}
10636 
10637 	UPDATE_OB_PKT_COUNT(ire);
10638 	ire->ire_last_used_time = lbolt;
10639 
10640 	/*
10641 	 * Remove reacability confirmation bit from version field
10642 	 * before looping back the packet.
10643 	 */
10644 	if (ip6h->ip6_vcf & IP_FORWARD_PROG) {
10645 		ip6h->ip6_vcf &= ~IP_FORWARD_PROG;
10646 	}
10647 
10648 	switch (nexthdr) {
10649 		case IPPROTO_TCP:
10650 			if (DB_TYPE(mp) == M_DATA) {
10651 				/*
10652 				 * M_DATA mblk, so init mblk (chain) for
10653 				 * no struio().
10654 				 */
10655 				mblk_t  *mp1 = mp;
10656 
10657 				do {
10658 					mp1->b_datap->db_struioflag = 0;
10659 				} while ((mp1 = mp1->b_cont) != NULL);
10660 			}
10661 			ports = *(uint32_t *)(mp->b_rptr + hdr_length +
10662 			    TCP_PORTS_OFFSET);
10663 			ip_fanout_tcp_v6(q, first_mp, ip6h, ill, ill,
10664 			    fanout_flags|IP_FF_SEND_ICMP|IP_FF_SYN_ADDIRE|
10665 			    IP_FF_IP6INFO|IP6_NO_IPPOLICY|IP_FF_LOOPBACK,
10666 			    hdr_length, mctl_present, ire->ire_zoneid);
10667 			return;
10668 
10669 		case IPPROTO_UDP:
10670 			ports = *(uint32_t *)(mp->b_rptr + hdr_length +
10671 			    UDP_PORTS_OFFSET);
10672 			ip_fanout_udp_v6(q, first_mp, ip6h, ports, ill, ill,
10673 			    fanout_flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO|
10674 			    IP6_NO_IPPOLICY, mctl_present, ire->ire_zoneid);
10675 			return;
10676 
10677 		case IPPROTO_SCTP:
10678 		{
10679 			uint_t	ipif_seqid = ire->ire_ipif->ipif_seqid;
10680 
10681 			ports = *(uint32_t *)(mp->b_rptr + hdr_length);
10682 			ip_fanout_sctp(mp, ill, (ipha_t *)ip6h, ports,
10683 			    fanout_flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO,
10684 			    mctl_present, IP6_NO_IPPOLICY, ipif_seqid,
10685 			    ire->ire_zoneid);
10686 			return;
10687 		}
10688 		case IPPROTO_ICMPV6: {
10689 			icmp6_t *icmp6;
10690 
10691 			/* check for full IPv6+ICMPv6 header */
10692 			if ((mp->b_wptr - mp->b_rptr) <
10693 			    (hdr_length + ICMP6_MINLEN)) {
10694 				if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) {
10695 					ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg"
10696 					    " failed\n"));
10697 					BUMP_MIB(mibptr, ipv6OutDiscards);
10698 					freemsg(first_mp);
10699 					return;
10700 				}
10701 				ip6h = (ip6_t *)mp->b_rptr;
10702 			}
10703 			icmp6 = (icmp6_t *)((uchar_t *)ip6h + hdr_length);
10704 
10705 			/* Update output mib stats */
10706 			icmp_update_out_mib_v6(ill, icmp6);
10707 
10708 			/* Check variable for testing applications */
10709 			if (ipv6_drop_inbound_icmpv6) {
10710 				freemsg(first_mp);
10711 				return;
10712 			}
10713 			/*
10714 			 * Assume that there is always at least one conn for
10715 			 * ICMPv6 (in.ndpd) i.e. don't optimize the case
10716 			 * where there is no conn.
10717 			 */
10718 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) &&
10719 			    !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) {
10720 				/*
10721 				 * In the multicast case, applications may have
10722 				 * joined the group from different zones, so we
10723 				 * need to deliver the packet to each of them.
10724 				 * Loop through the multicast memberships
10725 				 * structures (ilm) on the receive ill and send
10726 				 * a copy of the packet up each matching one.
10727 				 * However, we don't do this for multicasts sent
10728 				 * on the loopback interface (PHYI_LOOPBACK flag
10729 				 * set) as they must stay in the sender's zone.
10730 				 */
10731 				ILM_WALKER_HOLD(ill);
10732 				for (ilm = ill->ill_ilm; ilm != NULL;
10733 				    ilm = ilm->ilm_next) {
10734 					if (ilm->ilm_flags & ILM_DELETED)
10735 						continue;
10736 					if (!IN6_ARE_ADDR_EQUAL(
10737 					    &ilm->ilm_v6addr, &ip6h->ip6_dst))
10738 						continue;
10739 					if ((fanout_flags &
10740 					    IP_FF_NO_MCAST_LOOP) &&
10741 					    ilm->ilm_zoneid == ire->ire_zoneid)
10742 						continue;
10743 					if (!ipif_lookup_zoneid(ill,
10744 					    ilm->ilm_zoneid, IPIF_UP, NULL))
10745 						continue;
10746 
10747 					first_mp1 = ip_copymsg(first_mp);
10748 					if (first_mp1 == NULL)
10749 						continue;
10750 					icmp_inbound_v6(q, first_mp1, ill,
10751 					    hdr_length, mctl_present,
10752 					    IP6_NO_IPPOLICY, ilm->ilm_zoneid,
10753 					    NULL);
10754 				}
10755 				ILM_WALKER_RELE(ill);
10756 			} else {
10757 				first_mp1 = ip_copymsg(first_mp);
10758 				if (first_mp1 != NULL)
10759 					icmp_inbound_v6(q, first_mp1, ill,
10760 					    hdr_length, mctl_present,
10761 					    IP6_NO_IPPOLICY, ire->ire_zoneid,
10762 					    NULL);
10763 			}
10764 		}
10765 		/* FALLTHRU */
10766 		default: {
10767 			/*
10768 			 * Handle protocols with which IPv6 is less intimate.
10769 			 */
10770 			fanout_flags |= IP_FF_RAWIP|IP_FF_IP6INFO;
10771 
10772 			/*
10773 			 * Enable sending ICMP for "Unknown" nexthdr
10774 			 * case. i.e. where we did not FALLTHRU from
10775 			 * IPPROTO_ICMPV6 processing case above.
10776 			 */
10777 			if (nexthdr != IPPROTO_ICMPV6)
10778 				fanout_flags |= IP_FF_SEND_ICMP;
10779 			/*
10780 			 * Note: There can be more than one stream bound
10781 			 * to a particular protocol. When this is the case,
10782 			 * each one gets a copy of any incoming packets.
10783 			 */
10784 			ip_fanout_proto_v6(q, first_mp, ip6h, ill, ill, nexthdr,
10785 			    nexthdr_offset, fanout_flags|IP6_NO_IPPOLICY,
10786 			    mctl_present, ire->ire_zoneid);
10787 			return;
10788 		}
10789 	}
10790 }
10791 
10792 /*
10793  * Send packet using IRE.
10794  * Checksumming is controlled by cksum_request:
10795  *	-1 => normal i.e. TCP/UDP/SCTP/ICMPv6 are checksummed and nothing else.
10796  *	1 => Skip TCP/UDP/SCTP checksum
10797  * 	Otherwise => checksum_request contains insert offset for checksum
10798  *
10799  * Assumes that the following set of headers appear in the first
10800  * mblk:
10801  *	ip6_t
10802  *	Any extension headers
10803  *	TCP/UDP/SCTP header (if present)
10804  * The routine can handle an ICMPv6 header that is not in the first mblk.
10805  *
10806  * NOTE : This function does not ire_refrele the ire passed in as the
10807  *	  argument unlike ip_wput_ire where the REFRELE is done.
10808  *	  Refer to ip_wput_ire for more on this.
10809  */
10810 static void
10811 ip_wput_ire_v6(queue_t *q, mblk_t *mp, ire_t *ire, int unspec_src,
10812     int cksum_request, conn_t *connp, int caller, int attach_index, int flags,
10813     zoneid_t zoneid)
10814 {
10815 	ip6_t		*ip6h;
10816 	uint8_t		nexthdr;
10817 	uint16_t	hdr_length;
10818 	uint_t		reachable = 0x0;
10819 	ill_t		*ill;
10820 	mib2_ipv6IfStatsEntry_t	*mibptr;
10821 	mblk_t		*first_mp;
10822 	boolean_t	mctl_present;
10823 	ipsec_out_t	*io;
10824 	boolean_t	conn_dontroute;	/* conn value for multicast */
10825 	boolean_t	conn_multicast_loop;	/* conn value for multicast */
10826 	boolean_t 	multicast_forward;	/* Should we forward ? */
10827 	int		max_frag;
10828 
10829 	ill = ire_to_ill(ire);
10830 	first_mp = mp;
10831 	multicast_forward = B_FALSE;
10832 
10833 	if (mp->b_datap->db_type != M_CTL) {
10834 		ip6h = (ip6_t *)first_mp->b_rptr;
10835 	} else {
10836 		io = (ipsec_out_t *)first_mp->b_rptr;
10837 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
10838 		/*
10839 		 * Grab the zone id now because the M_CTL can be discarded by
10840 		 * ip_wput_ire_parse_ipsec_out() below.
10841 		 */
10842 		ASSERT(zoneid == io->ipsec_out_zoneid);
10843 		ASSERT(zoneid != ALL_ZONES);
10844 		ip6h = (ip6_t *)first_mp->b_cont->b_rptr;
10845 		/*
10846 		 * For the multicast case, ipsec_out carries conn_dontroute and
10847 		 * conn_multicast_loop as conn may not be available here. We
10848 		 * need this for multicast loopback and forwarding which is done
10849 		 * later in the code.
10850 		 */
10851 		if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
10852 			conn_dontroute = io->ipsec_out_dontroute;
10853 			conn_multicast_loop = io->ipsec_out_multicast_loop;
10854 			/*
10855 			 * If conn_dontroute is not set or conn_multicast_loop
10856 			 * is set, we need to do forwarding/loopback. For
10857 			 * datagrams from ip_wput_multicast, conn_dontroute is
10858 			 * set to B_TRUE and conn_multicast_loop is set to
10859 			 * B_FALSE so that we neither do forwarding nor
10860 			 * loopback.
10861 			 */
10862 			if (!conn_dontroute || conn_multicast_loop)
10863 				multicast_forward = B_TRUE;
10864 		}
10865 	}
10866 
10867 	/*
10868 	 * If the sender didn't supply the hop limit and there is a default
10869 	 * unicast hop limit associated with the output interface, we use
10870 	 * that if the packet is unicast.  Interface specific unicast hop
10871 	 * limits as set via the SIOCSLIFLNKINFO ioctl.
10872 	 */
10873 	if (ill->ill_max_hops != 0 && !(flags & IP6I_HOPLIMIT) &&
10874 	    !(IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst))) {
10875 		ip6h->ip6_hops = ill->ill_max_hops;
10876 	}
10877 
10878 	if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid &&
10879 	    ire->ire_zoneid != ALL_ZONES) {
10880 		/*
10881 		 * When a zone sends a packet to another zone, we try to deliver
10882 		 * the packet under the same conditions as if the destination
10883 		 * was a real node on the network. To do so, we look for a
10884 		 * matching route in the forwarding table.
10885 		 * RTF_REJECT and RTF_BLACKHOLE are handled just like
10886 		 * ip_newroute_v6() does.
10887 		 * Note that IRE_LOCAL are special, since they are used
10888 		 * when the zoneid doesn't match in some cases. This means that
10889 		 * we need to handle ipha_src differently since ire_src_addr
10890 		 * belongs to the receiving zone instead of the sending zone.
10891 		 * When ip_restrict_interzone_loopback is set, then
10892 		 * ire_cache_lookup_v6() ensures that IRE_LOCAL are only used
10893 		 * for loopback between zones when the logical "Ethernet" would
10894 		 * have looped them back.
10895 		 */
10896 		ire_t *src_ire;
10897 
10898 		src_ire = ire_ftable_lookup_v6(&ip6h->ip6_dst, 0, 0, 0,
10899 		    NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE |
10900 		    MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE));
10901 		if (src_ire != NULL &&
10902 		    !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) &&
10903 		    (!ip_restrict_interzone_loopback ||
10904 		    ire_local_same_ill_group(ire, src_ire))) {
10905 			if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) &&
10906 			    !unspec_src) {
10907 				ip6h->ip6_src = src_ire->ire_src_addr_v6;
10908 			}
10909 			ire_refrele(src_ire);
10910 		} else {
10911 			BUMP_MIB(ill->ill_ip6_mib, ipv6OutNoRoutes);
10912 			if (src_ire != NULL) {
10913 				if (src_ire->ire_flags & RTF_BLACKHOLE) {
10914 					ire_refrele(src_ire);
10915 					freemsg(first_mp);
10916 					return;
10917 				}
10918 				ire_refrele(src_ire);
10919 			}
10920 			if (ip_hdr_complete_v6(ip6h, zoneid)) {
10921 				/* Failed */
10922 				freemsg(first_mp);
10923 				return;
10924 			}
10925 			icmp_unreachable_v6(q, first_mp,
10926 			    ICMP6_DST_UNREACH_NOROUTE, B_FALSE, B_FALSE,
10927 			    zoneid);
10928 			return;
10929 		}
10930 	}
10931 
10932 	if (mp->b_datap->db_type == M_CTL || ipsec_outbound_v6_policy_present) {
10933 		mp = ip_wput_ire_parse_ipsec_out(first_mp, NULL, ip6h, ire,
10934 		    connp, unspec_src, zoneid);
10935 		if (mp == NULL) {
10936 			return;
10937 		}
10938 	}
10939 
10940 	first_mp = mp;
10941 	if (mp->b_datap->db_type == M_CTL) {
10942 		io = (ipsec_out_t *)mp->b_rptr;
10943 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
10944 		mp = mp->b_cont;
10945 		mctl_present = B_TRUE;
10946 	} else {
10947 		mctl_present = B_FALSE;
10948 	}
10949 
10950 	ip6h = (ip6_t *)mp->b_rptr;
10951 	nexthdr = ip6h->ip6_nxt;
10952 	mibptr = ill->ill_ip6_mib;
10953 
10954 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) && !unspec_src) {
10955 		ipif_t *ipif;
10956 
10957 		/*
10958 		 * Select the source address using ipif_select_source_v6.
10959 		 */
10960 		if (attach_index != 0) {
10961 			ipif = ipif_select_source_v6(ill, &ip6h->ip6_dst,
10962 			    RESTRICT_TO_ILL, IPV6_PREFER_SRC_DEFAULT, zoneid);
10963 		} else {
10964 			ipif = ipif_select_source_v6(ill, &ip6h->ip6_dst,
10965 			    RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT, zoneid);
10966 		}
10967 		if (ipif == NULL) {
10968 			if (ip_debug > 2) {
10969 				/* ip1dbg */
10970 				pr_addr_dbg("ip_wput_ire_v6: no src for "
10971 				    "dst %s\n, ", AF_INET6, &ip6h->ip6_dst);
10972 				printf("ip_wput_ire_v6: interface name %s\n",
10973 				    ill->ill_name);
10974 			}
10975 			freemsg(first_mp);
10976 			return;
10977 		}
10978 		ip6h->ip6_src = ipif->ipif_v6src_addr;
10979 		ipif_refrele(ipif);
10980 	}
10981 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
10982 		if ((connp != NULL && connp->conn_multicast_loop) ||
10983 		    !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) {
10984 			ilm_t	*ilm;
10985 
10986 			ILM_WALKER_HOLD(ill);
10987 			ilm = ilm_lookup_ill_v6(ill, &ip6h->ip6_dst, ALL_ZONES);
10988 			ILM_WALKER_RELE(ill);
10989 			if (ilm != NULL) {
10990 				mblk_t *nmp;
10991 				int fanout_flags = 0;
10992 
10993 				if (connp != NULL &&
10994 				    !connp->conn_multicast_loop) {
10995 					fanout_flags |= IP_FF_NO_MCAST_LOOP;
10996 				}
10997 				ip1dbg(("ip_wput_ire_v6: "
10998 				    "Loopback multicast\n"));
10999 				nmp = ip_copymsg(first_mp);
11000 				if (nmp != NULL) {
11001 					ip6_t	*nip6h;
11002 					mblk_t	*mp_ip6h;
11003 
11004 					if (mctl_present) {
11005 						nip6h = (ip6_t *)
11006 						    nmp->b_cont->b_rptr;
11007 						mp_ip6h = nmp->b_cont;
11008 					} else {
11009 						nip6h = (ip6_t *)nmp->b_rptr;
11010 						mp_ip6h = nmp;
11011 					}
11012 
11013 					DTRACE_PROBE4(
11014 					    ip6__loopback__out__start,
11015 					    ill_t *, NULL,
11016 					    ill_t *, ill,
11017 					    ip6_t *, nip6h,
11018 					    mblk_t *, nmp);
11019 
11020 					FW_HOOKS6(ip6_loopback_out_event,
11021 					    ipv6firewall_loopback_out,
11022 					    NULL, ill, nip6h, nmp, mp_ip6h);
11023 
11024 					DTRACE_PROBE1(
11025 					    ip6__loopback__out__end,
11026 					    mblk_t *, nmp);
11027 
11028 					if (nmp != NULL) {
11029 						/*
11030 						 * Deliver locally and to
11031 						 * every local zone, except
11032 						 * the sending zone when
11033 						 * IPV6_MULTICAST_LOOP is
11034 						 * disabled.
11035 						 */
11036 						ip_wput_local_v6(RD(q), ill,
11037 						    nip6h, nmp,
11038 						    ire, fanout_flags);
11039 					}
11040 				} else {
11041 					BUMP_MIB(mibptr, ipv6OutDiscards);
11042 					ip1dbg(("ip_wput_ire_v6: "
11043 					    "copymsg failed\n"));
11044 				}
11045 			}
11046 		}
11047 		if (ip6h->ip6_hops == 0 ||
11048 		    IN6_IS_ADDR_MC_NODELOCAL(&ip6h->ip6_dst) ||
11049 		    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) {
11050 			/*
11051 			 * Local multicast or just loopback on loopback
11052 			 * interface.
11053 			 */
11054 			BUMP_MIB(mibptr, ipv6OutMcastPkts);
11055 			ip1dbg(("ip_wput_ire_v6: local multicast only\n"));
11056 			freemsg(first_mp);
11057 			return;
11058 		}
11059 	}
11060 
11061 	if (ire->ire_stq != NULL) {
11062 		uint32_t	sum;
11063 		uint_t		ill_index =  ((ill_t *)ire->ire_stq->q_ptr)->
11064 		    ill_phyint->phyint_ifindex;
11065 		queue_t		*dev_q = ire->ire_stq->q_next;
11066 
11067 		/*
11068 		 * non-NULL send-to queue - packet is to be sent
11069 		 * out an interface.
11070 		 */
11071 
11072 		/* Driver is flow-controlling? */
11073 		if (!IP_FLOW_CONTROLLED_ULP(nexthdr) &&
11074 		    ((dev_q->q_next || dev_q->q_first) && !canput(dev_q))) {
11075 			/*
11076 			 * Queue packet if we have an conn to give back
11077 			 * pressure.  We can't queue packets intended for
11078 			 * hardware acceleration since we've tossed that
11079 			 * state already.  If the packet is being fed back
11080 			 * from ire_send_v6, we don't know the position in
11081 			 * the queue to enqueue the packet and we discard
11082 			 * the packet.
11083 			 */
11084 			if (ip_output_queue && connp != NULL &&
11085 			    !mctl_present && caller != IRE_SEND) {
11086 				if (caller == IP_WSRV) {
11087 					connp->conn_did_putbq = 1;
11088 					(void) putbq(connp->conn_wq, mp);
11089 					conn_drain_insert(connp);
11090 					/*
11091 					 * caller == IP_WSRV implies we are
11092 					 * the service thread, and the
11093 					 * queue is already noenabled.
11094 					 * The check for canput and
11095 					 * the putbq is not atomic.
11096 					 * So we need to check again.
11097 					 */
11098 					if (canput(dev_q))
11099 						connp->conn_did_putbq = 0;
11100 				} else {
11101 					(void) putq(connp->conn_wq, mp);
11102 				}
11103 				return;
11104 			}
11105 			BUMP_MIB(mibptr, ipv6OutDiscards);
11106 			freemsg(first_mp);
11107 			return;
11108 		}
11109 
11110 		/*
11111 		 * Look for reachability confirmations from the transport.
11112 		 */
11113 		if (ip6h->ip6_vcf & IP_FORWARD_PROG) {
11114 			reachable |= IPV6_REACHABILITY_CONFIRMATION;
11115 			ip6h->ip6_vcf &= ~IP_FORWARD_PROG;
11116 			if (mctl_present)
11117 				io->ipsec_out_reachable = B_TRUE;
11118 		}
11119 		/* Fastpath */
11120 		switch (nexthdr) {
11121 		case IPPROTO_TCP:
11122 		case IPPROTO_UDP:
11123 		case IPPROTO_ICMPV6:
11124 		case IPPROTO_SCTP:
11125 			hdr_length = IPV6_HDR_LEN;
11126 			break;
11127 		default: {
11128 			uint8_t	*nexthdrp;
11129 
11130 			if (!ip_hdr_length_nexthdr_v6(mp, ip6h,
11131 			    &hdr_length, &nexthdrp)) {
11132 				/* Malformed packet */
11133 				BUMP_MIB(mibptr, ipv6OutDiscards);
11134 				freemsg(first_mp);
11135 				return;
11136 			}
11137 			nexthdr = *nexthdrp;
11138 			break;
11139 		}
11140 		}
11141 
11142 		if (cksum_request != -1 && nexthdr != IPPROTO_ICMPV6) {
11143 			uint16_t	*up;
11144 			uint16_t	*insp;
11145 
11146 			/*
11147 			 * The packet header is processed once for all, even
11148 			 * in the multirouting case. We disable hardware
11149 			 * checksum if the packet is multirouted, as it will be
11150 			 * replicated via several interfaces, and not all of
11151 			 * them may have this capability.
11152 			 */
11153 			if (cksum_request == 1 &&
11154 			    !(ire->ire_flags & RTF_MULTIRT)) {
11155 				/* Skip the transport checksum */
11156 				goto cksum_done;
11157 			}
11158 			/*
11159 			 * Do user-configured raw checksum.
11160 			 * Compute checksum and insert at offset "cksum_request"
11161 			 */
11162 
11163 			/* check for enough headers for checksum */
11164 			cksum_request += hdr_length;	/* offset from rptr */
11165 			if ((mp->b_wptr - mp->b_rptr) <
11166 			    (cksum_request + sizeof (int16_t))) {
11167 				if (!pullupmsg(mp,
11168 				    cksum_request + sizeof (int16_t))) {
11169 					ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg"
11170 					    " failed\n"));
11171 					BUMP_MIB(mibptr, ipv6OutDiscards);
11172 					freemsg(first_mp);
11173 					return;
11174 				}
11175 				ip6h = (ip6_t *)mp->b_rptr;
11176 			}
11177 			insp = (uint16_t *)((uchar_t *)ip6h + cksum_request);
11178 			ASSERT(((uintptr_t)insp & 0x1) == 0);
11179 			up = (uint16_t *)&ip6h->ip6_src;
11180 			/*
11181 			 * icmp has placed length and routing
11182 			 * header adjustment in *insp.
11183 			 */
11184 			sum = htons(nexthdr) +
11185 			    up[0] + up[1] + up[2] + up[3] +
11186 			    up[4] + up[5] + up[6] + up[7] +
11187 			    up[8] + up[9] + up[10] + up[11] +
11188 			    up[12] + up[13] + up[14] + up[15];
11189 			sum = (sum & 0xffff) + (sum >> 16);
11190 			*insp = IP_CSUM(mp, hdr_length, sum);
11191 			if (*insp == 0)
11192 				*insp = 0xFFFF;
11193 		} else if (nexthdr == IPPROTO_TCP) {
11194 			uint16_t	*up;
11195 
11196 			/*
11197 			 * Check for full IPv6 header + enough TCP header
11198 			 * to get at the checksum field.
11199 			 */
11200 			if ((mp->b_wptr - mp->b_rptr) <
11201 			    (hdr_length + TCP_CHECKSUM_OFFSET +
11202 			    TCP_CHECKSUM_SIZE)) {
11203 				if (!pullupmsg(mp, hdr_length +
11204 				    TCP_CHECKSUM_OFFSET + TCP_CHECKSUM_SIZE)) {
11205 					ip1dbg(("ip_wput_v6: TCP hdr pullupmsg"
11206 					    " failed\n"));
11207 					BUMP_MIB(mibptr, ipv6OutDiscards);
11208 					freemsg(first_mp);
11209 					return;
11210 				}
11211 				ip6h = (ip6_t *)mp->b_rptr;
11212 			}
11213 
11214 			up = (uint16_t *)&ip6h->ip6_src;
11215 			/*
11216 			 * Note: The TCP module has stored the length value
11217 			 * into the tcp checksum field, so we don't
11218 			 * need to explicitly sum it in here.
11219 			 */
11220 			sum = up[0] + up[1] + up[2] + up[3] +
11221 			    up[4] + up[5] + up[6] + up[7] +
11222 			    up[8] + up[9] + up[10] + up[11] +
11223 			    up[12] + up[13] + up[14] + up[15];
11224 
11225 			/* Fold the initial sum */
11226 			sum = (sum & 0xffff) + (sum >> 16);
11227 
11228 			up = (uint16_t *)(((uchar_t *)ip6h) +
11229 			    hdr_length + TCP_CHECKSUM_OFFSET);
11230 
11231 			IP_CKSUM_XMIT(ill, ire, mp, ip6h, up, IPPROTO_TCP,
11232 			    hdr_length, ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN,
11233 			    ire->ire_max_frag, mctl_present, sum);
11234 
11235 			/* Software checksum? */
11236 			if (DB_CKSUMFLAGS(mp) == 0) {
11237 				IP6_STAT(ip6_out_sw_cksum);
11238 				IP6_STAT_UPDATE(ip6_tcp_out_sw_cksum_bytes,
11239 				    (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN) -
11240 				    hdr_length);
11241 			}
11242 		} else if (nexthdr == IPPROTO_UDP) {
11243 			uint16_t	*up;
11244 
11245 			/*
11246 			 * check for full IPv6 header + enough UDP header
11247 			 * to get at the UDP checksum field
11248 			 */
11249 			if ((mp->b_wptr - mp->b_rptr) < (hdr_length +
11250 			    UDP_CHECKSUM_OFFSET + UDP_CHECKSUM_SIZE)) {
11251 				if (!pullupmsg(mp, hdr_length +
11252 				    UDP_CHECKSUM_OFFSET + UDP_CHECKSUM_SIZE)) {
11253 					ip1dbg(("ip_wput_v6: UDP hdr pullupmsg"
11254 					    " failed\n"));
11255 					BUMP_MIB(mibptr, ipv6OutDiscards);
11256 					freemsg(first_mp);
11257 					return;
11258 				}
11259 				ip6h = (ip6_t *)mp->b_rptr;
11260 			}
11261 			up = (uint16_t *)&ip6h->ip6_src;
11262 			/*
11263 			 * Note: The UDP module has stored the length value
11264 			 * into the udp checksum field, so we don't
11265 			 * need to explicitly sum it in here.
11266 			 */
11267 			sum = up[0] + up[1] + up[2] + up[3] +
11268 			    up[4] + up[5] + up[6] + up[7] +
11269 			    up[8] + up[9] + up[10] + up[11] +
11270 			    up[12] + up[13] + up[14] + up[15];
11271 
11272 			/* Fold the initial sum */
11273 			sum = (sum & 0xffff) + (sum >> 16);
11274 
11275 			up = (uint16_t *)(((uchar_t *)ip6h) +
11276 			    hdr_length + UDP_CHECKSUM_OFFSET);
11277 
11278 			IP_CKSUM_XMIT(ill, ire, mp, ip6h, up, IPPROTO_UDP,
11279 			    hdr_length, ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN,
11280 			    ire->ire_max_frag, mctl_present, sum);
11281 
11282 			/* Software checksum? */
11283 			if (DB_CKSUMFLAGS(mp) == 0) {
11284 				IP6_STAT(ip6_out_sw_cksum);
11285 				IP6_STAT_UPDATE(ip6_udp_out_sw_cksum_bytes,
11286 				    (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN) -
11287 				    hdr_length);
11288 			}
11289 		} else if (nexthdr == IPPROTO_ICMPV6) {
11290 			uint16_t	*up;
11291 			icmp6_t *icmp6;
11292 
11293 			/* check for full IPv6+ICMPv6 header */
11294 			if ((mp->b_wptr - mp->b_rptr) <
11295 			    (hdr_length + ICMP6_MINLEN)) {
11296 				if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) {
11297 					ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg"
11298 					    " failed\n"));
11299 					BUMP_MIB(mibptr, ipv6OutDiscards);
11300 					freemsg(first_mp);
11301 					return;
11302 				}
11303 				ip6h = (ip6_t *)mp->b_rptr;
11304 			}
11305 			icmp6 = (icmp6_t *)((uchar_t *)ip6h + hdr_length);
11306 			up = (uint16_t *)&ip6h->ip6_src;
11307 			/*
11308 			 * icmp has placed length and routing
11309 			 * header adjustment in icmp6_cksum.
11310 			 */
11311 			sum = htons(IPPROTO_ICMPV6) +
11312 			    up[0] + up[1] + up[2] + up[3] +
11313 			    up[4] + up[5] + up[6] + up[7] +
11314 			    up[8] + up[9] + up[10] + up[11] +
11315 			    up[12] + up[13] + up[14] + up[15];
11316 			sum = (sum & 0xffff) + (sum >> 16);
11317 			icmp6->icmp6_cksum = IP_CSUM(mp, hdr_length, sum);
11318 			if (icmp6->icmp6_cksum == 0)
11319 				icmp6->icmp6_cksum = 0xFFFF;
11320 
11321 			/* Update output mib stats */
11322 			icmp_update_out_mib_v6(ill, icmp6);
11323 		} else if (nexthdr == IPPROTO_SCTP) {
11324 			sctp_hdr_t *sctph;
11325 
11326 			if (MBLKL(mp) < (hdr_length + sizeof (*sctph))) {
11327 				if (!pullupmsg(mp, hdr_length +
11328 				    sizeof (*sctph))) {
11329 					ip1dbg(("ip_wput_v6: SCTP hdr pullupmsg"
11330 					    " failed\n"));
11331 					BUMP_MIB(ill->ill_ip6_mib,
11332 					    ipv6OutDiscards);
11333 					freemsg(mp);
11334 					return;
11335 				}
11336 				ip6h = (ip6_t *)mp->b_rptr;
11337 			}
11338 			sctph = (sctp_hdr_t *)(mp->b_rptr + hdr_length);
11339 			sctph->sh_chksum = 0;
11340 			sctph->sh_chksum = sctp_cksum(mp, hdr_length);
11341 		}
11342 
11343 	cksum_done:
11344 		/*
11345 		 * We force the insertion of a fragment header using the
11346 		 * IPH_FRAG_HDR flag in two cases:
11347 		 * - after reception of an ICMPv6 "packet too big" message
11348 		 *   with a MTU < 1280 (cf. RFC 2460 section 5)
11349 		 * - for multirouted IPv6 packets, so that the receiver can
11350 		 *   discard duplicates according to their fragment identifier
11351 		 *
11352 		 * Two flags modifed from the API can modify this behavior.
11353 		 * The first is IPV6_USE_MIN_MTU.  With this API the user
11354 		 * can specify how to manage PMTUD for unicast and multicast.
11355 		 *
11356 		 * IPV6_DONTFRAG disallows fragmentation.
11357 		 */
11358 		max_frag = ire->ire_max_frag;
11359 		switch (IP6I_USE_MIN_MTU_API(flags)) {
11360 		case IPV6_USE_MIN_MTU_DEFAULT:
11361 		case IPV6_USE_MIN_MTU_UNICAST:
11362 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
11363 				max_frag = IPV6_MIN_MTU;
11364 			}
11365 			break;
11366 
11367 		case IPV6_USE_MIN_MTU_NEVER:
11368 			max_frag = IPV6_MIN_MTU;
11369 			break;
11370 		}
11371 		if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > max_frag ||
11372 		    (ire->ire_frag_flag & IPH_FRAG_HDR)) {
11373 			if (connp != NULL && (flags & IP6I_DONTFRAG)) {
11374 				icmp_pkt2big_v6(ire->ire_stq, first_mp,
11375 				    max_frag, B_FALSE, B_TRUE, zoneid);
11376 				return;
11377 			}
11378 
11379 			if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN !=
11380 			    (mp->b_cont ? msgdsize(mp) :
11381 			    mp->b_wptr - (uchar_t *)ip6h)) {
11382 				ip0dbg(("Packet length mismatch: %d, %ld\n",
11383 				    ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN,
11384 				    msgdsize(mp)));
11385 				freemsg(first_mp);
11386 				return;
11387 			}
11388 			/* Do IPSEC processing first */
11389 			if (mctl_present) {
11390 				if (attach_index != 0)
11391 					ipsec_out_attach_if(io, attach_index);
11392 				ipsec_out_process(q, first_mp, ire, ill_index);
11393 				return;
11394 			}
11395 			ASSERT(mp->b_prev == NULL);
11396 			ip2dbg(("Fragmenting Size = %d, mtu = %d\n",
11397 			    ntohs(ip6h->ip6_plen) +
11398 			    IPV6_HDR_LEN, max_frag));
11399 			ASSERT(mp == first_mp);
11400 			/* Initiate IPPF processing */
11401 			if (IPP_ENABLED(IPP_LOCAL_OUT)) {
11402 				ip_process(IPP_LOCAL_OUT, &mp, ill_index);
11403 				if (mp == NULL) {
11404 					return;
11405 				}
11406 			}
11407 			ip_wput_frag_v6(mp, ire, reachable, connp,
11408 			    caller, max_frag);
11409 			return;
11410 		}
11411 		/* Do IPSEC processing first */
11412 		if (mctl_present) {
11413 			int extra_len = ipsec_out_extra_length(first_mp);
11414 
11415 			if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN + extra_len >
11416 			    max_frag && ip_ulp_cando_pkt2big(nexthdr)) {
11417 				/*
11418 				 * IPsec headers will push the packet over the
11419 				 * MTU limit.  Issue an ICMPv6 Packet Too Big
11420 				 * message for this packet if the upper-layer
11421 				 * that issued this packet will be able to
11422 				 * react to the icmp_pkt2big_v6() that we'll
11423 				 * generate.
11424 				 */
11425 				icmp_pkt2big_v6(ire->ire_stq, first_mp,
11426 				    max_frag, B_FALSE, B_TRUE, zoneid);
11427 				return;
11428 			}
11429 			if (attach_index != 0)
11430 				ipsec_out_attach_if(io, attach_index);
11431 			ipsec_out_process(q, first_mp, ire, ill_index);
11432 			return;
11433 		}
11434 		/*
11435 		 * XXX multicast: add ip_mforward_v6() here.
11436 		 * Check conn_dontroute
11437 		 */
11438 #ifdef lint
11439 		/*
11440 		 * XXX The only purpose of this statement is to avoid lint
11441 		 * errors.  See the above "XXX multicast".  When that gets
11442 		 * fixed, remove this whole #ifdef lint section.
11443 		 */
11444 		ip3dbg(("multicast forward is %s.\n",
11445 		    (multicast_forward ? "TRUE" : "FALSE")));
11446 #endif
11447 
11448 		UPDATE_OB_PKT_COUNT(ire);
11449 		ire->ire_last_used_time = lbolt;
11450 		ASSERT(mp == first_mp);
11451 		ip_xmit_v6(mp, ire, reachable, connp, caller, NULL);
11452 	} else {
11453 		DTRACE_PROBE4(ip6__loopback__out__start,
11454 		    ill_t *, NULL, ill_t *, ill,
11455 		    ip6_t *, ip6h, mblk_t *, first_mp);
11456 		FW_HOOKS6(ip6_loopback_out_event, ipv6firewall_loopback_out,
11457 		    NULL, ill, ip6h, first_mp, mp);
11458 		DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, first_mp);
11459 		if (first_mp != NULL)
11460 			ip_wput_local_v6(RD(q), ill, ip6h, first_mp, ire, 0);
11461 	}
11462 }
11463 
11464 /*
11465  * Outbound IPv6 fragmentation routine using MDT.
11466  */
11467 static void
11468 ip_wput_frag_mdt_v6(mblk_t *mp, ire_t *ire, size_t max_chunk,
11469     size_t unfragmentable_len, uint8_t nexthdr, uint_t prev_nexthdr_offset)
11470 {
11471 	ip6_t		*ip6h = (ip6_t *)mp->b_rptr;
11472 	uint_t		pkts, wroff, hdr_chunk_len, pbuf_idx;
11473 	mblk_t		*hdr_mp, *md_mp = NULL;
11474 	int		i1;
11475 	multidata_t	*mmd;
11476 	unsigned char	*hdr_ptr, *pld_ptr;
11477 	ip_pdescinfo_t	pdi;
11478 	uint32_t	ident;
11479 	size_t		len;
11480 	uint16_t	offset;
11481 	queue_t		*stq = ire->ire_stq;
11482 	ill_t		*ill = (ill_t *)stq->q_ptr;
11483 
11484 	ASSERT(DB_TYPE(mp) == M_DATA);
11485 	ASSERT(MBLKL(mp) > unfragmentable_len);
11486 
11487 	/*
11488 	 * Move read ptr past unfragmentable portion, we don't want this part
11489 	 * of the data in our fragments.
11490 	 */
11491 	mp->b_rptr += unfragmentable_len;
11492 
11493 	/* Calculate how many packets we will send out  */
11494 	i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp);
11495 	pkts = (i1 + max_chunk - 1) / max_chunk;
11496 	ASSERT(pkts > 1);
11497 
11498 	/* Allocate a message block which will hold all the IP Headers. */
11499 	wroff = ip_wroff_extra;
11500 	hdr_chunk_len = wroff + unfragmentable_len + sizeof (ip6_frag_t);
11501 
11502 	i1 = pkts * hdr_chunk_len;
11503 	/*
11504 	 * Create the header buffer, Multidata and destination address
11505 	 * and SAP attribute that should be associated with it.
11506 	 */
11507 	if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL ||
11508 	    ((hdr_mp->b_wptr += i1),
11509 	    (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) ||
11510 	    !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) {
11511 		freemsg(mp);
11512 		if (md_mp == NULL) {
11513 			freemsg(hdr_mp);
11514 		} else {
11515 free_mmd:		IP6_STAT(ip6_frag_mdt_discarded);
11516 			freemsg(md_mp);
11517 		}
11518 		IP6_STAT(ip6_frag_mdt_allocfail);
11519 		BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails);
11520 		UPDATE_MIB(ill->ill_ip6_mib, ipv6OutDiscards, pkts);
11521 		return;
11522 	}
11523 	IP6_STAT(ip6_frag_mdt_allocd);
11524 
11525 	/*
11526 	 * Add a payload buffer to the Multidata; this operation must not
11527 	 * fail, or otherwise our logic in this routine is broken.  There
11528 	 * is no memory allocation done by the routine, so any returned
11529 	 * failure simply tells us that we've done something wrong.
11530 	 *
11531 	 * A failure tells us that either we're adding the same payload
11532 	 * buffer more than once, or we're trying to add more buffers than
11533 	 * allowed.  None of the above cases should happen, and we panic
11534 	 * because either there's horrible heap corruption, and/or
11535 	 * programming mistake.
11536 	 */
11537 	if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) {
11538 		goto pbuf_panic;
11539 	}
11540 
11541 	hdr_ptr = hdr_mp->b_rptr;
11542 	pld_ptr = mp->b_rptr;
11543 
11544 	pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF;
11545 
11546 	ident = htonl(atomic_add_32_nv(&ire->ire_ident, 1));
11547 
11548 	/*
11549 	 * len is the total length of the fragmentable data in this
11550 	 * datagram.  For each fragment sent, we will decrement len
11551 	 * by the amount of fragmentable data sent in that fragment
11552 	 * until len reaches zero.
11553 	 */
11554 	len = ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN);
11555 
11556 	offset = 0;
11557 	prev_nexthdr_offset += wroff;
11558 
11559 	while (len != 0) {
11560 		size_t		mlen;
11561 		ip6_t		*fip6h;
11562 		ip6_frag_t	*fraghdr;
11563 		int		error;
11564 
11565 		ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr);
11566 		mlen = MIN(len, max_chunk);
11567 		len -= mlen;
11568 
11569 		fip6h = (ip6_t *)(hdr_ptr + wroff);
11570 		ASSERT(OK_32PTR(fip6h));
11571 		bcopy(ip6h, fip6h, unfragmentable_len);
11572 		hdr_ptr[prev_nexthdr_offset] = IPPROTO_FRAGMENT;
11573 
11574 		fip6h->ip6_plen = htons((uint16_t)(mlen +
11575 		    unfragmentable_len - IPV6_HDR_LEN + sizeof (ip6_frag_t)));
11576 
11577 		fraghdr = (ip6_frag_t *)((unsigned char *)fip6h +
11578 		    unfragmentable_len);
11579 		fraghdr->ip6f_nxt = nexthdr;
11580 		fraghdr->ip6f_reserved = 0;
11581 		fraghdr->ip6f_offlg = htons(offset) |
11582 		    ((len != 0) ? IP6F_MORE_FRAG : 0);
11583 		fraghdr->ip6f_ident = ident;
11584 
11585 		/*
11586 		 * Record offset and size of header and data of the next packet
11587 		 * in the multidata message.
11588 		 */
11589 		PDESC_HDR_ADD(&pdi, hdr_ptr, wroff,
11590 		    unfragmentable_len + sizeof (ip6_frag_t), 0);
11591 		PDESC_PLD_INIT(&pdi);
11592 		i1 = MIN(mp->b_wptr - pld_ptr, mlen);
11593 		ASSERT(i1 > 0);
11594 		PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1);
11595 		if (i1 == mlen) {
11596 			pld_ptr += mlen;
11597 		} else {
11598 			i1 = mlen - i1;
11599 			mp = mp->b_cont;
11600 			ASSERT(mp != NULL);
11601 			ASSERT(MBLKL(mp) >= i1);
11602 			/*
11603 			 * Attach the next payload message block to the
11604 			 * multidata message.
11605 			 */
11606 			if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0)
11607 				goto pbuf_panic;
11608 			PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1);
11609 			pld_ptr = mp->b_rptr + i1;
11610 		}
11611 
11612 		if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error,
11613 		    KM_NOSLEEP)) == NULL) {
11614 			/*
11615 			 * Any failure other than ENOMEM indicates that we
11616 			 * have passed in invalid pdesc info or parameters
11617 			 * to mmd_addpdesc, which must not happen.
11618 			 *
11619 			 * EINVAL is a result of failure on boundary checks
11620 			 * against the pdesc info contents.  It should not
11621 			 * happen, and we panic because either there's
11622 			 * horrible heap corruption, and/or programming
11623 			 * mistake.
11624 			 */
11625 			if (error != ENOMEM) {
11626 				cmn_err(CE_PANIC, "ip_wput_frag_mdt_v6: "
11627 				    "pdesc logic error detected for "
11628 				    "mmd %p pinfo %p (%d)\n",
11629 				    (void *)mmd, (void *)&pdi, error);
11630 				/* NOTREACHED */
11631 			}
11632 			IP6_STAT(ip6_frag_mdt_addpdescfail);
11633 			/* Free unattached payload message blocks as well */
11634 			md_mp->b_cont = mp->b_cont;
11635 			goto free_mmd;
11636 		}
11637 
11638 		/* Advance fragment offset. */
11639 		offset += mlen;
11640 
11641 		/* Advance to location for next header in the buffer. */
11642 		hdr_ptr += hdr_chunk_len;
11643 
11644 		/* Did we reach the next payload message block? */
11645 		if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) {
11646 			mp = mp->b_cont;
11647 			/*
11648 			 * Attach the next message block with payload
11649 			 * data to the multidata message.
11650 			 */
11651 			if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0)
11652 				goto pbuf_panic;
11653 			pld_ptr = mp->b_rptr;
11654 		}
11655 	}
11656 
11657 	ASSERT(hdr_mp->b_wptr == hdr_ptr);
11658 	ASSERT(mp->b_wptr == pld_ptr);
11659 
11660 	/* Update IP statistics */
11661 	UPDATE_MIB(ill->ill_ip6_mib, ipv6OutFragCreates, pkts);
11662 	BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragOKs);
11663 	IP6_STAT_UPDATE(ip6_frag_mdt_pkt_out, pkts);
11664 
11665 	ire->ire_ob_pkt_count += pkts;
11666 	if (ire->ire_ipif != NULL)
11667 		atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts);
11668 
11669 	ire->ire_last_used_time = lbolt;
11670 	/* Send it down */
11671 	putnext(stq, md_mp);
11672 	return;
11673 
11674 pbuf_panic:
11675 	cmn_err(CE_PANIC, "ip_wput_frag_mdt_v6: payload buffer logic "
11676 	    "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp,
11677 	    pbuf_idx);
11678 	/* NOTREACHED */
11679 }
11680 
11681 /*
11682  * IPv6 fragmentation.  Essentially the same as IPv4 fragmentation.
11683  * We have not optimized this in terms of number of mblks
11684  * allocated. For instance, for each fragment sent we always allocate a
11685  * mblk to hold the IPv6 header and fragment header.
11686  *
11687  * Assumes that all the extension headers are contained in the first mblk.
11688  *
11689  * The fragment header is inserted after an hop-by-hop options header
11690  * and after [an optional destinations header followed by] a routing header.
11691  *
11692  * NOTE : This function does not ire_refrele the ire passed in as
11693  * the argument.
11694  */
11695 void
11696 ip_wput_frag_v6(mblk_t *mp, ire_t *ire, uint_t reachable, conn_t *connp,
11697     int caller, int max_frag)
11698 {
11699 	ip6_t		*ip6h = (ip6_t *)mp->b_rptr;
11700 	ip6_t		*fip6h;
11701 	mblk_t		*hmp;
11702 	mblk_t		*hmp0;
11703 	mblk_t		*dmp;
11704 	ip6_frag_t	*fraghdr;
11705 	size_t		unfragmentable_len;
11706 	size_t		len;
11707 	size_t		mlen;
11708 	size_t		max_chunk;
11709 	uint32_t	ident;
11710 	uint16_t	off_flags;
11711 	uint16_t	offset = 0;
11712 	ill_t		*ill;
11713 	uint8_t		nexthdr;
11714 	uint_t		prev_nexthdr_offset;
11715 	uint8_t		*ptr;
11716 
11717 	ASSERT(ire->ire_type == IRE_CACHE);
11718 	ill = (ill_t *)ire->ire_stq->q_ptr;
11719 
11720 	/*
11721 	 * Determine the length of the unfragmentable portion of this
11722 	 * datagram.  This consists of the IPv6 header, a potential
11723 	 * hop-by-hop options header, a potential pre-routing-header
11724 	 * destination options header, and a potential routing header.
11725 	 */
11726 	nexthdr = ip6h->ip6_nxt;
11727 	prev_nexthdr_offset = (uint8_t *)&ip6h->ip6_nxt - (uint8_t *)ip6h;
11728 	ptr = (uint8_t *)&ip6h[1];
11729 
11730 	if (nexthdr == IPPROTO_HOPOPTS) {
11731 		ip6_hbh_t	*hbh_hdr;
11732 		uint_t		hdr_len;
11733 
11734 		hbh_hdr = (ip6_hbh_t *)ptr;
11735 		hdr_len = 8 * (hbh_hdr->ip6h_len + 1);
11736 		nexthdr = hbh_hdr->ip6h_nxt;
11737 		prev_nexthdr_offset = (uint8_t *)&hbh_hdr->ip6h_nxt
11738 		    - (uint8_t *)ip6h;
11739 		ptr += hdr_len;
11740 	}
11741 	if (nexthdr == IPPROTO_DSTOPTS) {
11742 		ip6_dest_t	*dest_hdr;
11743 		uint_t		hdr_len;
11744 
11745 		dest_hdr = (ip6_dest_t *)ptr;
11746 		if (dest_hdr->ip6d_nxt == IPPROTO_ROUTING) {
11747 			hdr_len = 8 * (dest_hdr->ip6d_len + 1);
11748 			nexthdr = dest_hdr->ip6d_nxt;
11749 			prev_nexthdr_offset = (uint8_t *)&dest_hdr->ip6d_nxt
11750 			    - (uint8_t *)ip6h;
11751 			ptr += hdr_len;
11752 		}
11753 	}
11754 	if (nexthdr == IPPROTO_ROUTING) {
11755 		ip6_rthdr_t	*rthdr;
11756 		uint_t		hdr_len;
11757 
11758 		rthdr = (ip6_rthdr_t *)ptr;
11759 		nexthdr = rthdr->ip6r_nxt;
11760 		prev_nexthdr_offset = (uint8_t *)&rthdr->ip6r_nxt
11761 		    - (uint8_t *)ip6h;
11762 		hdr_len = 8 * (rthdr->ip6r_len + 1);
11763 		ptr += hdr_len;
11764 	}
11765 	unfragmentable_len = (uint_t)(ptr - (uint8_t *)ip6h);
11766 
11767 	max_chunk = (min(max_frag, ire->ire_max_frag) - unfragmentable_len -
11768 	    sizeof (ip6_frag_t)) & ~7;
11769 
11770 	/* Check if we can use MDT to send out the frags. */
11771 	ASSERT(!IRE_IS_LOCAL(ire));
11772 	if (ip_multidata_outbound && reachable == 0 &&
11773 	    !(ire->ire_flags & RTF_MULTIRT) && ILL_MDT_CAPABLE(ill) &&
11774 	    IP_CAN_FRAG_MDT(mp, unfragmentable_len, max_chunk)) {
11775 		ip_wput_frag_mdt_v6(mp, ire, max_chunk, unfragmentable_len,
11776 		    nexthdr, prev_nexthdr_offset);
11777 		return;
11778 	}
11779 
11780 	/*
11781 	 * Allocate an mblk with enough room for the link-layer
11782 	 * header, the unfragmentable part of the datagram, and the
11783 	 * fragment header.  This (or a copy) will be used as the
11784 	 * first mblk for each fragment we send.
11785 	 */
11786 	hmp = allocb(unfragmentable_len + sizeof (ip6_frag_t) + ip_wroff_extra,
11787 	    BPRI_HI);
11788 	if (hmp == NULL) {
11789 		BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails);
11790 		freemsg(mp);
11791 		return;
11792 	}
11793 	hmp->b_rptr += ip_wroff_extra;
11794 	hmp->b_wptr = hmp->b_rptr + unfragmentable_len + sizeof (ip6_frag_t);
11795 
11796 	fip6h = (ip6_t *)hmp->b_rptr;
11797 	fraghdr = (ip6_frag_t *)(hmp->b_rptr + unfragmentable_len);
11798 
11799 	bcopy(ip6h, fip6h, unfragmentable_len);
11800 	hmp->b_rptr[prev_nexthdr_offset] = IPPROTO_FRAGMENT;
11801 
11802 	ident = atomic_add_32_nv(&ire->ire_ident, 1);
11803 
11804 	fraghdr->ip6f_nxt = nexthdr;
11805 	fraghdr->ip6f_reserved = 0;
11806 	fraghdr->ip6f_offlg = 0;
11807 	fraghdr->ip6f_ident = htonl(ident);
11808 
11809 	/*
11810 	 * len is the total length of the fragmentable data in this
11811 	 * datagram.  For each fragment sent, we will decrement len
11812 	 * by the amount of fragmentable data sent in that fragment
11813 	 * until len reaches zero.
11814 	 */
11815 	len = ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN);
11816 
11817 	/*
11818 	 * Move read ptr past unfragmentable portion, we don't want this part
11819 	 * of the data in our fragments.
11820 	 */
11821 	mp->b_rptr += unfragmentable_len;
11822 
11823 	while (len != 0) {
11824 		mlen = MIN(len, max_chunk);
11825 		len -= mlen;
11826 		if (len != 0) {
11827 			/* Not last */
11828 			hmp0 = copyb(hmp);
11829 			if (hmp0 == NULL) {
11830 				freeb(hmp);
11831 				freemsg(mp);
11832 				BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails);
11833 				ip1dbg(("ip_wput_frag_v6: copyb failed\n"));
11834 				return;
11835 			}
11836 			off_flags = IP6F_MORE_FRAG;
11837 		} else {
11838 			/* Last fragment */
11839 			hmp0 = hmp;
11840 			hmp = NULL;
11841 			off_flags = 0;
11842 		}
11843 		fip6h = (ip6_t *)(hmp0->b_rptr);
11844 		fraghdr = (ip6_frag_t *)(hmp0->b_rptr + unfragmentable_len);
11845 
11846 		fip6h->ip6_plen = htons((uint16_t)(mlen +
11847 		    unfragmentable_len - IPV6_HDR_LEN + sizeof (ip6_frag_t)));
11848 		/*
11849 		 * Note: Optimization alert.
11850 		 * In IPv6 (and IPv4) protocol header, Fragment Offset
11851 		 * ("offset") is 13 bits wide and in 8-octet units.
11852 		 * In IPv6 protocol header (unlike IPv4) in a 16 bit field,
11853 		 * it occupies the most significant 13 bits.
11854 		 * (least significant 13 bits in IPv4).
11855 		 * We do not do any shifts here. Not shifting is same effect
11856 		 * as taking offset value in octet units, dividing by 8 and
11857 		 * then shifting 3 bits left to line it up in place in proper
11858 		 * place protocol header.
11859 		 */
11860 		fraghdr->ip6f_offlg = htons(offset) | off_flags;
11861 
11862 		if (!(dmp = ip_carve_mp(&mp, mlen))) {
11863 			/* mp has already been freed by ip_carve_mp() */
11864 			if (hmp != NULL)
11865 				freeb(hmp);
11866 			freeb(hmp0);
11867 			ip1dbg(("ip_carve_mp: failed\n"));
11868 			BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails);
11869 			return;
11870 		}
11871 		hmp0->b_cont = dmp;
11872 		/* Get the priority marking, if any */
11873 		hmp0->b_band = dmp->b_band;
11874 		UPDATE_OB_PKT_COUNT(ire);
11875 		ire->ire_last_used_time = lbolt;
11876 		ip_xmit_v6(hmp0, ire, reachable | IP6_NO_IPPOLICY, connp,
11877 		    caller, NULL);
11878 		reachable = 0;	/* No need to redo state machine in loop */
11879 		BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragCreates);
11880 		offset += mlen;
11881 	}
11882 	BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragOKs);
11883 }
11884 
11885 /*
11886  * Determine if the ill and multicast aspects of that packets
11887  * "matches" the conn.
11888  */
11889 boolean_t
11890 conn_wantpacket_v6(conn_t *connp, ill_t *ill, ip6_t *ip6h, int fanout_flags,
11891     zoneid_t zoneid)
11892 {
11893 	ill_t *in_ill;
11894 	boolean_t wantpacket = B_TRUE;
11895 	in6_addr_t *v6dst_ptr = &ip6h->ip6_dst;
11896 	in6_addr_t *v6src_ptr = &ip6h->ip6_src;
11897 
11898 	/*
11899 	 * conn_incoming_ill is set by IPV6_BOUND_IF which limits
11900 	 * unicast and multicast reception to conn_incoming_ill.
11901 	 * conn_wantpacket_v6 is called both for unicast and
11902 	 * multicast.
11903 	 *
11904 	 * 1) The unicast copy of the packet can come anywhere in
11905 	 *    the ill group if it is part of the group. Thus, we
11906 	 *    need to check to see whether the ill group matches
11907 	 *    if in_ill is part of a group.
11908 	 *
11909 	 * 2) ip_rput does not suppress duplicate multicast packets.
11910 	 *    If there are two interfaces in a ill group and we have
11911 	 *    2 applications (conns) joined a multicast group G on
11912 	 *    both the interfaces, ilm_lookup_ill filter in ip_rput
11913 	 *    will give us two packets because we join G on both the
11914 	 *    interfaces rather than nominating just one interface
11915 	 *    for receiving multicast like broadcast above. So,
11916 	 *    we have to call ilg_lookup_ill to filter out duplicate
11917 	 *    copies, if ill is part of a group, to supress duplicates.
11918 	 */
11919 	in_ill = connp->conn_incoming_ill;
11920 	if (in_ill != NULL) {
11921 		mutex_enter(&connp->conn_lock);
11922 		in_ill = connp->conn_incoming_ill;
11923 		mutex_enter(&ill->ill_lock);
11924 		/*
11925 		 * No IPMP, and the packet did not arrive on conn_incoming_ill
11926 		 * OR, IPMP in use and the packet arrived on an IPMP group
11927 		 * different from the conn_incoming_ill's IPMP group.
11928 		 * Reject the packet.
11929 		 */
11930 		if ((in_ill->ill_group == NULL && in_ill != ill) ||
11931 		    (in_ill->ill_group != NULL &&
11932 		    in_ill->ill_group !=  ill->ill_group)) {
11933 			wantpacket = B_FALSE;
11934 		}
11935 		mutex_exit(&ill->ill_lock);
11936 		mutex_exit(&connp->conn_lock);
11937 		if (!wantpacket)
11938 			return (B_FALSE);
11939 	}
11940 
11941 	if (connp->conn_multi_router)
11942 		return (B_TRUE);
11943 
11944 	if (!IN6_IS_ADDR_MULTICAST(v6dst_ptr) &&
11945 	    !IN6_IS_ADDR_V4MAPPED_CLASSD(v6dst_ptr)) {
11946 		/*
11947 		 * Unicast case: we match the conn only if it's in the specified
11948 		 * zone.
11949 		 */
11950 		return (IPCL_ZONE_MATCH(connp, zoneid));
11951 	}
11952 
11953 	if ((fanout_flags & IP_FF_NO_MCAST_LOOP) &&
11954 	    (connp->conn_zoneid == zoneid || zoneid == ALL_ZONES)) {
11955 		/*
11956 		 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP
11957 		 * disabled, therefore we don't dispatch the multicast packet to
11958 		 * the sending zone.
11959 		 */
11960 		return (B_FALSE);
11961 	}
11962 
11963 	if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) &&
11964 	    connp->conn_zoneid != zoneid && zoneid != ALL_ZONES) {
11965 		/*
11966 		 * Multicast packet on the loopback interface: we only match
11967 		 * conns who joined the group in the specified zone.
11968 		 */
11969 		return (B_FALSE);
11970 	}
11971 
11972 	mutex_enter(&connp->conn_lock);
11973 	wantpacket =
11974 	    ilg_lookup_ill_withsrc_v6(connp, v6dst_ptr, v6src_ptr, ill) != NULL;
11975 	mutex_exit(&connp->conn_lock);
11976 
11977 	return (wantpacket);
11978 }
11979 
11980 
11981 /*
11982  * Transmit a packet and update any NUD state based on the flags
11983  * XXX need to "recover" any ip6i_t when doing putq!
11984  *
11985  * NOTE : This function does not ire_refrele the ire passed in as the
11986  * argument.
11987  */
11988 void
11989 ip_xmit_v6(mblk_t *mp, ire_t *ire, uint_t flags, conn_t *connp,
11990     int caller, ipsec_out_t *io)
11991 {
11992 	mblk_t		*mp1;
11993 	nce_t		*nce = ire->ire_nce;
11994 	ill_t		*ill;
11995 	ill_t		*out_ill;
11996 	uint64_t	delta;
11997 	ip6_t		*ip6h;
11998 	queue_t		*stq = ire->ire_stq;
11999 	ire_t		*ire1 = NULL;
12000 	ire_t		*save_ire = ire;
12001 	boolean_t	multirt_send = B_FALSE;
12002 	mblk_t		*next_mp = NULL;
12003 
12004 	ip6h = (ip6_t *)mp->b_rptr;
12005 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ire->ire_addr_v6));
12006 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
12007 	ASSERT(nce != NULL);
12008 	ASSERT(mp->b_datap->db_type == M_DATA);
12009 	ASSERT(stq != NULL);
12010 
12011 	ill = ire_to_ill(ire);
12012 	if (!ill) {
12013 		ip0dbg(("ip_xmit_v6: ire_to_ill failed\n"));
12014 		freemsg(mp);
12015 		return;
12016 	}
12017 
12018 	/*
12019 	 * If a packet is to be sent out an interface that is a 6to4
12020 	 * tunnel, outgoing IPv6 packets, with a 6to4 addressed IPv6
12021 	 * destination, must be checked to have a 6to4 prefix
12022 	 * (2002:V4ADDR::/48) that is NOT equal to the 6to4 prefix of
12023 	 * address configured on the sending interface.  Otherwise,
12024 	 * the packet was delivered to this interface in error and the
12025 	 * packet must be dropped.
12026 	 */
12027 	if ((ill->ill_is_6to4tun) && IN6_IS_ADDR_6TO4(&ip6h->ip6_dst)) {
12028 		ipif_t *ipif = ill->ill_ipif;
12029 
12030 		if (IN6_ARE_6TO4_PREFIX_EQUAL(&ipif->ipif_v6lcl_addr,
12031 		    &ip6h->ip6_dst)) {
12032 			if (ip_debug > 2) {
12033 				/* ip1dbg */
12034 				pr_addr_dbg("ip_xmit_v6: attempting to "
12035 				    "send 6to4 addressed IPv6 "
12036 				    "destination (%s) out the wrong "
12037 				    "interface.\n", AF_INET6,
12038 				    &ip6h->ip6_dst);
12039 			}
12040 			BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards);
12041 			freemsg(mp);
12042 			return;
12043 		}
12044 	}
12045 
12046 	/* Flow-control check has been done in ip_wput_ire_v6 */
12047 	if (IP_FLOW_CONTROLLED_ULP(ip6h->ip6_nxt) || caller == IP_WPUT ||
12048 	    caller == IP_WSRV || canput(stq->q_next)) {
12049 		uint32_t ill_index;
12050 
12051 		/*
12052 		 * In most cases, the emission loop below is entered only
12053 		 * once. Only in the case where the ire holds the
12054 		 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT
12055 		 * flagged ires in the bucket, and send the packet
12056 		 * through all crossed RTF_MULTIRT routes.
12057 		 */
12058 		if (ire->ire_flags & RTF_MULTIRT) {
12059 			/*
12060 			 * Multirouting case. The bucket where ire is stored
12061 			 * probably holds other RTF_MULTIRT flagged ires
12062 			 * to the destination. In this call to ip_xmit_v6,
12063 			 * we attempt to send the packet through all
12064 			 * those ires. Thus, we first ensure that ire is the
12065 			 * first RTF_MULTIRT ire in the bucket,
12066 			 * before walking the ire list.
12067 			 */
12068 			ire_t *first_ire;
12069 			irb_t *irb = ire->ire_bucket;
12070 			ASSERT(irb != NULL);
12071 			multirt_send = B_TRUE;
12072 
12073 			/* Make sure we do not omit any multiroute ire. */
12074 			IRB_REFHOLD(irb);
12075 			for (first_ire = irb->irb_ire;
12076 			    first_ire != NULL;
12077 			    first_ire = first_ire->ire_next) {
12078 				if ((first_ire->ire_flags & RTF_MULTIRT) &&
12079 				    (IN6_ARE_ADDR_EQUAL(&first_ire->ire_addr_v6,
12080 				    &ire->ire_addr_v6)) &&
12081 				    !(first_ire->ire_marks &
12082 					(IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)))
12083 					break;
12084 			}
12085 
12086 			if ((first_ire != NULL) && (first_ire != ire)) {
12087 				IRE_REFHOLD(first_ire);
12088 				/* ire will be released by the caller */
12089 				ire = first_ire;
12090 				nce = ire->ire_nce;
12091 				stq = ire->ire_stq;
12092 				ill = ire_to_ill(ire);
12093 			}
12094 			IRB_REFRELE(irb);
12095 		} else if (connp != NULL && IPCL_IS_TCP(connp) &&
12096 		    connp->conn_mdt_ok && !connp->conn_tcp->tcp_mdt &&
12097 		    ILL_MDT_USABLE(ill)) {
12098 			/*
12099 			 * This tcp connection was marked as MDT-capable, but
12100 			 * it has been turned off due changes in the interface.
12101 			 * Now that the interface support is back, turn it on
12102 			 * by notifying tcp.  We don't directly modify tcp_mdt,
12103 			 * since we leave all the details to the tcp code that
12104 			 * knows better.
12105 			 */
12106 			mblk_t *mdimp = ip_mdinfo_alloc(ill->ill_mdt_capab);
12107 
12108 			if (mdimp == NULL) {
12109 				ip0dbg(("ip_xmit_v6: can't re-enable MDT for "
12110 				    "connp %p (ENOMEM)\n", (void *)connp));
12111 			} else {
12112 				CONN_INC_REF(connp);
12113 				squeue_fill(connp->conn_sqp, mdimp, tcp_input,
12114 				    connp, SQTAG_TCP_INPUT_MCTL);
12115 			}
12116 		}
12117 
12118 		do {
12119 			mblk_t *mp_ip6h;
12120 
12121 			if (multirt_send) {
12122 				irb_t *irb;
12123 				/*
12124 				 * We are in a multiple send case, need to get
12125 				 * the next ire and make a duplicate of the
12126 				 * packet. ire1 holds here the next ire to
12127 				 * process in the bucket. If multirouting is
12128 				 * expected, any non-RTF_MULTIRT ire that has
12129 				 * the right destination address is ignored.
12130 				 */
12131 				irb = ire->ire_bucket;
12132 				ASSERT(irb != NULL);
12133 
12134 				IRB_REFHOLD(irb);
12135 				for (ire1 = ire->ire_next;
12136 				    ire1 != NULL;
12137 				    ire1 = ire1->ire_next) {
12138 					if (!(ire1->ire_flags & RTF_MULTIRT))
12139 						continue;
12140 					if (!IN6_ARE_ADDR_EQUAL(
12141 					    &ire1->ire_addr_v6,
12142 					    &ire->ire_addr_v6))
12143 						continue;
12144 					if (ire1->ire_marks &
12145 					    (IRE_MARK_CONDEMNED|
12146 					    IRE_MARK_HIDDEN))
12147 						continue;
12148 
12149 					/* Got one */
12150 					if (ire1 != save_ire) {
12151 						IRE_REFHOLD(ire1);
12152 					}
12153 					break;
12154 				}
12155 				IRB_REFRELE(irb);
12156 
12157 				if (ire1 != NULL) {
12158 					next_mp = copyb(mp);
12159 					if ((next_mp == NULL) ||
12160 					    ((mp->b_cont != NULL) &&
12161 						((next_mp->b_cont =
12162 						    dupmsg(mp->b_cont)) ==
12163 						    NULL))) {
12164 						freemsg(next_mp);
12165 						next_mp = NULL;
12166 						ire_refrele(ire1);
12167 						ire1 = NULL;
12168 					}
12169 				}
12170 
12171 				/* Last multiroute ire; don't loop anymore. */
12172 				if (ire1 == NULL) {
12173 					multirt_send = B_FALSE;
12174 				}
12175 			}
12176 
12177 			ill_index =
12178 			    ((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex;
12179 
12180 			/* Initiate IPPF processing */
12181 			if (IP6_OUT_IPP(flags)) {
12182 				ip_process(IPP_LOCAL_OUT, &mp, ill_index);
12183 				if (mp == NULL) {
12184 					BUMP_MIB(ill->ill_ip6_mib,
12185 					    ipv6OutDiscards);
12186 					if (next_mp != NULL)
12187 						freemsg(next_mp);
12188 					if (ire != save_ire) {
12189 						ire_refrele(ire);
12190 					}
12191 					return;
12192 				}
12193 				ip6h = (ip6_t *)mp->b_rptr;
12194 			}
12195 			mp_ip6h = mp;
12196 
12197 			/*
12198 			 * Check for fastpath, we need to hold nce_lock to
12199 			 * prevent fastpath update from chaining nce_fp_mp.
12200 			 */
12201 
12202 			ASSERT(nce->nce_ipversion != IPV4_VERSION);
12203 			mutex_enter(&nce->nce_lock);
12204 			if ((mp1 = nce->nce_fp_mp) != NULL) {
12205 				uint32_t hlen;
12206 				uchar_t	*rptr;
12207 
12208 				hlen = MBLKL(mp1);
12209 				rptr = mp->b_rptr - hlen;
12210 				/*
12211 				 * make sure there is room for the fastpath
12212 				 * datalink header
12213 				 */
12214 				if (rptr < mp->b_datap->db_base) {
12215 					mp1 = copyb(mp1);
12216 					mutex_exit(&nce->nce_lock);
12217 					if (mp1 == NULL) {
12218 						BUMP_MIB(ill->ill_ip6_mib,
12219 						    ipv6OutDiscards);
12220 						freemsg(mp);
12221 						if (next_mp != NULL)
12222 							freemsg(next_mp);
12223 						if (ire != save_ire) {
12224 							ire_refrele(ire);
12225 						}
12226 						return;
12227 					}
12228 					mp1->b_cont = mp;
12229 
12230 					/* Get the priority marking, if any */
12231 					mp1->b_band = mp->b_band;
12232 					mp = mp1;
12233 				} else {
12234 					mp->b_rptr = rptr;
12235 					/*
12236 					 * fastpath -  pre-pend datalink
12237 					 * header
12238 					 */
12239 					bcopy(mp1->b_rptr, rptr, hlen);
12240 					mutex_exit(&nce->nce_lock);
12241 				}
12242 			} else {
12243 				/*
12244 				 * Get the DL_UNITDATA_REQ.
12245 				 */
12246 				mp1 = nce->nce_res_mp;
12247 				if (mp1 == NULL) {
12248 					mutex_exit(&nce->nce_lock);
12249 					ip1dbg(("ip_xmit_v6: No resolution "
12250 					    "block ire = %p\n", (void *)ire));
12251 					freemsg(mp);
12252 					if (next_mp != NULL)
12253 						freemsg(next_mp);
12254 					if (ire != save_ire) {
12255 						ire_refrele(ire);
12256 					}
12257 					return;
12258 				}
12259 				/*
12260 				 * Prepend the DL_UNITDATA_REQ.
12261 				 */
12262 				mp1 = copyb(mp1);
12263 				mutex_exit(&nce->nce_lock);
12264 				if (mp1 == NULL) {
12265 					BUMP_MIB(ill->ill_ip6_mib,
12266 					    ipv6OutDiscards);
12267 					freemsg(mp);
12268 					if (next_mp != NULL)
12269 						freemsg(next_mp);
12270 					if (ire != save_ire) {
12271 						ire_refrele(ire);
12272 					}
12273 					return;
12274 				}
12275 				mp1->b_cont = mp;
12276 
12277 				/* Get the priority marking, if any */
12278 				mp1->b_band = mp->b_band;
12279 				mp = mp1;
12280 			}
12281 
12282 			out_ill = (ill_t *)stq->q_ptr;
12283 
12284 			DTRACE_PROBE4(ip6__physical__out__start,
12285 			    ill_t *, NULL, ill_t *, out_ill,
12286 			    ip6_t *, ip6h, mblk_t *, mp);
12287 
12288 			FW_HOOKS6(ip6_physical_out_event,
12289 			    ipv6firewall_physical_out,
12290 			    NULL, out_ill, ip6h, mp, mp_ip6h);
12291 
12292 			DTRACE_PROBE1(ip6__physical__out__end, mblk_t *, mp);
12293 
12294 			if (mp == NULL) {
12295 				if (multirt_send) {
12296 					ASSERT(ire1 != NULL);
12297 					if (ire != save_ire) {
12298 						ire_refrele(ire);
12299 					}
12300 					/*
12301 					 * Proceed with the next RTF_MULTIRT
12302 					 * ire, also set up the send-to queue
12303 					 * accordingly.
12304 					 */
12305 					ire = ire1;
12306 					ire1 = NULL;
12307 					stq = ire->ire_stq;
12308 					nce = ire->ire_nce;
12309 					ill = ire_to_ill(ire);
12310 					mp = next_mp;
12311 					next_mp = NULL;
12312 					continue;
12313 				} else {
12314 					ASSERT(next_mp == NULL);
12315 					ASSERT(ire1 == NULL);
12316 					break;
12317 				}
12318 			}
12319 
12320 			/*
12321 			 * Update ire counters; for save_ire, this has been
12322 			 * done by the caller.
12323 			 */
12324 			if (ire != save_ire) {
12325 				UPDATE_OB_PKT_COUNT(ire);
12326 				ire->ire_last_used_time = lbolt;
12327 			}
12328 
12329 			/*
12330 			 * Send it down.  XXX Do we want to flow control AH/ESP
12331 			 * packets that carry TCP payloads?  We don't flow
12332 			 * control TCP packets, but we should also not
12333 			 * flow-control TCP packets that have been protected.
12334 			 * We don't have an easy way to find out if an AH/ESP
12335 			 * packet was originally TCP or not currently.
12336 			 */
12337 			if (io == NULL) {
12338 				putnext(stq, mp);
12339 			} else {
12340 				/*
12341 				 * Safety Pup says: make sure this is
12342 				 * going to the right interface!
12343 				 */
12344 				if (io->ipsec_out_capab_ill_index !=
12345 				    ill_index) {
12346 					/* IPsec kstats: bump lose counter */
12347 					freemsg(mp1);
12348 				} else {
12349 					ipsec_hw_putnext(stq, mp);
12350 				}
12351 			}
12352 
12353 			if (nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT)) {
12354 				if (ire != save_ire) {
12355 					ire_refrele(ire);
12356 				}
12357 				if (multirt_send) {
12358 					ASSERT(ire1 != NULL);
12359 					/*
12360 					 * Proceed with the next RTF_MULTIRT
12361 					 * ire, also set up the send-to queue
12362 					 * accordingly.
12363 					 */
12364 					ire = ire1;
12365 					ire1 = NULL;
12366 					stq = ire->ire_stq;
12367 					nce = ire->ire_nce;
12368 					ill = ire_to_ill(ire);
12369 					mp = next_mp;
12370 					next_mp = NULL;
12371 					continue;
12372 				}
12373 				ASSERT(next_mp == NULL);
12374 				ASSERT(ire1 == NULL);
12375 				return;
12376 			}
12377 
12378 			ASSERT(nce->nce_state != ND_INCOMPLETE);
12379 
12380 			/*
12381 			 * Check for upper layer advice
12382 			 */
12383 			if (flags & IPV6_REACHABILITY_CONFIRMATION) {
12384 				/*
12385 				 * It should be o.k. to check the state without
12386 				 * a lock here, at most we lose an advice.
12387 				 */
12388 				nce->nce_last = TICK_TO_MSEC(lbolt64);
12389 				if (nce->nce_state != ND_REACHABLE) {
12390 
12391 					mutex_enter(&nce->nce_lock);
12392 					nce->nce_state = ND_REACHABLE;
12393 					nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT;
12394 					mutex_exit(&nce->nce_lock);
12395 					(void) untimeout(nce->nce_timeout_id);
12396 					if (ip_debug > 2) {
12397 						/* ip1dbg */
12398 						pr_addr_dbg("ip_xmit_v6: state"
12399 						    " for %s changed to"
12400 						    " REACHABLE\n", AF_INET6,
12401 						    &ire->ire_addr_v6);
12402 					}
12403 				}
12404 				if (ire != save_ire) {
12405 					ire_refrele(ire);
12406 				}
12407 				if (multirt_send) {
12408 					ASSERT(ire1 != NULL);
12409 					/*
12410 					 * Proceed with the next RTF_MULTIRT
12411 					 * ire, also set up the send-to queue
12412 					 * accordingly.
12413 					 */
12414 					ire = ire1;
12415 					ire1 = NULL;
12416 					stq = ire->ire_stq;
12417 					nce = ire->ire_nce;
12418 					ill = ire_to_ill(ire);
12419 					mp = next_mp;
12420 					next_mp = NULL;
12421 					continue;
12422 				}
12423 				ASSERT(next_mp == NULL);
12424 				ASSERT(ire1 == NULL);
12425 				return;
12426 			}
12427 
12428 			delta =  TICK_TO_MSEC(lbolt64) - nce->nce_last;
12429 			ip1dbg(("ip_xmit_v6: delta = %" PRId64
12430 			    " ill_reachable_time = %d \n", delta,
12431 			    ill->ill_reachable_time));
12432 			if (delta > (uint64_t)ill->ill_reachable_time) {
12433 				nce = ire->ire_nce;
12434 				mutex_enter(&nce->nce_lock);
12435 				switch (nce->nce_state) {
12436 				case ND_REACHABLE:
12437 				case ND_STALE:
12438 					/*
12439 					 * ND_REACHABLE is identical to
12440 					 * ND_STALE in this specific case. If
12441 					 * reachable time has expired for this
12442 					 * neighbor (delta is greater than
12443 					 * reachable time), conceptually, the
12444 					 * neighbor cache is no longer in
12445 					 * REACHABLE state, but already in
12446 					 * STALE state.  So the correct
12447 					 * transition here is to ND_DELAY.
12448 					 */
12449 					nce->nce_state = ND_DELAY;
12450 					mutex_exit(&nce->nce_lock);
12451 					NDP_RESTART_TIMER(nce,
12452 					    delay_first_probe_time);
12453 					if (ip_debug > 3) {
12454 						/* ip2dbg */
12455 						pr_addr_dbg("ip_xmit_v6: state"
12456 						    " for %s changed to"
12457 						    " DELAY\n", AF_INET6,
12458 						    &ire->ire_addr_v6);
12459 					}
12460 					break;
12461 				case ND_DELAY:
12462 				case ND_PROBE:
12463 					mutex_exit(&nce->nce_lock);
12464 					/* Timers have already started */
12465 					break;
12466 				case ND_UNREACHABLE:
12467 					/*
12468 					 * ndp timer has detected that this nce
12469 					 * is unreachable and initiated deleting
12470 					 * this nce and all its associated IREs.
12471 					 * This is a race where we found the
12472 					 * ire before it was deleted and have
12473 					 * just sent out a packet using this
12474 					 * unreachable nce.
12475 					 */
12476 					mutex_exit(&nce->nce_lock);
12477 					break;
12478 				default:
12479 					ASSERT(0);
12480 				}
12481 			}
12482 
12483 			if (multirt_send) {
12484 				ASSERT(ire1 != NULL);
12485 				/*
12486 				 * Proceed with the next RTF_MULTIRT ire,
12487 				 * Also set up the send-to queue accordingly.
12488 				 */
12489 				if (ire != save_ire) {
12490 					ire_refrele(ire);
12491 				}
12492 				ire = ire1;
12493 				ire1 = NULL;
12494 				stq = ire->ire_stq;
12495 				nce = ire->ire_nce;
12496 				ill = ire_to_ill(ire);
12497 				mp = next_mp;
12498 				next_mp = NULL;
12499 			}
12500 		} while (multirt_send);
12501 		/*
12502 		 * In the multirouting case, release the last ire used for
12503 		 * emission. save_ire will be released by the caller.
12504 		 */
12505 		if (ire != save_ire) {
12506 			ire_refrele(ire);
12507 		}
12508 	} else {
12509 		/*
12510 		 * Queue packet if we have an conn to give back pressure.
12511 		 * We can't queue packets intended for hardware acceleration
12512 		 * since we've tossed that state already. If the packet is
12513 		 * being fed back from ire_send_v6, we don't know the
12514 		 * position in the queue to enqueue the packet and we discard
12515 		 * the packet.
12516 		 */
12517 		if (ip_output_queue && (connp != NULL) && (io == NULL) &&
12518 		    (caller != IRE_SEND)) {
12519 			if (caller == IP_WSRV) {
12520 				connp->conn_did_putbq = 1;
12521 				(void) putbq(connp->conn_wq, mp);
12522 				conn_drain_insert(connp);
12523 				/*
12524 				 * caller == IP_WSRV implies we are
12525 				 * the service thread, and the
12526 				 * queue is already noenabled.
12527 				 * The check for canput and
12528 				 * the putbq is not atomic.
12529 				 * So we need to check again.
12530 				 */
12531 				if (canput(stq->q_next))
12532 					connp->conn_did_putbq = 0;
12533 			} else {
12534 				(void) putq(connp->conn_wq, mp);
12535 			}
12536 			return;
12537 		}
12538 		BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards);
12539 		freemsg(mp);
12540 		return;
12541 	}
12542 }
12543 
12544 /*
12545  * pr_addr_dbg function provides the needed buffer space to call
12546  * inet_ntop() function's 3rd argument. This function should be
12547  * used by any kernel routine which wants to save INET6_ADDRSTRLEN
12548  * stack buffer space in it's own stack frame. This function uses
12549  * a buffer from it's own stack and prints the information.
12550  * Example: pr_addr_dbg("func: no route for %s\n ", AF_INET, addr)
12551  *
12552  * Note:    This function can call inet_ntop() once.
12553  */
12554 void
12555 pr_addr_dbg(char *fmt1, int af, const void *addr)
12556 {
12557 	char	buf[INET6_ADDRSTRLEN];
12558 
12559 	if (fmt1 == NULL) {
12560 		ip0dbg(("pr_addr_dbg: Wrong arguments\n"));
12561 		return;
12562 	}
12563 
12564 	/*
12565 	 * This does not compare debug level and just prints
12566 	 * out. Thus it is the responsibility of the caller
12567 	 * to check the appropriate debug-level before calling
12568 	 * this function.
12569 	 */
12570 	if (ip_debug > 0) {
12571 		printf(fmt1, inet_ntop(af, addr, buf, sizeof (buf)));
12572 	}
12573 
12574 
12575 }
12576 
12577 
12578 /*
12579  * Return the length in bytes of the IPv6 headers (base header, ip6i_t
12580  * if needed and extension headers) that will be needed based on the
12581  * ip6_pkt_t structure passed by the caller.
12582  *
12583  * The returned length does not include the length of the upper level
12584  * protocol (ULP) header.
12585  */
12586 int
12587 ip_total_hdrs_len_v6(ip6_pkt_t *ipp)
12588 {
12589 	int len;
12590 
12591 	len = IPV6_HDR_LEN;
12592 	if (ipp->ipp_fields & IPPF_HAS_IP6I)
12593 		len += sizeof (ip6i_t);
12594 	if (ipp->ipp_fields & IPPF_HOPOPTS) {
12595 		ASSERT(ipp->ipp_hopoptslen != 0);
12596 		len += ipp->ipp_hopoptslen;
12597 	}
12598 	if (ipp->ipp_fields & IPPF_RTHDR) {
12599 		ASSERT(ipp->ipp_rthdrlen != 0);
12600 		len += ipp->ipp_rthdrlen;
12601 	}
12602 	/*
12603 	 * En-route destination options
12604 	 * Only do them if there's a routing header as well
12605 	 */
12606 	if ((ipp->ipp_fields & (IPPF_RTDSTOPTS|IPPF_RTHDR)) ==
12607 	    (IPPF_RTDSTOPTS|IPPF_RTHDR)) {
12608 		ASSERT(ipp->ipp_rtdstoptslen != 0);
12609 		len += ipp->ipp_rtdstoptslen;
12610 	}
12611 	if (ipp->ipp_fields & IPPF_DSTOPTS) {
12612 		ASSERT(ipp->ipp_dstoptslen != 0);
12613 		len += ipp->ipp_dstoptslen;
12614 	}
12615 	return (len);
12616 }
12617 
12618 /*
12619  * All-purpose routine to build a header chain of an IPv6 header
12620  * followed by any required extension headers and a proto header,
12621  * preceeded (where necessary) by an ip6i_t private header.
12622  *
12623  * The fields of the IPv6 header that are derived from the ip6_pkt_t
12624  * will be filled in appropriately.
12625  * Thus the caller must fill in the rest of the IPv6 header, such as
12626  * traffic class/flowid, source address (if not set here), hoplimit (if not
12627  * set here) and destination address.
12628  *
12629  * The extension headers and ip6i_t header will all be fully filled in.
12630  */
12631 void
12632 ip_build_hdrs_v6(uchar_t *ext_hdrs, uint_t ext_hdrs_len,
12633     ip6_pkt_t *ipp, uint8_t protocol)
12634 {
12635 	uint8_t *nxthdr_ptr;
12636 	uint8_t *cp;
12637 	ip6i_t	*ip6i;
12638 	ip6_t	*ip6h = (ip6_t *)ext_hdrs;
12639 
12640 	/*
12641 	 * If sending private ip6i_t header down (checksum info, nexthop,
12642 	 * or ifindex), adjust ip header pointer and set ip6i_t header pointer,
12643 	 * then fill it in. (The checksum info will be filled in by icmp).
12644 	 */
12645 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
12646 		ip6i = (ip6i_t *)ip6h;
12647 		ip6h = (ip6_t *)&ip6i[1];
12648 
12649 		ip6i->ip6i_flags = 0;
12650 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
12651 		if (ipp->ipp_fields & IPPF_IFINDEX ||
12652 		    ipp->ipp_fields & IPPF_SCOPE_ID) {
12653 			ASSERT(ipp->ipp_ifindex != 0);
12654 			ip6i->ip6i_flags |= IP6I_IFINDEX;
12655 			ip6i->ip6i_ifindex = ipp->ipp_ifindex;
12656 		}
12657 		if (ipp->ipp_fields & IPPF_ADDR) {
12658 			/*
12659 			 * Enable per-packet source address verification if
12660 			 * IPV6_PKTINFO specified the source address.
12661 			 * ip6_src is set in the transport's _wput function.
12662 			 */
12663 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(
12664 			    &ipp->ipp_addr));
12665 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
12666 		}
12667 		if (ipp->ipp_fields & IPPF_UNICAST_HOPS) {
12668 			ip6h->ip6_hops = ipp->ipp_unicast_hops;
12669 			/*
12670 			 * We need to set this flag so that IP doesn't
12671 			 * rewrite the IPv6 header's hoplimit with the
12672 			 * current default value.
12673 			 */
12674 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
12675 		}
12676 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
12677 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(
12678 			    &ipp->ipp_nexthop));
12679 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
12680 			ip6i->ip6i_nexthop = ipp->ipp_nexthop;
12681 		}
12682 		/*
12683 		 * tell IP this is an ip6i_t private header
12684 		 */
12685 		ip6i->ip6i_nxt = IPPROTO_RAW;
12686 	}
12687 	/* Initialize IPv6 header */
12688 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
12689 	if (ipp->ipp_fields & IPPF_TCLASS) {
12690 		ip6h->ip6_vcf = (ip6h->ip6_vcf & ~IPV6_FLOWINFO_TCLASS) |
12691 		    (ipp->ipp_tclass << 20);
12692 	}
12693 	if (ipp->ipp_fields & IPPF_ADDR)
12694 		ip6h->ip6_src = ipp->ipp_addr;
12695 
12696 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
12697 	cp = (uint8_t *)&ip6h[1];
12698 	/*
12699 	 * Here's where we have to start stringing together
12700 	 * any extension headers in the right order:
12701 	 * Hop-by-hop, destination, routing, and final destination opts.
12702 	 */
12703 	if (ipp->ipp_fields & IPPF_HOPOPTS) {
12704 		/* Hop-by-hop options */
12705 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
12706 
12707 		*nxthdr_ptr = IPPROTO_HOPOPTS;
12708 		nxthdr_ptr = &hbh->ip6h_nxt;
12709 
12710 		bcopy(ipp->ipp_hopopts, cp, ipp->ipp_hopoptslen);
12711 		cp += ipp->ipp_hopoptslen;
12712 	}
12713 	/*
12714 	 * En-route destination options
12715 	 * Only do them if there's a routing header as well
12716 	 */
12717 	if ((ipp->ipp_fields & (IPPF_RTDSTOPTS|IPPF_RTHDR)) ==
12718 	    (IPPF_RTDSTOPTS|IPPF_RTHDR)) {
12719 		ip6_dest_t *dst = (ip6_dest_t *)cp;
12720 
12721 		*nxthdr_ptr = IPPROTO_DSTOPTS;
12722 		nxthdr_ptr = &dst->ip6d_nxt;
12723 
12724 		bcopy(ipp->ipp_rtdstopts, cp, ipp->ipp_rtdstoptslen);
12725 		cp += ipp->ipp_rtdstoptslen;
12726 	}
12727 	/*
12728 	 * Routing header next
12729 	 */
12730 	if (ipp->ipp_fields & IPPF_RTHDR) {
12731 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
12732 
12733 		*nxthdr_ptr = IPPROTO_ROUTING;
12734 		nxthdr_ptr = &rt->ip6r_nxt;
12735 
12736 		bcopy(ipp->ipp_rthdr, cp, ipp->ipp_rthdrlen);
12737 		cp += ipp->ipp_rthdrlen;
12738 	}
12739 	/*
12740 	 * Do ultimate destination options
12741 	 */
12742 	if (ipp->ipp_fields & IPPF_DSTOPTS) {
12743 		ip6_dest_t *dest = (ip6_dest_t *)cp;
12744 
12745 		*nxthdr_ptr = IPPROTO_DSTOPTS;
12746 		nxthdr_ptr = &dest->ip6d_nxt;
12747 
12748 		bcopy(ipp->ipp_dstopts, cp, ipp->ipp_dstoptslen);
12749 		cp += ipp->ipp_dstoptslen;
12750 	}
12751 	/*
12752 	 * Now set the last header pointer to the proto passed in
12753 	 */
12754 	*nxthdr_ptr = protocol;
12755 	ASSERT((int)(cp - ext_hdrs) == ext_hdrs_len);
12756 }
12757 
12758 /*
12759  * Return a pointer to the routing header extension header
12760  * in the IPv6 header(s) chain passed in.
12761  * If none found, return NULL
12762  * Assumes that all extension headers are in same mblk as the v6 header
12763  */
12764 ip6_rthdr_t *
12765 ip_find_rthdr_v6(ip6_t *ip6h, uint8_t *endptr)
12766 {
12767 	ip6_dest_t	*desthdr;
12768 	ip6_frag_t	*fraghdr;
12769 	uint_t		hdrlen;
12770 	uint8_t		nexthdr;
12771 	uint8_t		*ptr = (uint8_t *)&ip6h[1];
12772 
12773 	if (ip6h->ip6_nxt == IPPROTO_ROUTING)
12774 		return ((ip6_rthdr_t *)ptr);
12775 
12776 	/*
12777 	 * The routing header will precede all extension headers
12778 	 * other than the hop-by-hop and destination options
12779 	 * extension headers, so if we see anything other than those,
12780 	 * we're done and didn't find it.
12781 	 * We could see a destination options header alone but no
12782 	 * routing header, in which case we'll return NULL as soon as
12783 	 * we see anything after that.
12784 	 * Hop-by-hop and destination option headers are identical,
12785 	 * so we can use either one we want as a template.
12786 	 */
12787 	nexthdr = ip6h->ip6_nxt;
12788 	while (ptr < endptr) {
12789 		/* Is there enough left for len + nexthdr? */
12790 		if (ptr + MIN_EHDR_LEN > endptr)
12791 			return (NULL);
12792 
12793 		switch (nexthdr) {
12794 		case IPPROTO_HOPOPTS:
12795 		case IPPROTO_DSTOPTS:
12796 			/* Assumes the headers are identical for hbh and dst */
12797 			desthdr = (ip6_dest_t *)ptr;
12798 			hdrlen = 8 * (desthdr->ip6d_len + 1);
12799 			nexthdr = desthdr->ip6d_nxt;
12800 			break;
12801 
12802 		case IPPROTO_ROUTING:
12803 			return ((ip6_rthdr_t *)ptr);
12804 
12805 		case IPPROTO_FRAGMENT:
12806 			fraghdr = (ip6_frag_t *)ptr;
12807 			hdrlen = sizeof (ip6_frag_t);
12808 			nexthdr = fraghdr->ip6f_nxt;
12809 			break;
12810 
12811 		default:
12812 			return (NULL);
12813 		}
12814 		ptr += hdrlen;
12815 	}
12816 	return (NULL);
12817 }
12818 
12819 /*
12820  * Called for source-routed packets originating on this node.
12821  * Manipulates the original routing header by moving every entry up
12822  * one slot, placing the first entry in the v6 header's v6_dst field,
12823  * and placing the ultimate destination in the routing header's last
12824  * slot.
12825  *
12826  * Returns the checksum diference between the ultimate destination
12827  * (last hop in the routing header when the packet is sent) and
12828  * the first hop (ip6_dst when the packet is sent)
12829  */
12830 uint32_t
12831 ip_massage_options_v6(ip6_t *ip6h, ip6_rthdr_t *rth)
12832 {
12833 	uint_t		numaddr;
12834 	uint_t		i;
12835 	in6_addr_t	*addrptr;
12836 	in6_addr_t	tmp;
12837 	ip6_rthdr0_t	*rthdr = (ip6_rthdr0_t *)rth;
12838 	uint32_t	cksm;
12839 	uint32_t	addrsum = 0;
12840 	uint16_t	*ptr;
12841 
12842 	/*
12843 	 * Perform any processing needed for source routing.
12844 	 * We know that all extension headers will be in the same mblk
12845 	 * as the IPv6 header.
12846 	 */
12847 
12848 	/*
12849 	 * If no segments left in header, or the header length field is zero,
12850 	 * don't move hop addresses around;
12851 	 * Checksum difference is zero.
12852 	 */
12853 	if ((rthdr->ip6r0_segleft == 0) || (rthdr->ip6r0_len == 0))
12854 		return (0);
12855 
12856 	ptr = (uint16_t *)&ip6h->ip6_dst;
12857 	cksm = 0;
12858 	for (i = 0; i < (sizeof (in6_addr_t) / sizeof (uint16_t)); i++) {
12859 		cksm += ptr[i];
12860 	}
12861 	cksm = (cksm & 0xFFFF) + (cksm >> 16);
12862 
12863 	/*
12864 	 * Here's where the fun begins - we have to
12865 	 * move all addresses up one spot, take the
12866 	 * first hop and make it our first ip6_dst,
12867 	 * and place the ultimate destination in the
12868 	 * newly-opened last slot.
12869 	 */
12870 	addrptr = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
12871 	numaddr = rthdr->ip6r0_len / 2;
12872 	tmp = *addrptr;
12873 	for (i = 0; i < (numaddr - 1); addrptr++, i++) {
12874 		*addrptr = addrptr[1];
12875 	}
12876 	*addrptr = ip6h->ip6_dst;
12877 	ip6h->ip6_dst = tmp;
12878 
12879 	/*
12880 	 * From the checksummed ultimate destination subtract the checksummed
12881 	 * current ip6_dst (the first hop address). Return that number.
12882 	 * (In the v4 case, the second part of this is done in each routine
12883 	 *  that calls ip_massage_options(). We do it all in this one place
12884 	 *  for v6).
12885 	 */
12886 	ptr = (uint16_t *)&ip6h->ip6_dst;
12887 	for (i = 0; i < (sizeof (in6_addr_t) / sizeof (uint16_t)); i++) {
12888 		addrsum += ptr[i];
12889 	}
12890 	cksm -= ((addrsum >> 16) + (addrsum & 0xFFFF));
12891 	if ((int)cksm < 0)
12892 		cksm--;
12893 	cksm = (cksm & 0xFFFF) + (cksm >> 16);
12894 
12895 	return (cksm);
12896 }
12897 
12898 /*
12899  * See if the upper-level protocol indicated by 'proto' will be able
12900  * to do something with an ICMP_FRAGMENTATION_NEEDED (IPv4) or
12901  * ICMP6_PACKET_TOO_BIG (IPv6).
12902  */
12903 static boolean_t
12904 ip_ulp_cando_pkt2big(int proto)
12905 {
12906 	/*
12907 	 * For now, only TCP can handle this.
12908 	 * Tunnels may be able to also, but since tun isn't working over
12909 	 * IPv6 yet, don't worry about it for now.
12910 	 */
12911 	return (proto == IPPROTO_TCP);
12912 }
12913 
12914 
12915 /*
12916  * Propagate a multicast group membership operation (join/leave) (*fn) on
12917  * all interfaces crossed by the related multirt routes.
12918  * The call is considered successful if the operation succeeds
12919  * on at least one interface.
12920  * The function is called if the destination address in the packet to send
12921  * is multirouted.
12922  */
12923 int
12924 ip_multirt_apply_membership_v6(int (*fn)(conn_t *, boolean_t,
12925     const in6_addr_t *, int, mcast_record_t, const in6_addr_t *, mblk_t *),
12926     ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6grp,
12927     mcast_record_t fmode, const in6_addr_t *v6src, mblk_t *first_mp)
12928 {
12929 	ire_t		*ire_gw;
12930 	irb_t		*irb;
12931 	int		index, error = 0;
12932 	opt_restart_t	*or;
12933 
12934 	irb = ire->ire_bucket;
12935 	ASSERT(irb != NULL);
12936 
12937 	ASSERT(DB_TYPE(first_mp) == M_CTL);
12938 	or = (opt_restart_t *)first_mp->b_rptr;
12939 
12940 	IRB_REFHOLD(irb);
12941 	for (; ire != NULL; ire = ire->ire_next) {
12942 		if ((ire->ire_flags & RTF_MULTIRT) == 0)
12943 			continue;
12944 		if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6grp))
12945 			continue;
12946 
12947 		ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0,
12948 		    IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL,
12949 		    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE);
12950 		/* No resolver exists for the gateway; skip this ire. */
12951 		if (ire_gw == NULL)
12952 			continue;
12953 		index = ire_gw->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex;
12954 		/*
12955 		 * A resolver exists: we can get the interface on which we have
12956 		 * to apply the operation.
12957 		 */
12958 		error = fn(connp, checkonly, v6grp, index, fmode, v6src,
12959 		    first_mp);
12960 		if (error == 0)
12961 			or->or_private = CGTP_MCAST_SUCCESS;
12962 
12963 		if (ip_debug > 0) {
12964 			ulong_t	off;
12965 			char	*ksym;
12966 
12967 			ksym = kobj_getsymname((uintptr_t)fn, &off);
12968 			ip2dbg(("ip_multirt_apply_membership_v6: "
12969 			    "called %s, multirt group 0x%08x via itf 0x%08x, "
12970 			    "error %d [success %u]\n",
12971 			    ksym ? ksym : "?",
12972 			    ntohl(V4_PART_OF_V6((*v6grp))),
12973 			    ntohl(V4_PART_OF_V6(ire_gw->ire_src_addr_v6)),
12974 			    error, or->or_private));
12975 		}
12976 
12977 		ire_refrele(ire_gw);
12978 		if (error == EINPROGRESS) {
12979 			IRB_REFRELE(irb);
12980 			return (error);
12981 		}
12982 	}
12983 	IRB_REFRELE(irb);
12984 	/*
12985 	 * Consider the call as successful if we succeeded on at least
12986 	 * one interface. Otherwise, return the last encountered error.
12987 	 */
12988 	return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error);
12989 }
12990 
12991 void
12992 ip6_kstat_init(void)
12993 {
12994 	if ((ip6_kstat = kstat_create("ip", 0, "ip6stat",
12995 		"net", KSTAT_TYPE_NAMED,
12996 		sizeof (ip6_statistics) / sizeof (kstat_named_t),
12997 		KSTAT_FLAG_VIRTUAL)) != NULL) {
12998 		ip6_kstat->ks_data = &ip6_statistics;
12999 		kstat_install(ip6_kstat);
13000 	}
13001 }
13002 
13003 /*
13004  * The following two functions set and get the value for the
13005  * IPV6_SRC_PREFERENCES socket option.
13006  */
13007 int
13008 ip6_set_src_preferences(conn_t *connp, uint32_t prefs)
13009 {
13010 	/*
13011 	 * We only support preferences that are covered by
13012 	 * IPV6_PREFER_SRC_MASK.
13013 	 */
13014 	if (prefs & ~IPV6_PREFER_SRC_MASK)
13015 		return (EINVAL);
13016 
13017 	/*
13018 	 * Look for conflicting preferences or default preferences.  If
13019 	 * both bits of a related pair are clear, the application wants the
13020 	 * system's default value for that pair.  Both bits in a pair can't
13021 	 * be set.
13022 	 */
13023 	if ((prefs & IPV6_PREFER_SRC_MIPMASK) == 0) {
13024 		prefs |= IPV6_PREFER_SRC_MIPDEFAULT;
13025 	} else if ((prefs & IPV6_PREFER_SRC_MIPMASK) ==
13026 	    IPV6_PREFER_SRC_MIPMASK) {
13027 		return (EINVAL);
13028 	}
13029 	if ((prefs & IPV6_PREFER_SRC_TMPMASK) == 0) {
13030 		prefs |= IPV6_PREFER_SRC_TMPDEFAULT;
13031 	} else if ((prefs & IPV6_PREFER_SRC_TMPMASK) ==
13032 	    IPV6_PREFER_SRC_TMPMASK) {
13033 		return (EINVAL);
13034 	}
13035 	if ((prefs & IPV6_PREFER_SRC_CGAMASK) == 0) {
13036 		prefs |= IPV6_PREFER_SRC_CGADEFAULT;
13037 	} else if ((prefs & IPV6_PREFER_SRC_CGAMASK) ==
13038 	    IPV6_PREFER_SRC_CGAMASK) {
13039 		return (EINVAL);
13040 	}
13041 
13042 	connp->conn_src_preferences = prefs;
13043 	return (0);
13044 }
13045 
13046 size_t
13047 ip6_get_src_preferences(conn_t *connp, uint32_t *val)
13048 {
13049 	*val = connp->conn_src_preferences;
13050 	return (sizeof (connp->conn_src_preferences));
13051 }
13052 
13053 int
13054 ip6_set_pktinfo(cred_t *cr, conn_t *connp, struct in6_pktinfo *pkti, mblk_t *mp)
13055 {
13056 	ill_t	*ill;
13057 	ire_t	*ire;
13058 	int	error;
13059 
13060 	/*
13061 	 * Verify the source address and ifindex. Privileged users can use
13062 	 * any source address.  For ancillary data the source address is
13063 	 * checked in ip_wput_v6.
13064 	 */
13065 	if (pkti->ipi6_ifindex != 0) {
13066 		ASSERT(connp != NULL);
13067 		ill = ill_lookup_on_ifindex(pkti->ipi6_ifindex, B_TRUE,
13068 		    CONNP_TO_WQ(connp), mp, ip_restart_optmgmt, &error);
13069 		if (ill == NULL) {
13070 			/*
13071 			 * We just want to know if the interface exists, we
13072 			 * don't really care about the ill pointer itself.
13073 			 */
13074 			if (error != EINPROGRESS)
13075 				return (error);
13076 			error = 0;	/* Ensure we don't use it below */
13077 		} else {
13078 			ill_refrele(ill);
13079 		}
13080 	}
13081 	if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr) &&
13082 	    secpolicy_net_rawaccess(cr) != 0) {
13083 		ire = ire_route_lookup_v6(&pkti->ipi6_addr, 0, 0,
13084 		    (IRE_LOCAL|IRE_LOOPBACK), NULL, NULL,
13085 		    connp->conn_zoneid, NULL, MATCH_IRE_TYPE);
13086 		if (ire != NULL)
13087 			ire_refrele(ire);
13088 		else
13089 			return (ENXIO);
13090 	}
13091 	return (0);
13092 }
13093 
13094 /*
13095  * Get the size of the IP options (including the IP headers size)
13096  * without including the AH header's size. If till_ah is B_FALSE,
13097  * and if AH header is present, dest options beyond AH header will
13098  * also be included in the returned size.
13099  */
13100 int
13101 ipsec_ah_get_hdr_size_v6(mblk_t *mp, boolean_t till_ah)
13102 {
13103 	ip6_t *ip6h;
13104 	uint8_t nexthdr;
13105 	uint8_t *whereptr;
13106 	ip6_hbh_t *hbhhdr;
13107 	ip6_dest_t *dsthdr;
13108 	ip6_rthdr_t *rthdr;
13109 	int ehdrlen;
13110 	int size;
13111 	ah_t *ah;
13112 
13113 	ip6h = (ip6_t *)mp->b_rptr;
13114 	size = IPV6_HDR_LEN;
13115 	nexthdr = ip6h->ip6_nxt;
13116 	whereptr = (uint8_t *)&ip6h[1];
13117 	for (;;) {
13118 		/* Assume IP has already stripped it */
13119 		ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW);
13120 		switch (nexthdr) {
13121 		case IPPROTO_HOPOPTS:
13122 			hbhhdr = (ip6_hbh_t *)whereptr;
13123 			nexthdr = hbhhdr->ip6h_nxt;
13124 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
13125 			break;
13126 		case IPPROTO_DSTOPTS:
13127 			dsthdr = (ip6_dest_t *)whereptr;
13128 			nexthdr = dsthdr->ip6d_nxt;
13129 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
13130 			break;
13131 		case IPPROTO_ROUTING:
13132 			rthdr = (ip6_rthdr_t *)whereptr;
13133 			nexthdr = rthdr->ip6r_nxt;
13134 			ehdrlen = 8 * (rthdr->ip6r_len + 1);
13135 			break;
13136 		default :
13137 			if (till_ah) {
13138 				ASSERT(nexthdr == IPPROTO_AH);
13139 				return (size);
13140 			}
13141 			/*
13142 			 * If we don't have a AH header to traverse,
13143 			 * return now. This happens normally for
13144 			 * outbound datagrams where we have not inserted
13145 			 * the AH header.
13146 			 */
13147 			if (nexthdr != IPPROTO_AH) {
13148 				return (size);
13149 			}
13150 
13151 			/*
13152 			 * We don't include the AH header's size
13153 			 * to be symmetrical with other cases where
13154 			 * we either don't have a AH header (outbound)
13155 			 * or peek into the AH header yet (inbound and
13156 			 * not pulled up yet).
13157 			 */
13158 			ah = (ah_t *)whereptr;
13159 			nexthdr = ah->ah_nexthdr;
13160 			ehdrlen = (ah->ah_length << 2) + 8;
13161 
13162 			if (nexthdr == IPPROTO_DSTOPTS) {
13163 				if (whereptr + ehdrlen >= mp->b_wptr) {
13164 					/*
13165 					 * The destination options header
13166 					 * is not part of the first mblk.
13167 					 */
13168 					whereptr = mp->b_cont->b_rptr;
13169 				} else {
13170 					whereptr += ehdrlen;
13171 				}
13172 
13173 				dsthdr = (ip6_dest_t *)whereptr;
13174 				ehdrlen = 8 * (dsthdr->ip6d_len + 1);
13175 				size += ehdrlen;
13176 			}
13177 			return (size);
13178 		}
13179 		whereptr += ehdrlen;
13180 		size += ehdrlen;
13181 	}
13182 }
13183