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