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