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