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