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