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