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