xref: /titanic_52/usr/src/uts/common/inet/ip/ip6.c (revision d60099826db8cad80752050dc6430999f467c5e5)
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 2008 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_cred(IPV6_HDR_LEN + len, DB_CRED(mp));
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 		ll_multicast = ip_get_dlpi_mbcast(ill, mp);
6699 
6700 		/* Save the DLPI header. */
6701 		dl_mp = mp;
6702 		mp = mp->b_cont;
6703 		dl_mp->b_cont = NULL;
6704 		break;
6705 	case M_BREAK:
6706 		panic("ip_rput_v6: got an M_BREAK");
6707 		/*NOTREACHED*/
6708 	case M_IOCACK:
6709 		iocp = (struct iocblk *)mp->b_rptr;
6710 		switch (iocp->ioc_cmd) {
6711 		case DL_IOC_HDR_INFO:
6712 			ill = (ill_t *)q->q_ptr;
6713 			ill_fastpath_ack(ill, mp);
6714 			return;
6715 
6716 		case SIOCGTUNPARAM:
6717 		case OSIOCGTUNPARAM:
6718 			ip_rput_other(NULL, q, mp, NULL);
6719 			return;
6720 
6721 		case SIOCSTUNPARAM:
6722 		case OSIOCSTUNPARAM:
6723 			/* Go through qwriter */
6724 			break;
6725 		default:
6726 			putnext(q, mp);
6727 			return;
6728 		}
6729 		/* FALLTHRU */
6730 	case M_ERROR:
6731 	case M_HANGUP:
6732 		mutex_enter(&ill->ill_lock);
6733 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6734 			mutex_exit(&ill->ill_lock);
6735 			freemsg(mp);
6736 			return;
6737 		}
6738 		ill_refhold_locked(ill);
6739 		mutex_exit(&ill->ill_lock);
6740 		qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE);
6741 		return;
6742 	case M_CTL:
6743 		if ((MBLKL(mp) > sizeof (int)) &&
6744 		    ((da_ipsec_t *)mp->b_rptr)->da_type == IPHADA_M_CTL) {
6745 			ASSERT(MBLKL(mp) >= sizeof (da_ipsec_t));
6746 			mctl_present = B_TRUE;
6747 			break;
6748 		}
6749 		putnext(q, mp);
6750 		return;
6751 	case M_IOCNAK:
6752 		iocp = (struct iocblk *)mp->b_rptr;
6753 		switch (iocp->ioc_cmd) {
6754 		case DL_IOC_HDR_INFO:
6755 		case SIOCGTUNPARAM:
6756 		case OSIOCGTUNPARAM:
6757 			ip_rput_other(NULL, q, mp, NULL);
6758 			return;
6759 
6760 		case SIOCSTUNPARAM:
6761 		case OSIOCSTUNPARAM:
6762 			mutex_enter(&ill->ill_lock);
6763 			if (ill->ill_state_flags & ILL_CONDEMNED) {
6764 				mutex_exit(&ill->ill_lock);
6765 				freemsg(mp);
6766 				return;
6767 			}
6768 			ill_refhold_locked(ill);
6769 			mutex_exit(&ill->ill_lock);
6770 			qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE);
6771 			return;
6772 		default:
6773 			break;
6774 		}
6775 		/* FALLTHRU */
6776 	default:
6777 		putnext(q, mp);
6778 		return;
6779 	}
6780 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives);
6781 	UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets,
6782 	    (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp));
6783 	/*
6784 	 * if db_ref > 1 then copymsg and free original. Packet may be
6785 	 * changed and do not want other entity who has a reference to this
6786 	 * message to trip over the changes. This is a blind change because
6787 	 * trying to catch all places that might change packet is too
6788 	 * difficult (since it may be a module above this one).
6789 	 */
6790 	if (mp->b_datap->db_ref > 1) {
6791 		mblk_t  *mp1;
6792 
6793 		mp1 = copymsg(mp);
6794 		freemsg(mp);
6795 		if (mp1 == NULL) {
6796 			first_mp = NULL;
6797 			goto discard;
6798 		}
6799 		mp = mp1;
6800 	}
6801 	first_mp = mp;
6802 	if (mctl_present) {
6803 		hada_mp = first_mp;
6804 		mp = first_mp->b_cont;
6805 	}
6806 
6807 	if (ip_check_v6_mblk(mp, ill) == -1)
6808 		return;
6809 
6810 	ip6h = (ip6_t *)mp->b_rptr;
6811 
6812 	DTRACE_PROBE4(ip6__physical__in__start,
6813 	    ill_t *, ill, ill_t *, NULL,
6814 	    ip6_t *, ip6h, mblk_t *, first_mp);
6815 
6816 	FW_HOOKS6(ipst->ips_ip6_physical_in_event,
6817 	    ipst->ips_ipv6firewall_physical_in,
6818 	    ill, NULL, ip6h, first_mp, mp, ll_multicast, ipst);
6819 
6820 	DTRACE_PROBE1(ip6__physical__in__end, mblk_t *, first_mp);
6821 
6822 	if (first_mp == NULL)
6823 		return;
6824 
6825 	if ((ip6h->ip6_vcf & IPV6_VERS_AND_FLOW_MASK) ==
6826 	    IPV6_DEFAULT_VERS_AND_FLOW) {
6827 		/*
6828 		 * It may be a bit too expensive to do this mapped address
6829 		 * check here, but in the interest of robustness, it seems
6830 		 * like the correct place.
6831 		 * TODO: Avoid this check for e.g. connected TCP sockets
6832 		 */
6833 		if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_src)) {
6834 			ip1dbg(("ip_rput_v6: pkt with mapped src addr\n"));
6835 			goto discard;
6836 		}
6837 
6838 		if (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src)) {
6839 			ip1dbg(("ip_rput_v6: pkt with loopback src"));
6840 			goto discard;
6841 		} else if (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst)) {
6842 			ip1dbg(("ip_rput_v6: pkt with loopback dst"));
6843 			goto discard;
6844 		}
6845 
6846 		flags |= (ll_multicast ? IP6_IN_LLMCAST : 0);
6847 		ip_rput_data_v6(q, ill, mp, ip6h, flags, hada_mp, dl_mp);
6848 	} else {
6849 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
6850 		goto discard;
6851 	}
6852 	freemsg(dl_mp);
6853 	return;
6854 
6855 discard:
6856 	if (dl_mp != NULL)
6857 		freeb(dl_mp);
6858 	freemsg(first_mp);
6859 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6860 }
6861 
6862 /*
6863  * Walk through the IPv6 packet in mp and see if there's an AH header
6864  * in it.  See if the AH header needs to get done before other headers in
6865  * the packet.  (Worker function for ipsec_early_ah_v6().)
6866  */
6867 #define	IPSEC_HDR_DONT_PROCESS	0
6868 #define	IPSEC_HDR_PROCESS	1
6869 #define	IPSEC_MEMORY_ERROR	2
6870 static int
6871 ipsec_needs_processing_v6(mblk_t *mp, uint8_t *nexthdr)
6872 {
6873 	uint_t	length;
6874 	uint_t	ehdrlen;
6875 	uint8_t *whereptr;
6876 	uint8_t *endptr;
6877 	uint8_t *nexthdrp;
6878 	ip6_dest_t *desthdr;
6879 	ip6_rthdr_t *rthdr;
6880 	ip6_t	*ip6h;
6881 
6882 	/*
6883 	 * For now just pullup everything.  In general, the less pullups,
6884 	 * the better, but there's so much squirrelling through anyway,
6885 	 * it's just easier this way.
6886 	 */
6887 	if (!pullupmsg(mp, -1)) {
6888 		return (IPSEC_MEMORY_ERROR);
6889 	}
6890 
6891 	ip6h = (ip6_t *)mp->b_rptr;
6892 	length = IPV6_HDR_LEN;
6893 	whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */
6894 	endptr = mp->b_wptr;
6895 
6896 	/*
6897 	 * We can't just use the argument nexthdr in the place
6898 	 * of nexthdrp becaue we don't dereference nexthdrp
6899 	 * till we confirm whether it is a valid address.
6900 	 */
6901 	nexthdrp = &ip6h->ip6_nxt;
6902 	while (whereptr < endptr) {
6903 		/* Is there enough left for len + nexthdr? */
6904 		if (whereptr + MIN_EHDR_LEN > endptr)
6905 			return (IPSEC_MEMORY_ERROR);
6906 
6907 		switch (*nexthdrp) {
6908 		case IPPROTO_HOPOPTS:
6909 		case IPPROTO_DSTOPTS:
6910 			/* Assumes the headers are identical for hbh and dst */
6911 			desthdr = (ip6_dest_t *)whereptr;
6912 			ehdrlen = 8 * (desthdr->ip6d_len + 1);
6913 			if ((uchar_t *)desthdr +  ehdrlen > endptr)
6914 				return (IPSEC_MEMORY_ERROR);
6915 			/*
6916 			 * Return DONT_PROCESS because the destination
6917 			 * options header may be for each hop in a
6918 			 * routing-header, and we only want AH if we're
6919 			 * finished with routing headers.
6920 			 */
6921 			if (*nexthdrp == IPPROTO_DSTOPTS)
6922 				return (IPSEC_HDR_DONT_PROCESS);
6923 			nexthdrp = &desthdr->ip6d_nxt;
6924 			break;
6925 		case IPPROTO_ROUTING:
6926 			rthdr = (ip6_rthdr_t *)whereptr;
6927 
6928 			/*
6929 			 * If there's more hops left on the routing header,
6930 			 * return now with DON'T PROCESS.
6931 			 */
6932 			if (rthdr->ip6r_segleft > 0)
6933 				return (IPSEC_HDR_DONT_PROCESS);
6934 
6935 			ehdrlen =  8 * (rthdr->ip6r_len + 1);
6936 			if ((uchar_t *)rthdr +  ehdrlen > endptr)
6937 				return (IPSEC_MEMORY_ERROR);
6938 			nexthdrp = &rthdr->ip6r_nxt;
6939 			break;
6940 		case IPPROTO_FRAGMENT:
6941 			/* Wait for reassembly */
6942 			return (IPSEC_HDR_DONT_PROCESS);
6943 		case IPPROTO_AH:
6944 			*nexthdr = IPPROTO_AH;
6945 			return (IPSEC_HDR_PROCESS);
6946 		case IPPROTO_NONE:
6947 			/* No next header means we're finished */
6948 		default:
6949 			return (IPSEC_HDR_DONT_PROCESS);
6950 		}
6951 		length += ehdrlen;
6952 		whereptr += ehdrlen;
6953 	}
6954 	panic("ipsec_needs_processing_v6");
6955 	/*NOTREACHED*/
6956 }
6957 
6958 /*
6959  * Path for AH if options are present. If this is the first time we are
6960  * sending a datagram to AH, allocate a IPSEC_IN message and prepend it.
6961  * Otherwise, just fanout.  Return value answers the boolean question:
6962  * "Did I consume the mblk you sent me?"
6963  *
6964  * Sometimes AH needs to be done before other IPv6 headers for security
6965  * reasons.  This function (and its ipsec_needs_processing_v6() above)
6966  * indicates if that is so, and fans out to the appropriate IPsec protocol
6967  * for the datagram passed in.
6968  */
6969 static boolean_t
6970 ipsec_early_ah_v6(queue_t *q, mblk_t *first_mp, boolean_t mctl_present,
6971     ill_t *ill, mblk_t *hada_mp, zoneid_t zoneid)
6972 {
6973 	mblk_t *mp;
6974 	uint8_t nexthdr;
6975 	ipsec_in_t *ii = NULL;
6976 	ah_t *ah;
6977 	ipsec_status_t ipsec_rc;
6978 	ip_stack_t	*ipst = ill->ill_ipst;
6979 	netstack_t	*ns = ipst->ips_netstack;
6980 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
6981 
6982 	ASSERT((hada_mp == NULL) || (!mctl_present));
6983 
6984 	switch (ipsec_needs_processing_v6(
6985 	    (mctl_present ? first_mp->b_cont : first_mp), &nexthdr)) {
6986 	case IPSEC_MEMORY_ERROR:
6987 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6988 		freemsg(hada_mp);
6989 		freemsg(first_mp);
6990 		return (B_TRUE);
6991 	case IPSEC_HDR_DONT_PROCESS:
6992 		return (B_FALSE);
6993 	}
6994 
6995 	/* Default means send it to AH! */
6996 	ASSERT(nexthdr == IPPROTO_AH);
6997 	if (!mctl_present) {
6998 		mp = first_mp;
6999 		first_mp = ipsec_in_alloc(B_FALSE, ipst->ips_netstack);
7000 		if (first_mp == NULL) {
7001 			ip1dbg(("ipsec_early_ah_v6: IPSEC_IN "
7002 			    "allocation failure.\n"));
7003 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7004 			freemsg(hada_mp);
7005 			freemsg(mp);
7006 			return (B_TRUE);
7007 		}
7008 		/*
7009 		 * Store the ill_index so that when we come back
7010 		 * from IPSEC we ride on the same queue.
7011 		 */
7012 		ii = (ipsec_in_t *)first_mp->b_rptr;
7013 		ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex;
7014 		ii->ipsec_in_rill_index = ii->ipsec_in_ill_index;
7015 		first_mp->b_cont = mp;
7016 	}
7017 	/*
7018 	 * Cache hardware acceleration info.
7019 	 */
7020 	if (hada_mp != NULL) {
7021 		ASSERT(ii != NULL);
7022 		IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_early_ah_v6: "
7023 		    "caching data attr.\n"));
7024 		ii->ipsec_in_accelerated = B_TRUE;
7025 		ii->ipsec_in_da = hada_mp;
7026 	}
7027 
7028 	if (!ipsec_loaded(ipss)) {
7029 		ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, zoneid, ipst);
7030 		return (B_TRUE);
7031 	}
7032 
7033 	ah = ipsec_inbound_ah_sa(first_mp, ns);
7034 	if (ah == NULL)
7035 		return (B_TRUE);
7036 	ASSERT(ii->ipsec_in_ah_sa != NULL);
7037 	ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL);
7038 	ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(first_mp, ah);
7039 
7040 	switch (ipsec_rc) {
7041 	case IPSEC_STATUS_SUCCESS:
7042 		/* we're done with IPsec processing, send it up */
7043 		ip_fanout_proto_again(first_mp, ill, ill, NULL);
7044 		break;
7045 	case IPSEC_STATUS_FAILED:
7046 		BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards);
7047 		break;
7048 	case IPSEC_STATUS_PENDING:
7049 		/* no action needed */
7050 		break;
7051 	}
7052 	return (B_TRUE);
7053 }
7054 
7055 /*
7056  * Validate the IPv6 mblk for alignment.
7057  */
7058 int
7059 ip_check_v6_mblk(mblk_t *mp, ill_t *ill)
7060 {
7061 	int pkt_len, ip6_len;
7062 	ip6_t *ip6h = (ip6_t *)mp->b_rptr;
7063 
7064 	/* check for alignment and full IPv6 header */
7065 	if (!OK_32PTR((uchar_t *)ip6h) ||
7066 	    (mp->b_wptr - (uchar_t *)ip6h) < IPV6_HDR_LEN) {
7067 		if (!pullupmsg(mp, IPV6_HDR_LEN)) {
7068 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7069 			ip1dbg(("ip_rput_v6: pullupmsg failed\n"));
7070 			freemsg(mp);
7071 			return (-1);
7072 		}
7073 		ip6h = (ip6_t *)mp->b_rptr;
7074 	}
7075 
7076 	ASSERT(OK_32PTR((uchar_t *)ip6h) &&
7077 	    (mp->b_wptr - (uchar_t *)ip6h) >= IPV6_HDR_LEN);
7078 
7079 	if (mp->b_cont == NULL)
7080 		pkt_len = mp->b_wptr - mp->b_rptr;
7081 	else
7082 		pkt_len = msgdsize(mp);
7083 	ip6_len = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
7084 
7085 	/*
7086 	 * Check for bogus (too short packet) and packet which
7087 	 * was padded by the link layer.
7088 	 */
7089 	if (ip6_len != pkt_len) {
7090 		ssize_t diff;
7091 
7092 		if (ip6_len > pkt_len) {
7093 			ip1dbg(("ip_rput_data_v6: packet too short %d %d\n",
7094 			    ip6_len, pkt_len));
7095 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7096 			freemsg(mp);
7097 			return (-1);
7098 		}
7099 		diff = (ssize_t)(pkt_len - ip6_len);
7100 
7101 		if (!adjmsg(mp, -diff)) {
7102 			ip1dbg(("ip_rput_data_v6: adjmsg failed\n"));
7103 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7104 			freemsg(mp);
7105 			return (-1);
7106 		}
7107 	}
7108 	return (0);
7109 }
7110 
7111 /*
7112  * ip_rput_data_v6 -- received IPv6 packets in M_DATA messages show up here.
7113  * ip_rput_v6 has already verified alignment, the min length, the version,
7114  * and db_ref = 1.
7115  *
7116  * The ill passed in (the arg named inill) is the ill that the packet
7117  * actually arrived on.  We need to remember this when saving the
7118  * input interface index into potential IPV6_PKTINFO data in
7119  * ip_add_info_v6().
7120  *
7121  * This routine doesn't free dl_mp; that's the caller's responsibility on
7122  * return.  (Note that the callers are complex enough that there's no tail
7123  * recursion here anyway.)
7124  */
7125 void
7126 ip_rput_data_v6(queue_t *q, ill_t *inill, mblk_t *mp, ip6_t *ip6h,
7127     uint_t flags, mblk_t *hada_mp, mblk_t *dl_mp)
7128 {
7129 	ire_t		*ire = NULL;
7130 	ill_t		*ill = inill;
7131 	ill_t		*outill;
7132 	ipif_t		*ipif;
7133 	uint8_t		*whereptr;
7134 	uint8_t		nexthdr;
7135 	uint16_t	remlen;
7136 	uint_t		prev_nexthdr_offset;
7137 	uint_t		used;
7138 	size_t		old_pkt_len;
7139 	size_t		pkt_len;
7140 	uint16_t	ip6_len;
7141 	uint_t		hdr_len;
7142 	boolean_t	mctl_present;
7143 	mblk_t		*first_mp;
7144 	mblk_t		*first_mp1;
7145 	boolean_t	no_forward;
7146 	ip6_hbh_t	*hbhhdr;
7147 	boolean_t	ll_multicast = (flags & IP6_IN_LLMCAST);
7148 	conn_t		*connp;
7149 	ilm_t		*ilm;
7150 	uint32_t	ports;
7151 	zoneid_t	zoneid = GLOBAL_ZONEID;
7152 	uint16_t	hck_flags, reass_hck_flags;
7153 	uint32_t	reass_sum;
7154 	boolean_t	cksum_err;
7155 	mblk_t		*mp1;
7156 	ip_stack_t	*ipst = inill->ill_ipst;
7157 
7158 	EXTRACT_PKT_MP(mp, first_mp, mctl_present);
7159 
7160 	if (hada_mp != NULL) {
7161 		/*
7162 		 * It's an IPsec accelerated packet.
7163 		 * Keep a pointer to the data attributes around until
7164 		 * we allocate the ipsecinfo structure.
7165 		 */
7166 		IPSECHW_DEBUG(IPSECHW_PKT,
7167 		    ("ip_rput_data_v6: inbound HW accelerated IPsec pkt\n"));
7168 		hada_mp->b_cont = NULL;
7169 		/*
7170 		 * Since it is accelerated, it came directly from
7171 		 * the ill.
7172 		 */
7173 		ASSERT(mctl_present == B_FALSE);
7174 		ASSERT(mp->b_datap->db_type != M_CTL);
7175 	}
7176 
7177 	ip6h = (ip6_t *)mp->b_rptr;
7178 	ip6_len = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
7179 	old_pkt_len = pkt_len = ip6_len;
7180 
7181 	if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum)
7182 		hck_flags = DB_CKSUMFLAGS(mp);
7183 	else
7184 		hck_flags = 0;
7185 
7186 	/* Clear checksum flags in case we need to forward */
7187 	DB_CKSUMFLAGS(mp) = 0;
7188 	reass_sum = reass_hck_flags = 0;
7189 
7190 	nexthdr = ip6h->ip6_nxt;
7191 
7192 	prev_nexthdr_offset = (uint_t)((uchar_t *)&ip6h->ip6_nxt -
7193 	    (uchar_t *)ip6h);
7194 	whereptr = (uint8_t *)&ip6h[1];
7195 	remlen = pkt_len - IPV6_HDR_LEN;	/* Track how much is left */
7196 
7197 	/* Process hop by hop header options */
7198 	if (nexthdr == IPPROTO_HOPOPTS) {
7199 		uint_t ehdrlen;
7200 		uint8_t *optptr;
7201 
7202 		if (remlen < MIN_EHDR_LEN)
7203 			goto pkt_too_short;
7204 		if (mp->b_cont != NULL &&
7205 		    whereptr + MIN_EHDR_LEN > mp->b_wptr) {
7206 			if (!pullupmsg(mp, IPV6_HDR_LEN + MIN_EHDR_LEN)) {
7207 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7208 				freemsg(hada_mp);
7209 				freemsg(first_mp);
7210 				return;
7211 			}
7212 			ip6h = (ip6_t *)mp->b_rptr;
7213 			whereptr = (uint8_t *)ip6h + pkt_len - remlen;
7214 		}
7215 		hbhhdr = (ip6_hbh_t *)whereptr;
7216 		nexthdr = hbhhdr->ip6h_nxt;
7217 		prev_nexthdr_offset = (uint_t)(whereptr - (uint8_t *)ip6h);
7218 		ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
7219 
7220 		if (remlen < ehdrlen)
7221 			goto pkt_too_short;
7222 		if (mp->b_cont != NULL &&
7223 		    whereptr + ehdrlen > mp->b_wptr) {
7224 			if (!pullupmsg(mp, IPV6_HDR_LEN + ehdrlen)) {
7225 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7226 				freemsg(hada_mp);
7227 				freemsg(first_mp);
7228 				return;
7229 			}
7230 			ip6h = (ip6_t *)mp->b_rptr;
7231 			whereptr = (uint8_t *)ip6h + pkt_len - remlen;
7232 			hbhhdr = (ip6_hbh_t *)whereptr;
7233 		}
7234 
7235 		optptr = whereptr + 2;
7236 		whereptr += ehdrlen;
7237 		remlen -= ehdrlen;
7238 		switch (ip_process_options_v6(q, first_mp, ip6h, optptr,
7239 		    ehdrlen - 2, IPPROTO_HOPOPTS, ipst)) {
7240 		case -1:
7241 			/*
7242 			 * Packet has been consumed and any
7243 			 * needed ICMP messages sent.
7244 			 */
7245 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7246 			freemsg(hada_mp);
7247 			return;
7248 		case 0:
7249 			/* no action needed */
7250 			break;
7251 		case 1:
7252 			/* Known router alert */
7253 			goto ipv6forus;
7254 		}
7255 	}
7256 
7257 	/*
7258 	 * Attach any necessary label information to this packet.
7259 	 */
7260 	if (is_system_labeled() && !tsol_get_pkt_label(mp, IPV6_VERSION)) {
7261 		if (ip6opt_ls != 0)
7262 			ip0dbg(("tsol_get_pkt_label v6 failed\n"));
7263 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7264 		freemsg(hada_mp);
7265 		freemsg(first_mp);
7266 		return;
7267 	}
7268 
7269 	/*
7270 	 * On incoming v6 multicast packets we will bypass the ire table,
7271 	 * and assume that the read queue corresponds to the targetted
7272 	 * interface.
7273 	 *
7274 	 * The effect of this is the same as the IPv4 original code, but is
7275 	 * much cleaner I think.  See ip_rput for how that was done.
7276 	 */
7277 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
7278 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts);
7279 		UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, pkt_len);
7280 		/*
7281 		 * XXX TODO Give to mrouted to for multicast forwarding.
7282 		 */
7283 		ILM_WALKER_HOLD(ill);
7284 		ilm = ilm_lookup_ill_v6(ill, &ip6h->ip6_dst, ALL_ZONES);
7285 		ILM_WALKER_RELE(ill);
7286 		if (ilm == NULL) {
7287 			if (ip_debug > 3) {
7288 				/* ip2dbg */
7289 				pr_addr_dbg("ip_rput_data_v6: got mcast packet"
7290 				    "  which is not for us: %s\n", AF_INET6,
7291 				    &ip6h->ip6_dst);
7292 			}
7293 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7294 			freemsg(hada_mp);
7295 			freemsg(first_mp);
7296 			return;
7297 		}
7298 		if (ip_debug > 3) {
7299 			/* ip2dbg */
7300 			pr_addr_dbg("ip_rput_data_v6: multicast for us: %s\n",
7301 			    AF_INET6, &ip6h->ip6_dst);
7302 		}
7303 		zoneid = GLOBAL_ZONEID;
7304 		goto ipv6forus;
7305 	}
7306 
7307 	ipif = ill->ill_ipif;
7308 
7309 	/*
7310 	 * If a packet was received on an interface that is a 6to4 tunnel,
7311 	 * incoming IPv6 packets, with a 6to4 addressed IPv6 destination, must
7312 	 * be checked to have a 6to4 prefix (2002:V4ADDR::/48) that is equal to
7313 	 * the 6to4 prefix of the address configured on the receiving interface.
7314 	 * Otherwise, the packet was delivered to this interface in error and
7315 	 * the packet must be dropped.
7316 	 */
7317 	if ((ill->ill_is_6to4tun) && IN6_IS_ADDR_6TO4(&ip6h->ip6_dst)) {
7318 
7319 		if (!IN6_ARE_6TO4_PREFIX_EQUAL(&ipif->ipif_v6lcl_addr,
7320 		    &ip6h->ip6_dst)) {
7321 			if (ip_debug > 2) {
7322 				/* ip1dbg */
7323 				pr_addr_dbg("ip_rput_data_v6: received 6to4 "
7324 				    "addressed packet which is not for us: "
7325 				    "%s\n", AF_INET6, &ip6h->ip6_dst);
7326 			}
7327 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7328 			freemsg(first_mp);
7329 			return;
7330 		}
7331 	}
7332 
7333 	/*
7334 	 * Find an ire that matches destination. For link-local addresses
7335 	 * we have to match the ill.
7336 	 * TBD for site local addresses.
7337 	 */
7338 	if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst)) {
7339 		ire = ire_ctable_lookup_v6(&ip6h->ip6_dst, NULL,
7340 		    IRE_CACHE|IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL,
7341 		    MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP, ipst);
7342 	} else {
7343 		ire = ire_cache_lookup_v6(&ip6h->ip6_dst, ALL_ZONES,
7344 		    MBLK_GETLABEL(mp), ipst);
7345 	}
7346 	if (ire == NULL) {
7347 		/*
7348 		 * No matching IRE found.  Mark this packet as having
7349 		 * originated externally.
7350 		 */
7351 		if (!(ill->ill_flags & ILLF_ROUTER) || ll_multicast) {
7352 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
7353 			if (!(ill->ill_flags & ILLF_ROUTER)) {
7354 				BUMP_MIB(ill->ill_ip_mib,
7355 				    ipIfStatsInAddrErrors);
7356 			}
7357 			freemsg(hada_mp);
7358 			freemsg(first_mp);
7359 			return;
7360 		}
7361 		if (ip6h->ip6_hops <= 1) {
7362 			if (hada_mp != NULL)
7363 				goto hada_drop;
7364 			/* Sent by forwarding path, and router is global zone */
7365 			icmp_time_exceeded_v6(WR(q), first_mp,
7366 			    ICMP6_TIME_EXCEED_TRANSIT, ll_multicast, B_FALSE,
7367 			    GLOBAL_ZONEID, ipst);
7368 			return;
7369 		}
7370 		/*
7371 		 * Per RFC 3513 section 2.5.2, we must not forward packets with
7372 		 * an unspecified source address.
7373 		 */
7374 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
7375 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
7376 			freemsg(hada_mp);
7377 			freemsg(first_mp);
7378 			return;
7379 		}
7380 		mp->b_prev = (mblk_t *)(uintptr_t)
7381 		    ill->ill_phyint->phyint_ifindex;
7382 		ip_newroute_v6(q, mp, &ip6h->ip6_dst, &ip6h->ip6_src,
7383 		    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ? ill : NULL,
7384 		    ALL_ZONES, ipst);
7385 		return;
7386 	}
7387 	/* we have a matching IRE */
7388 	if (ire->ire_stq != NULL) {
7389 		ill_group_t *ill_group;
7390 		ill_group_t *ire_group;
7391 
7392 		/*
7393 		 * To be quicker, we may wish not to chase pointers
7394 		 * (ire->ire_ipif->ipif_ill...) and instead store the
7395 		 * forwarding policy in the ire.  An unfortunate side-
7396 		 * effect of this would be requiring an ire flush whenever
7397 		 * the ILLF_ROUTER flag changes.  For now, chase pointers
7398 		 * once and store in the boolean no_forward.
7399 		 *
7400 		 * This appears twice to keep it out of the non-forwarding,
7401 		 * yes-it's-for-us-on-the-right-interface case.
7402 		 */
7403 		no_forward = ((ill->ill_flags &
7404 		    ire->ire_ipif->ipif_ill->ill_flags & ILLF_ROUTER) == 0);
7405 
7406 
7407 		ASSERT(first_mp == mp);
7408 		/*
7409 		 * This ire has a send-to queue - forward the packet.
7410 		 */
7411 		if (no_forward || ll_multicast || (hada_mp != NULL)) {
7412 			freemsg(hada_mp);
7413 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
7414 			if (no_forward) {
7415 				BUMP_MIB(ill->ill_ip_mib,
7416 				    ipIfStatsInAddrErrors);
7417 			}
7418 			freemsg(mp);
7419 			ire_refrele(ire);
7420 			return;
7421 		}
7422 		/*
7423 		 * ipIfStatsHCInForwDatagrams should only be increment if there
7424 		 * will be an attempt to forward the packet, which is why we
7425 		 * increment after the above condition has been checked.
7426 		 */
7427 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams);
7428 		if (ip6h->ip6_hops <= 1) {
7429 			ip1dbg(("ip_rput_data_v6: hop limit expired.\n"));
7430 			/* Sent by forwarding path, and router is global zone */
7431 			icmp_time_exceeded_v6(WR(q), mp,
7432 			    ICMP6_TIME_EXCEED_TRANSIT, ll_multicast, B_FALSE,
7433 			    GLOBAL_ZONEID, ipst);
7434 			ire_refrele(ire);
7435 			return;
7436 		}
7437 		/*
7438 		 * Per RFC 3513 section 2.5.2, we must not forward packets with
7439 		 * an unspecified source address.
7440 		 */
7441 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
7442 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
7443 			freemsg(mp);
7444 			ire_refrele(ire);
7445 			return;
7446 		}
7447 
7448 		if (is_system_labeled()) {
7449 			mblk_t *mp1;
7450 
7451 			if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) {
7452 				BUMP_MIB(ill->ill_ip_mib,
7453 				    ipIfStatsForwProhibits);
7454 				freemsg(mp);
7455 				ire_refrele(ire);
7456 				return;
7457 			}
7458 			/* Size may have changed */
7459 			mp = mp1;
7460 			ip6h = (ip6_t *)mp->b_rptr;
7461 			pkt_len = msgdsize(mp);
7462 		}
7463 
7464 		if (pkt_len > ire->ire_max_frag) {
7465 			int max_frag = ire->ire_max_frag;
7466 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTooBigErrors);
7467 			/*
7468 			 * Handle labeled packet resizing.
7469 			 */
7470 			if (is_system_labeled()) {
7471 				max_frag = tsol_pmtu_adjust(mp, max_frag,
7472 				    pkt_len - old_pkt_len, AF_INET6);
7473 			}
7474 
7475 			/* Sent by forwarding path, and router is global zone */
7476 			icmp_pkt2big_v6(WR(q), mp, max_frag,
7477 			    ll_multicast, B_TRUE, GLOBAL_ZONEID, ipst);
7478 			ire_refrele(ire);
7479 			return;
7480 		}
7481 
7482 		/*
7483 		 * Check to see if we're forwarding the packet to a
7484 		 * different link from which it came.  If so, check the
7485 		 * source and destination addresses since routers must not
7486 		 * forward any packets with link-local source or
7487 		 * destination addresses to other links.  Otherwise (if
7488 		 * we're forwarding onto the same link), conditionally send
7489 		 * a redirect message.
7490 		 */
7491 		ill_group = ill->ill_group;
7492 		ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group;
7493 		if (ire->ire_rfq != q && (ill_group == NULL ||
7494 		    ill_group != ire_group)) {
7495 			if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ||
7496 			    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) {
7497 				BUMP_MIB(ill->ill_ip_mib,
7498 				    ipIfStatsInAddrErrors);
7499 				freemsg(mp);
7500 				ire_refrele(ire);
7501 				return;
7502 			}
7503 			/* TBD add site-local check at site boundary? */
7504 		} else if (ipst->ips_ipv6_send_redirects) {
7505 			in6_addr_t	*v6targ;
7506 			in6_addr_t	gw_addr_v6;
7507 			ire_t		*src_ire_v6 = NULL;
7508 
7509 			/*
7510 			 * Don't send a redirect when forwarding a source
7511 			 * routed packet.
7512 			 */
7513 			if (ip_source_routed_v6(ip6h, mp, ipst))
7514 				goto forward;
7515 
7516 			mutex_enter(&ire->ire_lock);
7517 			gw_addr_v6 = ire->ire_gateway_addr_v6;
7518 			mutex_exit(&ire->ire_lock);
7519 			if (!IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) {
7520 				v6targ = &gw_addr_v6;
7521 				/*
7522 				 * We won't send redirects to a router
7523 				 * that doesn't have a link local
7524 				 * address, but will forward.
7525 				 */
7526 				if (!IN6_IS_ADDR_LINKLOCAL(v6targ)) {
7527 					BUMP_MIB(ill->ill_ip_mib,
7528 					    ipIfStatsInAddrErrors);
7529 					goto forward;
7530 				}
7531 			} else {
7532 				v6targ = &ip6h->ip6_dst;
7533 			}
7534 
7535 			src_ire_v6 = ire_ftable_lookup_v6(&ip6h->ip6_src,
7536 			    NULL, NULL, IRE_INTERFACE, ire->ire_ipif, NULL,
7537 			    ALL_ZONES, 0, NULL,
7538 			    MATCH_IRE_IPIF | MATCH_IRE_TYPE,
7539 			    ipst);
7540 
7541 			if (src_ire_v6 != NULL) {
7542 				/*
7543 				 * The source is directly connected.
7544 				 */
7545 				mp1 = copymsg(mp);
7546 				if (mp1 != NULL) {
7547 					icmp_send_redirect_v6(WR(q),
7548 					    mp1, v6targ, &ip6h->ip6_dst,
7549 					    ill, B_FALSE);
7550 				}
7551 				ire_refrele(src_ire_v6);
7552 			}
7553 		}
7554 
7555 forward:
7556 		/* Hoplimit verified above */
7557 		ip6h->ip6_hops--;
7558 
7559 		outill = ire->ire_ipif->ipif_ill;
7560 
7561 		DTRACE_PROBE4(ip6__forwarding__start,
7562 		    ill_t *, inill, ill_t *, outill,
7563 		    ip6_t *, ip6h, mblk_t *, mp);
7564 
7565 		FW_HOOKS6(ipst->ips_ip6_forwarding_event,
7566 		    ipst->ips_ipv6firewall_forwarding,
7567 		    inill, outill, ip6h, mp, mp, 0, ipst);
7568 
7569 		DTRACE_PROBE1(ip6__forwarding__end, mblk_t *, mp);
7570 
7571 		if (mp != NULL) {
7572 			UPDATE_IB_PKT_COUNT(ire);
7573 			ire->ire_last_used_time = lbolt;
7574 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams);
7575 			ip_xmit_v6(mp, ire, 0, NULL, B_FALSE, NULL);
7576 		}
7577 		IRE_REFRELE(ire);
7578 		return;
7579 	}
7580 
7581 	/*
7582 	 * Need to put on correct queue for reassembly to find it.
7583 	 * No need to use put() since reassembly has its own locks.
7584 	 * Note: multicast packets and packets destined to addresses
7585 	 * assigned to loopback (ire_rfq is NULL) will be reassembled on
7586 	 * the arriving ill. Unlike the IPv4 case, enabling strict
7587 	 * destination multihoming will prevent accepting packets
7588 	 * addressed to an IRE_LOCAL on lo0.
7589 	 */
7590 	if (ire->ire_rfq != q) {
7591 		if ((ire = ip_check_multihome(&ip6h->ip6_dst, ire, ill))
7592 		    == NULL) {
7593 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
7594 			freemsg(hada_mp);
7595 			freemsg(first_mp);
7596 			return;
7597 		}
7598 		if (ire->ire_rfq != NULL) {
7599 			q = ire->ire_rfq;
7600 			ill = (ill_t *)q->q_ptr;
7601 			ASSERT(ill != NULL);
7602 		}
7603 	}
7604 
7605 	zoneid = ire->ire_zoneid;
7606 	UPDATE_IB_PKT_COUNT(ire);
7607 	ire->ire_last_used_time = lbolt;
7608 	/* Don't use the ire after this point, we'll NULL it out to be sure. */
7609 	ire_refrele(ire);
7610 	ire = NULL;
7611 ipv6forus:
7612 	/*
7613 	 * Looks like this packet is for us one way or another.
7614 	 * This is where we'll process destination headers etc.
7615 	 */
7616 	for (; ; ) {
7617 		switch (nexthdr) {
7618 		case IPPROTO_TCP: {
7619 			uint16_t	*up;
7620 			uint32_t	sum;
7621 			int		offset;
7622 
7623 			hdr_len = pkt_len - remlen;
7624 
7625 			if (hada_mp != NULL) {
7626 				ip0dbg(("tcp hada drop\n"));
7627 				goto hada_drop;
7628 			}
7629 
7630 
7631 			/* TCP needs all of the TCP header */
7632 			if (remlen < TCP_MIN_HEADER_LENGTH)
7633 				goto pkt_too_short;
7634 			if (mp->b_cont != NULL &&
7635 			    whereptr + TCP_MIN_HEADER_LENGTH > mp->b_wptr) {
7636 				if (!pullupmsg(mp,
7637 				    hdr_len + TCP_MIN_HEADER_LENGTH)) {
7638 					BUMP_MIB(ill->ill_ip_mib,
7639 					    ipIfStatsInDiscards);
7640 					freemsg(first_mp);
7641 					return;
7642 				}
7643 				hck_flags = 0;
7644 				ip6h = (ip6_t *)mp->b_rptr;
7645 				whereptr = (uint8_t *)ip6h + hdr_len;
7646 			}
7647 			/*
7648 			 * Extract the offset field from the TCP header.
7649 			 */
7650 			offset = ((uchar_t *)ip6h)[hdr_len + 12] >> 4;
7651 			if (offset != 5) {
7652 				if (offset < 5) {
7653 					ip1dbg(("ip_rput_data_v6: short "
7654 					    "TCP data offset"));
7655 					BUMP_MIB(ill->ill_ip_mib,
7656 					    ipIfStatsInDiscards);
7657 					freemsg(first_mp);
7658 					return;
7659 				}
7660 				/*
7661 				 * There must be TCP options.
7662 				 * Make sure we can grab them.
7663 				 */
7664 				offset <<= 2;
7665 				if (remlen < offset)
7666 					goto pkt_too_short;
7667 				if (mp->b_cont != NULL &&
7668 				    whereptr + offset > mp->b_wptr) {
7669 					if (!pullupmsg(mp,
7670 					    hdr_len + offset)) {
7671 						BUMP_MIB(ill->ill_ip_mib,
7672 						    ipIfStatsInDiscards);
7673 						freemsg(first_mp);
7674 						return;
7675 					}
7676 					hck_flags = 0;
7677 					ip6h = (ip6_t *)mp->b_rptr;
7678 					whereptr = (uint8_t *)ip6h + hdr_len;
7679 				}
7680 			}
7681 
7682 			up = (uint16_t *)&ip6h->ip6_src;
7683 			/*
7684 			 * TCP checksum calculation.  First sum up the
7685 			 * pseudo-header fields:
7686 			 *  -	Source IPv6 address
7687 			 *  -	Destination IPv6 address
7688 			 *  -	TCP payload length
7689 			 *  -	TCP protocol ID
7690 			 */
7691 			sum = htons(IPPROTO_TCP + remlen) +
7692 			    up[0] + up[1] + up[2] + up[3] +
7693 			    up[4] + up[5] + up[6] + up[7] +
7694 			    up[8] + up[9] + up[10] + up[11] +
7695 			    up[12] + up[13] + up[14] + up[15];
7696 
7697 			/* Fold initial sum */
7698 			sum = (sum & 0xffff) + (sum >> 16);
7699 
7700 			mp1 = mp->b_cont;
7701 
7702 			if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0)
7703 				IP6_STAT(ipst, ip6_in_sw_cksum);
7704 
7705 			IP_CKSUM_RECV(hck_flags, sum, (uchar_t *)
7706 			    ((uchar_t *)mp->b_rptr + DB_CKSUMSTART(mp)),
7707 			    (int32_t)(whereptr - (uchar_t *)mp->b_rptr),
7708 			    mp, mp1, cksum_err);
7709 
7710 			if (cksum_err) {
7711 				BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs);
7712 
7713 				if (hck_flags & HCK_FULLCKSUM) {
7714 					IP6_STAT(ipst,
7715 					    ip6_tcp_in_full_hw_cksum_err);
7716 				} else if (hck_flags & HCK_PARTIALCKSUM) {
7717 					IP6_STAT(ipst,
7718 					    ip6_tcp_in_part_hw_cksum_err);
7719 				} else {
7720 					IP6_STAT(ipst, ip6_tcp_in_sw_cksum_err);
7721 				}
7722 				freemsg(first_mp);
7723 				return;
7724 			}
7725 tcp_fanout:
7726 			ip_fanout_tcp_v6(q, first_mp, ip6h, ill, inill,
7727 			    (flags|IP_FF_SEND_ICMP|IP_FF_SYN_ADDIRE|
7728 			    IP_FF_IPINFO), hdr_len, mctl_present, zoneid);
7729 			return;
7730 		}
7731 		case IPPROTO_SCTP:
7732 		{
7733 			sctp_hdr_t *sctph;
7734 			uint32_t calcsum, pktsum;
7735 			uint_t hdr_len = pkt_len - remlen;
7736 			sctp_stack_t *sctps;
7737 
7738 			sctps = inill->ill_ipst->ips_netstack->netstack_sctp;
7739 
7740 			/* SCTP needs all of the SCTP header */
7741 			if (remlen < sizeof (*sctph)) {
7742 				goto pkt_too_short;
7743 			}
7744 			if (whereptr + sizeof (*sctph) > mp->b_wptr) {
7745 				ASSERT(mp->b_cont != NULL);
7746 				if (!pullupmsg(mp, hdr_len + sizeof (*sctph))) {
7747 					BUMP_MIB(ill->ill_ip_mib,
7748 					    ipIfStatsInDiscards);
7749 					freemsg(mp);
7750 					return;
7751 				}
7752 				ip6h = (ip6_t *)mp->b_rptr;
7753 				whereptr = (uint8_t *)ip6h + hdr_len;
7754 			}
7755 
7756 			sctph = (sctp_hdr_t *)(mp->b_rptr + hdr_len);
7757 			/* checksum */
7758 			pktsum = sctph->sh_chksum;
7759 			sctph->sh_chksum = 0;
7760 			calcsum = sctp_cksum(mp, hdr_len);
7761 			if (calcsum != pktsum) {
7762 				BUMP_MIB(&sctps->sctps_mib, sctpChecksumError);
7763 				freemsg(mp);
7764 				return;
7765 			}
7766 			sctph->sh_chksum = pktsum;
7767 			ports = *(uint32_t *)(mp->b_rptr + hdr_len);
7768 			if ((connp = sctp_fanout(&ip6h->ip6_src, &ip6h->ip6_dst,
7769 			    ports, zoneid, mp, sctps)) == NULL) {
7770 				ip_fanout_sctp_raw(first_mp, ill,
7771 				    (ipha_t *)ip6h, B_FALSE, ports,
7772 				    mctl_present,
7773 				    (flags|IP_FF_SEND_ICMP|IP_FF_IPINFO),
7774 				    B_TRUE, zoneid);
7775 				return;
7776 			}
7777 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
7778 			sctp_input(connp, (ipha_t *)ip6h, mp, first_mp, ill,
7779 			    B_FALSE, mctl_present);
7780 			return;
7781 		}
7782 		case IPPROTO_UDP: {
7783 			uint16_t	*up;
7784 			uint32_t	sum;
7785 
7786 			hdr_len = pkt_len - remlen;
7787 
7788 			if (hada_mp != NULL) {
7789 				ip0dbg(("udp hada drop\n"));
7790 				goto hada_drop;
7791 			}
7792 
7793 			/* Verify that at least the ports are present */
7794 			if (remlen < UDPH_SIZE)
7795 				goto pkt_too_short;
7796 			if (mp->b_cont != NULL &&
7797 			    whereptr + UDPH_SIZE > mp->b_wptr) {
7798 				if (!pullupmsg(mp, hdr_len + UDPH_SIZE)) {
7799 					BUMP_MIB(ill->ill_ip_mib,
7800 					    ipIfStatsInDiscards);
7801 					freemsg(first_mp);
7802 					return;
7803 				}
7804 				hck_flags = 0;
7805 				ip6h = (ip6_t *)mp->b_rptr;
7806 				whereptr = (uint8_t *)ip6h + hdr_len;
7807 			}
7808 
7809 			/*
7810 			 *  Before going through the regular checksum
7811 			 *  calculation, make sure the received checksum
7812 			 *  is non-zero. RFC 2460 says, a 0x0000 checksum
7813 			 *  in a UDP packet (within IPv6 packet) is invalid
7814 			 *  and should be replaced by 0xffff. This makes
7815 			 *  sense as regular checksum calculation will
7816 			 *  pass for both the cases i.e. 0x0000 and 0xffff.
7817 			 *  Removing one of the case makes error detection
7818 			 *  stronger.
7819 			 */
7820 
7821 			if (((udpha_t *)whereptr)->uha_checksum == 0) {
7822 				/* 0x0000 checksum is invalid */
7823 				ip1dbg(("ip_rput_data_v6: Invalid UDP "
7824 				    "checksum value 0x0000\n"));
7825 				BUMP_MIB(ill->ill_ip_mib,
7826 				    udpIfStatsInCksumErrs);
7827 				freemsg(first_mp);
7828 				return;
7829 			}
7830 
7831 			up = (uint16_t *)&ip6h->ip6_src;
7832 
7833 			/*
7834 			 * UDP checksum calculation.  First sum up the
7835 			 * pseudo-header fields:
7836 			 *  -	Source IPv6 address
7837 			 *  -	Destination IPv6 address
7838 			 *  -	UDP payload length
7839 			 *  -	UDP protocol ID
7840 			 */
7841 
7842 			sum = htons(IPPROTO_UDP + remlen) +
7843 			    up[0] + up[1] + up[2] + up[3] +
7844 			    up[4] + up[5] + up[6] + up[7] +
7845 			    up[8] + up[9] + up[10] + up[11] +
7846 			    up[12] + up[13] + up[14] + up[15];
7847 
7848 			/* Fold initial sum */
7849 			sum = (sum & 0xffff) + (sum >> 16);
7850 
7851 			if (reass_hck_flags != 0) {
7852 				hck_flags = reass_hck_flags;
7853 
7854 				IP_CKSUM_RECV_REASS(hck_flags,
7855 				    (int32_t)(whereptr - (uchar_t *)mp->b_rptr),
7856 				    sum, reass_sum, cksum_err);
7857 			} else {
7858 				mp1 = mp->b_cont;
7859 
7860 				IP_CKSUM_RECV(hck_flags, sum, (uchar_t *)
7861 				    ((uchar_t *)mp->b_rptr + DB_CKSUMSTART(mp)),
7862 				    (int32_t)(whereptr - (uchar_t *)mp->b_rptr),
7863 				    mp, mp1, cksum_err);
7864 			}
7865 
7866 			if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0)
7867 				IP6_STAT(ipst, ip6_in_sw_cksum);
7868 
7869 			if (cksum_err) {
7870 				BUMP_MIB(ill->ill_ip_mib,
7871 				    udpIfStatsInCksumErrs);
7872 
7873 				if (hck_flags & HCK_FULLCKSUM)
7874 					IP6_STAT(ipst,
7875 					    ip6_udp_in_full_hw_cksum_err);
7876 				else if (hck_flags & HCK_PARTIALCKSUM)
7877 					IP6_STAT(ipst,
7878 					    ip6_udp_in_part_hw_cksum_err);
7879 				else
7880 					IP6_STAT(ipst, ip6_udp_in_sw_cksum_err);
7881 
7882 				freemsg(first_mp);
7883 				return;
7884 			}
7885 			goto udp_fanout;
7886 		}
7887 		case IPPROTO_ICMPV6: {
7888 			uint16_t	*up;
7889 			uint32_t	sum;
7890 			uint_t		hdr_len = pkt_len - remlen;
7891 
7892 			if (hada_mp != NULL) {
7893 				ip0dbg(("icmp hada drop\n"));
7894 				goto hada_drop;
7895 			}
7896 
7897 			up = (uint16_t *)&ip6h->ip6_src;
7898 			sum = htons(IPPROTO_ICMPV6 + remlen) +
7899 			    up[0] + up[1] + up[2] + up[3] +
7900 			    up[4] + up[5] + up[6] + up[7] +
7901 			    up[8] + up[9] + up[10] + up[11] +
7902 			    up[12] + up[13] + up[14] + up[15];
7903 			sum = (sum & 0xffff) + (sum >> 16);
7904 			sum = IP_CSUM(mp, hdr_len, sum);
7905 			if (sum != 0) {
7906 				/* IPv6 ICMP checksum failed */
7907 				ip1dbg(("ip_rput_data_v6: ICMPv6 checksum "
7908 				    "failed %x\n",
7909 				    sum));
7910 				BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInMsgs);
7911 				BUMP_MIB(ill->ill_icmp6_mib,
7912 				    ipv6IfIcmpInErrors);
7913 				freemsg(first_mp);
7914 				return;
7915 			}
7916 
7917 		icmp_fanout:
7918 			/* Check variable for testing applications */
7919 			if (ipst->ips_ipv6_drop_inbound_icmpv6) {
7920 				freemsg(first_mp);
7921 				return;
7922 			}
7923 			/*
7924 			 * Assume that there is always at least one conn for
7925 			 * ICMPv6 (in.ndpd) i.e. don't optimize the case
7926 			 * where there is no conn.
7927 			 */
7928 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
7929 				ASSERT(!IS_LOOPBACK((ill)));
7930 				/*
7931 				 * In the multicast case, applications may have
7932 				 * joined the group from different zones, so we
7933 				 * need to deliver the packet to each of them.
7934 				 * Loop through the multicast memberships
7935 				 * structures (ilm) on the receive ill and send
7936 				 * a copy of the packet up each matching one.
7937 				 */
7938 				ILM_WALKER_HOLD(ill);
7939 				for (ilm = ill->ill_ilm; ilm != NULL;
7940 				    ilm = ilm->ilm_next) {
7941 					if (ilm->ilm_flags & ILM_DELETED)
7942 						continue;
7943 					if (!IN6_ARE_ADDR_EQUAL(
7944 					    &ilm->ilm_v6addr, &ip6h->ip6_dst))
7945 						continue;
7946 					if (!ipif_lookup_zoneid(ill,
7947 					    ilm->ilm_zoneid, IPIF_UP, NULL))
7948 						continue;
7949 
7950 					first_mp1 = ip_copymsg(first_mp);
7951 					if (first_mp1 == NULL)
7952 						continue;
7953 					icmp_inbound_v6(q, first_mp1, ill,
7954 					    hdr_len, mctl_present, 0,
7955 					    ilm->ilm_zoneid, dl_mp);
7956 				}
7957 				ILM_WALKER_RELE(ill);
7958 			} else {
7959 				first_mp1 = ip_copymsg(first_mp);
7960 				if (first_mp1 != NULL)
7961 					icmp_inbound_v6(q, first_mp1, ill,
7962 					    hdr_len, mctl_present, 0, zoneid,
7963 					    dl_mp);
7964 			}
7965 			/* FALLTHRU */
7966 		default: {
7967 			/*
7968 			 * Handle protocols with which IPv6 is less intimate.
7969 			 */
7970 			uint_t proto_flags = IP_FF_RAWIP|IP_FF_IPINFO;
7971 
7972 			if (hada_mp != NULL) {
7973 				ip0dbg(("default hada drop\n"));
7974 				goto hada_drop;
7975 			}
7976 
7977 			/*
7978 			 * Enable sending ICMP for "Unknown" nexthdr
7979 			 * case. i.e. where we did not FALLTHRU from
7980 			 * IPPROTO_ICMPV6 processing case above.
7981 			 * If we did FALLTHRU, then the packet has already been
7982 			 * processed for IPPF, don't process it again in
7983 			 * ip_fanout_proto_v6; set IP6_NO_IPPOLICY in the
7984 			 * flags
7985 			 */
7986 			if (nexthdr != IPPROTO_ICMPV6)
7987 				proto_flags |= IP_FF_SEND_ICMP;
7988 			else
7989 				proto_flags |= IP6_NO_IPPOLICY;
7990 
7991 			ip_fanout_proto_v6(q, first_mp, ip6h, ill, inill,
7992 			    nexthdr, prev_nexthdr_offset, (flags|proto_flags),
7993 			    mctl_present, zoneid);
7994 			return;
7995 		}
7996 
7997 		case IPPROTO_DSTOPTS: {
7998 			uint_t ehdrlen;
7999 			uint8_t *optptr;
8000 			ip6_dest_t *desthdr;
8001 
8002 			/* Check if AH is present. */
8003 			if (ipsec_early_ah_v6(q, first_mp, mctl_present, ill,
8004 			    hada_mp, zoneid)) {
8005 				ip0dbg(("dst early hada drop\n"));
8006 				return;
8007 			}
8008 
8009 			/*
8010 			 * Reinitialize pointers, as ipsec_early_ah_v6() does
8011 			 * complete pullups.  We don't have to do more pullups
8012 			 * as a result.
8013 			 */
8014 			whereptr = (uint8_t *)((uintptr_t)mp->b_rptr +
8015 			    (uintptr_t)(whereptr - ((uint8_t *)ip6h)));
8016 			ip6h = (ip6_t *)mp->b_rptr;
8017 
8018 			if (remlen < MIN_EHDR_LEN)
8019 				goto pkt_too_short;
8020 
8021 			desthdr = (ip6_dest_t *)whereptr;
8022 			nexthdr = desthdr->ip6d_nxt;
8023 			prev_nexthdr_offset = (uint_t)(whereptr -
8024 			    (uint8_t *)ip6h);
8025 			ehdrlen = 8 * (desthdr->ip6d_len + 1);
8026 			if (remlen < ehdrlen)
8027 				goto pkt_too_short;
8028 			optptr = whereptr + 2;
8029 			/*
8030 			 * Note: XXX This code does not seem to make
8031 			 * distinction between Destination Options Header
8032 			 * being before/after Routing Header which can
8033 			 * happen if we are at the end of source route.
8034 			 * This may become significant in future.
8035 			 * (No real significant Destination Options are
8036 			 * defined/implemented yet ).
8037 			 */
8038 			switch (ip_process_options_v6(q, first_mp, ip6h, optptr,
8039 			    ehdrlen - 2, IPPROTO_DSTOPTS, ipst)) {
8040 			case -1:
8041 				/*
8042 				 * Packet has been consumed and any needed
8043 				 * ICMP errors sent.
8044 				 */
8045 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
8046 				freemsg(hada_mp);
8047 				return;
8048 			case 0:
8049 				/* No action needed  continue */
8050 				break;
8051 			case 1:
8052 				/*
8053 				 * Unnexpected return value
8054 				 * (Router alert is a Hop-by-Hop option)
8055 				 */
8056 #ifdef DEBUG
8057 				panic("ip_rput_data_v6: router "
8058 				    "alert hbh opt indication in dest opt");
8059 				/*NOTREACHED*/
8060 #else
8061 				freemsg(hada_mp);
8062 				freemsg(first_mp);
8063 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8064 				return;
8065 #endif
8066 			}
8067 			used = ehdrlen;
8068 			break;
8069 		}
8070 		case IPPROTO_FRAGMENT: {
8071 			ip6_frag_t *fraghdr;
8072 			size_t no_frag_hdr_len;
8073 
8074 			if (hada_mp != NULL) {
8075 				ip0dbg(("frag hada drop\n"));
8076 				goto hada_drop;
8077 			}
8078 
8079 			ASSERT(first_mp == mp);
8080 			if (remlen < sizeof (ip6_frag_t))
8081 				goto pkt_too_short;
8082 
8083 			if (mp->b_cont != NULL &&
8084 			    whereptr + sizeof (ip6_frag_t) > mp->b_wptr) {
8085 				if (!pullupmsg(mp,
8086 				    pkt_len - remlen + sizeof (ip6_frag_t))) {
8087 					BUMP_MIB(ill->ill_ip_mib,
8088 					    ipIfStatsInDiscards);
8089 					freemsg(mp);
8090 					return;
8091 				}
8092 				hck_flags = 0;
8093 				ip6h = (ip6_t *)mp->b_rptr;
8094 				whereptr = (uint8_t *)ip6h + pkt_len - remlen;
8095 			}
8096 
8097 			fraghdr = (ip6_frag_t *)whereptr;
8098 			used = (uint_t)sizeof (ip6_frag_t);
8099 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
8100 
8101 			/*
8102 			 * Invoke the CGTP (multirouting) filtering module to
8103 			 * process the incoming packet. Packets identified as
8104 			 * duplicates must be discarded. Filtering is active
8105 			 * only if the the ip_cgtp_filter ndd variable is
8106 			 * non-zero.
8107 			 */
8108 			if (ipst->ips_ip_cgtp_filter &&
8109 			    ipst->ips_ip_cgtp_filter_ops != NULL) {
8110 				int cgtp_flt_pkt;
8111 				netstackid_t stackid;
8112 
8113 				stackid = ipst->ips_netstack->netstack_stackid;
8114 
8115 				cgtp_flt_pkt =
8116 				    ipst->ips_ip_cgtp_filter_ops->cfo_filter_v6(
8117 				    stackid, inill->ill_phyint->phyint_ifindex,
8118 				    ip6h, fraghdr);
8119 				if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) {
8120 					freemsg(mp);
8121 					return;
8122 				}
8123 			}
8124 
8125 			/* Restore the flags */
8126 			DB_CKSUMFLAGS(mp) = hck_flags;
8127 
8128 			mp = ip_rput_frag_v6(q, mp, ip6h, fraghdr,
8129 			    remlen - used, &prev_nexthdr_offset,
8130 			    &reass_sum, &reass_hck_flags);
8131 			if (mp == NULL) {
8132 				/* Reassembly is still pending */
8133 				return;
8134 			}
8135 			/* The first mblk are the headers before the frag hdr */
8136 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
8137 
8138 			first_mp = mp;	/* mp has most likely changed! */
8139 			no_frag_hdr_len = mp->b_wptr - mp->b_rptr;
8140 			ip6h = (ip6_t *)mp->b_rptr;
8141 			nexthdr = ((char *)ip6h)[prev_nexthdr_offset];
8142 			whereptr = mp->b_rptr + no_frag_hdr_len;
8143 			remlen = ntohs(ip6h->ip6_plen)  +
8144 			    (uint16_t)(IPV6_HDR_LEN - no_frag_hdr_len);
8145 			pkt_len = msgdsize(mp);
8146 			used = 0;
8147 			break;
8148 		}
8149 		case IPPROTO_HOPOPTS:
8150 			if (hada_mp != NULL) {
8151 				ip0dbg(("hop hada drop\n"));
8152 				goto hada_drop;
8153 			}
8154 			/*
8155 			 * Illegal header sequence.
8156 			 * (Hop-by-hop headers are processed above
8157 			 *  and required to immediately follow IPv6 header)
8158 			 */
8159 			icmp_param_problem_v6(WR(q), first_mp,
8160 			    ICMP6_PARAMPROB_NEXTHEADER,
8161 			    prev_nexthdr_offset,
8162 			    B_FALSE, B_FALSE, zoneid, ipst);
8163 			return;
8164 		}
8165 		case IPPROTO_ROUTING: {
8166 			uint_t ehdrlen;
8167 			ip6_rthdr_t *rthdr;
8168 
8169 			/* Check if AH is present. */
8170 			if (ipsec_early_ah_v6(q, first_mp, mctl_present, ill,
8171 			    hada_mp, zoneid)) {
8172 				ip0dbg(("routing hada drop\n"));
8173 				return;
8174 			}
8175 
8176 			/*
8177 			 * Reinitialize pointers, as ipsec_early_ah_v6() does
8178 			 * complete pullups.  We don't have to do more pullups
8179 			 * as a result.
8180 			 */
8181 			whereptr = (uint8_t *)((uintptr_t)mp->b_rptr +
8182 			    (uintptr_t)(whereptr - ((uint8_t *)ip6h)));
8183 			ip6h = (ip6_t *)mp->b_rptr;
8184 
8185 			if (remlen < MIN_EHDR_LEN)
8186 				goto pkt_too_short;
8187 			rthdr = (ip6_rthdr_t *)whereptr;
8188 			nexthdr = rthdr->ip6r_nxt;
8189 			prev_nexthdr_offset = (uint_t)(whereptr -
8190 			    (uint8_t *)ip6h);
8191 			ehdrlen = 8 * (rthdr->ip6r_len + 1);
8192 			if (remlen < ehdrlen)
8193 				goto pkt_too_short;
8194 			if (rthdr->ip6r_segleft != 0) {
8195 				/* Not end of source route */
8196 				if (ll_multicast) {
8197 					BUMP_MIB(ill->ill_ip_mib,
8198 					    ipIfStatsForwProhibits);
8199 					freemsg(hada_mp);
8200 					freemsg(mp);
8201 					return;
8202 				}
8203 				ip_process_rthdr(q, mp, ip6h, rthdr, ill,
8204 				    flags, hada_mp, dl_mp);
8205 				return;
8206 			}
8207 			used = ehdrlen;
8208 			break;
8209 		}
8210 		case IPPROTO_AH:
8211 		case IPPROTO_ESP: {
8212 			/*
8213 			 * Fast path for AH/ESP. If this is the first time
8214 			 * we are sending a datagram to AH/ESP, allocate
8215 			 * a IPSEC_IN message and prepend it. Otherwise,
8216 			 * just fanout.
8217 			 */
8218 
8219 			ipsec_in_t *ii;
8220 			int ipsec_rc;
8221 			ipsec_stack_t *ipss;
8222 
8223 			ipss = ipst->ips_netstack->netstack_ipsec;
8224 			if (!mctl_present) {
8225 				ASSERT(first_mp == mp);
8226 				first_mp = ipsec_in_alloc(B_FALSE,
8227 				    ipst->ips_netstack);
8228 				if (first_mp == NULL) {
8229 					ip1dbg(("ip_rput_data_v6: IPSEC_IN "
8230 					    "allocation failure.\n"));
8231 					BUMP_MIB(ill->ill_ip_mib,
8232 					    ipIfStatsInDiscards);
8233 					freemsg(mp);
8234 					return;
8235 				}
8236 				/*
8237 				 * Store the ill_index so that when we come back
8238 				 * from IPSEC we ride on the same queue.
8239 				 */
8240 				ii = (ipsec_in_t *)first_mp->b_rptr;
8241 				ii->ipsec_in_ill_index =
8242 				    ill->ill_phyint->phyint_ifindex;
8243 				ii->ipsec_in_rill_index =
8244 				    ii->ipsec_in_ill_index;
8245 				first_mp->b_cont = mp;
8246 				/*
8247 				 * Cache hardware acceleration info.
8248 				 */
8249 				if (hada_mp != NULL) {
8250 					IPSECHW_DEBUG(IPSECHW_PKT,
8251 					    ("ip_rput_data_v6: "
8252 					    "caching data attr.\n"));
8253 					ii->ipsec_in_accelerated = B_TRUE;
8254 					ii->ipsec_in_da = hada_mp;
8255 					hada_mp = NULL;
8256 				}
8257 			} else {
8258 				ii = (ipsec_in_t *)first_mp->b_rptr;
8259 			}
8260 
8261 			if (!ipsec_loaded(ipss)) {
8262 				ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP,
8263 				    zoneid, ipst);
8264 				return;
8265 			}
8266 
8267 			/* select inbound SA and have IPsec process the pkt */
8268 			if (nexthdr == IPPROTO_ESP) {
8269 				esph_t *esph = ipsec_inbound_esp_sa(first_mp,
8270 				    ipst->ips_netstack);
8271 				if (esph == NULL)
8272 					return;
8273 				ASSERT(ii->ipsec_in_esp_sa != NULL);
8274 				ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func !=
8275 				    NULL);
8276 				ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(
8277 				    first_mp, esph);
8278 			} else {
8279 				ah_t *ah = ipsec_inbound_ah_sa(first_mp,
8280 				    ipst->ips_netstack);
8281 				if (ah == NULL)
8282 					return;
8283 				ASSERT(ii->ipsec_in_ah_sa != NULL);
8284 				ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func !=
8285 				    NULL);
8286 				ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(
8287 				    first_mp, ah);
8288 			}
8289 
8290 			switch (ipsec_rc) {
8291 			case IPSEC_STATUS_SUCCESS:
8292 				break;
8293 			case IPSEC_STATUS_FAILED:
8294 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8295 				/* FALLTHRU */
8296 			case IPSEC_STATUS_PENDING:
8297 				return;
8298 			}
8299 			/* we're done with IPsec processing, send it up */
8300 			ip_fanout_proto_again(first_mp, ill, inill, NULL);
8301 			return;
8302 		}
8303 		case IPPROTO_NONE:
8304 			/* All processing is done. Count as "delivered". */
8305 			freemsg(hada_mp);
8306 			freemsg(first_mp);
8307 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
8308 			return;
8309 		}
8310 		whereptr += used;
8311 		ASSERT(remlen >= used);
8312 		remlen -= used;
8313 	}
8314 	/* NOTREACHED */
8315 
8316 pkt_too_short:
8317 	ip1dbg(("ip_rput_data_v6: packet too short %d %lu %d\n",
8318 	    ip6_len, pkt_len, remlen));
8319 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
8320 	freemsg(hada_mp);
8321 	freemsg(first_mp);
8322 	return;
8323 udp_fanout:
8324 	if (mctl_present || IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
8325 		connp = NULL;
8326 	} else {
8327 		connp = ipcl_classify_v6(mp, IPPROTO_UDP, hdr_len, zoneid,
8328 		    ipst);
8329 		if ((connp != NULL) && (connp->conn_upq == NULL)) {
8330 			CONN_DEC_REF(connp);
8331 			connp = NULL;
8332 		}
8333 	}
8334 
8335 	if (connp == NULL) {
8336 		uint32_t	ports;
8337 
8338 		ports = *(uint32_t *)(mp->b_rptr + hdr_len +
8339 		    UDP_PORTS_OFFSET);
8340 		IP6_STAT(ipst, ip6_udp_slow_path);
8341 		ip_fanout_udp_v6(q, first_mp, ip6h, ports, ill, inill,
8342 		    (flags|IP_FF_SEND_ICMP|IP_FF_IPINFO), mctl_present,
8343 		    zoneid);
8344 		return;
8345 	}
8346 
8347 	if (CONN_UDP_FLOWCTLD(connp)) {
8348 		freemsg(first_mp);
8349 		BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
8350 		CONN_DEC_REF(connp);
8351 		return;
8352 	}
8353 
8354 	/* Initiate IPPF processing */
8355 	if (IP6_IN_IPP(flags, ipst)) {
8356 		ip_process(IPP_LOCAL_IN, &mp, ill->ill_phyint->phyint_ifindex);
8357 		if (mp == NULL) {
8358 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8359 			CONN_DEC_REF(connp);
8360 			return;
8361 		}
8362 	}
8363 
8364 	if (connp->conn_ip_recvpktinfo ||
8365 	    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) {
8366 		mp = ip_add_info_v6(mp, inill, &ip6h->ip6_dst);
8367 		if (mp == NULL) {
8368 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8369 			CONN_DEC_REF(connp);
8370 			return;
8371 		}
8372 	}
8373 
8374 	IP6_STAT(ipst, ip6_udp_fast_path);
8375 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
8376 
8377 	/* Send it upstream */
8378 	(connp->conn_recv)(connp, mp, NULL);
8379 
8380 	CONN_DEC_REF(connp);
8381 	freemsg(hada_mp);
8382 	return;
8383 
8384 hada_drop:
8385 	ip1dbg(("ip_rput_data_v6: malformed accelerated packet\n"));
8386 	/* IPsec kstats: bump counter here */
8387 	freemsg(hada_mp);
8388 	freemsg(first_mp);
8389 }
8390 
8391 /*
8392  * Reassemble fragment.
8393  * When it returns a completed message the first mblk will only contain
8394  * the headers prior to the fragment header.
8395  *
8396  * prev_nexthdr_offset is an offset indication of where the nexthdr field is
8397  * of the preceding header.  This is needed to patch the previous header's
8398  * nexthdr field when reassembly completes.
8399  */
8400 static mblk_t *
8401 ip_rput_frag_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h,
8402     ip6_frag_t *fraghdr, uint_t remlen, uint_t *prev_nexthdr_offset,
8403     uint32_t *cksum_val, uint16_t *cksum_flags)
8404 {
8405 	ill_t		*ill = (ill_t *)q->q_ptr;
8406 	uint32_t	ident = ntohl(fraghdr->ip6f_ident);
8407 	uint16_t	offset;
8408 	boolean_t	more_frags;
8409 	uint8_t		nexthdr = fraghdr->ip6f_nxt;
8410 	in6_addr_t	*v6dst_ptr;
8411 	in6_addr_t	*v6src_ptr;
8412 	uint_t		end;
8413 	uint_t		hdr_length;
8414 	size_t		count;
8415 	ipf_t		*ipf;
8416 	ipf_t		**ipfp;
8417 	ipfb_t		*ipfb;
8418 	mblk_t		*mp1;
8419 	uint8_t		ecn_info = 0;
8420 	size_t		msg_len;
8421 	mblk_t		*tail_mp;
8422 	mblk_t		*t_mp;
8423 	boolean_t	pruned = B_FALSE;
8424 	uint32_t	sum_val;
8425 	uint16_t	sum_flags;
8426 	ip_stack_t	*ipst = ill->ill_ipst;
8427 
8428 	if (cksum_val != NULL)
8429 		*cksum_val = 0;
8430 	if (cksum_flags != NULL)
8431 		*cksum_flags = 0;
8432 
8433 	/*
8434 	 * We utilize hardware computed checksum info only for UDP since
8435 	 * IP fragmentation is a normal occurence for the protocol.  In
8436 	 * addition, checksum offload support for IP fragments carrying
8437 	 * UDP payload is commonly implemented across network adapters.
8438 	 */
8439 	ASSERT(ill != NULL);
8440 	if (nexthdr == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) &&
8441 	    (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
8442 		mblk_t *mp1 = mp->b_cont;
8443 		int32_t len;
8444 
8445 		/* Record checksum information from the packet */
8446 		sum_val = (uint32_t)DB_CKSUM16(mp);
8447 		sum_flags = DB_CKSUMFLAGS(mp);
8448 
8449 		/* fragmented payload offset from beginning of mblk */
8450 		offset = (uint16_t)((uchar_t *)&fraghdr[1] - mp->b_rptr);
8451 
8452 		if ((sum_flags & HCK_PARTIALCKSUM) &&
8453 		    (mp1 == NULL || mp1->b_cont == NULL) &&
8454 		    offset >= (uint16_t)DB_CKSUMSTART(mp) &&
8455 		    ((len = offset - (uint16_t)DB_CKSUMSTART(mp)) & 1) == 0) {
8456 			uint32_t adj;
8457 			/*
8458 			 * Partial checksum has been calculated by hardware
8459 			 * and attached to the packet; in addition, any
8460 			 * prepended extraneous data is even byte aligned.
8461 			 * If any such data exists, we adjust the checksum;
8462 			 * this would also handle any postpended data.
8463 			 */
8464 			IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
8465 			    mp, mp1, len, adj);
8466 
8467 			/* One's complement subtract extraneous checksum */
8468 			if (adj >= sum_val)
8469 				sum_val = ~(adj - sum_val) & 0xFFFF;
8470 			else
8471 				sum_val -= adj;
8472 		}
8473 	} else {
8474 		sum_val = 0;
8475 		sum_flags = 0;
8476 	}
8477 
8478 	/* Clear hardware checksumming flag */
8479 	DB_CKSUMFLAGS(mp) = 0;
8480 
8481 	/*
8482 	 * Note: Fragment offset in header is in 8-octet units.
8483 	 * Clearing least significant 3 bits not only extracts
8484 	 * it but also gets it in units of octets.
8485 	 */
8486 	offset = ntohs(fraghdr->ip6f_offlg) & ~7;
8487 	more_frags = (fraghdr->ip6f_offlg & IP6F_MORE_FRAG);
8488 
8489 	/*
8490 	 * Is the more frags flag on and the payload length not a multiple
8491 	 * of eight?
8492 	 */
8493 	if (more_frags && (ntohs(ip6h->ip6_plen) & 7)) {
8494 		zoneid_t zoneid;
8495 
8496 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
8497 		zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst, ill, ipst);
8498 		if (zoneid == ALL_ZONES) {
8499 			freemsg(mp);
8500 			return (NULL);
8501 		}
8502 		icmp_param_problem_v6(WR(q), mp, ICMP6_PARAMPROB_HEADER,
8503 		    (uint32_t)((char *)&ip6h->ip6_plen -
8504 		    (char *)ip6h), B_FALSE, B_FALSE, zoneid, ipst);
8505 		return (NULL);
8506 	}
8507 
8508 	v6src_ptr = &ip6h->ip6_src;
8509 	v6dst_ptr = &ip6h->ip6_dst;
8510 	end = remlen;
8511 
8512 	hdr_length = (uint_t)((char *)&fraghdr[1] - (char *)ip6h);
8513 	end += offset;
8514 
8515 	/*
8516 	 * Would fragment cause reassembled packet to have a payload length
8517 	 * greater than IP_MAXPACKET - the max payload size?
8518 	 */
8519 	if (end > IP_MAXPACKET) {
8520 		zoneid_t	zoneid;
8521 
8522 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
8523 		zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst, ill, ipst);
8524 		if (zoneid == ALL_ZONES) {
8525 			freemsg(mp);
8526 			return (NULL);
8527 		}
8528 		icmp_param_problem_v6(WR(q), mp, ICMP6_PARAMPROB_HEADER,
8529 		    (uint32_t)((char *)&fraghdr->ip6f_offlg -
8530 		    (char *)ip6h), B_FALSE, B_FALSE, zoneid, ipst);
8531 		return (NULL);
8532 	}
8533 
8534 	/*
8535 	 * This packet just has one fragment. Reassembly not
8536 	 * needed.
8537 	 */
8538 	if (!more_frags && offset == 0) {
8539 		goto reass_done;
8540 	}
8541 
8542 	/*
8543 	 * Drop the fragmented as early as possible, if
8544 	 * we don't have resource(s) to re-assemble.
8545 	 */
8546 	if (ipst->ips_ip_reass_queue_bytes == 0) {
8547 		freemsg(mp);
8548 		return (NULL);
8549 	}
8550 
8551 	/* Record the ECN field info. */
8552 	ecn_info = (uint8_t)(ntohl(ip6h->ip6_vcf & htonl(~0xFFCFFFFF)) >> 20);
8553 	/*
8554 	 * If this is not the first fragment, dump the unfragmentable
8555 	 * portion of the packet.
8556 	 */
8557 	if (offset)
8558 		mp->b_rptr = (uchar_t *)&fraghdr[1];
8559 
8560 	/*
8561 	 * Fragmentation reassembly.  Each ILL has a hash table for
8562 	 * queueing packets undergoing reassembly for all IPIFs
8563 	 * associated with the ILL.  The hash is based on the packet
8564 	 * IP ident field.  The ILL frag hash table was allocated
8565 	 * as a timer block at the time the ILL was created.  Whenever
8566 	 * there is anything on the reassembly queue, the timer will
8567 	 * be running.
8568 	 */
8569 	msg_len = MBLKSIZE(mp);
8570 	tail_mp = mp;
8571 	while (tail_mp->b_cont != NULL) {
8572 		tail_mp = tail_mp->b_cont;
8573 		msg_len += MBLKSIZE(tail_mp);
8574 	}
8575 	/*
8576 	 * If the reassembly list for this ILL will get too big
8577 	 * prune it.
8578 	 */
8579 
8580 	if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
8581 	    ipst->ips_ip_reass_queue_bytes) {
8582 		ill_frag_prune(ill,
8583 		    (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
8584 		    (ipst->ips_ip_reass_queue_bytes - msg_len));
8585 		pruned = B_TRUE;
8586 	}
8587 
8588 	ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH_V6(*v6src_ptr, ident)];
8589 	mutex_enter(&ipfb->ipfb_lock);
8590 
8591 	ipfp = &ipfb->ipfb_ipf;
8592 	/* Try to find an existing fragment queue for this packet. */
8593 	for (;;) {
8594 		ipf = ipfp[0];
8595 		if (ipf) {
8596 			/*
8597 			 * It has to match on ident, source address, and
8598 			 * dest address.
8599 			 */
8600 			if (ipf->ipf_ident == ident &&
8601 			    IN6_ARE_ADDR_EQUAL(&ipf->ipf_v6src, v6src_ptr) &&
8602 			    IN6_ARE_ADDR_EQUAL(&ipf->ipf_v6dst, v6dst_ptr)) {
8603 
8604 				/*
8605 				 * If we have received too many
8606 				 * duplicate fragments for this packet
8607 				 * free it.
8608 				 */
8609 				if (ipf->ipf_num_dups > ip_max_frag_dups) {
8610 					ill_frag_free_pkts(ill, ipfb, ipf, 1);
8611 					freemsg(mp);
8612 					mutex_exit(&ipfb->ipfb_lock);
8613 					return (NULL);
8614 				}
8615 
8616 				break;
8617 			}
8618 			ipfp = &ipf->ipf_hash_next;
8619 			continue;
8620 		}
8621 
8622 
8623 		/*
8624 		 * If we pruned the list, do we want to store this new
8625 		 * fragment?. We apply an optimization here based on the
8626 		 * fact that most fragments will be received in order.
8627 		 * So if the offset of this incoming fragment is zero,
8628 		 * it is the first fragment of a new packet. We will
8629 		 * keep it.  Otherwise drop the fragment, as we have
8630 		 * probably pruned the packet already (since the
8631 		 * packet cannot be found).
8632 		 */
8633 
8634 		if (pruned && offset != 0) {
8635 			mutex_exit(&ipfb->ipfb_lock);
8636 			freemsg(mp);
8637 			return (NULL);
8638 		}
8639 
8640 		/* New guy.  Allocate a frag message. */
8641 		mp1 = allocb(sizeof (*ipf), BPRI_MED);
8642 		if (!mp1) {
8643 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8644 			freemsg(mp);
8645 	partial_reass_done:
8646 			mutex_exit(&ipfb->ipfb_lock);
8647 			return (NULL);
8648 		}
8649 
8650 		if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst))  {
8651 			/*
8652 			 * Too many fragmented packets in this hash bucket.
8653 			 * Free the oldest.
8654 			 */
8655 			ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
8656 		}
8657 
8658 		mp1->b_cont = mp;
8659 
8660 		/* Initialize the fragment header. */
8661 		ipf = (ipf_t *)mp1->b_rptr;
8662 		ipf->ipf_mp = mp1;
8663 		ipf->ipf_ptphn = ipfp;
8664 		ipfp[0] = ipf;
8665 		ipf->ipf_hash_next = NULL;
8666 		ipf->ipf_ident = ident;
8667 		ipf->ipf_v6src = *v6src_ptr;
8668 		ipf->ipf_v6dst = *v6dst_ptr;
8669 		/* Record reassembly start time. */
8670 		ipf->ipf_timestamp = gethrestime_sec();
8671 		/* Record ipf generation and account for frag header */
8672 		ipf->ipf_gen = ill->ill_ipf_gen++;
8673 		ipf->ipf_count = MBLKSIZE(mp1);
8674 		ipf->ipf_protocol = nexthdr;
8675 		ipf->ipf_nf_hdr_len = 0;
8676 		ipf->ipf_prev_nexthdr_offset = 0;
8677 		ipf->ipf_last_frag_seen = B_FALSE;
8678 		ipf->ipf_ecn = ecn_info;
8679 		ipf->ipf_num_dups = 0;
8680 		ipfb->ipfb_frag_pkts++;
8681 		ipf->ipf_checksum = 0;
8682 		ipf->ipf_checksum_flags = 0;
8683 
8684 		/* Store checksum value in fragment header */
8685 		if (sum_flags != 0) {
8686 			sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8687 			sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8688 			ipf->ipf_checksum = sum_val;
8689 			ipf->ipf_checksum_flags = sum_flags;
8690 		}
8691 
8692 		/*
8693 		 * We handle reassembly two ways.  In the easy case,
8694 		 * where all the fragments show up in order, we do
8695 		 * minimal bookkeeping, and just clip new pieces on
8696 		 * the end.  If we ever see a hole, then we go off
8697 		 * to ip_reassemble which has to mark the pieces and
8698 		 * keep track of the number of holes, etc.  Obviously,
8699 		 * the point of having both mechanisms is so we can
8700 		 * handle the easy case as efficiently as possible.
8701 		 */
8702 		if (offset == 0) {
8703 			/* Easy case, in-order reassembly so far. */
8704 			/* Update the byte count */
8705 			ipf->ipf_count += msg_len;
8706 			ipf->ipf_tail_mp = tail_mp;
8707 			/*
8708 			 * Keep track of next expected offset in
8709 			 * ipf_end.
8710 			 */
8711 			ipf->ipf_end = end;
8712 			ipf->ipf_nf_hdr_len = hdr_length;
8713 			ipf->ipf_prev_nexthdr_offset = *prev_nexthdr_offset;
8714 		} else {
8715 			/* Hard case, hole at the beginning. */
8716 			ipf->ipf_tail_mp = NULL;
8717 			/*
8718 			 * ipf_end == 0 means that we have given up
8719 			 * on easy reassembly.
8720 			 */
8721 			ipf->ipf_end = 0;
8722 
8723 			/* Forget checksum offload from now on */
8724 			ipf->ipf_checksum_flags = 0;
8725 
8726 			/*
8727 			 * ipf_hole_cnt is set by ip_reassemble.
8728 			 * ipf_count is updated by ip_reassemble.
8729 			 * No need to check for return value here
8730 			 * as we don't expect reassembly to complete or
8731 			 * fail for the first fragment itself.
8732 			 */
8733 			(void) ip_reassemble(mp, ipf, offset, more_frags, ill,
8734 			    msg_len);
8735 		}
8736 		/* Update per ipfb and ill byte counts */
8737 		ipfb->ipfb_count += ipf->ipf_count;
8738 		ASSERT(ipfb->ipfb_count > 0);	/* Wraparound */
8739 		ill->ill_frag_count += ipf->ipf_count;
8740 		/* If the frag timer wasn't already going, start it. */
8741 		mutex_enter(&ill->ill_lock);
8742 		ill_frag_timer_start(ill);
8743 		mutex_exit(&ill->ill_lock);
8744 		goto partial_reass_done;
8745 	}
8746 
8747 	/*
8748 	 * If the packet's flag has changed (it could be coming up
8749 	 * from an interface different than the previous, therefore
8750 	 * possibly different checksum capability), then forget about
8751 	 * any stored checksum states.  Otherwise add the value to
8752 	 * the existing one stored in the fragment header.
8753 	 */
8754 	if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
8755 		sum_val += ipf->ipf_checksum;
8756 		sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8757 		sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
8758 		ipf->ipf_checksum = sum_val;
8759 	} else if (ipf->ipf_checksum_flags != 0) {
8760 		/* Forget checksum offload from now on */
8761 		ipf->ipf_checksum_flags = 0;
8762 	}
8763 
8764 	/*
8765 	 * We have a new piece of a datagram which is already being
8766 	 * reassembled.  Update the ECN info if all IP fragments
8767 	 * are ECN capable.  If there is one which is not, clear
8768 	 * all the info.  If there is at least one which has CE
8769 	 * code point, IP needs to report that up to transport.
8770 	 */
8771 	if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
8772 		if (ecn_info == IPH_ECN_CE)
8773 			ipf->ipf_ecn = IPH_ECN_CE;
8774 	} else {
8775 		ipf->ipf_ecn = IPH_ECN_NECT;
8776 	}
8777 
8778 	if (offset && ipf->ipf_end == offset) {
8779 		/* The new fragment fits at the end */
8780 		ipf->ipf_tail_mp->b_cont = mp;
8781 		/* Update the byte count */
8782 		ipf->ipf_count += msg_len;
8783 		/* Update per ipfb and ill byte counts */
8784 		ipfb->ipfb_count += msg_len;
8785 		ASSERT(ipfb->ipfb_count > 0);	/* Wraparound */
8786 		ill->ill_frag_count += msg_len;
8787 		if (more_frags) {
8788 			/* More to come. */
8789 			ipf->ipf_end = end;
8790 			ipf->ipf_tail_mp = tail_mp;
8791 			goto partial_reass_done;
8792 		}
8793 	} else {
8794 		/*
8795 		 * Go do the hard cases.
8796 		 * Call ip_reassemble().
8797 		 */
8798 		int ret;
8799 
8800 		if (offset == 0) {
8801 			if (ipf->ipf_prev_nexthdr_offset == 0) {
8802 				ipf->ipf_nf_hdr_len = hdr_length;
8803 				ipf->ipf_prev_nexthdr_offset =
8804 				    *prev_nexthdr_offset;
8805 			}
8806 		}
8807 		/* Save current byte count */
8808 		count = ipf->ipf_count;
8809 		ret = ip_reassemble(mp, ipf, offset, more_frags, ill, msg_len);
8810 
8811 		/* Count of bytes added and subtracted (freeb()ed) */
8812 		count = ipf->ipf_count - count;
8813 		if (count) {
8814 			/* Update per ipfb and ill byte counts */
8815 			ipfb->ipfb_count += count;
8816 			ASSERT(ipfb->ipfb_count > 0);	/* Wraparound */
8817 			ill->ill_frag_count += count;
8818 		}
8819 		if (ret == IP_REASS_PARTIAL) {
8820 			goto partial_reass_done;
8821 		} else if (ret == IP_REASS_FAILED) {
8822 			/* Reassembly failed. Free up all resources */
8823 			ill_frag_free_pkts(ill, ipfb, ipf, 1);
8824 			for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
8825 				IP_REASS_SET_START(t_mp, 0);
8826 				IP_REASS_SET_END(t_mp, 0);
8827 			}
8828 			freemsg(mp);
8829 			goto partial_reass_done;
8830 		}
8831 
8832 		/* We will reach here iff 'ret' is IP_REASS_COMPLETE */
8833 	}
8834 	/*
8835 	 * We have completed reassembly.  Unhook the frag header from
8836 	 * the reassembly list.
8837 	 *
8838 	 * Grab the unfragmentable header length next header value out
8839 	 * of the first fragment
8840 	 */
8841 	ASSERT(ipf->ipf_nf_hdr_len != 0);
8842 	hdr_length = ipf->ipf_nf_hdr_len;
8843 
8844 	/*
8845 	 * Before we free the frag header, record the ECN info
8846 	 * to report back to the transport.
8847 	 */
8848 	ecn_info = ipf->ipf_ecn;
8849 
8850 	/*
8851 	 * Store the nextheader field in the header preceding the fragment
8852 	 * header
8853 	 */
8854 	nexthdr = ipf->ipf_protocol;
8855 	*prev_nexthdr_offset = ipf->ipf_prev_nexthdr_offset;
8856 	ipfp = ipf->ipf_ptphn;
8857 
8858 	/* We need to supply these to caller */
8859 	if ((sum_flags = ipf->ipf_checksum_flags) != 0)
8860 		sum_val = ipf->ipf_checksum;
8861 	else
8862 		sum_val = 0;
8863 
8864 	mp1 = ipf->ipf_mp;
8865 	count = ipf->ipf_count;
8866 	ipf = ipf->ipf_hash_next;
8867 	if (ipf)
8868 		ipf->ipf_ptphn = ipfp;
8869 	ipfp[0] = ipf;
8870 	ill->ill_frag_count -= count;
8871 	ASSERT(ipfb->ipfb_count >= count);
8872 	ipfb->ipfb_count -= count;
8873 	ipfb->ipfb_frag_pkts--;
8874 	mutex_exit(&ipfb->ipfb_lock);
8875 	/* Ditch the frag header. */
8876 	mp = mp1->b_cont;
8877 	freeb(mp1);
8878 
8879 	/*
8880 	 * Make sure the packet is good by doing some sanity
8881 	 * check. If bad we can silentely drop the packet.
8882 	 */
8883 reass_done:
8884 	if (hdr_length < sizeof (ip6_frag_t)) {
8885 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
8886 		ip1dbg(("ip_rput_frag_v6: bad packet\n"));
8887 		freemsg(mp);
8888 		return (NULL);
8889 	}
8890 
8891 	/*
8892 	 * Remove the fragment header from the initial header by
8893 	 * splitting the mblk into the non-fragmentable header and
8894 	 * everthing after the fragment extension header.  This has the
8895 	 * side effect of putting all the headers that need destination
8896 	 * processing into the b_cont block-- on return this fact is
8897 	 * used in order to avoid having to look at the extensions
8898 	 * already processed.
8899 	 *
8900 	 * Note that this code assumes that the unfragmentable portion
8901 	 * of the header is in the first mblk and increments
8902 	 * the read pointer past it.  If this assumption is broken
8903 	 * this code fails badly.
8904 	 */
8905 	if (mp->b_rptr + hdr_length != mp->b_wptr) {
8906 		mblk_t *nmp;
8907 
8908 		if (!(nmp = dupb(mp))) {
8909 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8910 			ip1dbg(("ip_rput_frag_v6: dupb failed\n"));
8911 			freemsg(mp);
8912 			return (NULL);
8913 		}
8914 		nmp->b_cont = mp->b_cont;
8915 		mp->b_cont = nmp;
8916 		nmp->b_rptr += hdr_length;
8917 	}
8918 	mp->b_wptr = mp->b_rptr + hdr_length - sizeof (ip6_frag_t);
8919 
8920 	ip6h = (ip6_t *)mp->b_rptr;
8921 	((char *)ip6h)[*prev_nexthdr_offset] = nexthdr;
8922 
8923 	/* Restore original IP length in header. */
8924 	ip6h->ip6_plen = htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN));
8925 	/* Record the ECN info. */
8926 	ip6h->ip6_vcf &= htonl(0xFFCFFFFF);
8927 	ip6h->ip6_vcf |= htonl(ecn_info << 20);
8928 
8929 	/* Reassembly is successful; return checksum information if needed */
8930 	if (cksum_val != NULL)
8931 		*cksum_val = sum_val;
8932 	if (cksum_flags != NULL)
8933 		*cksum_flags = sum_flags;
8934 
8935 	return (mp);
8936 }
8937 
8938 /*
8939  * Walk through the options to see if there is a routing header.
8940  * If present get the destination which is the last address of
8941  * the option.
8942  */
8943 in6_addr_t
8944 ip_get_dst_v6(ip6_t *ip6h, boolean_t *is_fragment)
8945 {
8946 	uint8_t nexthdr;
8947 	uint8_t *whereptr;
8948 	ip6_hbh_t *hbhhdr;
8949 	ip6_dest_t *dsthdr;
8950 	ip6_rthdr0_t *rthdr;
8951 	ip6_frag_t *fraghdr;
8952 	int ehdrlen;
8953 	int left;
8954 	in6_addr_t *ap, rv;
8955 
8956 	if (is_fragment != NULL)
8957 		*is_fragment = B_FALSE;
8958 
8959 	rv = ip6h->ip6_dst;
8960 
8961 	nexthdr = ip6h->ip6_nxt;
8962 	whereptr = (uint8_t *)&ip6h[1];
8963 	for (;;) {
8964 
8965 		ASSERT(nexthdr != IPPROTO_RAW);
8966 		switch (nexthdr) {
8967 		case IPPROTO_HOPOPTS:
8968 			hbhhdr = (ip6_hbh_t *)whereptr;
8969 			nexthdr = hbhhdr->ip6h_nxt;
8970 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
8971 			break;
8972 		case IPPROTO_DSTOPTS:
8973 			dsthdr = (ip6_dest_t *)whereptr;
8974 			nexthdr = dsthdr->ip6d_nxt;
8975 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
8976 			break;
8977 		case IPPROTO_ROUTING:
8978 			rthdr = (ip6_rthdr0_t *)whereptr;
8979 			nexthdr = rthdr->ip6r0_nxt;
8980 			ehdrlen = 8 * (rthdr->ip6r0_len + 1);
8981 
8982 			left = rthdr->ip6r0_segleft;
8983 			ap = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
8984 			rv = *(ap + left - 1);
8985 			/*
8986 			 * If the caller doesn't care whether the packet
8987 			 * is a fragment or not, we can stop here since
8988 			 * we have our destination.
8989 			 */
8990 			if (is_fragment == NULL)
8991 				goto done;
8992 			break;
8993 		case IPPROTO_FRAGMENT:
8994 			fraghdr = (ip6_frag_t *)whereptr;
8995 			nexthdr = fraghdr->ip6f_nxt;
8996 			ehdrlen = sizeof (ip6_frag_t);
8997 			if (is_fragment != NULL)
8998 				*is_fragment = B_TRUE;
8999 			goto done;
9000 		default :
9001 			goto done;
9002 		}
9003 		whereptr += ehdrlen;
9004 	}
9005 
9006 done:
9007 	return (rv);
9008 }
9009 
9010 /*
9011  * ip_source_routed_v6:
9012  * This function is called by redirect code in ip_rput_data_v6 to
9013  * know whether this packet is source routed through this node i.e
9014  * whether this node (router) is part of the journey. This
9015  * function is called under two cases :
9016  *
9017  * case 1 : Routing header was processed by this node and
9018  *	    ip_process_rthdr replaced ip6_dst with the next hop
9019  *          and we are forwarding the packet to the next hop.
9020  *
9021  * case 2 : Routing header was not processed by this node and we
9022  *	    are just forwarding the packet.
9023  *
9024  * For case (1) we don't want to send redirects. For case(2) we
9025  * want to send redirects.
9026  */
9027 static boolean_t
9028 ip_source_routed_v6(ip6_t *ip6h, mblk_t *mp, ip_stack_t *ipst)
9029 {
9030 	uint8_t		nexthdr;
9031 	in6_addr_t	*addrptr;
9032 	ip6_rthdr0_t	*rthdr;
9033 	uint8_t		numaddr;
9034 	ip6_hbh_t	*hbhhdr;
9035 	uint_t		ehdrlen;
9036 	uint8_t		*byteptr;
9037 
9038 	ip2dbg(("ip_source_routed_v6\n"));
9039 	nexthdr = ip6h->ip6_nxt;
9040 	ehdrlen = IPV6_HDR_LEN;
9041 
9042 	/* if a routing hdr is preceeded by HOPOPT or DSTOPT */
9043 	while (nexthdr == IPPROTO_HOPOPTS ||
9044 	    nexthdr == IPPROTO_DSTOPTS) {
9045 		byteptr = (uint8_t *)ip6h + ehdrlen;
9046 		/*
9047 		 * Check if we have already processed
9048 		 * packets or we are just a forwarding
9049 		 * router which only pulled up msgs up
9050 		 * to IPV6HDR and  one HBH ext header
9051 		 */
9052 		if (byteptr + MIN_EHDR_LEN > mp->b_wptr) {
9053 			ip2dbg(("ip_source_routed_v6: Extension"
9054 			    " headers not processed\n"));
9055 			return (B_FALSE);
9056 		}
9057 		hbhhdr = (ip6_hbh_t *)byteptr;
9058 		nexthdr = hbhhdr->ip6h_nxt;
9059 		ehdrlen = ehdrlen + 8 * (hbhhdr->ip6h_len + 1);
9060 	}
9061 	switch (nexthdr) {
9062 	case IPPROTO_ROUTING:
9063 		byteptr = (uint8_t *)ip6h + ehdrlen;
9064 		/*
9065 		 * If for some reason, we haven't pulled up
9066 		 * the routing hdr data mblk, then we must
9067 		 * not have processed it at all. So for sure
9068 		 * we are not part of the source routed journey.
9069 		 */
9070 		if (byteptr + MIN_EHDR_LEN > mp->b_wptr) {
9071 			ip2dbg(("ip_source_routed_v6: Routing"
9072 			    " header not processed\n"));
9073 			return (B_FALSE);
9074 		}
9075 		rthdr = (ip6_rthdr0_t *)byteptr;
9076 		/*
9077 		 * Either we are an intermediate router or the
9078 		 * last hop before destination and we have
9079 		 * already processed the routing header.
9080 		 * If segment_left is greater than or equal to zero,
9081 		 * then we must be the (numaddr - segleft) entry
9082 		 * of the routing header. Although ip6r0_segleft
9083 		 * is a unit8_t variable, we still check for zero
9084 		 * or greater value, if in case the data type
9085 		 * is changed someday in future.
9086 		 */
9087 		if (rthdr->ip6r0_segleft > 0 ||
9088 		    rthdr->ip6r0_segleft == 0) {
9089 			ire_t 	*ire = NULL;
9090 
9091 			numaddr = rthdr->ip6r0_len / 2;
9092 			addrptr = (in6_addr_t *)((char *)rthdr +
9093 			    sizeof (*rthdr));
9094 			addrptr += (numaddr - (rthdr->ip6r0_segleft + 1));
9095 			if (addrptr != NULL) {
9096 				ire = ire_ctable_lookup_v6(addrptr, NULL,
9097 				    IRE_LOCAL, NULL, ALL_ZONES, NULL,
9098 				    MATCH_IRE_TYPE,
9099 				    ipst);
9100 				if (ire != NULL) {
9101 					ire_refrele(ire);
9102 					return (B_TRUE);
9103 				}
9104 				ip1dbg(("ip_source_routed_v6: No ire found\n"));
9105 			}
9106 		}
9107 	/* FALLTHRU */
9108 	default:
9109 		ip2dbg(("ip_source_routed_v6: Not source routed here\n"));
9110 		return (B_FALSE);
9111 	}
9112 }
9113 
9114 /*
9115  * ip_wput_v6 -- Packets sent down from transport modules show up here.
9116  * Assumes that the following set of headers appear in the first
9117  * mblk:
9118  *	ip6i_t (if present) CAN also appear as a separate mblk.
9119  *	ip6_t
9120  *	Any extension headers
9121  *	TCP/UDP/SCTP header (if present)
9122  * The routine can handle an ICMPv6 header that is not in the first mblk.
9123  *
9124  * The order to determine the outgoing interface is as follows:
9125  * 1. IPV6_BOUND_PIF is set, use that ill (conn_outgoing_pill)
9126  * 2. If conn_nofailover_ill is set then use that ill.
9127  * 3. If an ip6i_t with IP6I_IFINDEX set then use that ill.
9128  * 4. If q is an ill queue and (link local or multicast destination) then
9129  *    use that ill.
9130  * 5. If IPV6_BOUND_IF has been set use that ill.
9131  * 6. For multicast: if IPV6_MULTICAST_IF has been set use it. Otherwise
9132  *    look for the best IRE match for the unspecified group to determine
9133  *    the ill.
9134  * 7. For unicast: Just do an IRE lookup for the best match.
9135  *
9136  * arg2 is always a queue_t *.
9137  * When that queue is an ill_t (i.e. q_next != NULL), then arg must be
9138  * the zoneid.
9139  * When that queue is not an ill_t, then arg must be a conn_t pointer.
9140  */
9141 void
9142 ip_output_v6(void *arg, mblk_t *mp, void *arg2, int caller)
9143 {
9144 	conn_t		*connp = NULL;
9145 	queue_t		*q = (queue_t *)arg2;
9146 	ire_t		*ire = NULL;
9147 	ire_t		*sctp_ire = NULL;
9148 	ip6_t		*ip6h;
9149 	in6_addr_t	*v6dstp;
9150 	ill_t		*ill = NULL;
9151 	ipif_t		*ipif;
9152 	ip6i_t		*ip6i;
9153 	int		cksum_request;	/* -1 => normal. */
9154 			/* 1 => Skip TCP/UDP/SCTP checksum */
9155 			/* Otherwise contains insert offset for checksum */
9156 	int		unspec_src;
9157 	boolean_t	do_outrequests;	/* Increment OutRequests? */
9158 	mib2_ipIfStatsEntry_t	*mibptr;
9159 	int 		match_flags = MATCH_IRE_ILL_GROUP;
9160 	boolean_t	attach_if = B_FALSE;
9161 	mblk_t		*first_mp;
9162 	boolean_t	mctl_present;
9163 	ipsec_out_t	*io;
9164 	boolean_t	drop_if_delayed = B_FALSE;
9165 	boolean_t	multirt_need_resolve = B_FALSE;
9166 	mblk_t		*copy_mp = NULL;
9167 	int		err = 0;
9168 	int		ip6i_flags = 0;
9169 	zoneid_t	zoneid;
9170 	ill_t		*saved_ill = NULL;
9171 	boolean_t	conn_lock_held;
9172 	boolean_t	need_decref = B_FALSE;
9173 	ip_stack_t	*ipst;
9174 	int		adjust;
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 (is_system_labeled() && DB_TYPE(mp) == M_DATA &&
9302 	    (connp == NULL || !connp->conn_ulp_labeled)) {
9303 		if (connp != NULL) {
9304 			ASSERT(CONN_CRED(connp) != NULL);
9305 			err = tsol_check_label_v6(BEST_CRED(mp, connp),
9306 			    &mp, &adjust, connp->conn_mac_exempt, ipst);
9307 		} else if (DB_CRED(mp) != NULL) {
9308 			err = tsol_check_label_v6(DB_CRED(mp),
9309 			    &mp, &adjust, 0, ipst);
9310 		}
9311 		if (mctl_present)
9312 			first_mp->b_cont = mp;
9313 		else
9314 			first_mp = mp;
9315 		if (err != 0) {
9316 			DTRACE_PROBE3(
9317 			    tsol_ip_log_drop_checklabel_ip6, char *,
9318 			    "conn(1), failed to check/update mp(2)",
9319 			    conn_t, connp, mblk_t, mp);
9320 			freemsg(first_mp);
9321 			return;
9322 		}
9323 		ip6h = (ip6_t *)mp->b_rptr;
9324 	}
9325 	if (q->q_next != NULL) {
9326 		/*
9327 		 * We don't know if this ill will be used for IPv6
9328 		 * until the ILLF_IPV6 flag is set via SIOCSLIFNAME.
9329 		 * ipif_set_values() sets the ill_isv6 flag to true if
9330 		 * ILLF_IPV6 is set.  If the ill_isv6 flag isn't true,
9331 		 * just drop the packet.
9332 		 */
9333 		if (!ill->ill_isv6) {
9334 			ip1dbg(("ip_wput_v6: Received an IPv6 packet before "
9335 			    "ILLF_IPV6 was set\n"));
9336 			freemsg(first_mp);
9337 			return;
9338 		}
9339 		/* For uniformity do a refhold */
9340 		mutex_enter(&ill->ill_lock);
9341 		if (!ILL_CAN_LOOKUP(ill)) {
9342 			mutex_exit(&ill->ill_lock);
9343 			freemsg(first_mp);
9344 			return;
9345 		}
9346 		ill_refhold_locked(ill);
9347 		mutex_exit(&ill->ill_lock);
9348 		mibptr = ill->ill_ip_mib;
9349 
9350 		ASSERT(mibptr != NULL);
9351 		unspec_src = 0;
9352 		BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
9353 		do_outrequests = B_FALSE;
9354 		zoneid = (zoneid_t)(uintptr_t)arg;
9355 	} else {
9356 		ASSERT(connp != NULL);
9357 		zoneid = connp->conn_zoneid;
9358 
9359 		/* is queue flow controlled? */
9360 		if ((q->q_first || connp->conn_draining) &&
9361 		    (caller == IP_WPUT)) {
9362 			/*
9363 			 * 1) TCP sends down M_CTL for detached connections.
9364 			 * 2) AH/ESP sends down M_CTL.
9365 			 *
9366 			 * We don't flow control either of the above. Only
9367 			 * UDP and others are flow controlled for which we
9368 			 * can't have a M_CTL.
9369 			 */
9370 			ASSERT(first_mp == mp);
9371 			(void) putq(q, mp);
9372 			return;
9373 		}
9374 		mibptr = &ipst->ips_ip6_mib;
9375 		unspec_src = connp->conn_unspec_src;
9376 		do_outrequests = B_TRUE;
9377 		if (mp->b_flag & MSGHASREF) {
9378 			mp->b_flag &= ~MSGHASREF;
9379 			ASSERT(connp->conn_ulp == IPPROTO_SCTP);
9380 			SCTP_EXTRACT_IPINFO(mp, sctp_ire);
9381 			need_decref = B_TRUE;
9382 		}
9383 
9384 		/*
9385 		 * If there is a policy, try to attach an ipsec_out in
9386 		 * the front. At the end, first_mp either points to a
9387 		 * M_DATA message or IPSEC_OUT message linked to a
9388 		 * M_DATA message. We have to do it now as we might
9389 		 * lose the "conn" if we go through ip_newroute.
9390 		 */
9391 		if (!mctl_present &&
9392 		    (connp->conn_out_enforce_policy ||
9393 		    connp->conn_latch != NULL)) {
9394 			ASSERT(first_mp == mp);
9395 			/* XXX Any better way to get the protocol fast ? */
9396 			if (((mp = ipsec_attach_ipsec_out(&mp, connp, NULL,
9397 			    connp->conn_ulp, ipst->ips_netstack)) == NULL)) {
9398 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9399 				if (need_decref)
9400 					CONN_DEC_REF(connp);
9401 				return;
9402 			} else {
9403 				ASSERT(mp->b_datap->db_type == M_CTL);
9404 				first_mp = mp;
9405 				mp = mp->b_cont;
9406 				mctl_present = B_TRUE;
9407 				io = (ipsec_out_t *)first_mp->b_rptr;
9408 			}
9409 		}
9410 	}
9411 
9412 	/* check for alignment and full IPv6 header */
9413 	if (!OK_32PTR((uchar_t *)ip6h) ||
9414 	    (mp->b_wptr - (uchar_t *)ip6h) < IPV6_HDR_LEN) {
9415 		ip0dbg(("ip_wput_v6: bad alignment or length\n"));
9416 		if (do_outrequests)
9417 			BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
9418 		BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9419 		freemsg(first_mp);
9420 		if (ill != NULL)
9421 			ill_refrele(ill);
9422 		if (need_decref)
9423 			CONN_DEC_REF(connp);
9424 		return;
9425 	}
9426 	v6dstp = &ip6h->ip6_dst;
9427 	cksum_request = -1;
9428 	ip6i = NULL;
9429 
9430 	/*
9431 	 * Once neighbor discovery has completed, ndp_process() will provide
9432 	 * locally generated packets for which processing can be reattempted.
9433 	 * In these cases, connp is NULL and the original zone is part of a
9434 	 * prepended ipsec_out_t.
9435 	 */
9436 	if (io != NULL) {
9437 		/*
9438 		 * When coming from icmp_input_v6, the zoneid might not match
9439 		 * for the loopback case, because inside icmp_input_v6 the
9440 		 * queue_t is a conn queue from the sending side.
9441 		 */
9442 		zoneid = io->ipsec_out_zoneid;
9443 		ASSERT(zoneid != ALL_ZONES);
9444 	}
9445 
9446 	if (ip6h->ip6_nxt == IPPROTO_RAW) {
9447 		/*
9448 		 * This is an ip6i_t header followed by an ip6_hdr.
9449 		 * Check which fields are set.
9450 		 *
9451 		 * When the packet comes from a transport we should have
9452 		 * all needed headers in the first mblk. However, when
9453 		 * going through ip_newroute*_v6 the ip6i might be in
9454 		 * a separate mblk when we return here. In that case
9455 		 * we pullup everything to ensure that extension and transport
9456 		 * headers "stay" in the first mblk.
9457 		 */
9458 		ip6i = (ip6i_t *)ip6h;
9459 		ip6i_flags = ip6i->ip6i_flags;
9460 
9461 		ASSERT((mp->b_wptr - (uchar_t *)ip6i) == sizeof (ip6i_t) ||
9462 		    ((mp->b_wptr - (uchar_t *)ip6i) >=
9463 		    sizeof (ip6i_t) + IPV6_HDR_LEN));
9464 
9465 		if ((mp->b_wptr - (uchar_t *)ip6i) == sizeof (ip6i_t)) {
9466 			if (!pullupmsg(mp, -1)) {
9467 				ip1dbg(("ip_wput_v6: pullupmsg failed\n"));
9468 				if (do_outrequests) {
9469 					BUMP_MIB(mibptr,
9470 					    ipIfStatsHCOutRequests);
9471 				}
9472 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9473 				freemsg(first_mp);
9474 				if (ill != NULL)
9475 					ill_refrele(ill);
9476 				if (need_decref)
9477 					CONN_DEC_REF(connp);
9478 				return;
9479 			}
9480 			ip6h = (ip6_t *)mp->b_rptr;
9481 			v6dstp = &ip6h->ip6_dst;
9482 			ip6i = (ip6i_t *)ip6h;
9483 		}
9484 		ip6h = (ip6_t *)&ip6i[1];
9485 
9486 		/*
9487 		 * Advance rptr past the ip6i_t to get ready for
9488 		 * transmitting the packet. However, if the packet gets
9489 		 * passed to ip_newroute*_v6 then rptr is moved back so
9490 		 * that the ip6i_t header can be inspected when the
9491 		 * packet comes back here after passing through
9492 		 * ire_add_then_send.
9493 		 */
9494 		mp->b_rptr = (uchar_t *)ip6h;
9495 
9496 		/*
9497 		 * IP6I_ATTACH_IF is set in this function when we had a
9498 		 * conn and it was either bound to the IPFF_NOFAILOVER address
9499 		 * or IPV6_BOUND_PIF was set. These options override other
9500 		 * options that set the ifindex. We come here with
9501 		 * IP6I_ATTACH_IF set when we can't find the ire and
9502 		 * ip_newroute_v6 is feeding the packet for second time.
9503 		 */
9504 		if ((ip6i->ip6i_flags & IP6I_IFINDEX) ||
9505 		    (ip6i->ip6i_flags & IP6I_ATTACH_IF)) {
9506 			ASSERT(ip6i->ip6i_ifindex != 0);
9507 			if (ill != NULL)
9508 				ill_refrele(ill);
9509 			ill = ill_lookup_on_ifindex(ip6i->ip6i_ifindex, 1,
9510 			    NULL, NULL, NULL, NULL, ipst);
9511 			if (ill == NULL) {
9512 				if (do_outrequests) {
9513 					BUMP_MIB(mibptr,
9514 					    ipIfStatsHCOutRequests);
9515 				}
9516 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9517 				ip1dbg(("ip_wput_v6: bad ifindex %d\n",
9518 				    ip6i->ip6i_ifindex));
9519 				if (need_decref)
9520 					CONN_DEC_REF(connp);
9521 				freemsg(first_mp);
9522 				return;
9523 			}
9524 			mibptr = ill->ill_ip_mib;
9525 			if (ip6i->ip6i_flags & IP6I_IFINDEX) {
9526 				/*
9527 				 * Preserve the index so that when we return
9528 				 * from IPSEC processing, we know where to
9529 				 * send the packet.
9530 				 */
9531 				if (mctl_present) {
9532 					ASSERT(io != NULL);
9533 					io->ipsec_out_ill_index =
9534 					    ip6i->ip6i_ifindex;
9535 				}
9536 			}
9537 			if (ip6i->ip6i_flags & IP6I_ATTACH_IF) {
9538 				/*
9539 				 * This is a multipathing probe packet that has
9540 				 * been delayed in ND resolution. Drop the
9541 				 * packet for the reasons mentioned in
9542 				 * nce_queue_mp()
9543 				 */
9544 				if ((ip6i->ip6i_flags & IP6I_DROP_IFDELAYED) &&
9545 				    (ip6i->ip6i_flags & IP6I_ND_DELAYED)) {
9546 					freemsg(first_mp);
9547 					ill_refrele(ill);
9548 					if (need_decref)
9549 						CONN_DEC_REF(connp);
9550 					return;
9551 				}
9552 			}
9553 		}
9554 		if (ip6i->ip6i_flags & IP6I_VERIFY_SRC) {
9555 			cred_t *cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q));
9556 
9557 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src));
9558 			if (secpolicy_net_rawaccess(cr) != 0) {
9559 				/*
9560 				 * Use IPCL_ZONEID to honor SO_ALLZONES.
9561 				 */
9562 				ire = ire_route_lookup_v6(&ip6h->ip6_src,
9563 				    0, 0, (IRE_LOCAL|IRE_LOOPBACK), NULL,
9564 				    NULL, connp != NULL ?
9565 				    IPCL_ZONEID(connp) : zoneid, NULL,
9566 				    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9567 				if (ire == NULL) {
9568 					if (do_outrequests)
9569 						BUMP_MIB(mibptr,
9570 						    ipIfStatsHCOutRequests);
9571 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9572 					ip1dbg(("ip_wput_v6: bad source "
9573 					    "addr\n"));
9574 					freemsg(first_mp);
9575 					if (ill != NULL)
9576 						ill_refrele(ill);
9577 					if (need_decref)
9578 						CONN_DEC_REF(connp);
9579 					return;
9580 				}
9581 				ire_refrele(ire);
9582 			}
9583 			/* No need to verify again when using ip_newroute */
9584 			ip6i->ip6i_flags &= ~IP6I_VERIFY_SRC;
9585 		}
9586 		if (!(ip6i->ip6i_flags & IP6I_NEXTHOP)) {
9587 			/*
9588 			 * Make sure they match since ip_newroute*_v6 etc might
9589 			 * (unknown to them) inspect ip6i_nexthop when
9590 			 * they think they access ip6_dst.
9591 			 */
9592 			ip6i->ip6i_nexthop = ip6h->ip6_dst;
9593 		}
9594 		if (ip6i->ip6i_flags & IP6I_NO_ULP_CKSUM)
9595 			cksum_request = 1;
9596 		if (ip6i->ip6i_flags & IP6I_RAW_CHECKSUM)
9597 			cksum_request = ip6i->ip6i_checksum_off;
9598 		if (ip6i->ip6i_flags & IP6I_UNSPEC_SRC)
9599 			unspec_src = 1;
9600 
9601 		if (do_outrequests && ill != NULL) {
9602 			BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
9603 			do_outrequests = B_FALSE;
9604 		}
9605 		/*
9606 		 * Store ip6i_t info that we need after we come back
9607 		 * from IPSEC processing.
9608 		 */
9609 		if (mctl_present) {
9610 			ASSERT(io != NULL);
9611 			io->ipsec_out_unspec_src = unspec_src;
9612 		}
9613 	}
9614 	if (connp != NULL && connp->conn_dontroute)
9615 		ip6h->ip6_hops = 1;
9616 
9617 	if (IN6_IS_ADDR_MULTICAST(v6dstp))
9618 		goto ipv6multicast;
9619 
9620 	/* 1. IPV6_BOUND_PIF takes precedence over all the ifindex settings. */
9621 	if (connp != NULL && connp->conn_outgoing_pill != NULL) {
9622 		ill_t	*conn_outgoing_pill;
9623 
9624 		conn_outgoing_pill = conn_get_held_ill(connp,
9625 		    &connp->conn_outgoing_pill, &err);
9626 		if (err == ILL_LOOKUP_FAILED) {
9627 			if (ill != NULL)
9628 				ill_refrele(ill);
9629 			if (need_decref)
9630 				CONN_DEC_REF(connp);
9631 			freemsg(first_mp);
9632 			return;
9633 		}
9634 		if (conn_outgoing_pill != NULL) {
9635 			if (ill != NULL)
9636 				ill_refrele(ill);
9637 			ill = conn_outgoing_pill;
9638 			attach_if = B_TRUE;
9639 			match_flags = MATCH_IRE_ILL;
9640 			mibptr = ill->ill_ip_mib;
9641 
9642 			/*
9643 			 * Check if we need an ire that will not be
9644 			 * looked up by anybody else i.e. HIDDEN.
9645 			 */
9646 			if (ill_is_probeonly(ill))
9647 				match_flags |= MATCH_IRE_MARK_HIDDEN;
9648 			goto send_from_ill;
9649 		}
9650 	}
9651 
9652 	/* 2. If ipc_nofailover_ill is set then use that ill. */
9653 	if (connp != NULL && connp->conn_nofailover_ill != NULL) {
9654 		ill_t	*conn_nofailover_ill;
9655 
9656 		conn_nofailover_ill = conn_get_held_ill(connp,
9657 		    &connp->conn_nofailover_ill, &err);
9658 		if (err == ILL_LOOKUP_FAILED) {
9659 			if (ill != NULL)
9660 				ill_refrele(ill);
9661 			if (need_decref)
9662 				CONN_DEC_REF(connp);
9663 			freemsg(first_mp);
9664 			return;
9665 		}
9666 		if (conn_nofailover_ill != NULL) {
9667 			if (ill != NULL)
9668 				ill_refrele(ill);
9669 			ill = conn_nofailover_ill;
9670 			attach_if = B_TRUE;
9671 			/*
9672 			 * Assumes that ipc_nofailover_ill is used only for
9673 			 * multipathing probe packets. These packets are better
9674 			 * dropped, if they are delayed in ND resolution, for
9675 			 * the reasons described in nce_queue_mp().
9676 			 * IP6I_DROP_IFDELAYED will be set later on in this
9677 			 * function for this packet.
9678 			 */
9679 			drop_if_delayed = B_TRUE;
9680 			match_flags = MATCH_IRE_ILL;
9681 			mibptr = ill->ill_ip_mib;
9682 
9683 			/*
9684 			 * Check if we need an ire that will not be
9685 			 * looked up by anybody else i.e. HIDDEN.
9686 			 */
9687 			if (ill_is_probeonly(ill))
9688 				match_flags |= MATCH_IRE_MARK_HIDDEN;
9689 			goto send_from_ill;
9690 		}
9691 	}
9692 
9693 	/*
9694 	 * Redo 1. If we did not find an IRE_CACHE the first time, we should
9695 	 * have an ip6i_t with IP6I_ATTACH_IF if IPV6_BOUND_PIF or
9696 	 * bind to the IPIF_NOFAILOVER address was used on this endpoint.
9697 	 */
9698 	if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_ATTACH_IF)) {
9699 		ASSERT(ip6i->ip6i_ifindex != 0);
9700 		attach_if = B_TRUE;
9701 		ASSERT(ill != NULL);
9702 		match_flags = MATCH_IRE_ILL;
9703 
9704 		/*
9705 		 * Check if we need an ire that will not be
9706 		 * looked up by anybody else i.e. HIDDEN.
9707 		 */
9708 		if (ill_is_probeonly(ill))
9709 			match_flags |= MATCH_IRE_MARK_HIDDEN;
9710 		goto send_from_ill;
9711 	}
9712 
9713 	/* 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. */
9714 	if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_IFINDEX)) {
9715 		ASSERT(ill != NULL);
9716 		goto send_from_ill;
9717 	}
9718 
9719 	/*
9720 	 * 4. If q is an ill queue and (link local or multicast destination)
9721 	 *    then use that ill.
9722 	 */
9723 	if (ill != NULL && IN6_IS_ADDR_LINKLOCAL(v6dstp)) {
9724 		goto send_from_ill;
9725 	}
9726 
9727 	/* 5. If IPV6_BOUND_IF has been set use that ill. */
9728 	if (connp != NULL && connp->conn_outgoing_ill != NULL) {
9729 		ill_t	*conn_outgoing_ill;
9730 
9731 		conn_outgoing_ill = conn_get_held_ill(connp,
9732 		    &connp->conn_outgoing_ill, &err);
9733 		if (err == ILL_LOOKUP_FAILED) {
9734 			if (ill != NULL)
9735 				ill_refrele(ill);
9736 			if (need_decref)
9737 				CONN_DEC_REF(connp);
9738 			freemsg(first_mp);
9739 			return;
9740 		}
9741 		if (ill != NULL)
9742 			ill_refrele(ill);
9743 		ill = conn_outgoing_ill;
9744 		mibptr = ill->ill_ip_mib;
9745 		goto send_from_ill;
9746 	}
9747 
9748 	/*
9749 	 * 6. For unicast: Just do an IRE lookup for the best match.
9750 	 * If we get here for a link-local address it is rather random
9751 	 * what interface we pick on a multihomed host.
9752 	 * *If* there is an IRE_CACHE (and the link-local address
9753 	 * isn't duplicated on multi links) this will find the IRE_CACHE.
9754 	 * Otherwise it will use one of the matching IRE_INTERFACE routes
9755 	 * for the link-local prefix. Hence, applications
9756 	 * *should* be encouraged to specify an outgoing interface when sending
9757 	 * to a link local address.
9758 	 */
9759 	if (connp == NULL || (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) &&
9760 	    !connp->conn_fully_bound)) {
9761 		/*
9762 		 * We cache IRE_CACHEs to avoid lookups. We don't do
9763 		 * this for the tcp global queue and listen end point
9764 		 * as it does not really have a real destination to
9765 		 * talk to.
9766 		 */
9767 		ire = ire_cache_lookup_v6(v6dstp, zoneid, MBLK_GETLABEL(mp),
9768 		    ipst);
9769 	} else {
9770 		/*
9771 		 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't
9772 		 * grab a lock here to check for CONDEMNED as it is okay
9773 		 * to send a packet or two with the IRE_CACHE that is going
9774 		 * away.
9775 		 */
9776 		mutex_enter(&connp->conn_lock);
9777 		ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache;
9778 		if (ire != NULL &&
9779 		    IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6dstp) &&
9780 		    !(ire->ire_marks & IRE_MARK_CONDEMNED)) {
9781 
9782 			IRE_REFHOLD(ire);
9783 			mutex_exit(&connp->conn_lock);
9784 
9785 		} else {
9786 			boolean_t cached = B_FALSE;
9787 
9788 			connp->conn_ire_cache = NULL;
9789 			mutex_exit(&connp->conn_lock);
9790 			/* Release the old ire */
9791 			if (ire != NULL && sctp_ire == NULL)
9792 				IRE_REFRELE_NOTR(ire);
9793 
9794 			ire = ire_cache_lookup_v6(v6dstp, zoneid,
9795 			    MBLK_GETLABEL(mp), ipst);
9796 			if (ire != NULL) {
9797 				IRE_REFHOLD_NOTR(ire);
9798 
9799 				mutex_enter(&connp->conn_lock);
9800 				if (CONN_CACHE_IRE(connp) &&
9801 				    (connp->conn_ire_cache == NULL)) {
9802 					rw_enter(&ire->ire_bucket->irb_lock,
9803 					    RW_READER);
9804 					if (!(ire->ire_marks &
9805 					    IRE_MARK_CONDEMNED)) {
9806 						connp->conn_ire_cache = ire;
9807 						cached = B_TRUE;
9808 					}
9809 					rw_exit(&ire->ire_bucket->irb_lock);
9810 				}
9811 				mutex_exit(&connp->conn_lock);
9812 
9813 				/*
9814 				 * We can continue to use the ire but since it
9815 				 * was not cached, we should drop the extra
9816 				 * reference.
9817 				 */
9818 				if (!cached)
9819 					IRE_REFRELE_NOTR(ire);
9820 			}
9821 		}
9822 	}
9823 
9824 	if (ire != NULL) {
9825 		if (do_outrequests) {
9826 			/* Handle IRE_LOCAL's that might appear here */
9827 			if (ire->ire_type == IRE_CACHE) {
9828 				mibptr = ((ill_t *)ire->ire_stq->q_ptr)->
9829 				    ill_ip_mib;
9830 			} else {
9831 				mibptr = ire->ire_ipif->ipif_ill->ill_ip_mib;
9832 			}
9833 			BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
9834 		}
9835 		ASSERT(!attach_if);
9836 
9837 		/*
9838 		 * Check if the ire has the RTF_MULTIRT flag, inherited
9839 		 * from an IRE_OFFSUBNET ire entry in ip_newroute().
9840 		 */
9841 		if (ire->ire_flags & RTF_MULTIRT) {
9842 			/*
9843 			 * Force hop limit of multirouted packets if required.
9844 			 * The hop limit of such packets is bounded by the
9845 			 * ip_multirt_ttl ndd variable.
9846 			 * NDP packets must have a hop limit of 255; don't
9847 			 * change the hop limit in that case.
9848 			 */
9849 			if ((ipst->ips_ip_multirt_ttl > 0) &&
9850 			    (ip6h->ip6_hops > ipst->ips_ip_multirt_ttl) &&
9851 			    (ip6h->ip6_hops != IPV6_MAX_HOPS)) {
9852 				if (ip_debug > 3) {
9853 					ip2dbg(("ip_wput_v6: forcing multirt "
9854 					    "hop limit to %d (was %d) ",
9855 					    ipst->ips_ip_multirt_ttl,
9856 					    ip6h->ip6_hops));
9857 					pr_addr_dbg("v6dst %s\n", AF_INET6,
9858 					    &ire->ire_addr_v6);
9859 				}
9860 				ip6h->ip6_hops = ipst->ips_ip_multirt_ttl;
9861 			}
9862 
9863 			/*
9864 			 * We look at this point if there are pending
9865 			 * unresolved routes. ire_multirt_need_resolve_v6()
9866 			 * checks in O(n) that all IRE_OFFSUBNET ire
9867 			 * entries for the packet's destination and
9868 			 * flagged RTF_MULTIRT are currently resolved.
9869 			 * If some remain unresolved, we do a copy
9870 			 * of the current message. It will be used
9871 			 * to initiate additional route resolutions.
9872 			 */
9873 			multirt_need_resolve =
9874 			    ire_multirt_need_resolve_v6(&ire->ire_addr_v6,
9875 			    MBLK_GETLABEL(first_mp), ipst);
9876 			ip2dbg(("ip_wput_v6: ire %p, "
9877 			    "multirt_need_resolve %d, first_mp %p\n",
9878 			    (void *)ire, multirt_need_resolve,
9879 			    (void *)first_mp));
9880 			if (multirt_need_resolve) {
9881 				copy_mp = copymsg(first_mp);
9882 				if (copy_mp != NULL) {
9883 					MULTIRT_DEBUG_TAG(copy_mp);
9884 				}
9885 			}
9886 		}
9887 		ip_wput_ire_v6(q, first_mp, ire, unspec_src, cksum_request,
9888 		    connp, caller, 0, ip6i_flags, zoneid);
9889 		if (need_decref) {
9890 			CONN_DEC_REF(connp);
9891 			connp = NULL;
9892 		}
9893 		IRE_REFRELE(ire);
9894 
9895 		/*
9896 		 * Try to resolve another multiroute if
9897 		 * ire_multirt_need_resolve_v6() deemed it necessary.
9898 		 * copy_mp will be consumed (sent or freed) by
9899 		 * ip_newroute_v6().
9900 		 */
9901 		if (copy_mp != NULL) {
9902 			if (mctl_present) {
9903 				ip6h = (ip6_t *)copy_mp->b_cont->b_rptr;
9904 			} else {
9905 				ip6h = (ip6_t *)copy_mp->b_rptr;
9906 			}
9907 			ip_newroute_v6(q, copy_mp, &ip6h->ip6_dst,
9908 			    &ip6h->ip6_src, NULL, zoneid, ipst);
9909 		}
9910 		if (ill != NULL)
9911 			ill_refrele(ill);
9912 		return;
9913 	}
9914 
9915 	/*
9916 	 * No full IRE for this destination.  Send it to
9917 	 * ip_newroute_v6 to see if anything else matches.
9918 	 * Mark this packet as having originated on this
9919 	 * machine.
9920 	 * Update rptr if there was an ip6i_t header.
9921 	 */
9922 	mp->b_prev = NULL;
9923 	mp->b_next = NULL;
9924 	if (ip6i != NULL)
9925 		mp->b_rptr -= sizeof (ip6i_t);
9926 
9927 	if (unspec_src) {
9928 		if (ip6i == NULL) {
9929 			/*
9930 			 * Add ip6i_t header to carry unspec_src
9931 			 * until the packet comes back in ip_wput_v6.
9932 			 */
9933 			mp = ip_add_info_v6(mp, NULL, v6dstp);
9934 			if (mp == NULL) {
9935 				if (do_outrequests)
9936 					BUMP_MIB(mibptr,
9937 					    ipIfStatsHCOutRequests);
9938 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9939 				if (mctl_present)
9940 					freeb(first_mp);
9941 				if (ill != NULL)
9942 					ill_refrele(ill);
9943 				if (need_decref)
9944 					CONN_DEC_REF(connp);
9945 				return;
9946 			}
9947 			ip6i = (ip6i_t *)mp->b_rptr;
9948 
9949 			if (mctl_present) {
9950 				ASSERT(first_mp != mp);
9951 				first_mp->b_cont = mp;
9952 			} else {
9953 				first_mp = mp;
9954 			}
9955 
9956 			if ((mp->b_wptr - (uchar_t *)ip6i) ==
9957 			    sizeof (ip6i_t)) {
9958 				/*
9959 				 * ndp_resolver called from ip_newroute_v6
9960 				 * expects pulled up message.
9961 				 */
9962 				if (!pullupmsg(mp, -1)) {
9963 					ip1dbg(("ip_wput_v6: pullupmsg"
9964 					    " failed\n"));
9965 					if (do_outrequests) {
9966 						BUMP_MIB(mibptr,
9967 						    ipIfStatsHCOutRequests);
9968 					}
9969 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
9970 					freemsg(first_mp);
9971 					if (ill != NULL)
9972 						ill_refrele(ill);
9973 					if (need_decref)
9974 						CONN_DEC_REF(connp);
9975 					return;
9976 				}
9977 				ip6i = (ip6i_t *)mp->b_rptr;
9978 			}
9979 			ip6h = (ip6_t *)&ip6i[1];
9980 			v6dstp = &ip6h->ip6_dst;
9981 		}
9982 		ip6i->ip6i_flags |= IP6I_UNSPEC_SRC;
9983 		if (mctl_present) {
9984 			ASSERT(io != NULL);
9985 			io->ipsec_out_unspec_src = unspec_src;
9986 		}
9987 	}
9988 	if (do_outrequests)
9989 		BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
9990 	if (need_decref)
9991 		CONN_DEC_REF(connp);
9992 	ip_newroute_v6(q, first_mp, v6dstp, &ip6h->ip6_src, NULL, zoneid, ipst);
9993 	if (ill != NULL)
9994 		ill_refrele(ill);
9995 	return;
9996 
9997 
9998 	/*
9999 	 * Handle multicast packets with or without an conn.
10000 	 * Assumes that the transports set ip6_hops taking
10001 	 * IPV6_MULTICAST_HOPS (and the other ways to set the hoplimit)
10002 	 * into account.
10003 	 */
10004 ipv6multicast:
10005 	ip2dbg(("ip_wput_v6: multicast\n"));
10006 
10007 	/*
10008 	 * 1. IPV6_BOUND_PIF takes precedence over all the ifindex settings
10009 	 * 2. If conn_nofailover_ill is set then use that ill.
10010 	 *
10011 	 * Hold the conn_lock till we refhold the ill of interest that is
10012 	 * pointed to from the conn. Since we cannot do an ill/ipif_refrele
10013 	 * while holding any locks, postpone the refrele until after the
10014 	 * conn_lock is dropped.
10015 	 */
10016 	if (connp != NULL) {
10017 		mutex_enter(&connp->conn_lock);
10018 		conn_lock_held = B_TRUE;
10019 	} else {
10020 		conn_lock_held = B_FALSE;
10021 	}
10022 	if (connp != NULL && connp->conn_outgoing_pill != NULL) {
10023 		err = ill_check_and_refhold(connp->conn_outgoing_pill);
10024 		if (err == ILL_LOOKUP_FAILED) {
10025 			ip1dbg(("ip_output_v6: multicast"
10026 			    " conn_outgoing_pill no ipif\n"));
10027 multicast_discard:
10028 			ASSERT(saved_ill == NULL);
10029 			if (conn_lock_held)
10030 				mutex_exit(&connp->conn_lock);
10031 			if (ill != NULL)
10032 				ill_refrele(ill);
10033 			freemsg(first_mp);
10034 			if (do_outrequests)
10035 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10036 			if (need_decref)
10037 				CONN_DEC_REF(connp);
10038 			return;
10039 		}
10040 		saved_ill = ill;
10041 		ill = connp->conn_outgoing_pill;
10042 		attach_if = B_TRUE;
10043 		match_flags = MATCH_IRE_ILL;
10044 		mibptr = ill->ill_ip_mib;
10045 
10046 		/*
10047 		 * Check if we need an ire that will not be
10048 		 * looked up by anybody else i.e. HIDDEN.
10049 		 */
10050 		if (ill_is_probeonly(ill))
10051 			match_flags |= MATCH_IRE_MARK_HIDDEN;
10052 	} else if (connp != NULL && connp->conn_nofailover_ill != NULL) {
10053 		err = ill_check_and_refhold(connp->conn_nofailover_ill);
10054 		if (err == ILL_LOOKUP_FAILED) {
10055 			ip1dbg(("ip_output_v6: multicast"
10056 			    " conn_nofailover_ill no ipif\n"));
10057 			goto multicast_discard;
10058 		}
10059 		saved_ill = ill;
10060 		ill = connp->conn_nofailover_ill;
10061 		attach_if = B_TRUE;
10062 		match_flags = MATCH_IRE_ILL;
10063 
10064 		/*
10065 		 * Check if we need an ire that will not be
10066 		 * looked up by anybody else i.e. HIDDEN.
10067 		 */
10068 		if (ill_is_probeonly(ill))
10069 			match_flags |= MATCH_IRE_MARK_HIDDEN;
10070 	} else if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_ATTACH_IF)) {
10071 		/*
10072 		 * Redo 1. If we did not find an IRE_CACHE the first time,
10073 		 * we should have an ip6i_t with IP6I_ATTACH_IF if
10074 		 * IPV6_BOUND_PIF or bind to the IPIF_NOFAILOVER address was
10075 		 * used on this endpoint.
10076 		 */
10077 		ASSERT(ip6i->ip6i_ifindex != 0);
10078 		attach_if = B_TRUE;
10079 		ASSERT(ill != NULL);
10080 		match_flags = MATCH_IRE_ILL;
10081 
10082 		/*
10083 		 * Check if we need an ire that will not be
10084 		 * looked up by anybody else i.e. HIDDEN.
10085 		 */
10086 		if (ill_is_probeonly(ill))
10087 			match_flags |= MATCH_IRE_MARK_HIDDEN;
10088 	} else if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_IFINDEX)) {
10089 		/* 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. */
10090 
10091 		ASSERT(ill != NULL);
10092 	} else if (ill != NULL) {
10093 		/*
10094 		 * 4. If q is an ill queue and (link local or multicast
10095 		 * destination) then use that ill.
10096 		 * We don't need the ipif initialization here.
10097 		 * This useless assert below is just to prevent lint from
10098 		 * reporting a null body if statement.
10099 		 */
10100 		ASSERT(ill != NULL);
10101 	} else if (connp != NULL) {
10102 		/*
10103 		 * 5. If IPV6_BOUND_IF has been set use that ill.
10104 		 *
10105 		 * 6. For multicast: if IPV6_MULTICAST_IF has been set use it.
10106 		 * Otherwise look for the best IRE match for the unspecified
10107 		 * group to determine the ill.
10108 		 *
10109 		 * conn_multicast_ill is used for only IPv6 packets.
10110 		 * conn_multicast_ipif is used for only IPv4 packets.
10111 		 * Thus a PF_INET6 socket send both IPv4 and IPv6
10112 		 * multicast packets using different IP*_MULTICAST_IF
10113 		 * interfaces.
10114 		 */
10115 		if (connp->conn_outgoing_ill != NULL) {
10116 			err = ill_check_and_refhold(connp->conn_outgoing_ill);
10117 			if (err == ILL_LOOKUP_FAILED) {
10118 				ip1dbg(("ip_output_v6: multicast"
10119 				    " conn_outgoing_ill no ipif\n"));
10120 				goto multicast_discard;
10121 			}
10122 			ill = connp->conn_outgoing_ill;
10123 		} else if (connp->conn_multicast_ill != NULL) {
10124 			err = ill_check_and_refhold(connp->conn_multicast_ill);
10125 			if (err == ILL_LOOKUP_FAILED) {
10126 				ip1dbg(("ip_output_v6: multicast"
10127 				    " conn_multicast_ill no ipif\n"));
10128 				goto multicast_discard;
10129 			}
10130 			ill = connp->conn_multicast_ill;
10131 		} else {
10132 			mutex_exit(&connp->conn_lock);
10133 			conn_lock_held = B_FALSE;
10134 			ipif = ipif_lookup_group_v6(v6dstp, zoneid, ipst);
10135 			if (ipif == NULL) {
10136 				ip1dbg(("ip_output_v6: multicast no ipif\n"));
10137 				goto multicast_discard;
10138 			}
10139 			/*
10140 			 * We have a ref to this ipif, so we can safely
10141 			 * access ipif_ill.
10142 			 */
10143 			ill = ipif->ipif_ill;
10144 			mutex_enter(&ill->ill_lock);
10145 			if (!ILL_CAN_LOOKUP(ill)) {
10146 				mutex_exit(&ill->ill_lock);
10147 				ipif_refrele(ipif);
10148 				ill = NULL;
10149 				ip1dbg(("ip_output_v6: multicast no ipif\n"));
10150 				goto multicast_discard;
10151 			}
10152 			ill_refhold_locked(ill);
10153 			mutex_exit(&ill->ill_lock);
10154 			ipif_refrele(ipif);
10155 			/*
10156 			 * Save binding until IPV6_MULTICAST_IF
10157 			 * changes it
10158 			 */
10159 			mutex_enter(&connp->conn_lock);
10160 			connp->conn_multicast_ill = ill;
10161 			connp->conn_orig_multicast_ifindex =
10162 			    ill->ill_phyint->phyint_ifindex;
10163 			mutex_exit(&connp->conn_lock);
10164 		}
10165 	}
10166 	if (conn_lock_held)
10167 		mutex_exit(&connp->conn_lock);
10168 
10169 	if (saved_ill != NULL)
10170 		ill_refrele(saved_ill);
10171 
10172 	ASSERT(ill != NULL);
10173 	/*
10174 	 * For multicast loopback interfaces replace the multicast address
10175 	 * with a unicast address for the ire lookup.
10176 	 */
10177 	if (IS_LOOPBACK(ill))
10178 		v6dstp = &ill->ill_ipif->ipif_v6lcl_addr;
10179 
10180 	mibptr = ill->ill_ip_mib;
10181 	if (do_outrequests) {
10182 		BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
10183 		do_outrequests = B_FALSE;
10184 	}
10185 	BUMP_MIB(mibptr, ipIfStatsHCOutMcastPkts);
10186 	UPDATE_MIB(mibptr, ipIfStatsHCOutMcastOctets,
10187 	    ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN);
10188 
10189 	/*
10190 	 * As we may lose the conn by the time we reach ip_wput_ire_v6
10191 	 * we copy conn_multicast_loop and conn_dontroute on to an
10192 	 * ipsec_out. In case if this datagram goes out secure,
10193 	 * we need the ill_index also. Copy that also into the
10194 	 * ipsec_out.
10195 	 */
10196 	if (mctl_present) {
10197 		io = (ipsec_out_t *)first_mp->b_rptr;
10198 		ASSERT(first_mp->b_datap->db_type == M_CTL);
10199 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
10200 	} else {
10201 		ASSERT(mp == first_mp);
10202 		if ((first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack)) ==
10203 		    NULL) {
10204 			BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10205 			freemsg(mp);
10206 			if (ill != NULL)
10207 				ill_refrele(ill);
10208 			if (need_decref)
10209 				CONN_DEC_REF(connp);
10210 			return;
10211 		}
10212 		io = (ipsec_out_t *)first_mp->b_rptr;
10213 		/* This is not a secure packet */
10214 		io->ipsec_out_secure = B_FALSE;
10215 		io->ipsec_out_use_global_policy = B_TRUE;
10216 		io->ipsec_out_zoneid =
10217 		    (zoneid != ALL_ZONES ? zoneid : GLOBAL_ZONEID);
10218 		first_mp->b_cont = mp;
10219 		mctl_present = B_TRUE;
10220 	}
10221 	io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex;
10222 	io->ipsec_out_unspec_src = unspec_src;
10223 	if (connp != NULL)
10224 		io->ipsec_out_dontroute = connp->conn_dontroute;
10225 
10226 send_from_ill:
10227 	ASSERT(ill != NULL);
10228 	ASSERT(mibptr == ill->ill_ip_mib);
10229 	if (do_outrequests) {
10230 		BUMP_MIB(mibptr, ipIfStatsHCOutRequests);
10231 		do_outrequests = B_FALSE;
10232 	}
10233 
10234 	if (io != NULL)
10235 		io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex;
10236 
10237 	/*
10238 	 * When a specific ill is specified (using IPV6_PKTINFO,
10239 	 * IPV6_MULTICAST_IF, or IPV6_BOUND_IF) we will only match
10240 	 * on routing entries (ftable and ctable) that have a matching
10241 	 * ire->ire_ipif->ipif_ill. Thus this can only be used
10242 	 * for destinations that are on-link for the specific ill
10243 	 * and that can appear on multiple links. Thus it is useful
10244 	 * for multicast destinations, link-local destinations, and
10245 	 * at some point perhaps for site-local destinations (if the
10246 	 * node sits at a site boundary).
10247 	 * We create the cache entries in the regular ctable since
10248 	 * it can not "confuse" things for other destinations.
10249 	 * table.
10250 	 *
10251 	 * NOTE : conn_ire_cache is not used for caching ire_ctable_lookups.
10252 	 *	  It is used only when ire_cache_lookup is used above.
10253 	 */
10254 	ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ill->ill_ipif,
10255 	    zoneid, MBLK_GETLABEL(mp), match_flags, ipst);
10256 	if (ire != NULL) {
10257 		/*
10258 		 * Check if the ire has the RTF_MULTIRT flag, inherited
10259 		 * from an IRE_OFFSUBNET ire entry in ip_newroute().
10260 		 */
10261 		if (ire->ire_flags & RTF_MULTIRT) {
10262 			/*
10263 			 * Force hop limit of multirouted packets if required.
10264 			 * The hop limit of such packets is bounded by the
10265 			 * ip_multirt_ttl ndd variable.
10266 			 * NDP packets must have a hop limit of 255; don't
10267 			 * change the hop limit in that case.
10268 			 */
10269 			if ((ipst->ips_ip_multirt_ttl > 0) &&
10270 			    (ip6h->ip6_hops > ipst->ips_ip_multirt_ttl) &&
10271 			    (ip6h->ip6_hops != IPV6_MAX_HOPS)) {
10272 				if (ip_debug > 3) {
10273 					ip2dbg(("ip_wput_v6: forcing multirt "
10274 					    "hop limit to %d (was %d) ",
10275 					    ipst->ips_ip_multirt_ttl,
10276 					    ip6h->ip6_hops));
10277 					pr_addr_dbg("v6dst %s\n", AF_INET6,
10278 					    &ire->ire_addr_v6);
10279 				}
10280 				ip6h->ip6_hops = ipst->ips_ip_multirt_ttl;
10281 			}
10282 
10283 			/*
10284 			 * We look at this point if there are pending
10285 			 * unresolved routes. ire_multirt_need_resolve_v6()
10286 			 * checks in O(n) that all IRE_OFFSUBNET ire
10287 			 * entries for the packet's destination and
10288 			 * flagged RTF_MULTIRT are currently resolved.
10289 			 * If some remain unresolved, we make a copy
10290 			 * of the current message. It will be used
10291 			 * to initiate additional route resolutions.
10292 			 */
10293 			multirt_need_resolve =
10294 			    ire_multirt_need_resolve_v6(&ire->ire_addr_v6,
10295 			    MBLK_GETLABEL(first_mp), ipst);
10296 			ip2dbg(("ip_wput_v6[send_from_ill]: ire %p, "
10297 			    "multirt_need_resolve %d, first_mp %p\n",
10298 			    (void *)ire, multirt_need_resolve,
10299 			    (void *)first_mp));
10300 			if (multirt_need_resolve) {
10301 				copy_mp = copymsg(first_mp);
10302 				if (copy_mp != NULL) {
10303 					MULTIRT_DEBUG_TAG(copy_mp);
10304 				}
10305 			}
10306 		}
10307 
10308 		ip1dbg(("ip_wput_v6: send on %s, ire = %p, ill index = %d\n",
10309 		    ill->ill_name, (void *)ire,
10310 		    ill->ill_phyint->phyint_ifindex));
10311 		ip_wput_ire_v6(q, first_mp, ire, unspec_src, cksum_request,
10312 		    connp, caller,
10313 		    (attach_if ? ill->ill_phyint->phyint_ifindex : 0),
10314 		    ip6i_flags, zoneid);
10315 		ire_refrele(ire);
10316 		if (need_decref) {
10317 			CONN_DEC_REF(connp);
10318 			connp = NULL;
10319 		}
10320 
10321 		/*
10322 		 * Try to resolve another multiroute if
10323 		 * ire_multirt_need_resolve_v6() deemed it necessary.
10324 		 * copy_mp will be consumed (sent or freed) by
10325 		 * ip_newroute_[ipif_]v6().
10326 		 */
10327 		if (copy_mp != NULL) {
10328 			if (mctl_present) {
10329 				ip6h = (ip6_t *)copy_mp->b_cont->b_rptr;
10330 			} else {
10331 				ip6h = (ip6_t *)copy_mp->b_rptr;
10332 			}
10333 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
10334 				ipif = ipif_lookup_group_v6(&ip6h->ip6_dst,
10335 				    zoneid, ipst);
10336 				if (ipif == NULL) {
10337 					ip1dbg(("ip_wput_v6: No ipif for "
10338 					    "multicast\n"));
10339 					MULTIRT_DEBUG_UNTAG(copy_mp);
10340 					freemsg(copy_mp);
10341 					return;
10342 				}
10343 				ip_newroute_ipif_v6(q, copy_mp, ipif,
10344 				    ip6h->ip6_dst, unspec_src, zoneid);
10345 				ipif_refrele(ipif);
10346 			} else {
10347 				ip_newroute_v6(q, copy_mp, &ip6h->ip6_dst,
10348 				    &ip6h->ip6_src, ill, zoneid, ipst);
10349 			}
10350 		}
10351 		ill_refrele(ill);
10352 		return;
10353 	}
10354 	if (need_decref) {
10355 		CONN_DEC_REF(connp);
10356 		connp = NULL;
10357 	}
10358 
10359 	/* Update rptr if there was an ip6i_t header. */
10360 	if (ip6i != NULL)
10361 		mp->b_rptr -= sizeof (ip6i_t);
10362 	if (unspec_src || attach_if) {
10363 		if (ip6i == NULL) {
10364 			/*
10365 			 * Add ip6i_t header to carry unspec_src
10366 			 * or attach_if until the packet comes back in
10367 			 * ip_wput_v6.
10368 			 */
10369 			if (mctl_present) {
10370 				first_mp->b_cont =
10371 				    ip_add_info_v6(mp, NULL, v6dstp);
10372 				mp = first_mp->b_cont;
10373 				if (mp == NULL)
10374 					freeb(first_mp);
10375 			} else {
10376 				first_mp = mp = ip_add_info_v6(mp, NULL,
10377 				    v6dstp);
10378 			}
10379 			if (mp == NULL) {
10380 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10381 				ill_refrele(ill);
10382 				return;
10383 			}
10384 			ip6i = (ip6i_t *)mp->b_rptr;
10385 			if ((mp->b_wptr - (uchar_t *)ip6i) ==
10386 			    sizeof (ip6i_t)) {
10387 				/*
10388 				 * ndp_resolver called from ip_newroute_v6
10389 				 * expects a pulled up message.
10390 				 */
10391 				if (!pullupmsg(mp, -1)) {
10392 					ip1dbg(("ip_wput_v6: pullupmsg"
10393 					    " failed\n"));
10394 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10395 					freemsg(first_mp);
10396 					return;
10397 				}
10398 				ip6i = (ip6i_t *)mp->b_rptr;
10399 			}
10400 			ip6h = (ip6_t *)&ip6i[1];
10401 			v6dstp = &ip6h->ip6_dst;
10402 		}
10403 		if (unspec_src)
10404 			ip6i->ip6i_flags |= IP6I_UNSPEC_SRC;
10405 		if (attach_if) {
10406 			/*
10407 			 * Bind to nofailover/BOUND_PIF overrides ifindex.
10408 			 */
10409 			ip6i->ip6i_flags |= IP6I_ATTACH_IF;
10410 			ip6i->ip6i_flags &= ~IP6I_IFINDEX;
10411 			ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex;
10412 			if (drop_if_delayed) {
10413 				/* This is a multipathing probe packet */
10414 				ip6i->ip6i_flags |= IP6I_DROP_IFDELAYED;
10415 			}
10416 		}
10417 		if (mctl_present) {
10418 			ASSERT(io != NULL);
10419 			io->ipsec_out_unspec_src = unspec_src;
10420 		}
10421 	}
10422 	if (IN6_IS_ADDR_MULTICAST(v6dstp)) {
10423 		ip_newroute_ipif_v6(q, first_mp, ill->ill_ipif, *v6dstp,
10424 		    unspec_src, zoneid);
10425 	} else {
10426 		ip_newroute_v6(q, first_mp, v6dstp, &ip6h->ip6_src, ill,
10427 		    zoneid, ipst);
10428 	}
10429 	ill_refrele(ill);
10430 	return;
10431 
10432 notv6:
10433 	/* FIXME?: assume the caller calls the right version of ip_output? */
10434 	if (q->q_next == NULL) {
10435 		connp = Q_TO_CONN(q);
10436 
10437 		/*
10438 		 * We can change conn_send for all types of conn, even
10439 		 * though only TCP uses it right now.
10440 		 * FIXME: sctp could use conn_send but doesn't currently.
10441 		 */
10442 		ip_setpktversion(connp, B_FALSE, B_TRUE, ipst);
10443 	}
10444 	BUMP_MIB(mibptr, ipIfStatsOutWrongIPVersion);
10445 	(void) ip_output(arg, first_mp, arg2, caller);
10446 	if (ill != NULL)
10447 		ill_refrele(ill);
10448 }
10449 
10450 /*
10451  * If this is a conn_t queue, then we pass in the conn. This includes the
10452  * zoneid.
10453  * Otherwise, this is a message for an ill_t queue,
10454  * in which case we use the global zoneid since those are all part of
10455  * the global zone.
10456  */
10457 void
10458 ip_wput_v6(queue_t *q, mblk_t *mp)
10459 {
10460 	if (CONN_Q(q))
10461 		ip_output_v6(Q_TO_CONN(q), mp, q, IP_WPUT);
10462 	else
10463 		ip_output_v6(GLOBAL_ZONEID, mp, q, IP_WPUT);
10464 }
10465 
10466 static void
10467 ipsec_out_attach_if(ipsec_out_t *io, int attach_index)
10468 {
10469 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
10470 	io->ipsec_out_attach_if = B_TRUE;
10471 	io->ipsec_out_ill_index = attach_index;
10472 }
10473 
10474 /*
10475  * NULL send-to queue - packet is to be delivered locally.
10476  */
10477 void
10478 ip_wput_local_v6(queue_t *q, ill_t *ill, ip6_t *ip6h, mblk_t *first_mp,
10479     ire_t *ire, int fanout_flags)
10480 {
10481 	uint32_t	ports;
10482 	mblk_t		*mp = first_mp, *first_mp1;
10483 	boolean_t	mctl_present;
10484 	uint8_t		nexthdr;
10485 	uint16_t	hdr_length;
10486 	ipsec_out_t	*io;
10487 	mib2_ipIfStatsEntry_t	*mibptr;
10488 	ilm_t		*ilm;
10489 	uint_t	nexthdr_offset;
10490 	ip_stack_t	*ipst = ill->ill_ipst;
10491 
10492 	if (DB_TYPE(mp) == M_CTL) {
10493 		io = (ipsec_out_t *)mp->b_rptr;
10494 		if (!io->ipsec_out_secure) {
10495 			mp = mp->b_cont;
10496 			freeb(first_mp);
10497 			first_mp = mp;
10498 			mctl_present = B_FALSE;
10499 		} else {
10500 			mctl_present = B_TRUE;
10501 			mp = first_mp->b_cont;
10502 			ipsec_out_to_in(first_mp);
10503 		}
10504 	} else {
10505 		mctl_present = B_FALSE;
10506 	}
10507 
10508 	/*
10509 	 * Remove reachability confirmation bit from version field
10510 	 * before passing the packet on to any firewall hooks or
10511 	 * looping back the packet.
10512 	 */
10513 	if (ip6h->ip6_vcf & IP_FORWARD_PROG)
10514 		ip6h->ip6_vcf &= ~IP_FORWARD_PROG;
10515 
10516 	DTRACE_PROBE4(ip6__loopback__in__start,
10517 	    ill_t *, ill, ill_t *, NULL,
10518 	    ip6_t *, ip6h, mblk_t *, first_mp);
10519 
10520 	FW_HOOKS6(ipst->ips_ip6_loopback_in_event,
10521 	    ipst->ips_ipv6firewall_loopback_in,
10522 	    ill, NULL, ip6h, first_mp, mp, 0, ipst);
10523 
10524 	DTRACE_PROBE1(ip6__loopback__in__end, mblk_t *, first_mp);
10525 
10526 	if (first_mp == NULL)
10527 		return;
10528 
10529 	nexthdr = ip6h->ip6_nxt;
10530 	mibptr = ill->ill_ip_mib;
10531 
10532 	/* Fastpath */
10533 	switch (nexthdr) {
10534 	case IPPROTO_TCP:
10535 	case IPPROTO_UDP:
10536 	case IPPROTO_ICMPV6:
10537 	case IPPROTO_SCTP:
10538 		hdr_length = IPV6_HDR_LEN;
10539 		nexthdr_offset = (uint_t)((uchar_t *)&ip6h->ip6_nxt -
10540 		    (uchar_t *)ip6h);
10541 		break;
10542 	default: {
10543 		uint8_t	*nexthdrp;
10544 
10545 		if (!ip_hdr_length_nexthdr_v6(mp, ip6h,
10546 		    &hdr_length, &nexthdrp)) {
10547 			/* Malformed packet */
10548 			BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10549 			freemsg(first_mp);
10550 			return;
10551 		}
10552 		nexthdr = *nexthdrp;
10553 		nexthdr_offset = nexthdrp - (uint8_t *)ip6h;
10554 		break;
10555 	}
10556 	}
10557 
10558 	UPDATE_OB_PKT_COUNT(ire);
10559 	ire->ire_last_used_time = lbolt;
10560 
10561 	switch (nexthdr) {
10562 		case IPPROTO_TCP:
10563 			if (DB_TYPE(mp) == M_DATA) {
10564 				/*
10565 				 * M_DATA mblk, so init mblk (chain) for
10566 				 * no struio().
10567 				 */
10568 				mblk_t  *mp1 = mp;
10569 
10570 				do {
10571 					mp1->b_datap->db_struioflag = 0;
10572 				} while ((mp1 = mp1->b_cont) != NULL);
10573 			}
10574 			ports = *(uint32_t *)(mp->b_rptr + hdr_length +
10575 			    TCP_PORTS_OFFSET);
10576 			ip_fanout_tcp_v6(q, first_mp, ip6h, ill, ill,
10577 			    fanout_flags|IP_FF_SEND_ICMP|IP_FF_SYN_ADDIRE|
10578 			    IP_FF_IPINFO|IP6_NO_IPPOLICY|IP_FF_LOOPBACK,
10579 			    hdr_length, mctl_present, ire->ire_zoneid);
10580 			return;
10581 
10582 		case IPPROTO_UDP:
10583 			ports = *(uint32_t *)(mp->b_rptr + hdr_length +
10584 			    UDP_PORTS_OFFSET);
10585 			ip_fanout_udp_v6(q, first_mp, ip6h, ports, ill, ill,
10586 			    fanout_flags|IP_FF_SEND_ICMP|IP_FF_IPINFO|
10587 			    IP6_NO_IPPOLICY, mctl_present, ire->ire_zoneid);
10588 			return;
10589 
10590 		case IPPROTO_SCTP:
10591 		{
10592 			ports = *(uint32_t *)(mp->b_rptr + hdr_length);
10593 			ip_fanout_sctp(first_mp, ill, (ipha_t *)ip6h, ports,
10594 			    fanout_flags|IP_FF_SEND_ICMP|IP_FF_IPINFO,
10595 			    mctl_present, IP6_NO_IPPOLICY, ire->ire_zoneid);
10596 			return;
10597 		}
10598 		case IPPROTO_ICMPV6: {
10599 			icmp6_t *icmp6;
10600 
10601 			/* check for full IPv6+ICMPv6 header */
10602 			if ((mp->b_wptr - mp->b_rptr) <
10603 			    (hdr_length + ICMP6_MINLEN)) {
10604 				if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) {
10605 					ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg"
10606 					    " failed\n"));
10607 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10608 					freemsg(first_mp);
10609 					return;
10610 				}
10611 				ip6h = (ip6_t *)mp->b_rptr;
10612 			}
10613 			icmp6 = (icmp6_t *)((uchar_t *)ip6h + hdr_length);
10614 
10615 			/* Update output mib stats */
10616 			icmp_update_out_mib_v6(ill, icmp6);
10617 
10618 			/* Check variable for testing applications */
10619 			if (ipst->ips_ipv6_drop_inbound_icmpv6) {
10620 				freemsg(first_mp);
10621 				return;
10622 			}
10623 			/*
10624 			 * Assume that there is always at least one conn for
10625 			 * ICMPv6 (in.ndpd) i.e. don't optimize the case
10626 			 * where there is no conn.
10627 			 */
10628 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) &&
10629 			    !IS_LOOPBACK(ill)) {
10630 				/*
10631 				 * In the multicast case, applications may have
10632 				 * joined the group from different zones, so we
10633 				 * need to deliver the packet to each of them.
10634 				 * Loop through the multicast memberships
10635 				 * structures (ilm) on the receive ill and send
10636 				 * a copy of the packet up each matching one.
10637 				 * However, we don't do this for multicasts sent
10638 				 * on the loopback interface (PHYI_LOOPBACK flag
10639 				 * set) as they must stay in the sender's zone.
10640 				 */
10641 				ILM_WALKER_HOLD(ill);
10642 				for (ilm = ill->ill_ilm; ilm != NULL;
10643 				    ilm = ilm->ilm_next) {
10644 					if (ilm->ilm_flags & ILM_DELETED)
10645 						continue;
10646 					if (!IN6_ARE_ADDR_EQUAL(
10647 					    &ilm->ilm_v6addr, &ip6h->ip6_dst))
10648 						continue;
10649 					if ((fanout_flags &
10650 					    IP_FF_NO_MCAST_LOOP) &&
10651 					    ilm->ilm_zoneid == ire->ire_zoneid)
10652 						continue;
10653 					if (!ipif_lookup_zoneid(ill,
10654 					    ilm->ilm_zoneid, IPIF_UP, NULL))
10655 						continue;
10656 
10657 					first_mp1 = ip_copymsg(first_mp);
10658 					if (first_mp1 == NULL)
10659 						continue;
10660 					icmp_inbound_v6(q, first_mp1, ill,
10661 					    hdr_length, mctl_present,
10662 					    IP6_NO_IPPOLICY, ilm->ilm_zoneid,
10663 					    NULL);
10664 				}
10665 				ILM_WALKER_RELE(ill);
10666 			} else {
10667 				first_mp1 = ip_copymsg(first_mp);
10668 				if (first_mp1 != NULL)
10669 					icmp_inbound_v6(q, first_mp1, ill,
10670 					    hdr_length, mctl_present,
10671 					    IP6_NO_IPPOLICY, ire->ire_zoneid,
10672 					    NULL);
10673 			}
10674 		}
10675 		/* FALLTHRU */
10676 		default: {
10677 			/*
10678 			 * Handle protocols with which IPv6 is less intimate.
10679 			 */
10680 			fanout_flags |= IP_FF_RAWIP|IP_FF_IPINFO;
10681 
10682 			/*
10683 			 * Enable sending ICMP for "Unknown" nexthdr
10684 			 * case. i.e. where we did not FALLTHRU from
10685 			 * IPPROTO_ICMPV6 processing case above.
10686 			 */
10687 			if (nexthdr != IPPROTO_ICMPV6)
10688 				fanout_flags |= IP_FF_SEND_ICMP;
10689 			/*
10690 			 * Note: There can be more than one stream bound
10691 			 * to a particular protocol. When this is the case,
10692 			 * each one gets a copy of any incoming packets.
10693 			 */
10694 			ip_fanout_proto_v6(q, first_mp, ip6h, ill, ill, nexthdr,
10695 			    nexthdr_offset, fanout_flags|IP6_NO_IPPOLICY,
10696 			    mctl_present, ire->ire_zoneid);
10697 			return;
10698 		}
10699 	}
10700 }
10701 
10702 /*
10703  * Send packet using IRE.
10704  * Checksumming is controlled by cksum_request:
10705  *	-1 => normal i.e. TCP/UDP/SCTP/ICMPv6 are checksummed and nothing else.
10706  *	1 => Skip TCP/UDP/SCTP checksum
10707  * 	Otherwise => checksum_request contains insert offset for checksum
10708  *
10709  * Assumes that the following set of headers appear in the first
10710  * mblk:
10711  *	ip6_t
10712  *	Any extension headers
10713  *	TCP/UDP/SCTP header (if present)
10714  * The routine can handle an ICMPv6 header that is not in the first mblk.
10715  *
10716  * NOTE : This function does not ire_refrele the ire passed in as the
10717  *	  argument unlike ip_wput_ire where the REFRELE is done.
10718  *	  Refer to ip_wput_ire for more on this.
10719  */
10720 static void
10721 ip_wput_ire_v6(queue_t *q, mblk_t *mp, ire_t *ire, int unspec_src,
10722     int cksum_request, conn_t *connp, int caller, int attach_index, int flags,
10723     zoneid_t zoneid)
10724 {
10725 	ip6_t		*ip6h;
10726 	uint8_t		nexthdr;
10727 	uint16_t	hdr_length;
10728 	uint_t		reachable = 0x0;
10729 	ill_t		*ill;
10730 	mib2_ipIfStatsEntry_t	*mibptr;
10731 	mblk_t		*first_mp;
10732 	boolean_t	mctl_present;
10733 	ipsec_out_t	*io;
10734 	boolean_t	conn_dontroute;	/* conn value for multicast */
10735 	boolean_t	conn_multicast_loop;	/* conn value for multicast */
10736 	boolean_t 	multicast_forward;	/* Should we forward ? */
10737 	int		max_frag;
10738 	ip_stack_t	*ipst = ire->ire_ipst;
10739 	ipsec_stack_t	*ipss = ipst->ips_netstack->netstack_ipsec;
10740 
10741 	ill = ire_to_ill(ire);
10742 	first_mp = mp;
10743 	multicast_forward = B_FALSE;
10744 
10745 	if (mp->b_datap->db_type != M_CTL) {
10746 		ip6h = (ip6_t *)first_mp->b_rptr;
10747 	} else {
10748 		io = (ipsec_out_t *)first_mp->b_rptr;
10749 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
10750 		/*
10751 		 * Grab the zone id now because the M_CTL can be discarded by
10752 		 * ip_wput_ire_parse_ipsec_out() below.
10753 		 */
10754 		ASSERT(zoneid == io->ipsec_out_zoneid);
10755 		ASSERT(zoneid != ALL_ZONES);
10756 		ip6h = (ip6_t *)first_mp->b_cont->b_rptr;
10757 		/*
10758 		 * For the multicast case, ipsec_out carries conn_dontroute and
10759 		 * conn_multicast_loop as conn may not be available here. We
10760 		 * need this for multicast loopback and forwarding which is done
10761 		 * later in the code.
10762 		 */
10763 		if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
10764 			conn_dontroute = io->ipsec_out_dontroute;
10765 			conn_multicast_loop = io->ipsec_out_multicast_loop;
10766 			/*
10767 			 * If conn_dontroute is not set or conn_multicast_loop
10768 			 * is set, we need to do forwarding/loopback. For
10769 			 * datagrams from ip_wput_multicast, conn_dontroute is
10770 			 * set to B_TRUE and conn_multicast_loop is set to
10771 			 * B_FALSE so that we neither do forwarding nor
10772 			 * loopback.
10773 			 */
10774 			if (!conn_dontroute || conn_multicast_loop)
10775 				multicast_forward = B_TRUE;
10776 		}
10777 	}
10778 
10779 	/*
10780 	 * If the sender didn't supply the hop limit and there is a default
10781 	 * unicast hop limit associated with the output interface, we use
10782 	 * that if the packet is unicast.  Interface specific unicast hop
10783 	 * limits as set via the SIOCSLIFLNKINFO ioctl.
10784 	 */
10785 	if (ill->ill_max_hops != 0 && !(flags & IP6I_HOPLIMIT) &&
10786 	    !(IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst))) {
10787 		ip6h->ip6_hops = ill->ill_max_hops;
10788 	}
10789 
10790 	if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid &&
10791 	    ire->ire_zoneid != ALL_ZONES) {
10792 		/*
10793 		 * When a zone sends a packet to another zone, we try to deliver
10794 		 * the packet under the same conditions as if the destination
10795 		 * was a real node on the network. To do so, we look for a
10796 		 * matching route in the forwarding table.
10797 		 * RTF_REJECT and RTF_BLACKHOLE are handled just like
10798 		 * ip_newroute_v6() does.
10799 		 * Note that IRE_LOCAL are special, since they are used
10800 		 * when the zoneid doesn't match in some cases. This means that
10801 		 * we need to handle ipha_src differently since ire_src_addr
10802 		 * belongs to the receiving zone instead of the sending zone.
10803 		 * When ip_restrict_interzone_loopback is set, then
10804 		 * ire_cache_lookup_v6() ensures that IRE_LOCAL are only used
10805 		 * for loopback between zones when the logical "Ethernet" would
10806 		 * have looped them back.
10807 		 */
10808 		ire_t *src_ire;
10809 
10810 		src_ire = ire_ftable_lookup_v6(&ip6h->ip6_dst, 0, 0, 0,
10811 		    NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE |
10812 		    MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE), ipst);
10813 		if (src_ire != NULL &&
10814 		    !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) &&
10815 		    (!ipst->ips_ip_restrict_interzone_loopback ||
10816 		    ire_local_same_ill_group(ire, src_ire))) {
10817 			if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) &&
10818 			    !unspec_src) {
10819 				ip6h->ip6_src = src_ire->ire_src_addr_v6;
10820 			}
10821 			ire_refrele(src_ire);
10822 		} else {
10823 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutNoRoutes);
10824 			if (src_ire != NULL) {
10825 				if (src_ire->ire_flags & RTF_BLACKHOLE) {
10826 					ire_refrele(src_ire);
10827 					freemsg(first_mp);
10828 					return;
10829 				}
10830 				ire_refrele(src_ire);
10831 			}
10832 			if (ip_hdr_complete_v6(ip6h, zoneid, ipst)) {
10833 				/* Failed */
10834 				freemsg(first_mp);
10835 				return;
10836 			}
10837 			icmp_unreachable_v6(q, first_mp,
10838 			    ICMP6_DST_UNREACH_NOROUTE, B_FALSE, B_FALSE,
10839 			    zoneid, ipst);
10840 			return;
10841 		}
10842 	}
10843 
10844 	if (mp->b_datap->db_type == M_CTL ||
10845 	    ipss->ipsec_outbound_v6_policy_present) {
10846 		mp = ip_wput_ire_parse_ipsec_out(first_mp, NULL, ip6h, ire,
10847 		    connp, unspec_src, zoneid);
10848 		if (mp == NULL) {
10849 			return;
10850 		}
10851 	}
10852 
10853 	first_mp = mp;
10854 	if (mp->b_datap->db_type == M_CTL) {
10855 		io = (ipsec_out_t *)mp->b_rptr;
10856 		ASSERT(io->ipsec_out_type == IPSEC_OUT);
10857 		mp = mp->b_cont;
10858 		mctl_present = B_TRUE;
10859 	} else {
10860 		mctl_present = B_FALSE;
10861 	}
10862 
10863 	ip6h = (ip6_t *)mp->b_rptr;
10864 	nexthdr = ip6h->ip6_nxt;
10865 	mibptr = ill->ill_ip_mib;
10866 
10867 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) && !unspec_src) {
10868 		ipif_t *ipif;
10869 
10870 		/*
10871 		 * Select the source address using ipif_select_source_v6.
10872 		 */
10873 		if (attach_index != 0) {
10874 			ipif = ipif_select_source_v6(ill, &ip6h->ip6_dst,
10875 			    RESTRICT_TO_ILL, IPV6_PREFER_SRC_DEFAULT, zoneid);
10876 		} else {
10877 			ipif = ipif_select_source_v6(ill, &ip6h->ip6_dst,
10878 			    RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT, zoneid);
10879 		}
10880 		if (ipif == NULL) {
10881 			if (ip_debug > 2) {
10882 				/* ip1dbg */
10883 				pr_addr_dbg("ip_wput_ire_v6: no src for "
10884 				    "dst %s\n, ", AF_INET6, &ip6h->ip6_dst);
10885 				printf("ip_wput_ire_v6: interface name %s\n",
10886 				    ill->ill_name);
10887 			}
10888 			freemsg(first_mp);
10889 			return;
10890 		}
10891 		ip6h->ip6_src = ipif->ipif_v6src_addr;
10892 		ipif_refrele(ipif);
10893 	}
10894 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
10895 		if ((connp != NULL && connp->conn_multicast_loop) ||
10896 		    !IS_LOOPBACK(ill)) {
10897 			ilm_t	*ilm;
10898 
10899 			ILM_WALKER_HOLD(ill);
10900 			ilm = ilm_lookup_ill_v6(ill, &ip6h->ip6_dst, ALL_ZONES);
10901 			ILM_WALKER_RELE(ill);
10902 			if (ilm != NULL) {
10903 				mblk_t *nmp;
10904 				int fanout_flags = 0;
10905 
10906 				if (connp != NULL &&
10907 				    !connp->conn_multicast_loop) {
10908 					fanout_flags |= IP_FF_NO_MCAST_LOOP;
10909 				}
10910 				ip1dbg(("ip_wput_ire_v6: "
10911 				    "Loopback multicast\n"));
10912 				nmp = ip_copymsg(first_mp);
10913 				if (nmp != NULL) {
10914 					ip6_t	*nip6h;
10915 					mblk_t	*mp_ip6h;
10916 
10917 					if (mctl_present) {
10918 						nip6h = (ip6_t *)
10919 						    nmp->b_cont->b_rptr;
10920 						mp_ip6h = nmp->b_cont;
10921 					} else {
10922 						nip6h = (ip6_t *)nmp->b_rptr;
10923 						mp_ip6h = nmp;
10924 					}
10925 
10926 					DTRACE_PROBE4(
10927 					    ip6__loopback__out__start,
10928 					    ill_t *, NULL,
10929 					    ill_t *, ill,
10930 					    ip6_t *, nip6h,
10931 					    mblk_t *, nmp);
10932 
10933 					FW_HOOKS6(
10934 					    ipst->ips_ip6_loopback_out_event,
10935 					    ipst->ips_ipv6firewall_loopback_out,
10936 					    NULL, ill, nip6h, nmp, mp_ip6h,
10937 					    0, ipst);
10938 
10939 					DTRACE_PROBE1(
10940 					    ip6__loopback__out__end,
10941 					    mblk_t *, nmp);
10942 
10943 					if (nmp != NULL) {
10944 						/*
10945 						 * Deliver locally and to
10946 						 * every local zone, except
10947 						 * the sending zone when
10948 						 * IPV6_MULTICAST_LOOP is
10949 						 * disabled.
10950 						 */
10951 						ip_wput_local_v6(RD(q), ill,
10952 						    nip6h, nmp,
10953 						    ire, fanout_flags);
10954 					}
10955 				} else {
10956 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
10957 					ip1dbg(("ip_wput_ire_v6: "
10958 					    "copymsg failed\n"));
10959 				}
10960 			}
10961 		}
10962 		if (ip6h->ip6_hops == 0 ||
10963 		    IN6_IS_ADDR_MC_NODELOCAL(&ip6h->ip6_dst) ||
10964 		    IS_LOOPBACK(ill)) {
10965 			/*
10966 			 * Local multicast or just loopback on loopback
10967 			 * interface.
10968 			 */
10969 			BUMP_MIB(mibptr, ipIfStatsHCOutMcastPkts);
10970 			UPDATE_MIB(mibptr, ipIfStatsHCOutMcastOctets,
10971 			    ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN);
10972 			ip1dbg(("ip_wput_ire_v6: local multicast only\n"));
10973 			freemsg(first_mp);
10974 			return;
10975 		}
10976 	}
10977 
10978 	if (ire->ire_stq != NULL) {
10979 		uint32_t	sum;
10980 		uint_t		ill_index =  ((ill_t *)ire->ire_stq->q_ptr)->
10981 		    ill_phyint->phyint_ifindex;
10982 		queue_t		*dev_q = ire->ire_stq->q_next;
10983 
10984 		/*
10985 		 * non-NULL send-to queue - packet is to be sent
10986 		 * out an interface.
10987 		 */
10988 
10989 		/* Driver is flow-controlling? */
10990 		if (!IP_FLOW_CONTROLLED_ULP(nexthdr) &&
10991 		    ((dev_q->q_next || dev_q->q_first) && !canput(dev_q))) {
10992 			/*
10993 			 * Queue packet if we have an conn to give back
10994 			 * pressure.  We can't queue packets intended for
10995 			 * hardware acceleration since we've tossed that
10996 			 * state already.  If the packet is being fed back
10997 			 * from ire_send_v6, we don't know the position in
10998 			 * the queue to enqueue the packet and we discard
10999 			 * the packet.
11000 			 */
11001 			if (ipst->ips_ip_output_queue && connp != NULL &&
11002 			    !mctl_present && caller != IRE_SEND) {
11003 				if (caller == IP_WSRV) {
11004 					connp->conn_did_putbq = 1;
11005 					(void) putbq(connp->conn_wq, mp);
11006 					conn_drain_insert(connp);
11007 					/*
11008 					 * caller == IP_WSRV implies we are
11009 					 * the service thread, and the
11010 					 * queue is already noenabled.
11011 					 * The check for canput and
11012 					 * the putbq is not atomic.
11013 					 * So we need to check again.
11014 					 */
11015 					if (canput(dev_q))
11016 						connp->conn_did_putbq = 0;
11017 				} else {
11018 					(void) putq(connp->conn_wq, mp);
11019 				}
11020 				return;
11021 			}
11022 			BUMP_MIB(mibptr, ipIfStatsOutDiscards);
11023 			freemsg(first_mp);
11024 			return;
11025 		}
11026 
11027 		/*
11028 		 * Look for reachability confirmations from the transport.
11029 		 */
11030 		if (ip6h->ip6_vcf & IP_FORWARD_PROG) {
11031 			reachable |= IPV6_REACHABILITY_CONFIRMATION;
11032 			ip6h->ip6_vcf &= ~IP_FORWARD_PROG;
11033 			if (mctl_present)
11034 				io->ipsec_out_reachable = B_TRUE;
11035 		}
11036 		/* Fastpath */
11037 		switch (nexthdr) {
11038 		case IPPROTO_TCP:
11039 		case IPPROTO_UDP:
11040 		case IPPROTO_ICMPV6:
11041 		case IPPROTO_SCTP:
11042 			hdr_length = IPV6_HDR_LEN;
11043 			break;
11044 		default: {
11045 			uint8_t	*nexthdrp;
11046 
11047 			if (!ip_hdr_length_nexthdr_v6(mp, ip6h,
11048 			    &hdr_length, &nexthdrp)) {
11049 				/* Malformed packet */
11050 				BUMP_MIB(mibptr, ipIfStatsOutDiscards);
11051 				freemsg(first_mp);
11052 				return;
11053 			}
11054 			nexthdr = *nexthdrp;
11055 			break;
11056 		}
11057 		}
11058 
11059 		if (cksum_request != -1 && nexthdr != IPPROTO_ICMPV6) {
11060 			uint16_t	*up;
11061 			uint16_t	*insp;
11062 
11063 			/*
11064 			 * The packet header is processed once for all, even
11065 			 * in the multirouting case. We disable hardware
11066 			 * checksum if the packet is multirouted, as it will be
11067 			 * replicated via several interfaces, and not all of
11068 			 * them may have this capability.
11069 			 */
11070 			if (cksum_request == 1 &&
11071 			    !(ire->ire_flags & RTF_MULTIRT)) {
11072 				/* Skip the transport checksum */
11073 				goto cksum_done;
11074 			}
11075 			/*
11076 			 * Do user-configured raw checksum.
11077 			 * Compute checksum and insert at offset "cksum_request"
11078 			 */
11079 
11080 			/* check for enough headers for checksum */
11081 			cksum_request += hdr_length;	/* offset from rptr */
11082 			if ((mp->b_wptr - mp->b_rptr) <
11083 			    (cksum_request + sizeof (int16_t))) {
11084 				if (!pullupmsg(mp,
11085 				    cksum_request + sizeof (int16_t))) {
11086 					ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg"
11087 					    " failed\n"));
11088 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
11089 					freemsg(first_mp);
11090 					return;
11091 				}
11092 				ip6h = (ip6_t *)mp->b_rptr;
11093 			}
11094 			insp = (uint16_t *)((uchar_t *)ip6h + cksum_request);
11095 			ASSERT(((uintptr_t)insp & 0x1) == 0);
11096 			up = (uint16_t *)&ip6h->ip6_src;
11097 			/*
11098 			 * icmp has placed length and routing
11099 			 * header adjustment in *insp.
11100 			 */
11101 			sum = htons(nexthdr) +
11102 			    up[0] + up[1] + up[2] + up[3] +
11103 			    up[4] + up[5] + up[6] + up[7] +
11104 			    up[8] + up[9] + up[10] + up[11] +
11105 			    up[12] + up[13] + up[14] + up[15];
11106 			sum = (sum & 0xffff) + (sum >> 16);
11107 			*insp = IP_CSUM(mp, hdr_length, sum);
11108 		} else if (nexthdr == IPPROTO_TCP) {
11109 			uint16_t	*up;
11110 
11111 			/*
11112 			 * Check for full IPv6 header + enough TCP header
11113 			 * to get at the checksum field.
11114 			 */
11115 			if ((mp->b_wptr - mp->b_rptr) <
11116 			    (hdr_length + TCP_CHECKSUM_OFFSET +
11117 			    TCP_CHECKSUM_SIZE)) {
11118 				if (!pullupmsg(mp, hdr_length +
11119 				    TCP_CHECKSUM_OFFSET + TCP_CHECKSUM_SIZE)) {
11120 					ip1dbg(("ip_wput_v6: TCP hdr pullupmsg"
11121 					    " failed\n"));
11122 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
11123 					freemsg(first_mp);
11124 					return;
11125 				}
11126 				ip6h = (ip6_t *)mp->b_rptr;
11127 			}
11128 
11129 			up = (uint16_t *)&ip6h->ip6_src;
11130 			/*
11131 			 * Note: The TCP module has stored the length value
11132 			 * into the tcp checksum field, so we don't
11133 			 * need to explicitly sum it in here.
11134 			 */
11135 			sum = up[0] + up[1] + up[2] + up[3] +
11136 			    up[4] + up[5] + up[6] + up[7] +
11137 			    up[8] + up[9] + up[10] + up[11] +
11138 			    up[12] + up[13] + up[14] + up[15];
11139 
11140 			/* Fold the initial sum */
11141 			sum = (sum & 0xffff) + (sum >> 16);
11142 
11143 			up = (uint16_t *)(((uchar_t *)ip6h) +
11144 			    hdr_length + TCP_CHECKSUM_OFFSET);
11145 
11146 			IP_CKSUM_XMIT(ill, ire, mp, ip6h, up, IPPROTO_TCP,
11147 			    hdr_length, ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN,
11148 			    ire->ire_max_frag, mctl_present, sum);
11149 
11150 			/* Software checksum? */
11151 			if (DB_CKSUMFLAGS(mp) == 0) {
11152 				IP6_STAT(ipst, ip6_out_sw_cksum);
11153 				IP6_STAT_UPDATE(ipst,
11154 				    ip6_tcp_out_sw_cksum_bytes,
11155 				    (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN) -
11156 				    hdr_length);
11157 			}
11158 		} else if (nexthdr == IPPROTO_UDP) {
11159 			uint16_t	*up;
11160 
11161 			/*
11162 			 * check for full IPv6 header + enough UDP header
11163 			 * to get at the UDP checksum field
11164 			 */
11165 			if ((mp->b_wptr - mp->b_rptr) < (hdr_length +
11166 			    UDP_CHECKSUM_OFFSET + UDP_CHECKSUM_SIZE)) {
11167 				if (!pullupmsg(mp, hdr_length +
11168 				    UDP_CHECKSUM_OFFSET + UDP_CHECKSUM_SIZE)) {
11169 					ip1dbg(("ip_wput_v6: UDP hdr pullupmsg"
11170 					    " failed\n"));
11171 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
11172 					freemsg(first_mp);
11173 					return;
11174 				}
11175 				ip6h = (ip6_t *)mp->b_rptr;
11176 			}
11177 			up = (uint16_t *)&ip6h->ip6_src;
11178 			/*
11179 			 * Note: The UDP module has stored the length value
11180 			 * into the udp checksum field, so we don't
11181 			 * need to explicitly sum it in here.
11182 			 */
11183 			sum = up[0] + up[1] + up[2] + up[3] +
11184 			    up[4] + up[5] + up[6] + up[7] +
11185 			    up[8] + up[9] + up[10] + up[11] +
11186 			    up[12] + up[13] + up[14] + up[15];
11187 
11188 			/* Fold the initial sum */
11189 			sum = (sum & 0xffff) + (sum >> 16);
11190 
11191 			up = (uint16_t *)(((uchar_t *)ip6h) +
11192 			    hdr_length + UDP_CHECKSUM_OFFSET);
11193 
11194 			IP_CKSUM_XMIT(ill, ire, mp, ip6h, up, IPPROTO_UDP,
11195 			    hdr_length, ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN,
11196 			    ire->ire_max_frag, mctl_present, sum);
11197 
11198 			/* Software checksum? */
11199 			if (DB_CKSUMFLAGS(mp) == 0) {
11200 				IP6_STAT(ipst, ip6_out_sw_cksum);
11201 				IP6_STAT_UPDATE(ipst,
11202 				    ip6_udp_out_sw_cksum_bytes,
11203 				    (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN) -
11204 				    hdr_length);
11205 			}
11206 		} else if (nexthdr == IPPROTO_ICMPV6) {
11207 			uint16_t	*up;
11208 			icmp6_t *icmp6;
11209 
11210 			/* check for full IPv6+ICMPv6 header */
11211 			if ((mp->b_wptr - mp->b_rptr) <
11212 			    (hdr_length + ICMP6_MINLEN)) {
11213 				if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) {
11214 					ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg"
11215 					    " failed\n"));
11216 					BUMP_MIB(mibptr, ipIfStatsOutDiscards);
11217 					freemsg(first_mp);
11218 					return;
11219 				}
11220 				ip6h = (ip6_t *)mp->b_rptr;
11221 			}
11222 			icmp6 = (icmp6_t *)((uchar_t *)ip6h + hdr_length);
11223 			up = (uint16_t *)&ip6h->ip6_src;
11224 			/*
11225 			 * icmp has placed length and routing
11226 			 * header adjustment in icmp6_cksum.
11227 			 */
11228 			sum = htons(IPPROTO_ICMPV6) +
11229 			    up[0] + up[1] + up[2] + up[3] +
11230 			    up[4] + up[5] + up[6] + up[7] +
11231 			    up[8] + up[9] + up[10] + up[11] +
11232 			    up[12] + up[13] + up[14] + up[15];
11233 			sum = (sum & 0xffff) + (sum >> 16);
11234 			icmp6->icmp6_cksum = IP_CSUM(mp, hdr_length, sum);
11235 
11236 			/* Update output mib stats */
11237 			icmp_update_out_mib_v6(ill, icmp6);
11238 		} else if (nexthdr == IPPROTO_SCTP) {
11239 			sctp_hdr_t *sctph;
11240 
11241 			if (MBLKL(mp) < (hdr_length + sizeof (*sctph))) {
11242 				if (!pullupmsg(mp, hdr_length +
11243 				    sizeof (*sctph))) {
11244 					ip1dbg(("ip_wput_v6: SCTP hdr pullupmsg"
11245 					    " failed\n"));
11246 					BUMP_MIB(ill->ill_ip_mib,
11247 					    ipIfStatsOutDiscards);
11248 					freemsg(mp);
11249 					return;
11250 				}
11251 				ip6h = (ip6_t *)mp->b_rptr;
11252 			}
11253 			sctph = (sctp_hdr_t *)(mp->b_rptr + hdr_length);
11254 			sctph->sh_chksum = 0;
11255 			sctph->sh_chksum = sctp_cksum(mp, hdr_length);
11256 		}
11257 
11258 	cksum_done:
11259 		/*
11260 		 * We force the insertion of a fragment header using the
11261 		 * IPH_FRAG_HDR flag in two cases:
11262 		 * - after reception of an ICMPv6 "packet too big" message
11263 		 *   with a MTU < 1280 (cf. RFC 2460 section 5)
11264 		 * - for multirouted IPv6 packets, so that the receiver can
11265 		 *   discard duplicates according to their fragment identifier
11266 		 *
11267 		 * Two flags modifed from the API can modify this behavior.
11268 		 * The first is IPV6_USE_MIN_MTU.  With this API the user
11269 		 * can specify how to manage PMTUD for unicast and multicast.
11270 		 *
11271 		 * IPV6_DONTFRAG disallows fragmentation.
11272 		 */
11273 		max_frag = ire->ire_max_frag;
11274 		switch (IP6I_USE_MIN_MTU_API(flags)) {
11275 		case IPV6_USE_MIN_MTU_DEFAULT:
11276 		case IPV6_USE_MIN_MTU_UNICAST:
11277 			if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
11278 				max_frag = IPV6_MIN_MTU;
11279 			}
11280 			break;
11281 
11282 		case IPV6_USE_MIN_MTU_NEVER:
11283 			max_frag = IPV6_MIN_MTU;
11284 			break;
11285 		}
11286 		if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > max_frag ||
11287 		    (ire->ire_frag_flag & IPH_FRAG_HDR)) {
11288 			if (connp != NULL && (flags & IP6I_DONTFRAG)) {
11289 				icmp_pkt2big_v6(ire->ire_stq, first_mp,
11290 				    max_frag, B_FALSE, B_TRUE, zoneid, ipst);
11291 				return;
11292 			}
11293 
11294 			if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN !=
11295 			    (mp->b_cont ? msgdsize(mp) :
11296 			    mp->b_wptr - (uchar_t *)ip6h)) {
11297 				ip0dbg(("Packet length mismatch: %d, %ld\n",
11298 				    ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN,
11299 				    msgdsize(mp)));
11300 				freemsg(first_mp);
11301 				return;
11302 			}
11303 			/* Do IPSEC processing first */
11304 			if (mctl_present) {
11305 				if (attach_index != 0)
11306 					ipsec_out_attach_if(io, attach_index);
11307 				ipsec_out_process(q, first_mp, ire, ill_index);
11308 				return;
11309 			}
11310 			ASSERT(mp->b_prev == NULL);
11311 			ip2dbg(("Fragmenting Size = %d, mtu = %d\n",
11312 			    ntohs(ip6h->ip6_plen) +
11313 			    IPV6_HDR_LEN, max_frag));
11314 			ASSERT(mp == first_mp);
11315 			/* Initiate IPPF processing */
11316 			if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) {
11317 				ip_process(IPP_LOCAL_OUT, &mp, ill_index);
11318 				if (mp == NULL) {
11319 					return;
11320 				}
11321 			}
11322 			ip_wput_frag_v6(mp, ire, reachable, connp,
11323 			    caller, max_frag);
11324 			return;
11325 		}
11326 		/* Do IPSEC processing first */
11327 		if (mctl_present) {
11328 			int extra_len = ipsec_out_extra_length(first_mp);
11329 
11330 			if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN + extra_len >
11331 			    max_frag) {
11332 				/*
11333 				 * IPsec headers will push the packet over the
11334 				 * MTU limit.  Issue an ICMPv6 Packet Too Big
11335 				 * message for this packet if the upper-layer
11336 				 * that issued this packet will be able to
11337 				 * react to the icmp_pkt2big_v6() that we'll
11338 				 * generate.
11339 				 */
11340 				icmp_pkt2big_v6(ire->ire_stq, first_mp,
11341 				    max_frag, B_FALSE, B_TRUE, zoneid, ipst);
11342 				return;
11343 			}
11344 			if (attach_index != 0)
11345 				ipsec_out_attach_if(io, attach_index);
11346 			ipsec_out_process(q, first_mp, ire, ill_index);
11347 			return;
11348 		}
11349 		/*
11350 		 * XXX multicast: add ip_mforward_v6() here.
11351 		 * Check conn_dontroute
11352 		 */
11353 #ifdef lint
11354 		/*
11355 		 * XXX The only purpose of this statement is to avoid lint
11356 		 * errors.  See the above "XXX multicast".  When that gets
11357 		 * fixed, remove this whole #ifdef lint section.
11358 		 */
11359 		ip3dbg(("multicast forward is %s.\n",
11360 		    (multicast_forward ? "TRUE" : "FALSE")));
11361 #endif
11362 
11363 		UPDATE_OB_PKT_COUNT(ire);
11364 		ire->ire_last_used_time = lbolt;
11365 		ASSERT(mp == first_mp);
11366 		ip_xmit_v6(mp, ire, reachable, connp, caller, NULL);
11367 	} else {
11368 		DTRACE_PROBE4(ip6__loopback__out__start,
11369 		    ill_t *, NULL, ill_t *, ill,
11370 		    ip6_t *, ip6h, mblk_t *, first_mp);
11371 		FW_HOOKS6(ipst->ips_ip6_loopback_out_event,
11372 		    ipst->ips_ipv6firewall_loopback_out,
11373 		    NULL, ill, ip6h, first_mp, mp, 0, ipst);
11374 		DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, first_mp);
11375 		if (first_mp != NULL)
11376 			ip_wput_local_v6(RD(q), ill, ip6h, first_mp, ire, 0);
11377 	}
11378 }
11379 
11380 /*
11381  * Outbound IPv6 fragmentation routine using MDT.
11382  */
11383 static void
11384 ip_wput_frag_mdt_v6(mblk_t *mp, ire_t *ire, size_t max_chunk,
11385     size_t unfragmentable_len, uint8_t nexthdr, uint_t prev_nexthdr_offset)
11386 {
11387 	ip6_t		*ip6h = (ip6_t *)mp->b_rptr;
11388 	uint_t		pkts, wroff, hdr_chunk_len, pbuf_idx;
11389 	mblk_t		*hdr_mp, *md_mp = NULL;
11390 	int		i1;
11391 	multidata_t	*mmd;
11392 	unsigned char	*hdr_ptr, *pld_ptr;
11393 	ip_pdescinfo_t	pdi;
11394 	uint32_t	ident;
11395 	size_t		len;
11396 	uint16_t	offset;
11397 	queue_t		*stq = ire->ire_stq;
11398 	ill_t		*ill = (ill_t *)stq->q_ptr;
11399 	ip_stack_t	*ipst = ill->ill_ipst;
11400 
11401 	ASSERT(DB_TYPE(mp) == M_DATA);
11402 	ASSERT(MBLKL(mp) > unfragmentable_len);
11403 
11404 	/*
11405 	 * Move read ptr past unfragmentable portion, we don't want this part
11406 	 * of the data in our fragments.
11407 	 */
11408 	mp->b_rptr += unfragmentable_len;
11409 
11410 	/* Calculate how many packets we will send out  */
11411 	i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp);
11412 	pkts = (i1 + max_chunk - 1) / max_chunk;
11413 	ASSERT(pkts > 1);
11414 
11415 	/* Allocate a message block which will hold all the IP Headers. */
11416 	wroff = ipst->ips_ip_wroff_extra;
11417 	hdr_chunk_len = wroff + unfragmentable_len + sizeof (ip6_frag_t);
11418 
11419 	i1 = pkts * hdr_chunk_len;
11420 	/*
11421 	 * Create the header buffer, Multidata and destination address
11422 	 * and SAP attribute that should be associated with it.
11423 	 */
11424 	if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL ||
11425 	    ((hdr_mp->b_wptr += i1),
11426 	    (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) ||
11427 	    !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) {
11428 		freemsg(mp);
11429 		if (md_mp == NULL) {
11430 			freemsg(hdr_mp);
11431 		} else {
11432 free_mmd:		IP6_STAT(ipst, ip6_frag_mdt_discarded);
11433 			freemsg(md_mp);
11434 		}
11435 		IP6_STAT(ipst, ip6_frag_mdt_allocfail);
11436 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11437 		return;
11438 	}
11439 	IP6_STAT(ipst, ip6_frag_mdt_allocd);
11440 
11441 	/*
11442 	 * Add a payload buffer to the Multidata; this operation must not
11443 	 * fail, or otherwise our logic in this routine is broken.  There
11444 	 * is no memory allocation done by the routine, so any returned
11445 	 * failure simply tells us that we've done something wrong.
11446 	 *
11447 	 * A failure tells us that either we're adding the same payload
11448 	 * buffer more than once, or we're trying to add more buffers than
11449 	 * allowed.  None of the above cases should happen, and we panic
11450 	 * because either there's horrible heap corruption, and/or
11451 	 * programming mistake.
11452 	 */
11453 	if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) {
11454 		goto pbuf_panic;
11455 	}
11456 
11457 	hdr_ptr = hdr_mp->b_rptr;
11458 	pld_ptr = mp->b_rptr;
11459 
11460 	pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF;
11461 
11462 	ident = htonl(atomic_add_32_nv(&ire->ire_ident, 1));
11463 
11464 	/*
11465 	 * len is the total length of the fragmentable data in this
11466 	 * datagram.  For each fragment sent, we will decrement len
11467 	 * by the amount of fragmentable data sent in that fragment
11468 	 * until len reaches zero.
11469 	 */
11470 	len = ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN);
11471 
11472 	offset = 0;
11473 	prev_nexthdr_offset += wroff;
11474 
11475 	while (len != 0) {
11476 		size_t		mlen;
11477 		ip6_t		*fip6h;
11478 		ip6_frag_t	*fraghdr;
11479 		int		error;
11480 
11481 		ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr);
11482 		mlen = MIN(len, max_chunk);
11483 		len -= mlen;
11484 
11485 		fip6h = (ip6_t *)(hdr_ptr + wroff);
11486 		ASSERT(OK_32PTR(fip6h));
11487 		bcopy(ip6h, fip6h, unfragmentable_len);
11488 		hdr_ptr[prev_nexthdr_offset] = IPPROTO_FRAGMENT;
11489 
11490 		fip6h->ip6_plen = htons((uint16_t)(mlen +
11491 		    unfragmentable_len - IPV6_HDR_LEN + sizeof (ip6_frag_t)));
11492 
11493 		fraghdr = (ip6_frag_t *)((unsigned char *)fip6h +
11494 		    unfragmentable_len);
11495 		fraghdr->ip6f_nxt = nexthdr;
11496 		fraghdr->ip6f_reserved = 0;
11497 		fraghdr->ip6f_offlg = htons(offset) |
11498 		    ((len != 0) ? IP6F_MORE_FRAG : 0);
11499 		fraghdr->ip6f_ident = ident;
11500 
11501 		/*
11502 		 * Record offset and size of header and data of the next packet
11503 		 * in the multidata message.
11504 		 */
11505 		PDESC_HDR_ADD(&pdi, hdr_ptr, wroff,
11506 		    unfragmentable_len + sizeof (ip6_frag_t), 0);
11507 		PDESC_PLD_INIT(&pdi);
11508 		i1 = MIN(mp->b_wptr - pld_ptr, mlen);
11509 		ASSERT(i1 > 0);
11510 		PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1);
11511 		if (i1 == mlen) {
11512 			pld_ptr += mlen;
11513 		} else {
11514 			i1 = mlen - i1;
11515 			mp = mp->b_cont;
11516 			ASSERT(mp != NULL);
11517 			ASSERT(MBLKL(mp) >= i1);
11518 			/*
11519 			 * Attach the next payload message block to the
11520 			 * multidata message.
11521 			 */
11522 			if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0)
11523 				goto pbuf_panic;
11524 			PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1);
11525 			pld_ptr = mp->b_rptr + i1;
11526 		}
11527 
11528 		if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error,
11529 		    KM_NOSLEEP)) == NULL) {
11530 			/*
11531 			 * Any failure other than ENOMEM indicates that we
11532 			 * have passed in invalid pdesc info or parameters
11533 			 * to mmd_addpdesc, which must not happen.
11534 			 *
11535 			 * EINVAL is a result of failure on boundary checks
11536 			 * against the pdesc info contents.  It should not
11537 			 * happen, and we panic because either there's
11538 			 * horrible heap corruption, and/or programming
11539 			 * mistake.
11540 			 */
11541 			if (error != ENOMEM) {
11542 				cmn_err(CE_PANIC, "ip_wput_frag_mdt_v6: "
11543 				    "pdesc logic error detected for "
11544 				    "mmd %p pinfo %p (%d)\n",
11545 				    (void *)mmd, (void *)&pdi, error);
11546 				/* NOTREACHED */
11547 			}
11548 			IP6_STAT(ipst, ip6_frag_mdt_addpdescfail);
11549 			/* Free unattached payload message blocks as well */
11550 			md_mp->b_cont = mp->b_cont;
11551 			goto free_mmd;
11552 		}
11553 
11554 		/* Advance fragment offset. */
11555 		offset += mlen;
11556 
11557 		/* Advance to location for next header in the buffer. */
11558 		hdr_ptr += hdr_chunk_len;
11559 
11560 		/* Did we reach the next payload message block? */
11561 		if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) {
11562 			mp = mp->b_cont;
11563 			/*
11564 			 * Attach the next message block with payload
11565 			 * data to the multidata message.
11566 			 */
11567 			if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0)
11568 				goto pbuf_panic;
11569 			pld_ptr = mp->b_rptr;
11570 		}
11571 	}
11572 
11573 	ASSERT(hdr_mp->b_wptr == hdr_ptr);
11574 	ASSERT(mp->b_wptr == pld_ptr);
11575 
11576 	/* Update IP statistics */
11577 	UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts);
11578 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11579 	UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts);
11580 	/*
11581 	 * The ipv6 header len is accounted for in unfragmentable_len so
11582 	 * when calculating the fragmentation overhead just add the frag
11583 	 * header len.
11584 	 */
11585 	UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
11586 	    (ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN)) +
11587 	    pkts * (unfragmentable_len + sizeof (ip6_frag_t)));
11588 	IP6_STAT_UPDATE(ipst, ip6_frag_mdt_pkt_out, pkts);
11589 
11590 	ire->ire_ob_pkt_count += pkts;
11591 	if (ire->ire_ipif != NULL)
11592 		atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts);
11593 
11594 	ire->ire_last_used_time = lbolt;
11595 	/* Send it down */
11596 	putnext(stq, md_mp);
11597 	return;
11598 
11599 pbuf_panic:
11600 	cmn_err(CE_PANIC, "ip_wput_frag_mdt_v6: payload buffer logic "
11601 	    "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp,
11602 	    pbuf_idx);
11603 	/* NOTREACHED */
11604 }
11605 
11606 /*
11607  * IPv6 fragmentation.  Essentially the same as IPv4 fragmentation.
11608  * We have not optimized this in terms of number of mblks
11609  * allocated. For instance, for each fragment sent we always allocate a
11610  * mblk to hold the IPv6 header and fragment header.
11611  *
11612  * Assumes that all the extension headers are contained in the first mblk.
11613  *
11614  * The fragment header is inserted after an hop-by-hop options header
11615  * and after [an optional destinations header followed by] a routing header.
11616  *
11617  * NOTE : This function does not ire_refrele the ire passed in as
11618  * the argument.
11619  */
11620 void
11621 ip_wput_frag_v6(mblk_t *mp, ire_t *ire, uint_t reachable, conn_t *connp,
11622     int caller, int max_frag)
11623 {
11624 	ip6_t		*ip6h = (ip6_t *)mp->b_rptr;
11625 	ip6_t		*fip6h;
11626 	mblk_t		*hmp;
11627 	mblk_t		*hmp0;
11628 	mblk_t		*dmp;
11629 	ip6_frag_t	*fraghdr;
11630 	size_t		unfragmentable_len;
11631 	size_t		len;
11632 	size_t		mlen;
11633 	size_t		max_chunk;
11634 	uint32_t	ident;
11635 	uint16_t	off_flags;
11636 	uint16_t	offset = 0;
11637 	ill_t		*ill;
11638 	uint8_t		nexthdr;
11639 	uint_t		prev_nexthdr_offset;
11640 	uint8_t		*ptr;
11641 	ip_stack_t	*ipst = ire->ire_ipst;
11642 
11643 	ASSERT(ire->ire_type == IRE_CACHE);
11644 	ill = (ill_t *)ire->ire_stq->q_ptr;
11645 
11646 	if (max_frag <= 0) {
11647 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11648 		freemsg(mp);
11649 		return;
11650 	}
11651 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11652 
11653 	/*
11654 	 * Determine the length of the unfragmentable portion of this
11655 	 * datagram.  This consists of the IPv6 header, a potential
11656 	 * hop-by-hop options header, a potential pre-routing-header
11657 	 * destination options header, and a potential routing header.
11658 	 */
11659 	nexthdr = ip6h->ip6_nxt;
11660 	prev_nexthdr_offset = (uint8_t *)&ip6h->ip6_nxt - (uint8_t *)ip6h;
11661 	ptr = (uint8_t *)&ip6h[1];
11662 
11663 	if (nexthdr == IPPROTO_HOPOPTS) {
11664 		ip6_hbh_t	*hbh_hdr;
11665 		uint_t		hdr_len;
11666 
11667 		hbh_hdr = (ip6_hbh_t *)ptr;
11668 		hdr_len = 8 * (hbh_hdr->ip6h_len + 1);
11669 		nexthdr = hbh_hdr->ip6h_nxt;
11670 		prev_nexthdr_offset = (uint8_t *)&hbh_hdr->ip6h_nxt
11671 		    - (uint8_t *)ip6h;
11672 		ptr += hdr_len;
11673 	}
11674 	if (nexthdr == IPPROTO_DSTOPTS) {
11675 		ip6_dest_t	*dest_hdr;
11676 		uint_t		hdr_len;
11677 
11678 		dest_hdr = (ip6_dest_t *)ptr;
11679 		if (dest_hdr->ip6d_nxt == IPPROTO_ROUTING) {
11680 			hdr_len = 8 * (dest_hdr->ip6d_len + 1);
11681 			nexthdr = dest_hdr->ip6d_nxt;
11682 			prev_nexthdr_offset = (uint8_t *)&dest_hdr->ip6d_nxt
11683 			    - (uint8_t *)ip6h;
11684 			ptr += hdr_len;
11685 		}
11686 	}
11687 	if (nexthdr == IPPROTO_ROUTING) {
11688 		ip6_rthdr_t	*rthdr;
11689 		uint_t		hdr_len;
11690 
11691 		rthdr = (ip6_rthdr_t *)ptr;
11692 		nexthdr = rthdr->ip6r_nxt;
11693 		prev_nexthdr_offset = (uint8_t *)&rthdr->ip6r_nxt
11694 		    - (uint8_t *)ip6h;
11695 		hdr_len = 8 * (rthdr->ip6r_len + 1);
11696 		ptr += hdr_len;
11697 	}
11698 	unfragmentable_len = (uint_t)(ptr - (uint8_t *)ip6h);
11699 
11700 	max_chunk = (min(max_frag, ire->ire_max_frag) - unfragmentable_len -
11701 	    sizeof (ip6_frag_t)) & ~7;
11702 
11703 	/* Check if we can use MDT to send out the frags. */
11704 	ASSERT(!IRE_IS_LOCAL(ire));
11705 	if (ipst->ips_ip_multidata_outbound && reachable == 0 &&
11706 	    !(ire->ire_flags & RTF_MULTIRT) && ILL_MDT_CAPABLE(ill) &&
11707 	    IP_CAN_FRAG_MDT(mp, unfragmentable_len, max_chunk)) {
11708 		ip_wput_frag_mdt_v6(mp, ire, max_chunk, unfragmentable_len,
11709 		    nexthdr, prev_nexthdr_offset);
11710 		return;
11711 	}
11712 
11713 	/*
11714 	 * Allocate an mblk with enough room for the link-layer
11715 	 * header, the unfragmentable part of the datagram, and the
11716 	 * fragment header.  This (or a copy) will be used as the
11717 	 * first mblk for each fragment we send.
11718 	 */
11719 	hmp = allocb(unfragmentable_len + sizeof (ip6_frag_t) +
11720 	    ipst->ips_ip_wroff_extra, BPRI_HI);
11721 	if (hmp == NULL) {
11722 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11723 		freemsg(mp);
11724 		return;
11725 	}
11726 	hmp->b_rptr += ipst->ips_ip_wroff_extra;
11727 	hmp->b_wptr = hmp->b_rptr + unfragmentable_len + sizeof (ip6_frag_t);
11728 
11729 	fip6h = (ip6_t *)hmp->b_rptr;
11730 	fraghdr = (ip6_frag_t *)(hmp->b_rptr + unfragmentable_len);
11731 
11732 	bcopy(ip6h, fip6h, unfragmentable_len);
11733 	hmp->b_rptr[prev_nexthdr_offset] = IPPROTO_FRAGMENT;
11734 
11735 	ident = atomic_add_32_nv(&ire->ire_ident, 1);
11736 
11737 	fraghdr->ip6f_nxt = nexthdr;
11738 	fraghdr->ip6f_reserved = 0;
11739 	fraghdr->ip6f_offlg = 0;
11740 	fraghdr->ip6f_ident = htonl(ident);
11741 
11742 	/*
11743 	 * len is the total length of the fragmentable data in this
11744 	 * datagram.  For each fragment sent, we will decrement len
11745 	 * by the amount of fragmentable data sent in that fragment
11746 	 * until len reaches zero.
11747 	 */
11748 	len = ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN);
11749 
11750 	/*
11751 	 * Move read ptr past unfragmentable portion, we don't want this part
11752 	 * of the data in our fragments.
11753 	 */
11754 	mp->b_rptr += unfragmentable_len;
11755 
11756 	while (len != 0) {
11757 		mlen = MIN(len, max_chunk);
11758 		len -= mlen;
11759 		if (len != 0) {
11760 			/* Not last */
11761 			hmp0 = copyb(hmp);
11762 			if (hmp0 == NULL) {
11763 				freeb(hmp);
11764 				freemsg(mp);
11765 				BUMP_MIB(ill->ill_ip_mib,
11766 				    ipIfStatsOutFragFails);
11767 				ip1dbg(("ip_wput_frag_v6: copyb failed\n"));
11768 				return;
11769 			}
11770 			off_flags = IP6F_MORE_FRAG;
11771 		} else {
11772 			/* Last fragment */
11773 			hmp0 = hmp;
11774 			hmp = NULL;
11775 			off_flags = 0;
11776 		}
11777 		fip6h = (ip6_t *)(hmp0->b_rptr);
11778 		fraghdr = (ip6_frag_t *)(hmp0->b_rptr + unfragmentable_len);
11779 
11780 		fip6h->ip6_plen = htons((uint16_t)(mlen +
11781 		    unfragmentable_len - IPV6_HDR_LEN + sizeof (ip6_frag_t)));
11782 		/*
11783 		 * Note: Optimization alert.
11784 		 * In IPv6 (and IPv4) protocol header, Fragment Offset
11785 		 * ("offset") is 13 bits wide and in 8-octet units.
11786 		 * In IPv6 protocol header (unlike IPv4) in a 16 bit field,
11787 		 * it occupies the most significant 13 bits.
11788 		 * (least significant 13 bits in IPv4).
11789 		 * We do not do any shifts here. Not shifting is same effect
11790 		 * as taking offset value in octet units, dividing by 8 and
11791 		 * then shifting 3 bits left to line it up in place in proper
11792 		 * place protocol header.
11793 		 */
11794 		fraghdr->ip6f_offlg = htons(offset) | off_flags;
11795 
11796 		if (!(dmp = ip_carve_mp(&mp, mlen))) {
11797 			/* mp has already been freed by ip_carve_mp() */
11798 			if (hmp != NULL)
11799 				freeb(hmp);
11800 			freeb(hmp0);
11801 			ip1dbg(("ip_carve_mp: failed\n"));
11802 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11803 			return;
11804 		}
11805 		hmp0->b_cont = dmp;
11806 		/* Get the priority marking, if any */
11807 		hmp0->b_band = dmp->b_band;
11808 		UPDATE_OB_PKT_COUNT(ire);
11809 		ire->ire_last_used_time = lbolt;
11810 		ip_xmit_v6(hmp0, ire, reachable | IP6_NO_IPPOLICY, connp,
11811 		    caller, NULL);
11812 		reachable = 0;	/* No need to redo state machine in loop */
11813 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11814 		offset += mlen;
11815 	}
11816 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11817 }
11818 
11819 /*
11820  * Determine if the ill and multicast aspects of that packets
11821  * "matches" the conn.
11822  */
11823 boolean_t
11824 conn_wantpacket_v6(conn_t *connp, ill_t *ill, ip6_t *ip6h, int fanout_flags,
11825     zoneid_t zoneid)
11826 {
11827 	ill_t *in_ill;
11828 	boolean_t wantpacket = B_TRUE;
11829 	in6_addr_t *v6dst_ptr = &ip6h->ip6_dst;
11830 	in6_addr_t *v6src_ptr = &ip6h->ip6_src;
11831 
11832 	/*
11833 	 * conn_incoming_ill is set by IPV6_BOUND_IF which limits
11834 	 * unicast and multicast reception to conn_incoming_ill.
11835 	 * conn_wantpacket_v6 is called both for unicast and
11836 	 * multicast.
11837 	 *
11838 	 * 1) The unicast copy of the packet can come anywhere in
11839 	 *    the ill group if it is part of the group. Thus, we
11840 	 *    need to check to see whether the ill group matches
11841 	 *    if in_ill is part of a group.
11842 	 *
11843 	 * 2) ip_rput does not suppress duplicate multicast packets.
11844 	 *    If there are two interfaces in a ill group and we have
11845 	 *    2 applications (conns) joined a multicast group G on
11846 	 *    both the interfaces, ilm_lookup_ill filter in ip_rput
11847 	 *    will give us two packets because we join G on both the
11848 	 *    interfaces rather than nominating just one interface
11849 	 *    for receiving multicast like broadcast above. So,
11850 	 *    we have to call ilg_lookup_ill to filter out duplicate
11851 	 *    copies, if ill is part of a group, to supress duplicates.
11852 	 */
11853 	in_ill = connp->conn_incoming_ill;
11854 	if (in_ill != NULL) {
11855 		mutex_enter(&connp->conn_lock);
11856 		in_ill = connp->conn_incoming_ill;
11857 		mutex_enter(&ill->ill_lock);
11858 		/*
11859 		 * No IPMP, and the packet did not arrive on conn_incoming_ill
11860 		 * OR, IPMP in use and the packet arrived on an IPMP group
11861 		 * different from the conn_incoming_ill's IPMP group.
11862 		 * Reject the packet.
11863 		 */
11864 		if ((in_ill->ill_group == NULL && in_ill != ill) ||
11865 		    (in_ill->ill_group != NULL &&
11866 		    in_ill->ill_group !=  ill->ill_group)) {
11867 			wantpacket = B_FALSE;
11868 		}
11869 		mutex_exit(&ill->ill_lock);
11870 		mutex_exit(&connp->conn_lock);
11871 		if (!wantpacket)
11872 			return (B_FALSE);
11873 	}
11874 
11875 	if (connp->conn_multi_router)
11876 		return (B_TRUE);
11877 
11878 	if (!IN6_IS_ADDR_MULTICAST(v6dst_ptr) &&
11879 	    !IN6_IS_ADDR_V4MAPPED_CLASSD(v6dst_ptr)) {
11880 		/*
11881 		 * Unicast case: we match the conn only if it's in the specified
11882 		 * zone.
11883 		 */
11884 		return (IPCL_ZONE_MATCH(connp, zoneid));
11885 	}
11886 
11887 	if ((fanout_flags & IP_FF_NO_MCAST_LOOP) &&
11888 	    (connp->conn_zoneid == zoneid || zoneid == ALL_ZONES)) {
11889 		/*
11890 		 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP
11891 		 * disabled, therefore we don't dispatch the multicast packet to
11892 		 * the sending zone.
11893 		 */
11894 		return (B_FALSE);
11895 	}
11896 
11897 	if (IS_LOOPBACK(ill) && connp->conn_zoneid != zoneid &&
11898 	    zoneid != ALL_ZONES) {
11899 		/*
11900 		 * Multicast packet on the loopback interface: we only match
11901 		 * conns who joined the group in the specified zone.
11902 		 */
11903 		return (B_FALSE);
11904 	}
11905 
11906 	mutex_enter(&connp->conn_lock);
11907 	wantpacket =
11908 	    ilg_lookup_ill_withsrc_v6(connp, v6dst_ptr, v6src_ptr, ill) != NULL;
11909 	mutex_exit(&connp->conn_lock);
11910 
11911 	return (wantpacket);
11912 }
11913 
11914 
11915 /*
11916  * Transmit a packet and update any NUD state based on the flags
11917  * XXX need to "recover" any ip6i_t when doing putq!
11918  *
11919  * NOTE : This function does not ire_refrele the ire passed in as the
11920  * argument.
11921  */
11922 void
11923 ip_xmit_v6(mblk_t *mp, ire_t *ire, uint_t flags, conn_t *connp,
11924     int caller, ipsec_out_t *io)
11925 {
11926 	mblk_t		*mp1;
11927 	nce_t		*nce = ire->ire_nce;
11928 	ill_t		*ill;
11929 	ill_t		*out_ill;
11930 	uint64_t	delta;
11931 	ip6_t		*ip6h;
11932 	queue_t		*stq = ire->ire_stq;
11933 	ire_t		*ire1 = NULL;
11934 	ire_t		*save_ire = ire;
11935 	boolean_t	multirt_send = B_FALSE;
11936 	mblk_t		*next_mp = NULL;
11937 	ip_stack_t	*ipst = ire->ire_ipst;
11938 
11939 	ip6h = (ip6_t *)mp->b_rptr;
11940 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ire->ire_addr_v6));
11941 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
11942 	ASSERT(nce != NULL);
11943 	ASSERT(mp->b_datap->db_type == M_DATA);
11944 	ASSERT(stq != NULL);
11945 
11946 	ill = ire_to_ill(ire);
11947 	if (!ill) {
11948 		ip0dbg(("ip_xmit_v6: ire_to_ill failed\n"));
11949 		freemsg(mp);
11950 		return;
11951 	}
11952 
11953 	/*
11954 	 * If a packet is to be sent out an interface that is a 6to4
11955 	 * tunnel, outgoing IPv6 packets, with a 6to4 addressed IPv6
11956 	 * destination, must be checked to have a 6to4 prefix
11957 	 * (2002:V4ADDR::/48) that is NOT equal to the 6to4 prefix of
11958 	 * address configured on the sending interface.  Otherwise,
11959 	 * the packet was delivered to this interface in error and the
11960 	 * packet must be dropped.
11961 	 */
11962 	if ((ill->ill_is_6to4tun) && IN6_IS_ADDR_6TO4(&ip6h->ip6_dst)) {
11963 		ipif_t *ipif = ill->ill_ipif;
11964 
11965 		if (IN6_ARE_6TO4_PREFIX_EQUAL(&ipif->ipif_v6lcl_addr,
11966 		    &ip6h->ip6_dst)) {
11967 			if (ip_debug > 2) {
11968 				/* ip1dbg */
11969 				pr_addr_dbg("ip_xmit_v6: attempting to "
11970 				    "send 6to4 addressed IPv6 "
11971 				    "destination (%s) out the wrong "
11972 				    "interface.\n", AF_INET6,
11973 				    &ip6h->ip6_dst);
11974 			}
11975 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
11976 			freemsg(mp);
11977 			return;
11978 		}
11979 	}
11980 
11981 	/* Flow-control check has been done in ip_wput_ire_v6 */
11982 	if (IP_FLOW_CONTROLLED_ULP(ip6h->ip6_nxt) || caller == IP_WPUT ||
11983 	    caller == IP_WSRV || canput(stq->q_next)) {
11984 		uint32_t ill_index;
11985 
11986 		/*
11987 		 * In most cases, the emission loop below is entered only
11988 		 * once. Only in the case where the ire holds the
11989 		 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT
11990 		 * flagged ires in the bucket, and send the packet
11991 		 * through all crossed RTF_MULTIRT routes.
11992 		 */
11993 		if (ire->ire_flags & RTF_MULTIRT) {
11994 			/*
11995 			 * Multirouting case. The bucket where ire is stored
11996 			 * probably holds other RTF_MULTIRT flagged ires
11997 			 * to the destination. In this call to ip_xmit_v6,
11998 			 * we attempt to send the packet through all
11999 			 * those ires. Thus, we first ensure that ire is the
12000 			 * first RTF_MULTIRT ire in the bucket,
12001 			 * before walking the ire list.
12002 			 */
12003 			ire_t *first_ire;
12004 			irb_t *irb = ire->ire_bucket;
12005 			ASSERT(irb != NULL);
12006 			multirt_send = B_TRUE;
12007 
12008 			/* Make sure we do not omit any multiroute ire. */
12009 			IRB_REFHOLD(irb);
12010 			for (first_ire = irb->irb_ire;
12011 			    first_ire != NULL;
12012 			    first_ire = first_ire->ire_next) {
12013 				if ((first_ire->ire_flags & RTF_MULTIRT) &&
12014 				    (IN6_ARE_ADDR_EQUAL(&first_ire->ire_addr_v6,
12015 				    &ire->ire_addr_v6)) &&
12016 				    !(first_ire->ire_marks &
12017 				    (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)))
12018 					break;
12019 			}
12020 
12021 			if ((first_ire != NULL) && (first_ire != ire)) {
12022 				IRE_REFHOLD(first_ire);
12023 				/* ire will be released by the caller */
12024 				ire = first_ire;
12025 				nce = ire->ire_nce;
12026 				stq = ire->ire_stq;
12027 				ill = ire_to_ill(ire);
12028 			}
12029 			IRB_REFRELE(irb);
12030 		} else if (connp != NULL && IPCL_IS_TCP(connp) &&
12031 		    connp->conn_mdt_ok && !connp->conn_tcp->tcp_mdt &&
12032 		    ILL_MDT_USABLE(ill)) {
12033 			/*
12034 			 * This tcp connection was marked as MDT-capable, but
12035 			 * it has been turned off due changes in the interface.
12036 			 * Now that the interface support is back, turn it on
12037 			 * by notifying tcp.  We don't directly modify tcp_mdt,
12038 			 * since we leave all the details to the tcp code that
12039 			 * knows better.
12040 			 */
12041 			mblk_t *mdimp = ip_mdinfo_alloc(ill->ill_mdt_capab);
12042 
12043 			if (mdimp == NULL) {
12044 				ip0dbg(("ip_xmit_v6: can't re-enable MDT for "
12045 				    "connp %p (ENOMEM)\n", (void *)connp));
12046 			} else {
12047 				CONN_INC_REF(connp);
12048 				squeue_fill(connp->conn_sqp, mdimp, tcp_input,
12049 				    connp, SQTAG_TCP_INPUT_MCTL);
12050 			}
12051 		}
12052 
12053 		do {
12054 			mblk_t *mp_ip6h;
12055 
12056 			if (multirt_send) {
12057 				irb_t *irb;
12058 				/*
12059 				 * We are in a multiple send case, need to get
12060 				 * the next ire and make a duplicate of the
12061 				 * packet. ire1 holds here the next ire to
12062 				 * process in the bucket. If multirouting is
12063 				 * expected, any non-RTF_MULTIRT ire that has
12064 				 * the right destination address is ignored.
12065 				 */
12066 				irb = ire->ire_bucket;
12067 				ASSERT(irb != NULL);
12068 
12069 				IRB_REFHOLD(irb);
12070 				for (ire1 = ire->ire_next;
12071 				    ire1 != NULL;
12072 				    ire1 = ire1->ire_next) {
12073 					if (!(ire1->ire_flags & RTF_MULTIRT))
12074 						continue;
12075 					if (!IN6_ARE_ADDR_EQUAL(
12076 					    &ire1->ire_addr_v6,
12077 					    &ire->ire_addr_v6))
12078 						continue;
12079 					if (ire1->ire_marks &
12080 					    (IRE_MARK_CONDEMNED|
12081 					    IRE_MARK_HIDDEN))
12082 						continue;
12083 
12084 					/* Got one */
12085 					if (ire1 != save_ire) {
12086 						IRE_REFHOLD(ire1);
12087 					}
12088 					break;
12089 				}
12090 				IRB_REFRELE(irb);
12091 
12092 				if (ire1 != NULL) {
12093 					next_mp = copyb(mp);
12094 					if ((next_mp == NULL) ||
12095 					    ((mp->b_cont != NULL) &&
12096 					    ((next_mp->b_cont =
12097 					    dupmsg(mp->b_cont)) == NULL))) {
12098 						freemsg(next_mp);
12099 						next_mp = NULL;
12100 						ire_refrele(ire1);
12101 						ire1 = NULL;
12102 					}
12103 				}
12104 
12105 				/* Last multiroute ire; don't loop anymore. */
12106 				if (ire1 == NULL) {
12107 					multirt_send = B_FALSE;
12108 				}
12109 			}
12110 
12111 			ill_index =
12112 			    ((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex;
12113 
12114 			/* Initiate IPPF processing */
12115 			if (IP6_OUT_IPP(flags, ipst)) {
12116 				ip_process(IPP_LOCAL_OUT, &mp, ill_index);
12117 				if (mp == NULL) {
12118 					BUMP_MIB(ill->ill_ip_mib,
12119 					    ipIfStatsOutDiscards);
12120 					if (next_mp != NULL)
12121 						freemsg(next_mp);
12122 					if (ire != save_ire) {
12123 						ire_refrele(ire);
12124 					}
12125 					return;
12126 				}
12127 				ip6h = (ip6_t *)mp->b_rptr;
12128 			}
12129 			mp_ip6h = mp;
12130 
12131 			/*
12132 			 * Check for fastpath, we need to hold nce_lock to
12133 			 * prevent fastpath update from chaining nce_fp_mp.
12134 			 */
12135 
12136 			ASSERT(nce->nce_ipversion != IPV4_VERSION);
12137 			mutex_enter(&nce->nce_lock);
12138 			if ((mp1 = nce->nce_fp_mp) != NULL) {
12139 				uint32_t hlen;
12140 				uchar_t	*rptr;
12141 
12142 				hlen = MBLKL(mp1);
12143 				rptr = mp->b_rptr - hlen;
12144 				/*
12145 				 * make sure there is room for the fastpath
12146 				 * datalink header
12147 				 */
12148 				if (rptr < mp->b_datap->db_base) {
12149 					mp1 = copyb(mp1);
12150 					mutex_exit(&nce->nce_lock);
12151 					if (mp1 == NULL) {
12152 						BUMP_MIB(ill->ill_ip_mib,
12153 						    ipIfStatsOutDiscards);
12154 						freemsg(mp);
12155 						if (next_mp != NULL)
12156 							freemsg(next_mp);
12157 						if (ire != save_ire) {
12158 							ire_refrele(ire);
12159 						}
12160 						return;
12161 					}
12162 					mp1->b_cont = mp;
12163 
12164 					/* Get the priority marking, if any */
12165 					mp1->b_band = mp->b_band;
12166 					mp = mp1;
12167 				} else {
12168 					mp->b_rptr = rptr;
12169 					/*
12170 					 * fastpath -  pre-pend datalink
12171 					 * header
12172 					 */
12173 					bcopy(mp1->b_rptr, rptr, hlen);
12174 					mutex_exit(&nce->nce_lock);
12175 				}
12176 			} else {
12177 				/*
12178 				 * Get the DL_UNITDATA_REQ.
12179 				 */
12180 				mp1 = nce->nce_res_mp;
12181 				if (mp1 == NULL) {
12182 					mutex_exit(&nce->nce_lock);
12183 					ip1dbg(("ip_xmit_v6: No resolution "
12184 					    "block ire = %p\n", (void *)ire));
12185 					freemsg(mp);
12186 					if (next_mp != NULL)
12187 						freemsg(next_mp);
12188 					if (ire != save_ire) {
12189 						ire_refrele(ire);
12190 					}
12191 					return;
12192 				}
12193 				/*
12194 				 * Prepend the DL_UNITDATA_REQ.
12195 				 */
12196 				mp1 = copyb(mp1);
12197 				mutex_exit(&nce->nce_lock);
12198 				if (mp1 == NULL) {
12199 					BUMP_MIB(ill->ill_ip_mib,
12200 					    ipIfStatsOutDiscards);
12201 					freemsg(mp);
12202 					if (next_mp != NULL)
12203 						freemsg(next_mp);
12204 					if (ire != save_ire) {
12205 						ire_refrele(ire);
12206 					}
12207 					return;
12208 				}
12209 				mp1->b_cont = mp;
12210 
12211 				/* Get the priority marking, if any */
12212 				mp1->b_band = mp->b_band;
12213 				mp = mp1;
12214 			}
12215 
12216 			out_ill = (ill_t *)stq->q_ptr;
12217 
12218 			DTRACE_PROBE4(ip6__physical__out__start,
12219 			    ill_t *, NULL, ill_t *, out_ill,
12220 			    ip6_t *, ip6h, mblk_t *, mp);
12221 
12222 			FW_HOOKS6(ipst->ips_ip6_physical_out_event,
12223 			    ipst->ips_ipv6firewall_physical_out,
12224 			    NULL, out_ill, ip6h, mp, mp_ip6h, 0, ipst);
12225 
12226 			DTRACE_PROBE1(ip6__physical__out__end, mblk_t *, mp);
12227 
12228 			if (mp == NULL) {
12229 				if (multirt_send) {
12230 					ASSERT(ire1 != NULL);
12231 					if (ire != save_ire) {
12232 						ire_refrele(ire);
12233 					}
12234 					/*
12235 					 * Proceed with the next RTF_MULTIRT
12236 					 * ire, also set up the send-to queue
12237 					 * accordingly.
12238 					 */
12239 					ire = ire1;
12240 					ire1 = NULL;
12241 					stq = ire->ire_stq;
12242 					nce = ire->ire_nce;
12243 					ill = ire_to_ill(ire);
12244 					mp = next_mp;
12245 					next_mp = NULL;
12246 					continue;
12247 				} else {
12248 					ASSERT(next_mp == NULL);
12249 					ASSERT(ire1 == NULL);
12250 					break;
12251 				}
12252 			}
12253 
12254 			/*
12255 			 * Update ire and MIB counters; for save_ire, this has
12256 			 * been done by the caller.
12257 			 */
12258 			if (ire != save_ire) {
12259 				UPDATE_OB_PKT_COUNT(ire);
12260 				ire->ire_last_used_time = lbolt;
12261 
12262 				if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
12263 					BUMP_MIB(ill->ill_ip_mib,
12264 					    ipIfStatsHCOutMcastPkts);
12265 					UPDATE_MIB(ill->ill_ip_mib,
12266 					    ipIfStatsHCOutMcastOctets,
12267 					    ntohs(ip6h->ip6_plen) +
12268 					    IPV6_HDR_LEN);
12269 				}
12270 			}
12271 
12272 			/*
12273 			 * Send it down.  XXX Do we want to flow control AH/ESP
12274 			 * packets that carry TCP payloads?  We don't flow
12275 			 * control TCP packets, but we should also not
12276 			 * flow-control TCP packets that have been protected.
12277 			 * We don't have an easy way to find out if an AH/ESP
12278 			 * packet was originally TCP or not currently.
12279 			 */
12280 			if (io == NULL) {
12281 				BUMP_MIB(ill->ill_ip_mib,
12282 				    ipIfStatsHCOutTransmits);
12283 				UPDATE_MIB(ill->ill_ip_mib,
12284 				    ipIfStatsHCOutOctets,
12285 				    ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN);
12286 				putnext(stq, mp);
12287 			} else {
12288 				/*
12289 				 * Safety Pup says: make sure this is
12290 				 * going to the right interface!
12291 				 */
12292 				if (io->ipsec_out_capab_ill_index !=
12293 				    ill_index) {
12294 					/* IPsec kstats: bump lose counter */
12295 					freemsg(mp1);
12296 				} else {
12297 					BUMP_MIB(ill->ill_ip_mib,
12298 					    ipIfStatsHCOutTransmits);
12299 					UPDATE_MIB(ill->ill_ip_mib,
12300 					    ipIfStatsHCOutOctets,
12301 					    ntohs(ip6h->ip6_plen) +
12302 					    IPV6_HDR_LEN);
12303 					ipsec_hw_putnext(stq, mp);
12304 				}
12305 			}
12306 
12307 			if (nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT)) {
12308 				if (ire != save_ire) {
12309 					ire_refrele(ire);
12310 				}
12311 				if (multirt_send) {
12312 					ASSERT(ire1 != NULL);
12313 					/*
12314 					 * Proceed with the next RTF_MULTIRT
12315 					 * ire, also set up the send-to queue
12316 					 * accordingly.
12317 					 */
12318 					ire = ire1;
12319 					ire1 = NULL;
12320 					stq = ire->ire_stq;
12321 					nce = ire->ire_nce;
12322 					ill = ire_to_ill(ire);
12323 					mp = next_mp;
12324 					next_mp = NULL;
12325 					continue;
12326 				}
12327 				ASSERT(next_mp == NULL);
12328 				ASSERT(ire1 == NULL);
12329 				return;
12330 			}
12331 
12332 			ASSERT(nce->nce_state != ND_INCOMPLETE);
12333 
12334 			/*
12335 			 * Check for upper layer advice
12336 			 */
12337 			if (flags & IPV6_REACHABILITY_CONFIRMATION) {
12338 				/*
12339 				 * It should be o.k. to check the state without
12340 				 * a lock here, at most we lose an advice.
12341 				 */
12342 				nce->nce_last = TICK_TO_MSEC(lbolt64);
12343 				if (nce->nce_state != ND_REACHABLE) {
12344 
12345 					mutex_enter(&nce->nce_lock);
12346 					nce->nce_state = ND_REACHABLE;
12347 					nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT;
12348 					mutex_exit(&nce->nce_lock);
12349 					(void) untimeout(nce->nce_timeout_id);
12350 					if (ip_debug > 2) {
12351 						/* ip1dbg */
12352 						pr_addr_dbg("ip_xmit_v6: state"
12353 						    " for %s changed to"
12354 						    " REACHABLE\n", AF_INET6,
12355 						    &ire->ire_addr_v6);
12356 					}
12357 				}
12358 				if (ire != save_ire) {
12359 					ire_refrele(ire);
12360 				}
12361 				if (multirt_send) {
12362 					ASSERT(ire1 != NULL);
12363 					/*
12364 					 * Proceed with the next RTF_MULTIRT
12365 					 * ire, also set up the send-to queue
12366 					 * accordingly.
12367 					 */
12368 					ire = ire1;
12369 					ire1 = NULL;
12370 					stq = ire->ire_stq;
12371 					nce = ire->ire_nce;
12372 					ill = ire_to_ill(ire);
12373 					mp = next_mp;
12374 					next_mp = NULL;
12375 					continue;
12376 				}
12377 				ASSERT(next_mp == NULL);
12378 				ASSERT(ire1 == NULL);
12379 				return;
12380 			}
12381 
12382 			delta =  TICK_TO_MSEC(lbolt64) - nce->nce_last;
12383 			ip1dbg(("ip_xmit_v6: delta = %" PRId64
12384 			    " ill_reachable_time = %d \n", delta,
12385 			    ill->ill_reachable_time));
12386 			if (delta > (uint64_t)ill->ill_reachable_time) {
12387 				nce = ire->ire_nce;
12388 				mutex_enter(&nce->nce_lock);
12389 				switch (nce->nce_state) {
12390 				case ND_REACHABLE:
12391 				case ND_STALE:
12392 					/*
12393 					 * ND_REACHABLE is identical to
12394 					 * ND_STALE in this specific case. If
12395 					 * reachable time has expired for this
12396 					 * neighbor (delta is greater than
12397 					 * reachable time), conceptually, the
12398 					 * neighbor cache is no longer in
12399 					 * REACHABLE state, but already in
12400 					 * STALE state.  So the correct
12401 					 * transition here is to ND_DELAY.
12402 					 */
12403 					nce->nce_state = ND_DELAY;
12404 					mutex_exit(&nce->nce_lock);
12405 					NDP_RESTART_TIMER(nce,
12406 					    ipst->ips_delay_first_probe_time);
12407 					if (ip_debug > 3) {
12408 						/* ip2dbg */
12409 						pr_addr_dbg("ip_xmit_v6: state"
12410 						    " for %s changed to"
12411 						    " DELAY\n", AF_INET6,
12412 						    &ire->ire_addr_v6);
12413 					}
12414 					break;
12415 				case ND_DELAY:
12416 				case ND_PROBE:
12417 					mutex_exit(&nce->nce_lock);
12418 					/* Timers have already started */
12419 					break;
12420 				case ND_UNREACHABLE:
12421 					/*
12422 					 * ndp timer has detected that this nce
12423 					 * is unreachable and initiated deleting
12424 					 * this nce and all its associated IREs.
12425 					 * This is a race where we found the
12426 					 * ire before it was deleted and have
12427 					 * just sent out a packet using this
12428 					 * unreachable nce.
12429 					 */
12430 					mutex_exit(&nce->nce_lock);
12431 					break;
12432 				default:
12433 					ASSERT(0);
12434 				}
12435 			}
12436 
12437 			if (multirt_send) {
12438 				ASSERT(ire1 != NULL);
12439 				/*
12440 				 * Proceed with the next RTF_MULTIRT ire,
12441 				 * Also set up the send-to queue accordingly.
12442 				 */
12443 				if (ire != save_ire) {
12444 					ire_refrele(ire);
12445 				}
12446 				ire = ire1;
12447 				ire1 = NULL;
12448 				stq = ire->ire_stq;
12449 				nce = ire->ire_nce;
12450 				ill = ire_to_ill(ire);
12451 				mp = next_mp;
12452 				next_mp = NULL;
12453 			}
12454 		} while (multirt_send);
12455 		/*
12456 		 * In the multirouting case, release the last ire used for
12457 		 * emission. save_ire will be released by the caller.
12458 		 */
12459 		if (ire != save_ire) {
12460 			ire_refrele(ire);
12461 		}
12462 	} else {
12463 		/*
12464 		 * Queue packet if we have an conn to give back pressure.
12465 		 * We can't queue packets intended for hardware acceleration
12466 		 * since we've tossed that state already. If the packet is
12467 		 * being fed back from ire_send_v6, we don't know the
12468 		 * position in the queue to enqueue the packet and we discard
12469 		 * the packet.
12470 		 */
12471 		if (ipst->ips_ip_output_queue && (connp != NULL) &&
12472 		    (io == NULL) && (caller != IRE_SEND)) {
12473 			if (caller == IP_WSRV) {
12474 				connp->conn_did_putbq = 1;
12475 				(void) putbq(connp->conn_wq, mp);
12476 				conn_drain_insert(connp);
12477 				/*
12478 				 * caller == IP_WSRV implies we are
12479 				 * the service thread, and the
12480 				 * queue is already noenabled.
12481 				 * The check for canput and
12482 				 * the putbq is not atomic.
12483 				 * So we need to check again.
12484 				 */
12485 				if (canput(stq->q_next))
12486 					connp->conn_did_putbq = 0;
12487 			} else {
12488 				(void) putq(connp->conn_wq, mp);
12489 			}
12490 			return;
12491 		}
12492 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12493 		freemsg(mp);
12494 		return;
12495 	}
12496 }
12497 
12498 /*
12499  * pr_addr_dbg function provides the needed buffer space to call
12500  * inet_ntop() function's 3rd argument. This function should be
12501  * used by any kernel routine which wants to save INET6_ADDRSTRLEN
12502  * stack buffer space in it's own stack frame. This function uses
12503  * a buffer from it's own stack and prints the information.
12504  * Example: pr_addr_dbg("func: no route for %s\n ", AF_INET, addr)
12505  *
12506  * Note:    This function can call inet_ntop() once.
12507  */
12508 void
12509 pr_addr_dbg(char *fmt1, int af, const void *addr)
12510 {
12511 	char	buf[INET6_ADDRSTRLEN];
12512 
12513 	if (fmt1 == NULL) {
12514 		ip0dbg(("pr_addr_dbg: Wrong arguments\n"));
12515 		return;
12516 	}
12517 
12518 	/*
12519 	 * This does not compare debug level and just prints
12520 	 * out. Thus it is the responsibility of the caller
12521 	 * to check the appropriate debug-level before calling
12522 	 * this function.
12523 	 */
12524 	if (ip_debug > 0) {
12525 		printf(fmt1, inet_ntop(af, addr, buf, sizeof (buf)));
12526 	}
12527 
12528 
12529 }
12530 
12531 
12532 /*
12533  * Return the length in bytes of the IPv6 headers (base header, ip6i_t
12534  * if needed and extension headers) that will be needed based on the
12535  * ip6_pkt_t structure passed by the caller.
12536  *
12537  * The returned length does not include the length of the upper level
12538  * protocol (ULP) header.
12539  */
12540 int
12541 ip_total_hdrs_len_v6(ip6_pkt_t *ipp)
12542 {
12543 	int len;
12544 
12545 	len = IPV6_HDR_LEN;
12546 	if (ipp->ipp_fields & IPPF_HAS_IP6I)
12547 		len += sizeof (ip6i_t);
12548 	if (ipp->ipp_fields & IPPF_HOPOPTS) {
12549 		ASSERT(ipp->ipp_hopoptslen != 0);
12550 		len += ipp->ipp_hopoptslen;
12551 	}
12552 	if (ipp->ipp_fields & IPPF_RTHDR) {
12553 		ASSERT(ipp->ipp_rthdrlen != 0);
12554 		len += ipp->ipp_rthdrlen;
12555 	}
12556 	/*
12557 	 * En-route destination options
12558 	 * Only do them if there's a routing header as well
12559 	 */
12560 	if ((ipp->ipp_fields & (IPPF_RTDSTOPTS|IPPF_RTHDR)) ==
12561 	    (IPPF_RTDSTOPTS|IPPF_RTHDR)) {
12562 		ASSERT(ipp->ipp_rtdstoptslen != 0);
12563 		len += ipp->ipp_rtdstoptslen;
12564 	}
12565 	if (ipp->ipp_fields & IPPF_DSTOPTS) {
12566 		ASSERT(ipp->ipp_dstoptslen != 0);
12567 		len += ipp->ipp_dstoptslen;
12568 	}
12569 	return (len);
12570 }
12571 
12572 /*
12573  * All-purpose routine to build a header chain of an IPv6 header
12574  * followed by any required extension headers and a proto header,
12575  * preceeded (where necessary) by an ip6i_t private header.
12576  *
12577  * The fields of the IPv6 header that are derived from the ip6_pkt_t
12578  * will be filled in appropriately.
12579  * Thus the caller must fill in the rest of the IPv6 header, such as
12580  * traffic class/flowid, source address (if not set here), hoplimit (if not
12581  * set here) and destination address.
12582  *
12583  * The extension headers and ip6i_t header will all be fully filled in.
12584  */
12585 void
12586 ip_build_hdrs_v6(uchar_t *ext_hdrs, uint_t ext_hdrs_len,
12587     ip6_pkt_t *ipp, uint8_t protocol)
12588 {
12589 	uint8_t *nxthdr_ptr;
12590 	uint8_t *cp;
12591 	ip6i_t	*ip6i;
12592 	ip6_t	*ip6h = (ip6_t *)ext_hdrs;
12593 
12594 	/*
12595 	 * If sending private ip6i_t header down (checksum info, nexthop,
12596 	 * or ifindex), adjust ip header pointer and set ip6i_t header pointer,
12597 	 * then fill it in. (The checksum info will be filled in by icmp).
12598 	 */
12599 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
12600 		ip6i = (ip6i_t *)ip6h;
12601 		ip6h = (ip6_t *)&ip6i[1];
12602 
12603 		ip6i->ip6i_flags = 0;
12604 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
12605 		if (ipp->ipp_fields & IPPF_IFINDEX ||
12606 		    ipp->ipp_fields & IPPF_SCOPE_ID) {
12607 			ASSERT(ipp->ipp_ifindex != 0);
12608 			ip6i->ip6i_flags |= IP6I_IFINDEX;
12609 			ip6i->ip6i_ifindex = ipp->ipp_ifindex;
12610 		}
12611 		if (ipp->ipp_fields & IPPF_ADDR) {
12612 			/*
12613 			 * Enable per-packet source address verification if
12614 			 * IPV6_PKTINFO specified the source address.
12615 			 * ip6_src is set in the transport's _wput function.
12616 			 */
12617 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(
12618 			    &ipp->ipp_addr));
12619 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
12620 		}
12621 		if (ipp->ipp_fields & IPPF_UNICAST_HOPS) {
12622 			ip6h->ip6_hops = ipp->ipp_unicast_hops;
12623 			/*
12624 			 * We need to set this flag so that IP doesn't
12625 			 * rewrite the IPv6 header's hoplimit with the
12626 			 * current default value.
12627 			 */
12628 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
12629 		}
12630 		if (ipp->ipp_fields & IPPF_NEXTHOP) {
12631 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(
12632 			    &ipp->ipp_nexthop));
12633 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
12634 			ip6i->ip6i_nexthop = ipp->ipp_nexthop;
12635 		}
12636 		/*
12637 		 * tell IP this is an ip6i_t private header
12638 		 */
12639 		ip6i->ip6i_nxt = IPPROTO_RAW;
12640 	}
12641 	/* Initialize IPv6 header */
12642 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
12643 	if (ipp->ipp_fields & IPPF_TCLASS) {
12644 		ip6h->ip6_vcf = (ip6h->ip6_vcf & ~IPV6_FLOWINFO_TCLASS) |
12645 		    (ipp->ipp_tclass << 20);
12646 	}
12647 	if (ipp->ipp_fields & IPPF_ADDR)
12648 		ip6h->ip6_src = ipp->ipp_addr;
12649 
12650 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
12651 	cp = (uint8_t *)&ip6h[1];
12652 	/*
12653 	 * Here's where we have to start stringing together
12654 	 * any extension headers in the right order:
12655 	 * Hop-by-hop, destination, routing, and final destination opts.
12656 	 */
12657 	if (ipp->ipp_fields & IPPF_HOPOPTS) {
12658 		/* Hop-by-hop options */
12659 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
12660 
12661 		*nxthdr_ptr = IPPROTO_HOPOPTS;
12662 		nxthdr_ptr = &hbh->ip6h_nxt;
12663 
12664 		bcopy(ipp->ipp_hopopts, cp, ipp->ipp_hopoptslen);
12665 		cp += ipp->ipp_hopoptslen;
12666 	}
12667 	/*
12668 	 * En-route destination options
12669 	 * Only do them if there's a routing header as well
12670 	 */
12671 	if ((ipp->ipp_fields & (IPPF_RTDSTOPTS|IPPF_RTHDR)) ==
12672 	    (IPPF_RTDSTOPTS|IPPF_RTHDR)) {
12673 		ip6_dest_t *dst = (ip6_dest_t *)cp;
12674 
12675 		*nxthdr_ptr = IPPROTO_DSTOPTS;
12676 		nxthdr_ptr = &dst->ip6d_nxt;
12677 
12678 		bcopy(ipp->ipp_rtdstopts, cp, ipp->ipp_rtdstoptslen);
12679 		cp += ipp->ipp_rtdstoptslen;
12680 	}
12681 	/*
12682 	 * Routing header next
12683 	 */
12684 	if (ipp->ipp_fields & IPPF_RTHDR) {
12685 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
12686 
12687 		*nxthdr_ptr = IPPROTO_ROUTING;
12688 		nxthdr_ptr = &rt->ip6r_nxt;
12689 
12690 		bcopy(ipp->ipp_rthdr, cp, ipp->ipp_rthdrlen);
12691 		cp += ipp->ipp_rthdrlen;
12692 	}
12693 	/*
12694 	 * Do ultimate destination options
12695 	 */
12696 	if (ipp->ipp_fields & IPPF_DSTOPTS) {
12697 		ip6_dest_t *dest = (ip6_dest_t *)cp;
12698 
12699 		*nxthdr_ptr = IPPROTO_DSTOPTS;
12700 		nxthdr_ptr = &dest->ip6d_nxt;
12701 
12702 		bcopy(ipp->ipp_dstopts, cp, ipp->ipp_dstoptslen);
12703 		cp += ipp->ipp_dstoptslen;
12704 	}
12705 	/*
12706 	 * Now set the last header pointer to the proto passed in
12707 	 */
12708 	*nxthdr_ptr = protocol;
12709 	ASSERT((int)(cp - ext_hdrs) == ext_hdrs_len);
12710 }
12711 
12712 /*
12713  * Return a pointer to the routing header extension header
12714  * in the IPv6 header(s) chain passed in.
12715  * If none found, return NULL
12716  * Assumes that all extension headers are in same mblk as the v6 header
12717  */
12718 ip6_rthdr_t *
12719 ip_find_rthdr_v6(ip6_t *ip6h, uint8_t *endptr)
12720 {
12721 	ip6_dest_t	*desthdr;
12722 	ip6_frag_t	*fraghdr;
12723 	uint_t		hdrlen;
12724 	uint8_t		nexthdr;
12725 	uint8_t		*ptr = (uint8_t *)&ip6h[1];
12726 
12727 	if (ip6h->ip6_nxt == IPPROTO_ROUTING)
12728 		return ((ip6_rthdr_t *)ptr);
12729 
12730 	/*
12731 	 * The routing header will precede all extension headers
12732 	 * other than the hop-by-hop and destination options
12733 	 * extension headers, so if we see anything other than those,
12734 	 * we're done and didn't find it.
12735 	 * We could see a destination options header alone but no
12736 	 * routing header, in which case we'll return NULL as soon as
12737 	 * we see anything after that.
12738 	 * Hop-by-hop and destination option headers are identical,
12739 	 * so we can use either one we want as a template.
12740 	 */
12741 	nexthdr = ip6h->ip6_nxt;
12742 	while (ptr < endptr) {
12743 		/* Is there enough left for len + nexthdr? */
12744 		if (ptr + MIN_EHDR_LEN > endptr)
12745 			return (NULL);
12746 
12747 		switch (nexthdr) {
12748 		case IPPROTO_HOPOPTS:
12749 		case IPPROTO_DSTOPTS:
12750 			/* Assumes the headers are identical for hbh and dst */
12751 			desthdr = (ip6_dest_t *)ptr;
12752 			hdrlen = 8 * (desthdr->ip6d_len + 1);
12753 			nexthdr = desthdr->ip6d_nxt;
12754 			break;
12755 
12756 		case IPPROTO_ROUTING:
12757 			return ((ip6_rthdr_t *)ptr);
12758 
12759 		case IPPROTO_FRAGMENT:
12760 			fraghdr = (ip6_frag_t *)ptr;
12761 			hdrlen = sizeof (ip6_frag_t);
12762 			nexthdr = fraghdr->ip6f_nxt;
12763 			break;
12764 
12765 		default:
12766 			return (NULL);
12767 		}
12768 		ptr += hdrlen;
12769 	}
12770 	return (NULL);
12771 }
12772 
12773 /*
12774  * Called for source-routed packets originating on this node.
12775  * Manipulates the original routing header by moving every entry up
12776  * one slot, placing the first entry in the v6 header's v6_dst field,
12777  * and placing the ultimate destination in the routing header's last
12778  * slot.
12779  *
12780  * Returns the checksum diference between the ultimate destination
12781  * (last hop in the routing header when the packet is sent) and
12782  * the first hop (ip6_dst when the packet is sent)
12783  */
12784 /* ARGSUSED2 */
12785 uint32_t
12786 ip_massage_options_v6(ip6_t *ip6h, ip6_rthdr_t *rth, netstack_t *ns)
12787 {
12788 	uint_t		numaddr;
12789 	uint_t		i;
12790 	in6_addr_t	*addrptr;
12791 	in6_addr_t	tmp;
12792 	ip6_rthdr0_t	*rthdr = (ip6_rthdr0_t *)rth;
12793 	uint32_t	cksm;
12794 	uint32_t	addrsum = 0;
12795 	uint16_t	*ptr;
12796 
12797 	/*
12798 	 * Perform any processing needed for source routing.
12799 	 * We know that all extension headers will be in the same mblk
12800 	 * as the IPv6 header.
12801 	 */
12802 
12803 	/*
12804 	 * If no segments left in header, or the header length field is zero,
12805 	 * don't move hop addresses around;
12806 	 * Checksum difference is zero.
12807 	 */
12808 	if ((rthdr->ip6r0_segleft == 0) || (rthdr->ip6r0_len == 0))
12809 		return (0);
12810 
12811 	ptr = (uint16_t *)&ip6h->ip6_dst;
12812 	cksm = 0;
12813 	for (i = 0; i < (sizeof (in6_addr_t) / sizeof (uint16_t)); i++) {
12814 		cksm += ptr[i];
12815 	}
12816 	cksm = (cksm & 0xFFFF) + (cksm >> 16);
12817 
12818 	/*
12819 	 * Here's where the fun begins - we have to
12820 	 * move all addresses up one spot, take the
12821 	 * first hop and make it our first ip6_dst,
12822 	 * and place the ultimate destination in the
12823 	 * newly-opened last slot.
12824 	 */
12825 	addrptr = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
12826 	numaddr = rthdr->ip6r0_len / 2;
12827 	tmp = *addrptr;
12828 	for (i = 0; i < (numaddr - 1); addrptr++, i++) {
12829 		*addrptr = addrptr[1];
12830 	}
12831 	*addrptr = ip6h->ip6_dst;
12832 	ip6h->ip6_dst = tmp;
12833 
12834 	/*
12835 	 * From the checksummed ultimate destination subtract the checksummed
12836 	 * current ip6_dst (the first hop address). Return that number.
12837 	 * (In the v4 case, the second part of this is done in each routine
12838 	 *  that calls ip_massage_options(). We do it all in this one place
12839 	 *  for v6).
12840 	 */
12841 	ptr = (uint16_t *)&ip6h->ip6_dst;
12842 	for (i = 0; i < (sizeof (in6_addr_t) / sizeof (uint16_t)); i++) {
12843 		addrsum += ptr[i];
12844 	}
12845 	cksm -= ((addrsum >> 16) + (addrsum & 0xFFFF));
12846 	if ((int)cksm < 0)
12847 		cksm--;
12848 	cksm = (cksm & 0xFFFF) + (cksm >> 16);
12849 
12850 	return (cksm);
12851 }
12852 
12853 /*
12854  * Propagate a multicast group membership operation (join/leave) (*fn) on
12855  * all interfaces crossed by the related multirt routes.
12856  * The call is considered successful if the operation succeeds
12857  * on at least one interface.
12858  * The function is called if the destination address in the packet to send
12859  * is multirouted.
12860  */
12861 int
12862 ip_multirt_apply_membership_v6(int (*fn)(conn_t *, boolean_t,
12863     const in6_addr_t *, int, mcast_record_t, const in6_addr_t *, mblk_t *),
12864     ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6grp,
12865     mcast_record_t fmode, const in6_addr_t *v6src, mblk_t *first_mp)
12866 {
12867 	ire_t		*ire_gw;
12868 	irb_t		*irb;
12869 	int		index, error = 0;
12870 	opt_restart_t	*or;
12871 	ip_stack_t	*ipst = ire->ire_ipst;
12872 
12873 	irb = ire->ire_bucket;
12874 	ASSERT(irb != NULL);
12875 
12876 	ASSERT(DB_TYPE(first_mp) == M_CTL);
12877 	or = (opt_restart_t *)first_mp->b_rptr;
12878 
12879 	IRB_REFHOLD(irb);
12880 	for (; ire != NULL; ire = ire->ire_next) {
12881 		if ((ire->ire_flags & RTF_MULTIRT) == 0)
12882 			continue;
12883 		if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6grp))
12884 			continue;
12885 
12886 		ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0,
12887 		    IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL,
12888 		    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE, ipst);
12889 		/* No resolver exists for the gateway; skip this ire. */
12890 		if (ire_gw == NULL)
12891 			continue;
12892 		index = ire_gw->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex;
12893 		/*
12894 		 * A resolver exists: we can get the interface on which we have
12895 		 * to apply the operation.
12896 		 */
12897 		error = fn(connp, checkonly, v6grp, index, fmode, v6src,
12898 		    first_mp);
12899 		if (error == 0)
12900 			or->or_private = CGTP_MCAST_SUCCESS;
12901 
12902 		if (ip_debug > 0) {
12903 			ulong_t	off;
12904 			char	*ksym;
12905 
12906 			ksym = kobj_getsymname((uintptr_t)fn, &off);
12907 			ip2dbg(("ip_multirt_apply_membership_v6: "
12908 			    "called %s, multirt group 0x%08x via itf 0x%08x, "
12909 			    "error %d [success %u]\n",
12910 			    ksym ? ksym : "?",
12911 			    ntohl(V4_PART_OF_V6((*v6grp))),
12912 			    ntohl(V4_PART_OF_V6(ire_gw->ire_src_addr_v6)),
12913 			    error, or->or_private));
12914 		}
12915 
12916 		ire_refrele(ire_gw);
12917 		if (error == EINPROGRESS) {
12918 			IRB_REFRELE(irb);
12919 			return (error);
12920 		}
12921 	}
12922 	IRB_REFRELE(irb);
12923 	/*
12924 	 * Consider the call as successful if we succeeded on at least
12925 	 * one interface. Otherwise, return the last encountered error.
12926 	 */
12927 	return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error);
12928 }
12929 
12930 void
12931 *ip6_kstat_init(netstackid_t stackid, ip6_stat_t *ip6_statisticsp)
12932 {
12933 	kstat_t *ksp;
12934 
12935 	ip6_stat_t template = {
12936 		{ "ip6_udp_fast_path", 	KSTAT_DATA_UINT64 },
12937 		{ "ip6_udp_slow_path", 	KSTAT_DATA_UINT64 },
12938 		{ "ip6_udp_fannorm", 	KSTAT_DATA_UINT64 },
12939 		{ "ip6_udp_fanmb", 	KSTAT_DATA_UINT64 },
12940 		{ "ip6_out_sw_cksum",			KSTAT_DATA_UINT64 },
12941 		{ "ip6_in_sw_cksum",			KSTAT_DATA_UINT64 },
12942 		{ "ip6_tcp_in_full_hw_cksum_err",	KSTAT_DATA_UINT64 },
12943 		{ "ip6_tcp_in_part_hw_cksum_err",	KSTAT_DATA_UINT64 },
12944 		{ "ip6_tcp_in_sw_cksum_err",		KSTAT_DATA_UINT64 },
12945 		{ "ip6_tcp_out_sw_cksum_bytes",		KSTAT_DATA_UINT64 },
12946 		{ "ip6_udp_in_full_hw_cksum_err",	KSTAT_DATA_UINT64 },
12947 		{ "ip6_udp_in_part_hw_cksum_err",	KSTAT_DATA_UINT64 },
12948 		{ "ip6_udp_in_sw_cksum_err",		KSTAT_DATA_UINT64 },
12949 		{ "ip6_udp_out_sw_cksum_bytes",		KSTAT_DATA_UINT64 },
12950 		{ "ip6_frag_mdt_pkt_out",		KSTAT_DATA_UINT64 },
12951 		{ "ip6_frag_mdt_discarded",		KSTAT_DATA_UINT64 },
12952 		{ "ip6_frag_mdt_allocfail",		KSTAT_DATA_UINT64 },
12953 		{ "ip6_frag_mdt_addpdescfail",		KSTAT_DATA_UINT64 },
12954 		{ "ip6_frag_mdt_allocd",		KSTAT_DATA_UINT64 },
12955 	};
12956 	ksp = kstat_create_netstack("ip", 0, "ip6stat", "net",
12957 	    KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
12958 	    KSTAT_FLAG_VIRTUAL, stackid);
12959 
12960 	if (ksp == NULL)
12961 		return (NULL);
12962 
12963 	bcopy(&template, ip6_statisticsp, sizeof (template));
12964 	ksp->ks_data = (void *)ip6_statisticsp;
12965 	ksp->ks_private = (void *)(uintptr_t)stackid;
12966 
12967 	kstat_install(ksp);
12968 	return (ksp);
12969 }
12970 
12971 void
12972 ip6_kstat_fini(netstackid_t stackid, kstat_t *ksp)
12973 {
12974 	if (ksp != NULL) {
12975 		ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
12976 		kstat_delete_netstack(ksp, stackid);
12977 	}
12978 }
12979 
12980 /*
12981  * The following two functions set and get the value for the
12982  * IPV6_SRC_PREFERENCES socket option.
12983  */
12984 int
12985 ip6_set_src_preferences(conn_t *connp, uint32_t prefs)
12986 {
12987 	/*
12988 	 * We only support preferences that are covered by
12989 	 * IPV6_PREFER_SRC_MASK.
12990 	 */
12991 	if (prefs & ~IPV6_PREFER_SRC_MASK)
12992 		return (EINVAL);
12993 
12994 	/*
12995 	 * Look for conflicting preferences or default preferences.  If
12996 	 * both bits of a related pair are clear, the application wants the
12997 	 * system's default value for that pair.  Both bits in a pair can't
12998 	 * be set.
12999 	 */
13000 	if ((prefs & IPV6_PREFER_SRC_MIPMASK) == 0) {
13001 		prefs |= IPV6_PREFER_SRC_MIPDEFAULT;
13002 	} else if ((prefs & IPV6_PREFER_SRC_MIPMASK) ==
13003 	    IPV6_PREFER_SRC_MIPMASK) {
13004 		return (EINVAL);
13005 	}
13006 	if ((prefs & IPV6_PREFER_SRC_TMPMASK) == 0) {
13007 		prefs |= IPV6_PREFER_SRC_TMPDEFAULT;
13008 	} else if ((prefs & IPV6_PREFER_SRC_TMPMASK) ==
13009 	    IPV6_PREFER_SRC_TMPMASK) {
13010 		return (EINVAL);
13011 	}
13012 	if ((prefs & IPV6_PREFER_SRC_CGAMASK) == 0) {
13013 		prefs |= IPV6_PREFER_SRC_CGADEFAULT;
13014 	} else if ((prefs & IPV6_PREFER_SRC_CGAMASK) ==
13015 	    IPV6_PREFER_SRC_CGAMASK) {
13016 		return (EINVAL);
13017 	}
13018 
13019 	connp->conn_src_preferences = prefs;
13020 	return (0);
13021 }
13022 
13023 size_t
13024 ip6_get_src_preferences(conn_t *connp, uint32_t *val)
13025 {
13026 	*val = connp->conn_src_preferences;
13027 	return (sizeof (connp->conn_src_preferences));
13028 }
13029 
13030 int
13031 ip6_set_pktinfo(cred_t *cr, conn_t *connp, struct in6_pktinfo *pkti, mblk_t *mp)
13032 {
13033 	ill_t	*ill;
13034 	ire_t	*ire;
13035 	int	error;
13036 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
13037 
13038 	/*
13039 	 * Verify the source address and ifindex. Privileged users can use
13040 	 * any source address.  For ancillary data the source address is
13041 	 * checked in ip_wput_v6.
13042 	 */
13043 	if (pkti->ipi6_ifindex != 0) {
13044 		ASSERT(connp != NULL);
13045 		ill = ill_lookup_on_ifindex(pkti->ipi6_ifindex, B_TRUE,
13046 		    CONNP_TO_WQ(connp), mp, ip_restart_optmgmt, &error, ipst);
13047 		if (ill == NULL) {
13048 			/*
13049 			 * We just want to know if the interface exists, we
13050 			 * don't really care about the ill pointer itself.
13051 			 */
13052 			if (error != EINPROGRESS)
13053 				return (error);
13054 			error = 0;	/* Ensure we don't use it below */
13055 		} else {
13056 			ill_refrele(ill);
13057 		}
13058 	}
13059 	if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr) &&
13060 	    secpolicy_net_rawaccess(cr) != 0) {
13061 		ire = ire_route_lookup_v6(&pkti->ipi6_addr, 0, 0,
13062 		    (IRE_LOCAL|IRE_LOOPBACK), NULL, NULL,
13063 		    connp->conn_zoneid, NULL, MATCH_IRE_TYPE, ipst);
13064 		if (ire != NULL)
13065 			ire_refrele(ire);
13066 		else
13067 			return (ENXIO);
13068 	}
13069 	return (0);
13070 }
13071 
13072 /*
13073  * Get the size of the IP options (including the IP headers size)
13074  * without including the AH header's size. If till_ah is B_FALSE,
13075  * and if AH header is present, dest options beyond AH header will
13076  * also be included in the returned size.
13077  */
13078 int
13079 ipsec_ah_get_hdr_size_v6(mblk_t *mp, boolean_t till_ah)
13080 {
13081 	ip6_t *ip6h;
13082 	uint8_t nexthdr;
13083 	uint8_t *whereptr;
13084 	ip6_hbh_t *hbhhdr;
13085 	ip6_dest_t *dsthdr;
13086 	ip6_rthdr_t *rthdr;
13087 	int ehdrlen;
13088 	int size;
13089 	ah_t *ah;
13090 
13091 	ip6h = (ip6_t *)mp->b_rptr;
13092 	size = IPV6_HDR_LEN;
13093 	nexthdr = ip6h->ip6_nxt;
13094 	whereptr = (uint8_t *)&ip6h[1];
13095 	for (;;) {
13096 		/* Assume IP has already stripped it */
13097 		ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW);
13098 		switch (nexthdr) {
13099 		case IPPROTO_HOPOPTS:
13100 			hbhhdr = (ip6_hbh_t *)whereptr;
13101 			nexthdr = hbhhdr->ip6h_nxt;
13102 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
13103 			break;
13104 		case IPPROTO_DSTOPTS:
13105 			dsthdr = (ip6_dest_t *)whereptr;
13106 			nexthdr = dsthdr->ip6d_nxt;
13107 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
13108 			break;
13109 		case IPPROTO_ROUTING:
13110 			rthdr = (ip6_rthdr_t *)whereptr;
13111 			nexthdr = rthdr->ip6r_nxt;
13112 			ehdrlen = 8 * (rthdr->ip6r_len + 1);
13113 			break;
13114 		default :
13115 			if (till_ah) {
13116 				ASSERT(nexthdr == IPPROTO_AH);
13117 				return (size);
13118 			}
13119 			/*
13120 			 * If we don't have a AH header to traverse,
13121 			 * return now. This happens normally for
13122 			 * outbound datagrams where we have not inserted
13123 			 * the AH header.
13124 			 */
13125 			if (nexthdr != IPPROTO_AH) {
13126 				return (size);
13127 			}
13128 
13129 			/*
13130 			 * We don't include the AH header's size
13131 			 * to be symmetrical with other cases where
13132 			 * we either don't have a AH header (outbound)
13133 			 * or peek into the AH header yet (inbound and
13134 			 * not pulled up yet).
13135 			 */
13136 			ah = (ah_t *)whereptr;
13137 			nexthdr = ah->ah_nexthdr;
13138 			ehdrlen = (ah->ah_length << 2) + 8;
13139 
13140 			if (nexthdr == IPPROTO_DSTOPTS) {
13141 				if (whereptr + ehdrlen >= mp->b_wptr) {
13142 					/*
13143 					 * The destination options header
13144 					 * is not part of the first mblk.
13145 					 */
13146 					whereptr = mp->b_cont->b_rptr;
13147 				} else {
13148 					whereptr += ehdrlen;
13149 				}
13150 
13151 				dsthdr = (ip6_dest_t *)whereptr;
13152 				ehdrlen = 8 * (dsthdr->ip6d_len + 1);
13153 				size += ehdrlen;
13154 			}
13155 			return (size);
13156 		}
13157 		whereptr += ehdrlen;
13158 		size += ehdrlen;
13159 	}
13160 }
13161