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