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