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