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