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