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