xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_input.c (revision 0d166b18feda26f6f45f5be1c0c8c5e539b90e6c)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/dlpi.h>
31 #include <sys/stropts.h>
32 #include <sys/sysmacros.h>
33 #include <sys/strsubr.h>
34 #include <sys/strlog.h>
35 #include <sys/strsun.h>
36 #include <sys/zone.h>
37 #define	_SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/xti_inet.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/debug.h>
44 #include <sys/kobj.h>
45 #include <sys/modctl.h>
46 #include <sys/atomic.h>
47 #include <sys/policy.h>
48 #include <sys/priv.h>
49 
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/kmem.h>
53 #include <sys/sdt.h>
54 #include <sys/socket.h>
55 #include <sys/vtrace.h>
56 #include <sys/isa_defs.h>
57 #include <sys/mac.h>
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <net/if_dl.h>
64 
65 #include <inet/common.h>
66 #include <inet/mi.h>
67 #include <inet/mib2.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/snmpcom.h>
71 #include <inet/kstatcom.h>
72 
73 #include <netinet/igmp_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet/icmp6.h>
76 #include <netinet/sctp.h>
77 
78 #include <inet/ip.h>
79 #include <inet/ip_impl.h>
80 #include <inet/ip6.h>
81 #include <inet/ip6_asp.h>
82 #include <inet/optcom.h>
83 #include <inet/tcp.h>
84 #include <inet/tcp_impl.h>
85 #include <inet/ip_multi.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_ire.h>
88 #include <inet/ip_ftable.h>
89 #include <inet/ip_rts.h>
90 #include <inet/ip_ndp.h>
91 #include <inet/ip_listutils.h>
92 #include <netinet/igmp.h>
93 #include <netinet/ip_mroute.h>
94 #include <inet/ipp_common.h>
95 
96 #include <net/pfkeyv2.h>
97 #include <inet/sadb.h>
98 #include <inet/ipsec_impl.h>
99 #include <inet/ipdrop.h>
100 #include <inet/ip_netinfo.h>
101 #include <inet/ilb_ip.h>
102 #include <sys/squeue_impl.h>
103 #include <sys/squeue.h>
104 
105 #include <sys/ethernet.h>
106 #include <net/if_types.h>
107 #include <sys/cpuvar.h>
108 
109 #include <ipp/ipp.h>
110 #include <ipp/ipp_impl.h>
111 #include <ipp/ipgpc/ipgpc.h>
112 
113 #include <sys/pattr.h>
114 #include <inet/ipclassifier.h>
115 #include <inet/sctp_ip.h>
116 #include <inet/sctp/sctp_impl.h>
117 #include <inet/udp_impl.h>
118 #include <sys/sunddi.h>
119 
120 #include <sys/tsol/label.h>
121 #include <sys/tsol/tnet.h>
122 
123 #include <sys/clock_impl.h>	/* For LBOLT_FASTPATH{,64} */
124 
125 #ifdef	DEBUG
126 extern boolean_t skip_sctp_cksum;
127 #endif
128 
129 static void	ip_input_local_v4(ire_t *, mblk_t *, ipha_t *,
130     ip_recv_attr_t *);
131 
132 static void	ip_input_broadcast_v4(ire_t *, mblk_t *, ipha_t *,
133     ip_recv_attr_t *);
134 static void	ip_input_multicast_v4(ire_t *, mblk_t *, ipha_t *,
135     ip_recv_attr_t *);
136 
137 #pragma inline(ip_input_common_v4, ip_input_local_v4, ip_forward_xmit_v4)
138 
139 /*
140  * Direct read side procedure capable of dealing with chains. GLDv3 based
141  * drivers call this function directly with mblk chains while STREAMS
142  * read side procedure ip_rput() calls this for single packet with ip_ring
143  * set to NULL to process one packet at a time.
144  *
145  * The ill will always be valid if this function is called directly from
146  * the driver.
147  *
148  * If ip_input() is called from GLDv3:
149  *
150  *   - This must be a non-VLAN IP stream.
151  *   - 'mp' is either an untagged or a special priority-tagged packet.
152  *   - Any VLAN tag that was in the MAC header has been stripped.
153  *
154  * If the IP header in packet is not 32-bit aligned, every message in the
155  * chain will be aligned before further operations. This is required on SPARC
156  * platform.
157  */
158 void
159 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain,
160     struct mac_header_info_s *mhip)
161 {
162 	(void) ip_input_common_v4(ill, ip_ring, mp_chain, mhip, NULL, NULL,
163 	    NULL);
164 }
165 
166 /*
167  * ip_accept_tcp() - This function is called by the squeue when it retrieves
168  * a chain of packets in the poll mode. The packets have gone through the
169  * data link processing but not IP processing. For performance and latency
170  * reasons, the squeue wants to process the chain in line instead of feeding
171  * it back via ip_input path.
172  *
173  * We set up the ip_recv_attr_t with IRAF_TARGET_SQP to that ip_fanout_v4
174  * will pass back any TCP packets matching the target sqp to
175  * ip_input_common_v4 using ira_target_sqp_mp. Other packets are handled by
176  * ip_input_v4 and ip_fanout_v4 as normal.
177  * The TCP packets that match the target squeue are returned to the caller
178  * as a b_next chain after each packet has been prepend with an mblk
179  * from ip_recv_attr_to_mblk.
180  */
181 mblk_t *
182 ip_accept_tcp(ill_t *ill, ill_rx_ring_t *ip_ring, squeue_t *target_sqp,
183     mblk_t *mp_chain, mblk_t **last, uint_t *cnt)
184 {
185 	return (ip_input_common_v4(ill, ip_ring, mp_chain, NULL, target_sqp,
186 	    last, cnt));
187 }
188 
189 /*
190  * Used by ip_input and ip_accept_tcp
191  * The last three arguments are only used by ip_accept_tcp, and mhip is
192  * only used by ip_input.
193  */
194 mblk_t *
195 ip_input_common_v4(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain,
196     struct mac_header_info_s *mhip, squeue_t *target_sqp,
197     mblk_t **last, uint_t *cnt)
198 {
199 	mblk_t		*mp;
200 	ipha_t		*ipha;
201 	ip_recv_attr_t	iras;	/* Receive attributes */
202 	rtc_t		rtc;
203 	iaflags_t	chain_flags = 0;	/* Fixed for chain */
204 	mblk_t 		*ahead = NULL;	/* Accepted head */
205 	mblk_t		*atail = NULL;	/* Accepted tail */
206 	uint_t		acnt = 0;	/* Accepted count */
207 
208 	ASSERT(mp_chain != NULL);
209 	ASSERT(ill != NULL);
210 
211 	/* These ones do not change as we loop over packets */
212 	iras.ira_ill = iras.ira_rill = ill;
213 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
214 	iras.ira_rifindex = iras.ira_ruifindex;
215 	iras.ira_sqp = NULL;
216 	iras.ira_ring = ip_ring;
217 	/* For ECMP and outbound transmit ring selection */
218 	iras.ira_xmit_hint = ILL_RING_TO_XMIT_HINT(ip_ring);
219 
220 	iras.ira_target_sqp = target_sqp;
221 	iras.ira_target_sqp_mp = NULL;
222 	if (target_sqp != NULL)
223 		chain_flags |= IRAF_TARGET_SQP;
224 
225 	/*
226 	 * We try to have a mhip pointer when possible, but
227 	 * it might be NULL in some cases. In those cases we
228 	 * have to assume unicast.
229 	 */
230 	iras.ira_mhip = mhip;
231 	iras.ira_flags = 0;
232 	if (mhip != NULL) {
233 		switch (mhip->mhi_dsttype) {
234 		case MAC_ADDRTYPE_MULTICAST :
235 			chain_flags |= IRAF_L2DST_MULTICAST;
236 			break;
237 		case MAC_ADDRTYPE_BROADCAST :
238 			chain_flags |= IRAF_L2DST_BROADCAST;
239 			break;
240 		}
241 	}
242 
243 	/*
244 	 * Initialize the one-element route cache.
245 	 *
246 	 * We do ire caching from one iteration to
247 	 * another. In the event the packet chain contains
248 	 * all packets from the same dst, this caching saves
249 	 * an ire_route_recursive for each of the succeeding
250 	 * packets in a packet chain.
251 	 */
252 	rtc.rtc_ire = NULL;
253 	rtc.rtc_ipaddr = INADDR_ANY;
254 
255 	/* Loop over b_next */
256 	for (mp = mp_chain; mp != NULL; mp = mp_chain) {
257 		mp_chain = mp->b_next;
258 		mp->b_next = NULL;
259 
260 		ASSERT(DB_TYPE(mp) == M_DATA);
261 
262 
263 		/*
264 		 * if db_ref > 1 then copymsg and free original. Packet
265 		 * may be changed and we do not want the other entity
266 		 * who has a reference to this message to trip over the
267 		 * changes. This is a blind change because trying to
268 		 * catch all places that might change the packet is too
269 		 * difficult.
270 		 *
271 		 * This corresponds to the fast path case, where we have
272 		 * a chain of M_DATA mblks.  We check the db_ref count
273 		 * of only the 1st data block in the mblk chain. There
274 		 * doesn't seem to be a reason why a device driver would
275 		 * send up data with varying db_ref counts in the mblk
276 		 * chain. In any case the Fast path is a private
277 		 * interface, and our drivers don't do such a thing.
278 		 * Given the above assumption, there is no need to walk
279 		 * down the entire mblk chain (which could have a
280 		 * potential performance problem)
281 		 *
282 		 * The "(DB_REF(mp) > 1)" check was moved from ip_rput()
283 		 * to here because of exclusive ip stacks and vnics.
284 		 * Packets transmitted from exclusive stack over vnic
285 		 * can have db_ref > 1 and when it gets looped back to
286 		 * another vnic in a different zone, you have ip_input()
287 		 * getting dblks with db_ref > 1. So if someone
288 		 * complains of TCP performance under this scenario,
289 		 * take a serious look here on the impact of copymsg().
290 		 */
291 		if (DB_REF(mp) > 1) {
292 			if ((mp = ip_fix_dbref(mp, &iras)) == NULL) {
293 				/* mhip might point into 1st packet in chain */
294 				iras.ira_mhip = NULL;
295 				continue;
296 			}
297 		}
298 
299 		/*
300 		 * IP header ptr not aligned?
301 		 * OR IP header not complete in first mblk
302 		 */
303 		ipha = (ipha_t *)mp->b_rptr;
304 		if (!OK_32PTR(ipha) || MBLKL(mp) < IP_SIMPLE_HDR_LENGTH) {
305 			mp = ip_check_and_align_header(mp, IP_SIMPLE_HDR_LENGTH,
306 			    &iras);
307 			if (mp == NULL) {
308 				/* mhip might point into 1st packet in chain */
309 				iras.ira_mhip = NULL;
310 				continue;
311 			}
312 			ipha = (ipha_t *)mp->b_rptr;
313 		}
314 
315 		/* Protect against a mix of Ethertypes and IP versions */
316 		if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
317 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
318 			ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
319 			freemsg(mp);
320 			/* mhip might point into 1st packet in the chain. */
321 			iras.ira_mhip = NULL;
322 			continue;
323 		}
324 
325 		/*
326 		 * Check for Martian addrs; we have to explicitly
327 		 * test for for zero dst since this is also used as
328 		 * an indication that the rtc is not used.
329 		 */
330 		if (ipha->ipha_dst == INADDR_ANY) {
331 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors);
332 			ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
333 			freemsg(mp);
334 			/* mhip might point into 1st packet in the chain. */
335 			iras.ira_mhip = NULL;
336 			continue;
337 		}
338 
339 		/*
340 		 * Keep L2SRC from a previous packet in chain since mhip
341 		 * might point into an earlier packet in the chain.
342 		 * Keep IRAF_VERIFIED_SRC to avoid redoing broadcast
343 		 * source check in forwarding path.
344 		 */
345 		chain_flags |= (iras.ira_flags &
346 		    (IRAF_L2SRC_SET|IRAF_VERIFIED_SRC));
347 
348 		iras.ira_flags = IRAF_IS_IPV4 | IRAF_VERIFY_IP_CKSUM |
349 		    IRAF_VERIFY_ULP_CKSUM | chain_flags;
350 		iras.ira_free_flags = 0;
351 		iras.ira_cred = NULL;
352 		iras.ira_cpid = NOPID;
353 		iras.ira_tsl = NULL;
354 		iras.ira_zoneid = ALL_ZONES;	/* Default for forwarding */
355 
356 		/*
357 		 * We must count all incoming packets, even if they end
358 		 * up being dropped later on. Defer counting bytes until
359 		 * we have the whole IP header in first mblk.
360 		 */
361 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives);
362 
363 		iras.ira_pktlen = ntohs(ipha->ipha_length);
364 		UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets,
365 		    iras.ira_pktlen);
366 
367 		/*
368 		 * Call one of:
369 		 * 	ill_input_full_v4
370 		 *	ill_input_short_v4
371 		 * The former is used in unusual cases. See ill_set_inputfn().
372 		 */
373 		(*ill->ill_inputfn)(mp, ipha, &ipha->ipha_dst, &iras, &rtc);
374 
375 		/* Any references to clean up? No hold on ira_ill */
376 		if (iras.ira_flags & (IRAF_IPSEC_SECURE|IRAF_SYSTEM_LABELED))
377 			ira_cleanup(&iras, B_FALSE);
378 
379 		if (iras.ira_target_sqp_mp != NULL) {
380 			/* Better be called from ip_accept_tcp */
381 			ASSERT(target_sqp != NULL);
382 
383 			/* Found one packet to accept */
384 			mp = iras.ira_target_sqp_mp;
385 			iras.ira_target_sqp_mp = NULL;
386 			ASSERT(ip_recv_attr_is_mblk(mp));
387 
388 			if (atail != NULL)
389 				atail->b_next = mp;
390 			else
391 				ahead = mp;
392 			atail = mp;
393 			acnt++;
394 			mp = NULL;
395 		}
396 		/* mhip might point into 1st packet in the chain. */
397 		iras.ira_mhip = NULL;
398 	}
399 	/* Any remaining references to the route cache? */
400 	if (rtc.rtc_ire != NULL) {
401 		ASSERT(rtc.rtc_ipaddr != INADDR_ANY);
402 		ire_refrele(rtc.rtc_ire);
403 	}
404 
405 	if (ahead != NULL) {
406 		/* Better be called from ip_accept_tcp */
407 		ASSERT(target_sqp != NULL);
408 		*last = atail;
409 		*cnt = acnt;
410 		return (ahead);
411 	}
412 
413 	return (NULL);
414 }
415 
416 /*
417  * This input function is used when
418  *  - is_system_labeled()
419  *  - CGTP filtering
420  *  - DHCP unicast before we have an IP address configured
421  *  - there is an listener for IPPROTO_RSVP
422  */
423 void
424 ill_input_full_v4(mblk_t *mp, void *iph_arg, void *nexthop_arg,
425     ip_recv_attr_t *ira, rtc_t *rtc)
426 {
427 	ipha_t		*ipha = (ipha_t *)iph_arg;
428 	ipaddr_t	nexthop = *(ipaddr_t *)nexthop_arg;
429 	ill_t		*ill = ira->ira_ill;
430 	ip_stack_t	*ipst = ill->ill_ipst;
431 	int		cgtp_flt_pkt;
432 
433 	ASSERT(ira->ira_tsl == NULL);
434 
435 	/*
436 	 * Attach any necessary label information to
437 	 * this packet
438 	 */
439 	if (is_system_labeled()) {
440 		ira->ira_flags |= IRAF_SYSTEM_LABELED;
441 
442 		/*
443 		 * This updates ira_cred, ira_tsl and ira_free_flags based
444 		 * on the label.
445 		 */
446 		if (!tsol_get_pkt_label(mp, IPV4_VERSION, ira)) {
447 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
448 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
449 			freemsg(mp);
450 			return;
451 		}
452 		/* Note that ira_tsl can be NULL here. */
453 
454 		/* tsol_get_pkt_label sometimes does pullupmsg */
455 		ipha = (ipha_t *)mp->b_rptr;
456 	}
457 
458 	/*
459 	 * Invoke the CGTP (multirouting) filtering module to process
460 	 * the incoming packet. Packets identified as duplicates
461 	 * must be discarded. Filtering is active only if the
462 	 * the ip_cgtp_filter ndd variable is non-zero.
463 	 */
464 	cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP;
465 	if (ipst->ips_ip_cgtp_filter &&
466 	    ipst->ips_ip_cgtp_filter_ops != NULL) {
467 		netstackid_t stackid;
468 
469 		stackid = ipst->ips_netstack->netstack_stackid;
470 		/*
471 		 * CGTP and IPMP are mutually exclusive so
472 		 * phyint_ifindex is fine here.
473 		 */
474 		cgtp_flt_pkt =
475 		    ipst->ips_ip_cgtp_filter_ops->cfo_filter(stackid,
476 		    ill->ill_phyint->phyint_ifindex, mp);
477 		if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) {
478 			ip_drop_input("CGTP_IP_PKT_DUPLICATE", mp, ill);
479 			freemsg(mp);
480 			return;
481 		}
482 	}
483 
484 	/*
485 	 * Brutal hack for DHCPv4 unicast: RFC2131 allows a DHCP
486 	 * server to unicast DHCP packets to a DHCP client using the
487 	 * IP address it is offering to the client.  This can be
488 	 * disabled through the "broadcast bit", but not all DHCP
489 	 * servers honor that bit.  Therefore, to interoperate with as
490 	 * many DHCP servers as possible, the DHCP client allows the
491 	 * server to unicast, but we treat those packets as broadcast
492 	 * here.  Note that we don't rewrite the packet itself since
493 	 * (a) that would mess up the checksums and (b) the DHCP
494 	 * client conn is bound to INADDR_ANY so ip_fanout_udp() will
495 	 * hand it the packet regardless.
496 	 */
497 	if (ill->ill_dhcpinit != 0 &&
498 	    ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION &&
499 	    ipha->ipha_protocol == IPPROTO_UDP) {
500 		udpha_t *udpha;
501 
502 		ipha = ip_pullup(mp, sizeof (ipha_t) + sizeof (udpha_t), ira);
503 		if (ipha == NULL) {
504 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
505 			ip_drop_input("ipIfStatsInDiscards - dhcp", mp, ill);
506 			freemsg(mp);
507 			return;
508 		}
509 		/* Reload since pullupmsg() can change b_rptr. */
510 		udpha = (udpha_t *)&ipha[1];
511 
512 		if (ntohs(udpha->uha_dst_port) == IPPORT_BOOTPC) {
513 			DTRACE_PROBE2(ip4__dhcpinit__pkt, ill_t *, ill,
514 			    mblk_t *, mp);
515 			/*
516 			 * This assumes that we deliver to all conns for
517 			 * multicast and broadcast packets.
518 			 */
519 			nexthop = INADDR_BROADCAST;
520 			ira->ira_flags |= IRAF_DHCP_UNICAST;
521 		}
522 	}
523 
524 	/*
525 	 * If rsvpd is running, let RSVP daemon handle its processing
526 	 * and forwarding of RSVP multicast/unicast packets.
527 	 * If rsvpd is not running but mrouted is running, RSVP
528 	 * multicast packets are forwarded as multicast traffic
529 	 * and RSVP unicast packets are forwarded by unicast router.
530 	 * If neither rsvpd nor mrouted is running, RSVP multicast
531 	 * packets are not forwarded, but the unicast packets are
532 	 * forwarded like unicast traffic.
533 	 */
534 	if (ipha->ipha_protocol == IPPROTO_RSVP &&
535 	    ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head != NULL) {
536 		/* RSVP packet and rsvpd running. Treat as ours */
537 		ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(nexthop)));
538 		/*
539 		 * We use a multicast address to get the packet to
540 		 * ire_recv_multicast_v4. There will not be a membership
541 		 * check since we set IRAF_RSVP
542 		 */
543 		nexthop = htonl(INADDR_UNSPEC_GROUP);
544 		ira->ira_flags |= IRAF_RSVP;
545 	}
546 
547 	ill_input_short_v4(mp, ipha, &nexthop, ira, rtc);
548 }
549 
550 /*
551  * This is the tail-end of the full receive side packet handling.
552  * It can be used directly when the configuration is simple.
553  */
554 void
555 ill_input_short_v4(mblk_t *mp, void *iph_arg, void *nexthop_arg,
556     ip_recv_attr_t *ira, rtc_t *rtc)
557 {
558 	ire_t		*ire;
559 	uint_t		opt_len;
560 	ill_t		*ill = ira->ira_ill;
561 	ip_stack_t	*ipst = ill->ill_ipst;
562 	uint_t		pkt_len;
563 	ssize_t 	len;
564 	ipha_t		*ipha = (ipha_t *)iph_arg;
565 	ipaddr_t	nexthop = *(ipaddr_t *)nexthop_arg;
566 	ilb_stack_t	*ilbs = ipst->ips_netstack->netstack_ilb;
567 #define	rptr	((uchar_t *)ipha)
568 
569 	ASSERT(DB_TYPE(mp) == M_DATA);
570 
571 	/*
572 	 * The following test for loopback is faster than
573 	 * IP_LOOPBACK_ADDR(), because it avoids any bitwise
574 	 * operations.
575 	 * Note that these addresses are always in network byte order
576 	 */
577 	if (((*(uchar_t *)&ipha->ipha_dst) == 127) ||
578 	    ((*(uchar_t *)&ipha->ipha_src) == 127)) {
579 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors);
580 		ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
581 		freemsg(mp);
582 		return;
583 	}
584 
585 	len = mp->b_wptr - rptr;
586 	pkt_len = ira->ira_pktlen;
587 
588 	/* multiple mblk or too short */
589 	len -= pkt_len;
590 	if (len != 0) {
591 		mp = ip_check_length(mp, rptr, len, pkt_len,
592 		    IP_SIMPLE_HDR_LENGTH, ira);
593 		if (mp == NULL)
594 			return;
595 		ipha = (ipha_t *)mp->b_rptr;
596 	}
597 
598 	DTRACE_IP7(receive, mblk_t *, mp, conn_t *, NULL, void_ip_t *,
599 	    ipha, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, ip6_t *, NULL,
600 	    int, 0);
601 
602 	/*
603 	 * The event for packets being received from a 'physical'
604 	 * interface is placed after validation of the source and/or
605 	 * destination address as being local so that packets can be
606 	 * redirected to loopback addresses using ipnat.
607 	 */
608 	DTRACE_PROBE4(ip4__physical__in__start,
609 	    ill_t *, ill, ill_t *, NULL,
610 	    ipha_t *, ipha, mblk_t *, mp);
611 
612 	if (HOOKS4_INTERESTED_PHYSICAL_IN(ipst)) {
613 		int	ll_multicast = 0;
614 		int	error;
615 		ipaddr_t orig_dst = ipha->ipha_dst;
616 
617 		if (ira->ira_flags & IRAF_L2DST_MULTICAST)
618 			ll_multicast = HPE_MULTICAST;
619 		else if (ira->ira_flags & IRAF_L2DST_BROADCAST)
620 			ll_multicast = HPE_BROADCAST;
621 
622 		FW_HOOKS(ipst->ips_ip4_physical_in_event,
623 		    ipst->ips_ipv4firewall_physical_in,
624 		    ill, NULL, ipha, mp, mp, ll_multicast, ipst, error);
625 
626 		DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, mp);
627 
628 		if (mp == NULL)
629 			return;
630 		/* The length could have changed */
631 		ipha = (ipha_t *)mp->b_rptr;
632 		ira->ira_pktlen = ntohs(ipha->ipha_length);
633 		pkt_len = ira->ira_pktlen;
634 
635 		/*
636 		 * In case the destination changed we override any previous
637 		 * change to nexthop.
638 		 */
639 		if (orig_dst != ipha->ipha_dst)
640 			nexthop = ipha->ipha_dst;
641 		if (nexthop == INADDR_ANY) {
642 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors);
643 			ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
644 			freemsg(mp);
645 			return;
646 		}
647 	}
648 
649 	if (ipst->ips_ip4_observe.he_interested) {
650 		zoneid_t dzone;
651 
652 		/*
653 		 * On the inbound path the src zone will be unknown as
654 		 * this packet has come from the wire.
655 		 */
656 		dzone = ip_get_zoneid_v4(nexthop, mp, ira, ALL_ZONES);
657 		ipobs_hook(mp, IPOBS_HOOK_INBOUND, ALL_ZONES, dzone, ill, ipst);
658 	}
659 
660 	/*
661 	 * If there is a good HW IP header checksum we clear the need
662 	 * look at the IP header checksum.
663 	 */
664 	if ((DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM) &&
665 	    ILL_HCKSUM_CAPABLE(ill) && dohwcksum) {
666 		/* Header checksum was ok. Clear the flag */
667 		DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM;
668 		ira->ira_flags &= ~IRAF_VERIFY_IP_CKSUM;
669 	}
670 
671 	/*
672 	 * Here we check to see if we machine is setup as
673 	 * L3 loadbalancer and if the incoming packet is for a VIP
674 	 *
675 	 * Check the following:
676 	 * - there is at least a rule
677 	 * - protocol of the packet is supported
678 	 */
679 	if (ilb_has_rules(ilbs) && ILB_SUPP_L4(ipha->ipha_protocol)) {
680 		ipaddr_t	lb_dst;
681 		int		lb_ret;
682 
683 		/* For convenience, we pull up the mblk. */
684 		if (mp->b_cont != NULL) {
685 			if (pullupmsg(mp, -1) == 0) {
686 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
687 				ip_drop_input("ipIfStatsInDiscards - pullupmsg",
688 				    mp, ill);
689 				freemsg(mp);
690 				return;
691 			}
692 			ipha = (ipha_t *)mp->b_rptr;
693 		}
694 
695 		/*
696 		 * We just drop all fragments going to any VIP, at
697 		 * least for now....
698 		 */
699 		if (ntohs(ipha->ipha_fragment_offset_and_flags) &
700 		    (IPH_MF | IPH_OFFSET)) {
701 			if (!ilb_rule_match_vip_v4(ilbs, nexthop, NULL)) {
702 				goto after_ilb;
703 			}
704 
705 			ILB_KSTAT_UPDATE(ilbs, ip_frag_in, 1);
706 			ILB_KSTAT_UPDATE(ilbs, ip_frag_dropped, 1);
707 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
708 			ip_drop_input("ILB fragment", mp, ill);
709 			freemsg(mp);
710 			return;
711 		}
712 		lb_ret = ilb_check_v4(ilbs, ill, mp, ipha, ipha->ipha_protocol,
713 		    (uint8_t *)ipha + IPH_HDR_LENGTH(ipha), &lb_dst);
714 
715 		if (lb_ret == ILB_DROPPED) {
716 			/* Is this the right counter to increase? */
717 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
718 			ip_drop_input("ILB_DROPPED", mp, ill);
719 			freemsg(mp);
720 			return;
721 		}
722 		if (lb_ret == ILB_BALANCED) {
723 			/* Set the dst to that of the chosen server */
724 			nexthop = lb_dst;
725 			DB_CKSUMFLAGS(mp) = 0;
726 		}
727 	}
728 
729 after_ilb:
730 	opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION;
731 	ira->ira_ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
732 	if (opt_len != 0) {
733 		int error = 0;
734 
735 		ira->ira_ip_hdr_length += (opt_len << 2);
736 		ira->ira_flags |= IRAF_IPV4_OPTIONS;
737 
738 		/* IP Options present!  Validate the length. */
739 		mp = ip_check_optlen(mp, ipha, opt_len, pkt_len, ira);
740 		if (mp == NULL)
741 			return;
742 
743 		/* Might have changed */
744 		ipha = (ipha_t *)mp->b_rptr;
745 
746 		/* Verify IP header checksum before parsing the options */
747 		if ((ira->ira_flags & IRAF_VERIFY_IP_CKSUM) &&
748 		    ip_csum_hdr(ipha)) {
749 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
750 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
751 			freemsg(mp);
752 			return;
753 		}
754 		ira->ira_flags &= ~IRAF_VERIFY_IP_CKSUM;
755 
756 		/*
757 		 * Go off to ip_input_options which returns the next hop
758 		 * destination address, which may have been affected
759 		 * by source routing.
760 		 */
761 		IP_STAT(ipst, ip_opt);
762 
763 		nexthop = ip_input_options(ipha, nexthop, mp, ira, &error);
764 		if (error != 0) {
765 			/*
766 			 * An ICMP error has been sent and the packet has
767 			 * been dropped.
768 			 */
769 			return;
770 		}
771 	}
772 	/* Can not use route cache with TX since the labels can differ */
773 	if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
774 		if (CLASSD(nexthop)) {
775 			ire = ire_multicast(ill);
776 		} else {
777 			/* Match destination and label */
778 			ire = ire_route_recursive_v4(nexthop, 0, NULL,
779 			    ALL_ZONES, ira->ira_tsl, MATCH_IRE_SECATTR,
780 			    (ill->ill_flags & ILLF_ROUTER),
781 			    ira->ira_xmit_hint, ipst, NULL, NULL, NULL);
782 		}
783 		/* Update the route cache so we do the ire_refrele */
784 		ASSERT(ire != NULL);
785 		if (rtc->rtc_ire != NULL)
786 			ire_refrele(rtc->rtc_ire);
787 		rtc->rtc_ire = ire;
788 		rtc->rtc_ipaddr = nexthop;
789 	} else if (nexthop == rtc->rtc_ipaddr) {
790 		/* Use the route cache */
791 		ASSERT(rtc->rtc_ire != NULL);
792 		ire = rtc->rtc_ire;
793 	} else {
794 		/* Update the route cache */
795 		if (CLASSD(nexthop)) {
796 			ire = ire_multicast(ill);
797 		} else {
798 			/* Just match the destination */
799 			ire = ire_route_recursive_dstonly_v4(nexthop,
800 			    (ill->ill_flags & ILLF_ROUTER), ira->ira_xmit_hint,
801 			    ipst);
802 		}
803 		ASSERT(ire != NULL);
804 		if (rtc->rtc_ire != NULL)
805 			ire_refrele(rtc->rtc_ire);
806 		rtc->rtc_ire = ire;
807 		rtc->rtc_ipaddr = nexthop;
808 	}
809 
810 	ire->ire_ib_pkt_count++;
811 
812 	/*
813 	 * Based on ire_type and ire_flags call one of:
814 	 *	ire_recv_local_v4 - for IRE_LOCAL
815 	 *	ire_recv_loopback_v4 - for IRE_LOOPBACK
816 	 *	ire_recv_multirt_v4 - if RTF_MULTIRT
817 	 *	ire_recv_noroute_v4 - if RTF_REJECT or RTF_BLACHOLE
818 	 *	ire_recv_multicast_v4 - for IRE_MULTICAST
819 	 *	ire_recv_broadcast_v4 - for IRE_BROADCAST
820 	 *	ire_recv_noaccept_v4 - for ire_noaccept ones
821 	 *	ire_recv_forward_v4 - for the rest.
822 	 */
823 	(*ire->ire_recvfn)(ire, mp, ipha, ira);
824 }
825 #undef rptr
826 
827 /*
828  * ire_recvfn for IREs that need forwarding
829  */
830 void
831 ire_recv_forward_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
832 {
833 	ipha_t		*ipha = (ipha_t *)iph_arg;
834 	ill_t		*ill = ira->ira_ill;
835 	ip_stack_t	*ipst = ill->ill_ipst;
836 	ill_t		*dst_ill;
837 	nce_t		*nce;
838 	ipaddr_t	src = ipha->ipha_src;
839 	uint32_t	added_tx_len;
840 	uint32_t	mtu, iremtu;
841 
842 	if (ira->ira_flags & (IRAF_L2DST_MULTICAST|IRAF_L2DST_BROADCAST)) {
843 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
844 		ip_drop_input("l2 multicast not forwarded", mp, ill);
845 		freemsg(mp);
846 		return;
847 	}
848 
849 	if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) {
850 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
851 		ip_drop_input("ipIfStatsForwProhibits", mp, ill);
852 		freemsg(mp);
853 		return;
854 	}
855 
856 	/*
857 	 * Either ire_nce_capable or ire_dep_parent would be set for the IRE
858 	 * when it is found by ire_route_recursive, but that some other thread
859 	 * could have changed the routes with the effect of clearing
860 	 * ire_dep_parent. In that case we'd end up dropping the packet, or
861 	 * finding a new nce below.
862 	 * Get, allocate, or update the nce.
863 	 * We get a refhold on ire_nce_cache as a result of this to avoid races
864 	 * where ire_nce_cache is deleted.
865 	 *
866 	 * This ensures that we don't forward if the interface is down since
867 	 * ipif_down removes all the nces.
868 	 */
869 	mutex_enter(&ire->ire_lock);
870 	nce = ire->ire_nce_cache;
871 	if (nce == NULL) {
872 		/* Not yet set up - try to set one up */
873 		mutex_exit(&ire->ire_lock);
874 		(void) ire_revalidate_nce(ire);
875 		mutex_enter(&ire->ire_lock);
876 		nce = ire->ire_nce_cache;
877 		if (nce == NULL) {
878 			mutex_exit(&ire->ire_lock);
879 			/* The ire_dep_parent chain went bad, or no memory */
880 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
881 			ip_drop_input("No ire_dep_parent", mp, ill);
882 			freemsg(mp);
883 			return;
884 		}
885 	}
886 	nce_refhold(nce);
887 	mutex_exit(&ire->ire_lock);
888 
889 	if (nce->nce_is_condemned) {
890 		nce_t *nce1;
891 
892 		nce1 = ire_handle_condemned_nce(nce, ire, ipha, NULL, B_FALSE);
893 		nce_refrele(nce);
894 		if (nce1 == NULL) {
895 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
896 			ip_drop_input("No nce", mp, ill);
897 			freemsg(mp);
898 			return;
899 		}
900 		nce = nce1;
901 	}
902 	dst_ill = nce->nce_ill;
903 
904 	/*
905 	 * Unless we are forwarding, drop the packet.
906 	 * We have to let source routed packets through if they go out
907 	 * the same interface i.e., they are 'ping -l' packets.
908 	 */
909 	if (!(dst_ill->ill_flags & ILLF_ROUTER) &&
910 	    !(ip_source_routed(ipha, ipst) && dst_ill == ill)) {
911 		if (ip_source_routed(ipha, ipst)) {
912 			ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
913 			icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
914 			nce_refrele(nce);
915 			return;
916 		}
917 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
918 		ip_drop_input("ipIfStatsForwProhibits", mp, ill);
919 		freemsg(mp);
920 		nce_refrele(nce);
921 		return;
922 	}
923 
924 	if (ire->ire_zoneid != GLOBAL_ZONEID && ire->ire_zoneid != ALL_ZONES) {
925 		ipaddr_t	dst = ipha->ipha_dst;
926 
927 		ire->ire_ib_pkt_count--;
928 		/*
929 		 * Should only use IREs that are visible from the
930 		 * global zone for forwarding.
931 		 * Take a source route into account the same way as ip_input
932 		 * did.
933 		 */
934 		if (ira->ira_flags & IRAF_IPV4_OPTIONS) {
935 			int		error = 0;
936 
937 			dst = ip_input_options(ipha, dst, mp, ira, &error);
938 			ASSERT(error == 0);	/* ip_input checked */
939 		}
940 		ire = ire_route_recursive_v4(dst, 0, NULL, GLOBAL_ZONEID,
941 		    ira->ira_tsl, MATCH_IRE_SECATTR,
942 		    (ill->ill_flags & ILLF_ROUTER), ira->ira_xmit_hint, ipst,
943 		    NULL, NULL, NULL);
944 		ire->ire_ib_pkt_count++;
945 		(*ire->ire_recvfn)(ire, mp, ipha, ira);
946 		ire_refrele(ire);
947 		nce_refrele(nce);
948 		return;
949 	}
950 
951 	/*
952 	 * ipIfStatsHCInForwDatagrams should only be increment if there
953 	 * will be an attempt to forward the packet, which is why we
954 	 * increment after the above condition has been checked.
955 	 */
956 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams);
957 
958 	/* Initiate Read side IPPF processing */
959 	if (IPP_ENABLED(IPP_FWD_IN, ipst)) {
960 		/* ip_process translates an IS_UNDER_IPMP */
961 		mp = ip_process(IPP_FWD_IN, mp, ill, ill);
962 		if (mp == NULL) {
963 			/* ip_drop_packet and MIB done */
964 			ip2dbg(("ire_recv_forward_v4: pkt dropped/deferred "
965 			    "during IPPF processing\n"));
966 			nce_refrele(nce);
967 			return;
968 		}
969 	}
970 
971 	DTRACE_PROBE4(ip4__forwarding__start,
972 	    ill_t *, ill, ill_t *, dst_ill, ipha_t *, ipha, mblk_t *, mp);
973 
974 	if (HOOKS4_INTERESTED_FORWARDING(ipst)) {
975 		int error;
976 
977 		FW_HOOKS(ipst->ips_ip4_forwarding_event,
978 		    ipst->ips_ipv4firewall_forwarding,
979 		    ill, dst_ill, ipha, mp, mp, 0, ipst, error);
980 
981 		DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp);
982 
983 		if (mp == NULL) {
984 			nce_refrele(nce);
985 			return;
986 		}
987 		/*
988 		 * Even if the destination was changed by the filter we use the
989 		 * forwarding decision that was made based on the address
990 		 * in ip_input.
991 		 */
992 
993 		/* Might have changed */
994 		ipha = (ipha_t *)mp->b_rptr;
995 		ira->ira_pktlen = ntohs(ipha->ipha_length);
996 	}
997 
998 	/* Packet is being forwarded. Turning off hwcksum flag. */
999 	DB_CKSUMFLAGS(mp) = 0;
1000 
1001 	/*
1002 	 * Martian Address Filtering [RFC 1812, Section 5.3.7]
1003 	 * The loopback address check for both src and dst has already
1004 	 * been checked in ip_input
1005 	 * In the future one can envision adding RPF checks using number 3.
1006 	 * If we already checked the same source address we can skip this.
1007 	 */
1008 	if (!(ira->ira_flags & IRAF_VERIFIED_SRC) ||
1009 	    src != ira->ira_verified_src) {
1010 		switch (ipst->ips_src_check) {
1011 		case 0:
1012 			break;
1013 		case 2:
1014 			if (ip_type_v4(src, ipst) == IRE_BROADCAST) {
1015 				BUMP_MIB(ill->ill_ip_mib,
1016 				    ipIfStatsForwProhibits);
1017 				BUMP_MIB(ill->ill_ip_mib,
1018 				    ipIfStatsInAddrErrors);
1019 				ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
1020 				freemsg(mp);
1021 				nce_refrele(nce);
1022 				return;
1023 			}
1024 			/* FALLTHRU */
1025 
1026 		case 1:
1027 			if (CLASSD(src)) {
1028 				BUMP_MIB(ill->ill_ip_mib,
1029 				    ipIfStatsForwProhibits);
1030 				BUMP_MIB(ill->ill_ip_mib,
1031 				    ipIfStatsInAddrErrors);
1032 				ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
1033 				freemsg(mp);
1034 				nce_refrele(nce);
1035 				return;
1036 			}
1037 			break;
1038 		}
1039 		/* Remember for next packet */
1040 		ira->ira_flags |= IRAF_VERIFIED_SRC;
1041 		ira->ira_verified_src = src;
1042 	}
1043 
1044 	/*
1045 	 * Check if packet is going out the same link on which it arrived.
1046 	 * Means we might need to send a redirect.
1047 	 */
1048 	if (IS_ON_SAME_LAN(dst_ill, ill) && ipst->ips_ip_g_send_redirects) {
1049 		ip_send_potential_redirect_v4(mp, ipha, ire, ira);
1050 	}
1051 
1052 	added_tx_len = 0;
1053 	if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
1054 		mblk_t		*mp1;
1055 		uint32_t	old_pkt_len = ira->ira_pktlen;
1056 
1057 		/*
1058 		 * Check if it can be forwarded and add/remove
1059 		 * CIPSO options as needed.
1060 		 */
1061 		if ((mp1 = tsol_ip_forward(ire, mp, ira)) == NULL) {
1062 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1063 			ip_drop_input("tsol_ip_forward", mp, ill);
1064 			freemsg(mp);
1065 			nce_refrele(nce);
1066 			return;
1067 		}
1068 		/*
1069 		 * Size may have changed. Remember amount added in case
1070 		 * IP needs to send an ICMP too big.
1071 		 */
1072 		mp = mp1;
1073 		ipha = (ipha_t *)mp->b_rptr;
1074 		ira->ira_pktlen = ntohs(ipha->ipha_length);
1075 		ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
1076 		if (ira->ira_pktlen > old_pkt_len)
1077 			added_tx_len = ira->ira_pktlen - old_pkt_len;
1078 
1079 		/* Options can have been added or removed */
1080 		if (ira->ira_ip_hdr_length != IP_SIMPLE_HDR_LENGTH)
1081 			ira->ira_flags |= IRAF_IPV4_OPTIONS;
1082 		else
1083 			ira->ira_flags &= ~IRAF_IPV4_OPTIONS;
1084 	}
1085 
1086 	mtu = dst_ill->ill_mtu;
1087 	if ((iremtu = ire->ire_metrics.iulp_mtu) != 0 && iremtu < mtu)
1088 		mtu = iremtu;
1089 	ip_forward_xmit_v4(nce, ill, mp, ipha, ira, mtu, added_tx_len);
1090 	nce_refrele(nce);
1091 }
1092 
1093 /*
1094  * Used for sending out unicast and multicast packets that are
1095  * forwarded.
1096  */
1097 void
1098 ip_forward_xmit_v4(nce_t *nce, ill_t *ill, mblk_t *mp, ipha_t *ipha,
1099     ip_recv_attr_t *ira, uint32_t mtu, uint32_t added_tx_len)
1100 {
1101 	ill_t		*dst_ill = nce->nce_ill;
1102 	uint32_t	pkt_len;
1103 	uint32_t	sum;
1104 	iaflags_t	iraflags = ira->ira_flags;
1105 	ip_stack_t	*ipst = ill->ill_ipst;
1106 	iaflags_t	ixaflags;
1107 
1108 	if (ipha->ipha_ttl <= 1) {
1109 		/* Perhaps the checksum was bad */
1110 		if ((iraflags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1111 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1112 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1113 			freemsg(mp);
1114 			return;
1115 		}
1116 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1117 		ip_drop_input("ICMP_TTL_EXCEEDED", mp, ill);
1118 		icmp_time_exceeded(mp, ICMP_TTL_EXCEEDED, ira);
1119 		return;
1120 	}
1121 	ipha->ipha_ttl--;
1122 	/* Adjust the checksum to reflect the ttl decrement. */
1123 	sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST;
1124 	ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16));
1125 
1126 	/* Check if there are options to update */
1127 	if (iraflags & IRAF_IPV4_OPTIONS) {
1128 		ASSERT(ipha->ipha_version_and_hdr_length !=
1129 		    IP_SIMPLE_HDR_VERSION);
1130 		ASSERT(!(iraflags & IRAF_VERIFY_IP_CKSUM));
1131 
1132 		if (!ip_forward_options(mp, ipha, dst_ill, ira)) {
1133 			/* ipIfStatsForwProhibits and ip_drop_input done */
1134 			return;
1135 		}
1136 
1137 		ipha->ipha_hdr_checksum = 0;
1138 		ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
1139 	}
1140 
1141 	/* Initiate Write side IPPF processing before any fragmentation */
1142 	if (IPP_ENABLED(IPP_FWD_OUT, ipst)) {
1143 		/* ip_process translates an IS_UNDER_IPMP */
1144 		mp = ip_process(IPP_FWD_OUT, mp, dst_ill, dst_ill);
1145 		if (mp == NULL) {
1146 			/* ip_drop_packet and MIB done */
1147 			ip2dbg(("ire_recv_forward_v4: pkt dropped/deferred" \
1148 			    " during IPPF processing\n"));
1149 			return;
1150 		}
1151 	}
1152 
1153 	pkt_len = ira->ira_pktlen;
1154 
1155 	BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams);
1156 
1157 	ixaflags = IXAF_IS_IPV4 | IXAF_NO_DEV_FLOW_CTL;
1158 
1159 	if (pkt_len > mtu) {
1160 		/*
1161 		 * It needs fragging on its way out.  If we haven't
1162 		 * verified the header checksum yet we do it now since
1163 		 * are going to put a surely good checksum in the
1164 		 * outgoing header, we have to make sure that it
1165 		 * was good coming in.
1166 		 */
1167 		if ((iraflags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1168 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1169 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1170 			freemsg(mp);
1171 			return;
1172 		}
1173 		if (ipha->ipha_fragment_offset_and_flags & IPH_DF_HTONS) {
1174 			BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutFragFails);
1175 			ip_drop_output("ipIfStatsOutFragFails", mp, dst_ill);
1176 			if (iraflags & IRAF_SYSTEM_LABELED) {
1177 				/*
1178 				 * Remove any CIPSO option added by
1179 				 * tsol_ip_forward, and make sure we report
1180 				 * a path MTU so that there
1181 				 * is room to add such a CIPSO option for future
1182 				 * packets.
1183 				 */
1184 				mtu = tsol_pmtu_adjust(mp, mtu, added_tx_len,
1185 				    AF_INET);
1186 			}
1187 
1188 			icmp_frag_needed(mp, mtu, ira);
1189 			return;
1190 		}
1191 
1192 		(void) ip_fragment_v4(mp, nce, ixaflags, pkt_len, mtu,
1193 		    ira->ira_xmit_hint, GLOBAL_ZONEID, 0, ip_xmit, NULL);
1194 		return;
1195 	}
1196 
1197 	ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
1198 	if (iraflags & IRAF_LOOPBACK_COPY) {
1199 		/*
1200 		 * IXAF_NO_LOOP_ZONEID is not set hence 7th arg
1201 		 * is don't care
1202 		 */
1203 		(void) ip_postfrag_loopcheck(mp, nce,
1204 		    ixaflags | IXAF_LOOPBACK_COPY,
1205 		    pkt_len, ira->ira_xmit_hint, GLOBAL_ZONEID, 0, NULL);
1206 	} else {
1207 		(void) ip_xmit(mp, nce, ixaflags, pkt_len, ira->ira_xmit_hint,
1208 		    GLOBAL_ZONEID, 0, NULL);
1209 	}
1210 }
1211 
1212 /*
1213  * ire_recvfn for RTF_REJECT and RTF_BLACKHOLE routes, including IRE_NOROUTE,
1214  * which is what ire_route_recursive returns when there is no matching ire.
1215  * Send ICMP unreachable unless blackhole.
1216  */
1217 void
1218 ire_recv_noroute_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1219 {
1220 	ipha_t		*ipha = (ipha_t *)iph_arg;
1221 	ill_t		*ill = ira->ira_ill;
1222 	ip_stack_t	*ipst = ill->ill_ipst;
1223 
1224 	/* Would we have forwarded this packet if we had a route? */
1225 	if (ira->ira_flags & (IRAF_L2DST_MULTICAST|IRAF_L2DST_BROADCAST)) {
1226 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1227 		ip_drop_input("l2 multicast not forwarded", mp, ill);
1228 		freemsg(mp);
1229 		return;
1230 	}
1231 
1232 	if (!(ill->ill_flags & ILLF_ROUTER)) {
1233 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1234 		ip_drop_input("ipIfStatsForwProhibits", mp, ill);
1235 		freemsg(mp);
1236 		return;
1237 	}
1238 	/*
1239 	 * If we had a route this could have been forwarded. Count as such.
1240 	 *
1241 	 * ipIfStatsHCInForwDatagrams should only be increment if there
1242 	 * will be an attempt to forward the packet, which is why we
1243 	 * increment after the above condition has been checked.
1244 	 */
1245 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams);
1246 
1247 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInNoRoutes);
1248 
1249 	ip_rts_change(RTM_MISS, ipha->ipha_dst, 0, 0, 0, 0, 0, 0, RTA_DST,
1250 	    ipst);
1251 
1252 	if (ire->ire_flags & RTF_BLACKHOLE) {
1253 		ip_drop_input("ipIfStatsInNoRoutes RTF_BLACKHOLE", mp, ill);
1254 		freemsg(mp);
1255 	} else {
1256 		ip_drop_input("ipIfStatsInNoRoutes RTF_REJECT", mp, ill);
1257 
1258 		if (ip_source_routed(ipha, ipst)) {
1259 			icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
1260 		} else {
1261 			icmp_unreachable(mp, ICMP_HOST_UNREACHABLE, ira);
1262 		}
1263 	}
1264 }
1265 
1266 /*
1267  * ire_recvfn for IRE_LOCALs marked with ire_noaccept. Such IREs are used for
1268  * VRRP when in noaccept mode.
1269  * We silently drop the packet. ARP handles packets even if noaccept is set.
1270  */
1271 /* ARGSUSED */
1272 void
1273 ire_recv_noaccept_v4(ire_t *ire, mblk_t *mp, void *iph_arg,
1274     ip_recv_attr_t *ira)
1275 {
1276 	ill_t		*ill = ira->ira_ill;
1277 
1278 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1279 	ip_drop_input("ipIfStatsInDiscards - noaccept", mp, ill);
1280 	freemsg(mp);
1281 }
1282 
1283 /*
1284  * ire_recvfn for IRE_BROADCAST.
1285  */
1286 void
1287 ire_recv_broadcast_v4(ire_t *ire, mblk_t *mp, void *iph_arg,
1288     ip_recv_attr_t *ira)
1289 {
1290 	ipha_t		*ipha = (ipha_t *)iph_arg;
1291 	ill_t		*ill = ira->ira_ill;
1292 	ill_t		*dst_ill = ire->ire_ill;
1293 	ip_stack_t	*ipst = ill->ill_ipst;
1294 	ire_t		*alt_ire;
1295 	nce_t		*nce;
1296 	ipaddr_t	ipha_dst;
1297 
1298 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts);
1299 
1300 	/* Tag for higher-level protocols */
1301 	ira->ira_flags |= IRAF_BROADCAST;
1302 
1303 	/*
1304 	 * Whether local or directed broadcast forwarding: don't allow
1305 	 * for TCP.
1306 	 */
1307 	if (ipha->ipha_protocol == IPPROTO_TCP) {
1308 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1309 		ip_drop_input("ipIfStatsInDiscards", mp, ill);
1310 		freemsg(mp);
1311 		return;
1312 	}
1313 
1314 	/*
1315 	 * So that we don't end up with dups, only one ill an IPMP group is
1316 	 * nominated to receive broadcast traffic.
1317 	 * If we have no cast_ill we are liberal and accept everything.
1318 	 */
1319 	if (IS_UNDER_IPMP(ill)) {
1320 		/* For an under ill_grp can change under lock */
1321 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1322 		if (!ill->ill_nom_cast && ill->ill_grp != NULL &&
1323 		    ill->ill_grp->ig_cast_ill != NULL) {
1324 			rw_exit(&ipst->ips_ill_g_lock);
1325 			/* No MIB since this is normal operation */
1326 			ip_drop_input("not nom_cast", mp, ill);
1327 			freemsg(mp);
1328 			return;
1329 		}
1330 		rw_exit(&ipst->ips_ill_g_lock);
1331 
1332 		ira->ira_ruifindex = ill_get_upper_ifindex(ill);
1333 	}
1334 
1335 	/*
1336 	 * After reassembly and IPsec we will need to duplicate the
1337 	 * broadcast packet for all matching zones on the ill.
1338 	 */
1339 	ira->ira_zoneid = ALL_ZONES;
1340 
1341 	/*
1342 	 * Check for directed broadcast i.e. ire->ire_ill is different than
1343 	 * the incoming ill.
1344 	 * The same broadcast address can be assigned to multiple interfaces
1345 	 * so have to check explicitly for that case by looking up the alt_ire
1346 	 */
1347 	if (dst_ill == ill && !(ire->ire_flags & RTF_MULTIRT)) {
1348 		/* Reassemble on the ill on which the packet arrived */
1349 		ip_input_local_v4(ire, mp, ipha, ira);
1350 		/* Restore */
1351 		ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1352 		return;
1353 	}
1354 
1355 	/* Is there an IRE_BROADCAST on the incoming ill? */
1356 	ipha_dst = ((ira->ira_flags & IRAF_DHCP_UNICAST) ? INADDR_BROADCAST :
1357 	    ipha->ipha_dst);
1358 	alt_ire = ire_ftable_lookup_v4(ipha_dst, 0, 0, IRE_BROADCAST, ill,
1359 	    ALL_ZONES, ira->ira_tsl,
1360 	    MATCH_IRE_TYPE|MATCH_IRE_ILL|MATCH_IRE_SECATTR, 0, ipst, NULL);
1361 	if (alt_ire != NULL) {
1362 		/* Not a directed broadcast */
1363 		/*
1364 		 * In the special case of multirouted broadcast
1365 		 * packets, we unconditionally need to "gateway"
1366 		 * them to the appropriate interface here so that reassembly
1367 		 * works. We know that the IRE_BROADCAST on cgtp0 doesn't
1368 		 * have RTF_MULTIRT set so we look for such an IRE in the
1369 		 * bucket.
1370 		 */
1371 		if (alt_ire->ire_flags & RTF_MULTIRT) {
1372 			irb_t		*irb;
1373 			ire_t		*ire1;
1374 
1375 			irb = ire->ire_bucket;
1376 			irb_refhold(irb);
1377 			for (ire1 = irb->irb_ire; ire1 != NULL;
1378 			    ire1 = ire1->ire_next) {
1379 				if (IRE_IS_CONDEMNED(ire1))
1380 					continue;
1381 				if (!(ire1->ire_type & IRE_BROADCAST) ||
1382 				    (ire1->ire_flags & RTF_MULTIRT))
1383 					continue;
1384 				ill = ire1->ire_ill;
1385 				ill_refhold(ill);
1386 				break;
1387 			}
1388 			irb_refrele(irb);
1389 			if (ire1 != NULL) {
1390 				ill_t *orig_ill = ira->ira_ill;
1391 
1392 				ire_refrele(alt_ire);
1393 				/* Reassemble on the new ill */
1394 				ira->ira_ill = ill;
1395 				ip_input_local_v4(ire, mp, ipha, ira);
1396 				ill_refrele(ill);
1397 				/* Restore */
1398 				ira->ira_ill = orig_ill;
1399 				ira->ira_ruifindex =
1400 				    orig_ill->ill_phyint->phyint_ifindex;
1401 				return;
1402 			}
1403 		}
1404 		ire_refrele(alt_ire);
1405 		/* Reassemble on the ill on which the packet arrived */
1406 		ip_input_local_v4(ire, mp, ipha, ira);
1407 		goto done;
1408 	}
1409 
1410 	/*
1411 	 * This is a directed broadcast
1412 	 *
1413 	 * If directed broadcast is allowed, then forward the packet out
1414 	 * the destination interface with IXAF_LOOPBACK_COPY set. That will
1415 	 * result in ip_input() receiving a copy of the packet on the
1416 	 * appropriate ill. (We could optimize this to avoid the extra trip
1417 	 * via ip_input(), but since directed broadcasts are normally disabled
1418 	 * it doesn't make sense to optimize it.)
1419 	 */
1420 	if (!ipst->ips_ip_g_forward_directed_bcast ||
1421 	    (ira->ira_flags & (IRAF_L2DST_MULTICAST|IRAF_L2DST_BROADCAST))) {
1422 		ip_drop_input("directed broadcast not allowed", mp, ill);
1423 		freemsg(mp);
1424 		goto done;
1425 	}
1426 	if ((ira->ira_flags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1427 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1428 		ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1429 		freemsg(mp);
1430 		goto done;
1431 	}
1432 
1433 	/*
1434 	 * Clear the indication that this may have hardware
1435 	 * checksum as we are not using it for forwarding.
1436 	 */
1437 	DB_CKSUMFLAGS(mp) = 0;
1438 
1439 	/*
1440 	 * Adjust ttl to 2 (1+1 - the forward engine will decrement it by one.
1441 	 */
1442 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1;
1443 	ipha->ipha_hdr_checksum = 0;
1444 	ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
1445 
1446 	/*
1447 	 * We use ip_forward_xmit to do any fragmentation.
1448 	 * and loopback copy on the outbound interface.
1449 	 *
1450 	 * Make it so that IXAF_LOOPBACK_COPY to be set on transmit side.
1451 	 */
1452 	ira->ira_flags |= IRAF_LOOPBACK_COPY;
1453 
1454 	nce = arp_nce_init(dst_ill, ipha->ipha_dst, IRE_BROADCAST);
1455 	if (nce == NULL) {
1456 		BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards);
1457 		ip_drop_output("No nce", mp, dst_ill);
1458 		freemsg(mp);
1459 		goto done;
1460 	}
1461 
1462 	ip_forward_xmit_v4(nce, ill, mp, ipha, ira, dst_ill->ill_mtu, 0);
1463 	nce_refrele(nce);
1464 done:
1465 	/* Restore */
1466 	ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1467 }
1468 
1469 /*
1470  * ire_recvfn for IRE_MULTICAST.
1471  */
1472 void
1473 ire_recv_multicast_v4(ire_t *ire, mblk_t *mp, void *iph_arg,
1474     ip_recv_attr_t *ira)
1475 {
1476 	ipha_t		*ipha = (ipha_t *)iph_arg;
1477 	ill_t		*ill = ira->ira_ill;
1478 	ip_stack_t	*ipst = ill->ill_ipst;
1479 
1480 	ASSERT(ire->ire_ill == ira->ira_ill);
1481 
1482 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts);
1483 	UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, ira->ira_pktlen);
1484 
1485 	/* RSVP hook */
1486 	if (ira->ira_flags & IRAF_RSVP)
1487 		goto forus;
1488 
1489 	/* Tag for higher-level protocols */
1490 	ira->ira_flags |= IRAF_MULTICAST;
1491 
1492 	/*
1493 	 * So that we don't end up with dups, only one ill an IPMP group is
1494 	 * nominated to receive multicast traffic.
1495 	 * If we have no cast_ill we are liberal and accept everything.
1496 	 */
1497 	if (IS_UNDER_IPMP(ill)) {
1498 		ip_stack_t	*ipst = ill->ill_ipst;
1499 
1500 		/* For an under ill_grp can change under lock */
1501 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1502 		if (!ill->ill_nom_cast && ill->ill_grp != NULL &&
1503 		    ill->ill_grp->ig_cast_ill != NULL) {
1504 			rw_exit(&ipst->ips_ill_g_lock);
1505 			ip_drop_input("not on cast ill", mp, ill);
1506 			freemsg(mp);
1507 			return;
1508 		}
1509 		rw_exit(&ipst->ips_ill_g_lock);
1510 		/*
1511 		 * We switch to the upper ill so that mrouter and hasmembers
1512 		 * can operate on upper here and in ip_input_multicast.
1513 		 */
1514 		ill = ipmp_ill_hold_ipmp_ill(ill);
1515 		if (ill != NULL) {
1516 			ASSERT(ill != ira->ira_ill);
1517 			ASSERT(ire->ire_ill == ira->ira_ill);
1518 			ira->ira_ill = ill;
1519 			ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1520 		} else {
1521 			ill = ira->ira_ill;
1522 		}
1523 	}
1524 
1525 	/*
1526 	 * Check if we are a multicast router - send ip_mforward a copy of
1527 	 * the packet.
1528 	 * Due to mroute_decap tunnels we consider forwarding packets even if
1529 	 * mrouted has not joined the allmulti group on this interface.
1530 	 */
1531 	if (ipst->ips_ip_g_mrouter) {
1532 		int retval;
1533 
1534 		/*
1535 		 * Clear the indication that this may have hardware
1536 		 * checksum as we are not using it for forwarding.
1537 		 */
1538 		DB_CKSUMFLAGS(mp) = 0;
1539 
1540 		/*
1541 		 * ip_mforward helps us make these distinctions: If received
1542 		 * on tunnel and not IGMP, then drop.
1543 		 * If IGMP packet, then don't check membership
1544 		 * If received on a phyint and IGMP or PIM, then
1545 		 * don't check membership
1546 		 */
1547 		retval = ip_mforward(mp, ira);
1548 		/* ip_mforward updates mib variables if needed */
1549 
1550 		switch (retval) {
1551 		case 0:
1552 			/*
1553 			 * pkt is okay and arrived on phyint.
1554 			 *
1555 			 * If we are running as a multicast router
1556 			 * we need to see all IGMP and/or PIM packets.
1557 			 */
1558 			if ((ipha->ipha_protocol == IPPROTO_IGMP) ||
1559 			    (ipha->ipha_protocol == IPPROTO_PIM)) {
1560 				goto forus;
1561 			}
1562 			break;
1563 		case -1:
1564 			/* pkt is mal-formed, toss it */
1565 			freemsg(mp);
1566 			goto done;
1567 		case 1:
1568 			/*
1569 			 * pkt is okay and arrived on a tunnel
1570 			 *
1571 			 * If we are running a multicast router
1572 			 * we need to see all igmp packets.
1573 			 */
1574 			if (ipha->ipha_protocol == IPPROTO_IGMP) {
1575 				goto forus;
1576 			}
1577 			ip_drop_input("Multicast on tunnel ignored", mp, ill);
1578 			freemsg(mp);
1579 			goto done;
1580 		}
1581 	}
1582 
1583 	/*
1584 	 * Check if we have members on this ill. This is not necessary for
1585 	 * correctness because even if the NIC/GLD had a leaky filter, we
1586 	 * filter before passing to each conn_t.
1587 	 */
1588 	if (!ill_hasmembers_v4(ill, ipha->ipha_dst)) {
1589 		/*
1590 		 * Nobody interested
1591 		 *
1592 		 * This might just be caused by the fact that
1593 		 * multiple IP Multicast addresses map to the same
1594 		 * link layer multicast - no need to increment counter!
1595 		 */
1596 		ip_drop_input("Multicast with no members", mp, ill);
1597 		freemsg(mp);
1598 		goto done;
1599 	}
1600 forus:
1601 	ip2dbg(("ire_recv_multicast_v4: multicast for us: 0x%x\n",
1602 	    ntohl(ipha->ipha_dst)));
1603 
1604 	/*
1605 	 * After reassembly and IPsec we will need to duplicate the
1606 	 * multicast packet for all matching zones on the ill.
1607 	 */
1608 	ira->ira_zoneid = ALL_ZONES;
1609 
1610 	/* Reassemble on the ill on which the packet arrived */
1611 	ip_input_local_v4(ire, mp, ipha, ira);
1612 done:
1613 	if (ill != ire->ire_ill) {
1614 		ill_refrele(ill);
1615 		ira->ira_ill = ire->ire_ill;
1616 		ira->ira_ruifindex = ira->ira_ill->ill_phyint->phyint_ifindex;
1617 	}
1618 }
1619 
1620 /*
1621  * ire_recvfn for IRE_OFFLINK with RTF_MULTIRT.
1622  * Drop packets since we don't forward out multirt routes.
1623  */
1624 /* ARGSUSED */
1625 void
1626 ire_recv_multirt_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1627 {
1628 	ill_t		*ill = ira->ira_ill;
1629 
1630 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInNoRoutes);
1631 	ip_drop_input("Not forwarding out MULTIRT", mp, ill);
1632 	freemsg(mp);
1633 }
1634 
1635 /*
1636  * ire_recvfn for IRE_LOOPBACK. This is only used when a FW_HOOK
1637  * has rewritten the packet to have a loopback destination address (We
1638  * filter out packet with a loopback destination from arriving over the wire).
1639  * We don't know what zone to use, thus we always use the GLOBAL_ZONEID.
1640  */
1641 void
1642 ire_recv_loopback_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1643 {
1644 	ipha_t		*ipha = (ipha_t *)iph_arg;
1645 	ill_t		*ill = ira->ira_ill;
1646 	ill_t		*ire_ill = ire->ire_ill;
1647 
1648 	ira->ira_zoneid = GLOBAL_ZONEID;
1649 
1650 	/* Switch to the lo0 ill for further processing  */
1651 	if (ire_ill != ill) {
1652 		/*
1653 		 * Update ira_ill to be the ILL on which the IP address
1654 		 * is hosted.
1655 		 * No need to hold the ill since we have a hold on the ire
1656 		 */
1657 		ASSERT(ira->ira_ill == ira->ira_rill);
1658 		ira->ira_ill = ire_ill;
1659 
1660 		ip_input_local_v4(ire, mp, ipha, ira);
1661 
1662 		/* Restore */
1663 		ASSERT(ira->ira_ill == ire_ill);
1664 		ira->ira_ill = ill;
1665 		return;
1666 
1667 	}
1668 	ip_input_local_v4(ire, mp, ipha, ira);
1669 }
1670 
1671 /*
1672  * ire_recvfn for IRE_LOCAL.
1673  */
1674 void
1675 ire_recv_local_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1676 {
1677 	ipha_t		*ipha = (ipha_t *)iph_arg;
1678 	ill_t		*ill = ira->ira_ill;
1679 	ill_t		*ire_ill = ire->ire_ill;
1680 
1681 	/* Make a note for DAD that this address is in use */
1682 	ire->ire_last_used_time = LBOLT_FASTPATH;
1683 
1684 	/* Only target the IRE_LOCAL with the right zoneid. */
1685 	ira->ira_zoneid = ire->ire_zoneid;
1686 
1687 	/*
1688 	 * If the packet arrived on the wrong ill, we check that
1689 	 * this is ok.
1690 	 * If it is, then we ensure that we do the reassembly on
1691 	 * the ill on which the address is hosted. We keep ira_rill as
1692 	 * the one on which the packet arrived, so that IP_PKTINFO and
1693 	 * friends can report this.
1694 	 */
1695 	if (ire_ill != ill) {
1696 		ire_t *new_ire;
1697 
1698 		new_ire = ip_check_multihome(&ipha->ipha_dst, ire, ill);
1699 		if (new_ire == NULL) {
1700 			/* Drop packet */
1701 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1702 			ip_drop_input("ipIfStatsInForwProhibits", mp, ill);
1703 			freemsg(mp);
1704 			return;
1705 		}
1706 		/*
1707 		 * Update ira_ill to be the ILL on which the IP address
1708 		 * is hosted. No need to hold the ill since we have a
1709 		 * hold on the ire. Note that we do the switch even if
1710 		 * new_ire == ire (for IPMP, ire would be the one corresponding
1711 		 * to the IPMP ill).
1712 		 */
1713 		ASSERT(ira->ira_ill == ira->ira_rill);
1714 		ira->ira_ill = new_ire->ire_ill;
1715 
1716 		/* ira_ruifindex tracks the upper for ira_rill */
1717 		if (IS_UNDER_IPMP(ill))
1718 			ira->ira_ruifindex = ill_get_upper_ifindex(ill);
1719 
1720 		ip_input_local_v4(new_ire, mp, ipha, ira);
1721 
1722 		/* Restore */
1723 		ASSERT(ira->ira_ill == new_ire->ire_ill);
1724 		ira->ira_ill = ill;
1725 		ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1726 
1727 		if (new_ire != ire)
1728 			ire_refrele(new_ire);
1729 		return;
1730 	}
1731 
1732 	ip_input_local_v4(ire, mp, ipha, ira);
1733 }
1734 
1735 /*
1736  * Common function for packets arriving for the host. Handles
1737  * checksum verification, reassembly checks, etc.
1738  */
1739 static void
1740 ip_input_local_v4(ire_t *ire, mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
1741 {
1742 	ill_t		*ill = ira->ira_ill;
1743 	iaflags_t	iraflags = ira->ira_flags;
1744 
1745 	/*
1746 	 * Verify IP header checksum. If the packet was AH or ESP then
1747 	 * this flag has already been cleared. Likewise if the packet
1748 	 * had a hardware checksum.
1749 	 */
1750 	if ((iraflags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1751 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1752 		ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1753 		freemsg(mp);
1754 		return;
1755 	}
1756 
1757 	if (iraflags & IRAF_IPV4_OPTIONS) {
1758 		if (!ip_input_local_options(mp, ipha, ira)) {
1759 			/* Error has been sent and mp consumed */
1760 			return;
1761 		}
1762 		/*
1763 		 * Some old hardware does partial checksum by including the
1764 		 * whole IP header, so the partial checksum value might have
1765 		 * become invalid if any option in the packet have been
1766 		 * updated. Always clear partial checksum flag here.
1767 		 */
1768 		DB_CKSUMFLAGS(mp) &= ~HCK_PARTIALCKSUM;
1769 	}
1770 
1771 	/*
1772 	 * Is packet part of fragmented IP packet?
1773 	 * We compare against defined values in network byte order
1774 	 */
1775 	if (ipha->ipha_fragment_offset_and_flags &
1776 	    (IPH_MF_HTONS | IPH_OFFSET_HTONS)) {
1777 		/*
1778 		 * Make sure we have ira_l2src before we loose the original
1779 		 * mblk
1780 		 */
1781 		if (!(ira->ira_flags & IRAF_L2SRC_SET))
1782 			ip_setl2src(mp, ira, ira->ira_rill);
1783 
1784 		mp = ip_input_fragment(mp, ipha, ira);
1785 		if (mp == NULL)
1786 			return;
1787 		/* Completed reassembly */
1788 		ipha = (ipha_t *)mp->b_rptr;
1789 	}
1790 
1791 	/*
1792 	 * For broadcast and multicast we need some extra work before
1793 	 * we call ip_fanout_v4(), since in the case of shared-IP zones
1794 	 * we need to pretend that a packet arrived for each zoneid.
1795 	 */
1796 	if (iraflags & IRAF_MULTIBROADCAST) {
1797 		if (iraflags & IRAF_BROADCAST)
1798 			ip_input_broadcast_v4(ire, mp, ipha, ira);
1799 		else
1800 			ip_input_multicast_v4(ire, mp, ipha, ira);
1801 		return;
1802 	}
1803 	ip_fanout_v4(mp, ipha, ira);
1804 }
1805 
1806 
1807 /*
1808  * Handle multiple zones which match the same broadcast address
1809  * and ill by delivering a packet to each of them.
1810  * Walk the bucket and look for different ire_zoneid but otherwise
1811  * the same IRE (same ill/addr/mask/type).
1812  * Note that ire_add() tracks IREs that are identical in all
1813  * fields (addr/mask/type/gw/ill/zoneid) within a single IRE by
1814  * increasing ire_identical_cnt. Thus we don't need to be concerned
1815  * about those.
1816  */
1817 static void
1818 ip_input_broadcast_v4(ire_t *ire, mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
1819 {
1820 	ill_t		*ill = ira->ira_ill;
1821 	ip_stack_t	*ipst = ill->ill_ipst;
1822 	netstack_t	*ns = ipst->ips_netstack;
1823 	irb_t		*irb;
1824 	ire_t		*ire1;
1825 	mblk_t		*mp1;
1826 	ipha_t		*ipha1;
1827 
1828 	irb = ire->ire_bucket;
1829 
1830 	/*
1831 	 * If we don't have more than one shared-IP zone, or if
1832 	 * there can't be more than one IRE_BROADCAST for this
1833 	 * IP address, then just set the zoneid and proceed.
1834 	 */
1835 	if (ns->netstack_numzones == 1 || irb->irb_ire_cnt == 1) {
1836 		ira->ira_zoneid = ire->ire_zoneid;
1837 
1838 		ip_fanout_v4(mp, ipha, ira);
1839 		return;
1840 	}
1841 	irb_refhold(irb);
1842 	for (ire1 = irb->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
1843 		/* We do the main IRE after the end of the loop */
1844 		if (ire1 == ire)
1845 			continue;
1846 
1847 		/*
1848 		 * Only IREs for the same IP address should be in the same
1849 		 * bucket.
1850 		 * But could have IRE_HOSTs in the case of CGTP.
1851 		 */
1852 		ASSERT(ire1->ire_addr == ire->ire_addr);
1853 		if (!(ire1->ire_type & IRE_BROADCAST))
1854 			continue;
1855 
1856 		if (IRE_IS_CONDEMNED(ire1))
1857 			continue;
1858 
1859 		mp1 = copymsg(mp);
1860 		if (mp1 == NULL) {
1861 			/* Failed to deliver to one zone */
1862 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1863 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
1864 			continue;
1865 		}
1866 		ira->ira_zoneid = ire1->ire_zoneid;
1867 		ipha1 = (ipha_t *)mp1->b_rptr;
1868 		ip_fanout_v4(mp1, ipha1, ira);
1869 	}
1870 	irb_refrele(irb);
1871 	/* Do the main ire */
1872 	ira->ira_zoneid = ire->ire_zoneid;
1873 	ip_fanout_v4(mp, ipha, ira);
1874 }
1875 
1876 /*
1877  * Handle multiple zones which want to receive the same multicast packets
1878  * on this ill by delivering a packet to each of them.
1879  *
1880  * Note that for packets delivered to transports we could instead do this
1881  * as part of the fanout code, but since we need to handle icmp_inbound
1882  * it is simpler to have multicast work the same as broadcast.
1883  *
1884  * The ip_fanout matching for multicast matches based on ilm independent of
1885  * zoneid since the zoneid restriction is applied when joining a multicast
1886  * group.
1887  */
1888 /* ARGSUSED */
1889 static void
1890 ip_input_multicast_v4(ire_t *ire, mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
1891 {
1892 	ill_t		*ill = ira->ira_ill;
1893 	iaflags_t	iraflags = ira->ira_flags;
1894 	ip_stack_t	*ipst = ill->ill_ipst;
1895 	netstack_t	*ns = ipst->ips_netstack;
1896 	zoneid_t	zoneid;
1897 	mblk_t		*mp1;
1898 	ipha_t		*ipha1;
1899 
1900 	/* ire_recv_multicast has switched to the upper ill for IPMP */
1901 	ASSERT(!IS_UNDER_IPMP(ill));
1902 
1903 	/*
1904 	 * If we don't have more than one shared-IP zone, or if
1905 	 * there are no members in anything but the global zone,
1906 	 * then just set the zoneid and proceed.
1907 	 */
1908 	if (ns->netstack_numzones == 1 ||
1909 	    !ill_hasmembers_otherzones_v4(ill, ipha->ipha_dst,
1910 	    GLOBAL_ZONEID)) {
1911 		ira->ira_zoneid = GLOBAL_ZONEID;
1912 
1913 		/* If sender didn't want this zone to receive it, drop */
1914 		if ((iraflags & IRAF_NO_LOOP_ZONEID_SET) &&
1915 		    ira->ira_no_loop_zoneid == ira->ira_zoneid) {
1916 			ip_drop_input("Multicast but wrong zoneid", mp, ill);
1917 			freemsg(mp);
1918 			return;
1919 		}
1920 		ip_fanout_v4(mp, ipha, ira);
1921 		return;
1922 	}
1923 
1924 	/*
1925 	 * Here we loop over all zoneids that have members in the group
1926 	 * and deliver a packet to ip_fanout for each zoneid.
1927 	 *
1928 	 * First find any members in the lowest numeric zoneid by looking for
1929 	 * first zoneid larger than -1 (ALL_ZONES).
1930 	 * We terminate the loop when we receive -1 (ALL_ZONES).
1931 	 */
1932 	zoneid = ill_hasmembers_nextzone_v4(ill, ipha->ipha_dst, ALL_ZONES);
1933 	for (; zoneid != ALL_ZONES;
1934 	    zoneid = ill_hasmembers_nextzone_v4(ill, ipha->ipha_dst, zoneid)) {
1935 		/*
1936 		 * Avoid an extra copymsg/freemsg by skipping global zone here
1937 		 * and doing that at the end.
1938 		 */
1939 		if (zoneid == GLOBAL_ZONEID)
1940 			continue;
1941 
1942 		ira->ira_zoneid = zoneid;
1943 
1944 		/* If sender didn't want this zone to receive it, skip */
1945 		if ((iraflags & IRAF_NO_LOOP_ZONEID_SET) &&
1946 		    ira->ira_no_loop_zoneid == ira->ira_zoneid)
1947 			continue;
1948 
1949 		mp1 = copymsg(mp);
1950 		if (mp1 == NULL) {
1951 			/* Failed to deliver to one zone */
1952 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1953 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
1954 			continue;
1955 		}
1956 		ipha1 = (ipha_t *)mp1->b_rptr;
1957 		ip_fanout_v4(mp1, ipha1, ira);
1958 	}
1959 
1960 	/* Do the main ire */
1961 	ira->ira_zoneid = GLOBAL_ZONEID;
1962 	/* If sender didn't want this zone to receive it, drop */
1963 	if ((iraflags & IRAF_NO_LOOP_ZONEID_SET) &&
1964 	    ira->ira_no_loop_zoneid == ira->ira_zoneid) {
1965 		ip_drop_input("Multicast but wrong zoneid", mp, ill);
1966 		freemsg(mp);
1967 	} else {
1968 		ip_fanout_v4(mp, ipha, ira);
1969 	}
1970 }
1971 
1972 
1973 /*
1974  * Determine the zoneid and IRAF_TX_* flags if trusted extensions
1975  * is in use. Updates ira_zoneid and ira_flags as a result.
1976  */
1977 static void
1978 ip_fanout_tx_v4(mblk_t *mp, ipha_t *ipha, uint8_t protocol,
1979     uint_t ip_hdr_length, ip_recv_attr_t *ira)
1980 {
1981 	uint16_t	*up;
1982 	uint16_t	lport;
1983 	zoneid_t	zoneid;
1984 
1985 	ASSERT(ira->ira_flags & IRAF_SYSTEM_LABELED);
1986 
1987 	/*
1988 	 * If the packet is unlabeled we might allow read-down
1989 	 * for MAC_EXEMPT. Below we clear this if it is a multi-level
1990 	 * port (MLP).
1991 	 * Note that ira_tsl can be NULL here.
1992 	 */
1993 	if (ira->ira_tsl != NULL && ira->ira_tsl->tsl_flags & TSLF_UNLABELED)
1994 		ira->ira_flags |= IRAF_TX_MAC_EXEMPTABLE;
1995 
1996 	if (ira->ira_zoneid != ALL_ZONES)
1997 		return;
1998 
1999 	ira->ira_flags |= IRAF_TX_SHARED_ADDR;
2000 
2001 	up = (uint16_t *)((uchar_t *)ipha + ip_hdr_length);
2002 	switch (protocol) {
2003 	case IPPROTO_TCP:
2004 	case IPPROTO_SCTP:
2005 	case IPPROTO_UDP:
2006 		/* Caller ensures this */
2007 		ASSERT(((uchar_t *)ipha) + ip_hdr_length +4 <= mp->b_wptr);
2008 
2009 		/*
2010 		 * Only these transports support MLP.
2011 		 * We know their destination port numbers is in
2012 		 * the same place in the header.
2013 		 */
2014 		lport = up[1];
2015 
2016 		/*
2017 		 * No need to handle exclusive-stack zones
2018 		 * since ALL_ZONES only applies to the shared IP instance.
2019 		 */
2020 		zoneid = tsol_mlp_findzone(protocol, lport);
2021 		/*
2022 		 * If no shared MLP is found, tsol_mlp_findzone returns
2023 		 * ALL_ZONES.  In that case, we assume it's SLP, and
2024 		 * search for the zone based on the packet label.
2025 		 *
2026 		 * If there is such a zone, we prefer to find a
2027 		 * connection in it.  Otherwise, we look for a
2028 		 * MAC-exempt connection in any zone whose label
2029 		 * dominates the default label on the packet.
2030 		 */
2031 		if (zoneid == ALL_ZONES)
2032 			zoneid = tsol_attr_to_zoneid(ira);
2033 		else
2034 			ira->ira_flags &= ~IRAF_TX_MAC_EXEMPTABLE;
2035 		break;
2036 	default:
2037 		/* Handle shared address for other protocols */
2038 		zoneid = tsol_attr_to_zoneid(ira);
2039 		break;
2040 	}
2041 	ira->ira_zoneid = zoneid;
2042 }
2043 
2044 /*
2045  * Increment checksum failure statistics
2046  */
2047 static void
2048 ip_input_cksum_err_v4(uint8_t protocol, uint16_t hck_flags, ill_t *ill)
2049 {
2050 	ip_stack_t	*ipst = ill->ill_ipst;
2051 
2052 	switch (protocol) {
2053 	case IPPROTO_TCP:
2054 		BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs);
2055 
2056 		if (hck_flags & HCK_FULLCKSUM)
2057 			IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err);
2058 		else if (hck_flags & HCK_PARTIALCKSUM)
2059 			IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err);
2060 		else
2061 			IP_STAT(ipst, ip_tcp_in_sw_cksum_err);
2062 		break;
2063 	case IPPROTO_UDP:
2064 		BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs);
2065 		if (hck_flags & HCK_FULLCKSUM)
2066 			IP_STAT(ipst, ip_udp_in_full_hw_cksum_err);
2067 		else if (hck_flags & HCK_PARTIALCKSUM)
2068 			IP_STAT(ipst, ip_udp_in_part_hw_cksum_err);
2069 		else
2070 			IP_STAT(ipst, ip_udp_in_sw_cksum_err);
2071 		break;
2072 	case IPPROTO_ICMP:
2073 		BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs);
2074 		break;
2075 	default:
2076 		ASSERT(0);
2077 		break;
2078 	}
2079 }
2080 
2081 /* Calculate the IPv4 pseudo-header checksum */
2082 uint32_t
2083 ip_input_cksum_pseudo_v4(ipha_t *ipha, ip_recv_attr_t *ira)
2084 {
2085 	uint_t		ulp_len;
2086 	uint32_t	cksum;
2087 	uint8_t		protocol = ira->ira_protocol;
2088 	uint16_t	ip_hdr_length = ira->ira_ip_hdr_length;
2089 
2090 #define	iphs    ((uint16_t *)ipha)
2091 
2092 	switch (protocol) {
2093 	case IPPROTO_TCP:
2094 		ulp_len = ira->ira_pktlen - ip_hdr_length;
2095 
2096 		/* Protocol and length */
2097 		cksum = htons(ulp_len) + IP_TCP_CSUM_COMP;
2098 		/* IP addresses */
2099 		cksum += iphs[6] + iphs[7] + iphs[8] + iphs[9];
2100 		break;
2101 
2102 	case IPPROTO_UDP: {
2103 		udpha_t		*udpha;
2104 
2105 		udpha = (udpha_t  *)((uchar_t *)ipha + ip_hdr_length);
2106 
2107 		/* Protocol and length */
2108 		cksum = udpha->uha_length + IP_UDP_CSUM_COMP;
2109 		/* IP addresses */
2110 		cksum += iphs[6] + iphs[7] + iphs[8] + iphs[9];
2111 		break;
2112 	}
2113 
2114 	default:
2115 		cksum = 0;
2116 		break;
2117 	}
2118 #undef	iphs
2119 	return (cksum);
2120 }
2121 
2122 
2123 /*
2124  * Software verification of the ULP checksums.
2125  * Returns B_TRUE if ok.
2126  * Increments statistics of failed.
2127  */
2128 static boolean_t
2129 ip_input_sw_cksum_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
2130 {
2131 	ip_stack_t	*ipst = ira->ira_ill->ill_ipst;
2132 	uint32_t	cksum;
2133 	uint8_t		protocol = ira->ira_protocol;
2134 	uint16_t	ip_hdr_length = ira->ira_ip_hdr_length;
2135 
2136 	IP_STAT(ipst, ip_in_sw_cksum);
2137 
2138 	ASSERT(protocol == IPPROTO_TCP || protocol == IPPROTO_UDP);
2139 
2140 	cksum = ip_input_cksum_pseudo_v4(ipha, ira);
2141 	cksum = IP_CSUM(mp, ip_hdr_length, cksum);
2142 	if (cksum == 0)
2143 		return (B_TRUE);
2144 
2145 	ip_input_cksum_err_v4(protocol, 0, ira->ira_ill);
2146 	return (B_FALSE);
2147 }
2148 
2149 /*
2150  * Verify the ULP checksums.
2151  * Returns B_TRUE if ok, or if the ULP doesn't have a well-defined checksum
2152  * algorithm.
2153  * Increments statistics if failed.
2154  */
2155 static boolean_t
2156 ip_input_cksum_v4(iaflags_t iraflags, mblk_t *mp, ipha_t *ipha,
2157     ip_recv_attr_t *ira)
2158 {
2159 	ill_t		*ill = ira->ira_rill;
2160 	uint16_t	hck_flags;
2161 	uint32_t	cksum;
2162 	mblk_t		*mp1;
2163 	int32_t		len;
2164 	uint8_t		protocol = ira->ira_protocol;
2165 	uint16_t	ip_hdr_length = ira->ira_ip_hdr_length;
2166 
2167 
2168 	switch (protocol) {
2169 	case IPPROTO_TCP:
2170 		break;
2171 
2172 	case IPPROTO_UDP: {
2173 		udpha_t		*udpha;
2174 
2175 		udpha = (udpha_t  *)((uchar_t *)ipha + ip_hdr_length);
2176 		if (udpha->uha_checksum == 0) {
2177 			/* Packet doesn't have a UDP checksum */
2178 			return (B_TRUE);
2179 		}
2180 		break;
2181 	}
2182 	case IPPROTO_SCTP: {
2183 		sctp_hdr_t	*sctph;
2184 		uint32_t	pktsum;
2185 
2186 		sctph = (sctp_hdr_t *)((uchar_t *)ipha + ip_hdr_length);
2187 #ifdef	DEBUG
2188 		if (skip_sctp_cksum)
2189 			return (B_TRUE);
2190 #endif
2191 		pktsum = sctph->sh_chksum;
2192 		sctph->sh_chksum = 0;
2193 		cksum = sctp_cksum(mp, ip_hdr_length);
2194 		sctph->sh_chksum = pktsum;
2195 		if (cksum == pktsum)
2196 			return (B_TRUE);
2197 
2198 		/*
2199 		 * Defer until later whether a bad checksum is ok
2200 		 * in order to allow RAW sockets to use Adler checksum
2201 		 * with SCTP.
2202 		 */
2203 		ira->ira_flags |= IRAF_SCTP_CSUM_ERR;
2204 		return (B_TRUE);
2205 	}
2206 
2207 	default:
2208 		/* No ULP checksum to verify. */
2209 		return (B_TRUE);
2210 	}
2211 	/*
2212 	 * Revert to software checksum calculation if the interface
2213 	 * isn't capable of checksum offload.
2214 	 * We clear DB_CKSUMFLAGS when going through IPsec in ip_fanout.
2215 	 * Note: IRAF_NO_HW_CKSUM is not currently used.
2216 	 */
2217 	ASSERT(!IS_IPMP(ill));
2218 	if ((iraflags & IRAF_NO_HW_CKSUM) || !ILL_HCKSUM_CAPABLE(ill) ||
2219 	    !dohwcksum) {
2220 		return (ip_input_sw_cksum_v4(mp, ipha, ira));
2221 	}
2222 
2223 	/*
2224 	 * We apply this for all ULP protocols. Does the HW know to
2225 	 * not set the flags for SCTP and other protocols.
2226 	 */
2227 
2228 	hck_flags = DB_CKSUMFLAGS(mp);
2229 
2230 	if (hck_flags & HCK_FULLCKSUM) {
2231 		/*
2232 		 * Full checksum has been computed by the hardware
2233 		 * and has been attached.  If the driver wants us to
2234 		 * verify the correctness of the attached value, in
2235 		 * order to protect against faulty hardware, compare
2236 		 * it against -0 (0xFFFF) to see if it's valid.
2237 		 */
2238 		if (hck_flags & HCK_FULLCKSUM_OK)
2239 			return (B_TRUE);
2240 
2241 		cksum = DB_CKSUM16(mp);
2242 		if (cksum == 0xFFFF)
2243 			return (B_TRUE);
2244 		ip_input_cksum_err_v4(protocol, hck_flags, ira->ira_ill);
2245 		return (B_FALSE);
2246 	}
2247 
2248 	mp1 = mp->b_cont;
2249 	if ((hck_flags & HCK_PARTIALCKSUM) &&
2250 	    (mp1 == NULL || mp1->b_cont == NULL) &&
2251 	    ip_hdr_length >= DB_CKSUMSTART(mp) &&
2252 	    ((len = ip_hdr_length - DB_CKSUMSTART(mp)) & 1) == 0) {
2253 		uint32_t	adj;
2254 		uchar_t		*cksum_start;
2255 
2256 		cksum = ip_input_cksum_pseudo_v4(ipha, ira);
2257 
2258 		cksum_start = ((uchar_t *)ipha + DB_CKSUMSTART(mp));
2259 
2260 		/*
2261 		 * Partial checksum has been calculated by hardware
2262 		 * and attached to the packet; in addition, any
2263 		 * prepended extraneous data is even byte aligned,
2264 		 * and there are at most two mblks associated with
2265 		 * the packet.  If any such data exists, we adjust
2266 		 * the checksum; also take care any postpended data.
2267 		 */
2268 		IP_ADJCKSUM_PARTIAL(cksum_start, mp, mp1, len, adj);
2269 		/*
2270 		 * One's complement subtract extraneous checksum
2271 		 */
2272 		cksum += DB_CKSUM16(mp);
2273 		if (adj >= cksum)
2274 			cksum = ~(adj - cksum) & 0xFFFF;
2275 		else
2276 			cksum -= adj;
2277 		cksum = (cksum & 0xFFFF) + ((int)cksum >> 16);
2278 		cksum = (cksum & 0xFFFF) + ((int)cksum >> 16);
2279 		if (!(~cksum & 0xFFFF))
2280 			return (B_TRUE);
2281 
2282 		ip_input_cksum_err_v4(protocol, hck_flags, ira->ira_ill);
2283 		return (B_FALSE);
2284 	}
2285 	return (ip_input_sw_cksum_v4(mp, ipha, ira));
2286 }
2287 
2288 
2289 /*
2290  * Handle fanout of received packets.
2291  * Unicast packets that are looped back (from ire_send_local_v4) and packets
2292  * from the wire are differentiated by checking IRAF_VERIFY_ULP_CKSUM.
2293  *
2294  * IPQoS Notes
2295  * Before sending it to the client, invoke IPPF processing. Policy processing
2296  * takes place only if the callout_position, IPP_LOCAL_IN, is enabled.
2297  */
2298 void
2299 ip_fanout_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
2300 {
2301 	ill_t		*ill = ira->ira_ill;
2302 	iaflags_t	iraflags = ira->ira_flags;
2303 	ip_stack_t	*ipst = ill->ill_ipst;
2304 	uint8_t		protocol = ipha->ipha_protocol;
2305 	conn_t		*connp;
2306 #define	rptr	((uchar_t *)ipha)
2307 	uint_t		ip_hdr_length;
2308 	uint_t		min_ulp_header_length;
2309 	int		offset;
2310 	ssize_t		len;
2311 	netstack_t	*ns = ipst->ips_netstack;
2312 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2313 	ill_t		*rill = ira->ira_rill;
2314 
2315 	ASSERT(ira->ira_pktlen == ntohs(ipha->ipha_length));
2316 
2317 	ip_hdr_length = ira->ira_ip_hdr_length;
2318 	ira->ira_protocol = protocol;
2319 
2320 	/*
2321 	 * Time for IPP once we've done reassembly and IPsec.
2322 	 * We skip this for loopback packets since we don't do IPQoS
2323 	 * on loopback.
2324 	 */
2325 	if (IPP_ENABLED(IPP_LOCAL_IN, ipst) &&
2326 	    !(iraflags & IRAF_LOOPBACK) &&
2327 	    (protocol != IPPROTO_ESP || protocol != IPPROTO_AH)) {
2328 		/*
2329 		 * Use the interface on which the packet arrived - not where
2330 		 * the IP address is hosted.
2331 		 */
2332 		/* ip_process translates an IS_UNDER_IPMP */
2333 		mp = ip_process(IPP_LOCAL_IN, mp, rill, ill);
2334 		if (mp == NULL) {
2335 			/* ip_drop_packet and MIB done */
2336 			return;
2337 		}
2338 	}
2339 
2340 	/* Determine the minimum required size of the upper-layer header */
2341 	/* Need to do this for at least the set of ULPs that TX handles. */
2342 	switch (protocol) {
2343 	case IPPROTO_TCP:
2344 		min_ulp_header_length = TCP_MIN_HEADER_LENGTH;
2345 		break;
2346 	case IPPROTO_SCTP:
2347 		min_ulp_header_length = SCTP_COMMON_HDR_LENGTH;
2348 		break;
2349 	case IPPROTO_UDP:
2350 		min_ulp_header_length = UDPH_SIZE;
2351 		break;
2352 	case IPPROTO_ICMP:
2353 		min_ulp_header_length = ICMPH_SIZE;
2354 		break;
2355 	default:
2356 		min_ulp_header_length = 0;
2357 		break;
2358 	}
2359 	/* Make sure we have the min ULP header length */
2360 	len = mp->b_wptr - rptr;
2361 	if (len < ip_hdr_length + min_ulp_header_length) {
2362 		if (ira->ira_pktlen < ip_hdr_length + min_ulp_header_length) {
2363 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2364 			ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2365 			freemsg(mp);
2366 			return;
2367 		}
2368 		IP_STAT(ipst, ip_recv_pullup);
2369 		ipha = ip_pullup(mp, ip_hdr_length + min_ulp_header_length,
2370 		    ira);
2371 		if (ipha == NULL)
2372 			goto discard;
2373 		len = mp->b_wptr - rptr;
2374 	}
2375 
2376 	/*
2377 	 * If trusted extensions then determine the zoneid and TX specific
2378 	 * ira_flags.
2379 	 */
2380 	if (iraflags & IRAF_SYSTEM_LABELED) {
2381 		/* This can update ira->ira_flags and ira->ira_zoneid */
2382 		ip_fanout_tx_v4(mp, ipha, protocol, ip_hdr_length, ira);
2383 		iraflags = ira->ira_flags;
2384 	}
2385 
2386 
2387 	/* Verify ULP checksum. Handles TCP, UDP, and SCTP */
2388 	if (iraflags & IRAF_VERIFY_ULP_CKSUM) {
2389 		if (!ip_input_cksum_v4(iraflags, mp, ipha, ira)) {
2390 			/* Bad checksum. Stats are already incremented */
2391 			ip_drop_input("Bad ULP checksum", mp, ill);
2392 			freemsg(mp);
2393 			return;
2394 		}
2395 		/* IRAF_SCTP_CSUM_ERR could have been set */
2396 		iraflags = ira->ira_flags;
2397 	}
2398 	switch (protocol) {
2399 	case IPPROTO_TCP:
2400 		/* For TCP, discard broadcast and multicast packets. */
2401 		if (iraflags & IRAF_MULTIBROADCAST)
2402 			goto discard;
2403 
2404 		/* First mblk contains IP+TCP headers per above check */
2405 		ASSERT(len >= ip_hdr_length + TCP_MIN_HEADER_LENGTH);
2406 
2407 		/* TCP options present? */
2408 		offset = ((uchar_t *)ipha)[ip_hdr_length + 12] >> 4;
2409 		if (offset != 5) {
2410 			if (offset < 5)
2411 				goto discard;
2412 
2413 			/*
2414 			 * There must be TCP options.
2415 			 * Make sure we can grab them.
2416 			 */
2417 			offset <<= 2;
2418 			offset += ip_hdr_length;
2419 			if (len < offset) {
2420 				if (ira->ira_pktlen < offset) {
2421 					BUMP_MIB(ill->ill_ip_mib,
2422 					    ipIfStatsInTruncatedPkts);
2423 					ip_drop_input(
2424 					    "ipIfStatsInTruncatedPkts",
2425 					    mp, ill);
2426 					freemsg(mp);
2427 					return;
2428 				}
2429 				IP_STAT(ipst, ip_recv_pullup);
2430 				ipha = ip_pullup(mp, offset, ira);
2431 				if (ipha == NULL)
2432 					goto discard;
2433 				len = mp->b_wptr - rptr;
2434 			}
2435 		}
2436 
2437 		/*
2438 		 * Pass up a squeue hint to tcp.
2439 		 * If ira_sqp is already set (this is loopback) we leave it
2440 		 * alone.
2441 		 */
2442 		if (ira->ira_sqp == NULL) {
2443 			ira->ira_sqp = ip_squeue_get(ira->ira_ring);
2444 		}
2445 
2446 		/* Look for AF_INET or AF_INET6 that matches */
2447 		connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_length,
2448 		    ira, ipst);
2449 		if (connp == NULL) {
2450 			/* Send the TH_RST */
2451 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2452 			tcp_xmit_listeners_reset(mp, ira, ipst, NULL);
2453 			return;
2454 		}
2455 		if (connp->conn_incoming_ifindex != 0 &&
2456 		    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
2457 			CONN_DEC_REF(connp);
2458 
2459 			/* Send the TH_RST */
2460 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2461 			tcp_xmit_listeners_reset(mp, ira, ipst, NULL);
2462 			return;
2463 		}
2464 		if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2465 		    (iraflags & IRAF_IPSEC_SECURE)) {
2466 			mp = ipsec_check_inbound_policy(mp, connp,
2467 			    ipha, NULL, ira);
2468 			if (mp == NULL) {
2469 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2470 				/* Note that mp is NULL */
2471 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2472 				CONN_DEC_REF(connp);
2473 				return;
2474 			}
2475 		}
2476 		/* Found a client; up it goes */
2477 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2478 		ira->ira_ill = ira->ira_rill = NULL;
2479 		if (!IPCL_IS_TCP(connp)) {
2480 			/* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2481 			(connp->conn_recv)(connp, mp, NULL, ira);
2482 			CONN_DEC_REF(connp);
2483 			ira->ira_ill = ill;
2484 			ira->ira_rill = rill;
2485 			return;
2486 		}
2487 
2488 		/*
2489 		 * We do different processing whether called from
2490 		 * ip_accept_tcp and we match the target, don't match
2491 		 * the target, and when we are called by ip_input.
2492 		 */
2493 		if (iraflags & IRAF_TARGET_SQP) {
2494 			if (ira->ira_target_sqp == connp->conn_sqp) {
2495 				mblk_t	*attrmp;
2496 
2497 				attrmp = ip_recv_attr_to_mblk(ira);
2498 				if (attrmp == NULL) {
2499 					BUMP_MIB(ill->ill_ip_mib,
2500 					    ipIfStatsInDiscards);
2501 					ip_drop_input("ipIfStatsInDiscards",
2502 					    mp, ill);
2503 					freemsg(mp);
2504 					CONN_DEC_REF(connp);
2505 				} else {
2506 					SET_SQUEUE(attrmp, connp->conn_recv,
2507 					    connp);
2508 					attrmp->b_cont = mp;
2509 					ASSERT(ira->ira_target_sqp_mp == NULL);
2510 					ira->ira_target_sqp_mp = attrmp;
2511 					/*
2512 					 * Conn ref release when drained from
2513 					 * the squeue.
2514 					 */
2515 				}
2516 			} else {
2517 				SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2518 				    connp->conn_recv, connp, ira, SQ_FILL,
2519 				    SQTAG_IP_TCP_INPUT);
2520 			}
2521 		} else {
2522 			SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv,
2523 			    connp, ira, ip_squeue_flag, SQTAG_IP_TCP_INPUT);
2524 		}
2525 		ira->ira_ill = ill;
2526 		ira->ira_rill = rill;
2527 		return;
2528 
2529 	case IPPROTO_SCTP: {
2530 		sctp_hdr_t	*sctph;
2531 		in6_addr_t	map_src, map_dst;
2532 		uint32_t	ports;	/* Source and destination ports */
2533 		sctp_stack_t	*sctps = ipst->ips_netstack->netstack_sctp;
2534 
2535 		/* For SCTP, discard broadcast and multicast packets. */
2536 		if (iraflags & IRAF_MULTIBROADCAST)
2537 			goto discard;
2538 
2539 		/*
2540 		 * Since there is no SCTP h/w cksum support yet, just
2541 		 * clear the flag.
2542 		 */
2543 		DB_CKSUMFLAGS(mp) = 0;
2544 
2545 		/* Length ensured above */
2546 		ASSERT(MBLKL(mp) >= ip_hdr_length + SCTP_COMMON_HDR_LENGTH);
2547 		sctph = (sctp_hdr_t *)(rptr + ip_hdr_length);
2548 
2549 		/* get the ports */
2550 		ports = *(uint32_t *)&sctph->sh_sport;
2551 
2552 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst);
2553 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src);
2554 		if (iraflags & IRAF_SCTP_CSUM_ERR) {
2555 			/*
2556 			 * No potential sctp checksum errors go to the Sun
2557 			 * sctp stack however they might be Adler-32 summed
2558 			 * packets a userland stack bound to a raw IP socket
2559 			 * could reasonably use. Note though that Adler-32 is
2560 			 * a long deprecated algorithm and customer sctp
2561 			 * networks should eventually migrate to CRC-32 at
2562 			 * which time this facility should be removed.
2563 			 */
2564 			ip_fanout_sctp_raw(mp, ipha, NULL, ports, ira);
2565 			return;
2566 		}
2567 		connp = sctp_fanout(&map_src, &map_dst, ports, ira, mp, sctps);
2568 		if (connp == NULL) {
2569 			/* Check for raw socket or OOTB handling */
2570 			ip_fanout_sctp_raw(mp, ipha, NULL, ports, ira);
2571 			return;
2572 		}
2573 		if (connp->conn_incoming_ifindex != 0 &&
2574 		    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
2575 			CONN_DEC_REF(connp);
2576 			/* Check for raw socket or OOTB handling */
2577 			ip_fanout_sctp_raw(mp, ipha, NULL, ports, ira);
2578 			return;
2579 		}
2580 
2581 		/* Found a client; up it goes */
2582 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2583 		sctp_input(connp, ipha, NULL, mp, ira);
2584 		/* sctp_input does a rele of the sctp_t */
2585 		return;
2586 	}
2587 
2588 	case IPPROTO_UDP:
2589 		/* First mblk contains IP+UDP headers as checked above */
2590 		ASSERT(MBLKL(mp) >= ip_hdr_length + UDPH_SIZE);
2591 
2592 		if (iraflags & IRAF_MULTIBROADCAST) {
2593 			uint16_t *up;	/* Pointer to ports in ULP header */
2594 
2595 			up = (uint16_t *)((uchar_t *)ipha + ip_hdr_length);
2596 			ip_fanout_udp_multi_v4(mp, ipha, up[1], up[0], ira);
2597 			return;
2598 		}
2599 
2600 		/* Look for AF_INET or AF_INET6 that matches */
2601 		connp = ipcl_classify_v4(mp, IPPROTO_UDP, ip_hdr_length,
2602 		    ira, ipst);
2603 		if (connp == NULL) {
2604 	no_udp_match:
2605 			if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].
2606 			    connf_head != NULL) {
2607 				ASSERT(ira->ira_protocol == IPPROTO_UDP);
2608 				ip_fanout_proto_v4(mp, ipha, ira);
2609 			} else {
2610 				ip_fanout_send_icmp_v4(mp,
2611 				    ICMP_DEST_UNREACHABLE,
2612 				    ICMP_PORT_UNREACHABLE, ira);
2613 			}
2614 			return;
2615 
2616 		}
2617 		if (connp->conn_incoming_ifindex != 0 &&
2618 		    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
2619 			CONN_DEC_REF(connp);
2620 			goto no_udp_match;
2621 		}
2622 		if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
2623 		    !canputnext(connp->conn_rq)) {
2624 			CONN_DEC_REF(connp);
2625 			BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
2626 			ip_drop_input("udpIfStatsInOverflows", mp, ill);
2627 			freemsg(mp);
2628 			return;
2629 		}
2630 		if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2631 		    (iraflags & IRAF_IPSEC_SECURE)) {
2632 			mp = ipsec_check_inbound_policy(mp, connp,
2633 			    ipha, NULL, ira);
2634 			if (mp == NULL) {
2635 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2636 				/* Note that mp is NULL */
2637 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2638 				CONN_DEC_REF(connp);
2639 				return;
2640 			}
2641 		}
2642 		/*
2643 		 * Remove 0-spi if it's 0, or move everything behind
2644 		 * the UDP header over it and forward to ESP via
2645 		 * ip_fanout_v4().
2646 		 */
2647 		if (connp->conn_udp->udp_nat_t_endpoint) {
2648 			if (iraflags & IRAF_IPSEC_SECURE) {
2649 				ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2650 				    DROPPER(ipss, ipds_esp_nat_t_ipsec),
2651 				    &ipss->ipsec_dropper);
2652 				CONN_DEC_REF(connp);
2653 				return;
2654 			}
2655 
2656 			mp = zero_spi_check(mp, ira);
2657 			if (mp == NULL) {
2658 				/*
2659 				 * Packet was consumed - probably sent to
2660 				 * ip_fanout_v4.
2661 				 */
2662 				CONN_DEC_REF(connp);
2663 				return;
2664 			}
2665 			/* Else continue like a normal UDP packet. */
2666 			ipha = (ipha_t *)mp->b_rptr;
2667 			protocol = ipha->ipha_protocol;
2668 			ira->ira_protocol = protocol;
2669 		}
2670 		/* Found a client; up it goes */
2671 		IP_STAT(ipst, ip_udp_fannorm);
2672 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2673 		ira->ira_ill = ira->ira_rill = NULL;
2674 		(connp->conn_recv)(connp, mp, NULL, ira);
2675 		CONN_DEC_REF(connp);
2676 		ira->ira_ill = ill;
2677 		ira->ira_rill = rill;
2678 		return;
2679 	default:
2680 		break;
2681 	}
2682 
2683 	/*
2684 	 * Clear hardware checksumming flag as it is currently only
2685 	 * used by TCP and UDP.
2686 	 */
2687 	DB_CKSUMFLAGS(mp) = 0;
2688 
2689 	switch (protocol) {
2690 	case IPPROTO_ICMP:
2691 		/*
2692 		 * We need to accomodate icmp messages coming in clear
2693 		 * until we get everything secure from the wire. If
2694 		 * icmp_accept_clear_messages is zero we check with
2695 		 * the global policy and act accordingly. If it is
2696 		 * non-zero, we accept the message without any checks.
2697 		 * But *this does not mean* that this will be delivered
2698 		 * to RAW socket clients. By accepting we might send
2699 		 * replies back, change our MTU value etc.,
2700 		 * but delivery to the ULP/clients depends on their
2701 		 * policy dispositions.
2702 		 */
2703 		if (ipst->ips_icmp_accept_clear_messages == 0) {
2704 			mp = ipsec_check_global_policy(mp, NULL,
2705 			    ipha, NULL, ira, ns);
2706 			if (mp == NULL)
2707 				return;
2708 		}
2709 
2710 		/*
2711 		 * On a labeled system, we have to check whether the zone
2712 		 * itself is permitted to receive raw traffic.
2713 		 */
2714 		if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
2715 			if (!tsol_can_accept_raw(mp, ira, B_FALSE)) {
2716 				BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
2717 				ip_drop_input("tsol_can_accept_raw", mp, ill);
2718 				freemsg(mp);
2719 				return;
2720 			}
2721 		}
2722 
2723 		/*
2724 		 * ICMP header checksum, including checksum field,
2725 		 * should be zero.
2726 		 */
2727 		if (IP_CSUM(mp, ip_hdr_length, 0)) {
2728 			BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs);
2729 			ip_drop_input("icmpInCksumErrs", mp, ill);
2730 			freemsg(mp);
2731 			return;
2732 		}
2733 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2734 		mp = icmp_inbound_v4(mp, ira);
2735 		if (mp == NULL) {
2736 			/* No need to pass to RAW sockets */
2737 			return;
2738 		}
2739 		break;
2740 
2741 	case IPPROTO_IGMP:
2742 		/*
2743 		 * If we are not willing to accept IGMP packets in clear,
2744 		 * then check with global policy.
2745 		 */
2746 		if (ipst->ips_igmp_accept_clear_messages == 0) {
2747 			mp = ipsec_check_global_policy(mp, NULL,
2748 			    ipha, NULL, ira, ns);
2749 			if (mp == NULL)
2750 				return;
2751 		}
2752 		if ((ira->ira_flags & IRAF_SYSTEM_LABELED) &&
2753 		    !tsol_can_accept_raw(mp, ira, B_TRUE)) {
2754 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2755 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2756 			freemsg(mp);
2757 			return;
2758 		}
2759 		/*
2760 		 * Validate checksum
2761 		 */
2762 		if (IP_CSUM(mp, ip_hdr_length, 0)) {
2763 			++ipst->ips_igmpstat.igps_rcv_badsum;
2764 			ip_drop_input("igps_rcv_badsum", mp, ill);
2765 			freemsg(mp);
2766 			return;
2767 		}
2768 
2769 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2770 		mp = igmp_input(mp, ira);
2771 		if (mp == NULL) {
2772 			/* Bad packet - discarded by igmp_input */
2773 			return;
2774 		}
2775 		break;
2776 	case IPPROTO_PIM:
2777 		/*
2778 		 * If we are not willing to accept PIM packets in clear,
2779 		 * then check with global policy.
2780 		 */
2781 		if (ipst->ips_pim_accept_clear_messages == 0) {
2782 			mp = ipsec_check_global_policy(mp, NULL,
2783 			    ipha, NULL, ira, ns);
2784 			if (mp == NULL)
2785 				return;
2786 		}
2787 		if ((ira->ira_flags & IRAF_SYSTEM_LABELED) &&
2788 		    !tsol_can_accept_raw(mp, ira, B_TRUE)) {
2789 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2790 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2791 			freemsg(mp);
2792 			return;
2793 		}
2794 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2795 
2796 		/* Checksum is verified in pim_input */
2797 		mp = pim_input(mp, ira);
2798 		if (mp == NULL) {
2799 			/* Bad packet - discarded by pim_input */
2800 			return;
2801 		}
2802 		break;
2803 	case IPPROTO_AH:
2804 	case IPPROTO_ESP: {
2805 		/*
2806 		 * Fast path for AH/ESP.
2807 		 */
2808 		netstack_t *ns = ipst->ips_netstack;
2809 		ipsec_stack_t *ipss = ns->netstack_ipsec;
2810 
2811 		IP_STAT(ipst, ipsec_proto_ahesp);
2812 
2813 		if (!ipsec_loaded(ipss)) {
2814 			ip_proto_not_sup(mp, ira);
2815 			return;
2816 		}
2817 
2818 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2819 		/* select inbound SA and have IPsec process the pkt */
2820 		if (protocol == IPPROTO_ESP) {
2821 			esph_t *esph;
2822 			boolean_t esp_in_udp_sa;
2823 			boolean_t esp_in_udp_packet;
2824 
2825 			mp = ipsec_inbound_esp_sa(mp, ira, &esph);
2826 			if (mp == NULL)
2827 				return;
2828 
2829 			ASSERT(esph != NULL);
2830 			ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
2831 			ASSERT(ira->ira_ipsec_esp_sa != NULL);
2832 			ASSERT(ira->ira_ipsec_esp_sa->ipsa_input_func != NULL);
2833 
2834 			esp_in_udp_sa = ((ira->ira_ipsec_esp_sa->ipsa_flags &
2835 			    IPSA_F_NATT) != 0);
2836 			esp_in_udp_packet =
2837 			    (ira->ira_flags & IRAF_ESP_UDP_PORTS) != 0;
2838 
2839 			/*
2840 			 * The following is a fancy, but quick, way of saying:
2841 			 * ESP-in-UDP SA and Raw ESP packet --> drop
2842 			 *    OR
2843 			 * ESP SA and ESP-in-UDP packet --> drop
2844 			 */
2845 			if (esp_in_udp_sa != esp_in_udp_packet) {
2846 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2847 				ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2848 				    DROPPER(ipss, ipds_esp_no_sa),
2849 				    &ipss->ipsec_dropper);
2850 				return;
2851 			}
2852 			mp = ira->ira_ipsec_esp_sa->ipsa_input_func(mp, esph,
2853 			    ira);
2854 		} else {
2855 			ah_t *ah;
2856 
2857 			mp = ipsec_inbound_ah_sa(mp, ira, &ah);
2858 			if (mp == NULL)
2859 				return;
2860 
2861 			ASSERT(ah != NULL);
2862 			ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
2863 			ASSERT(ira->ira_ipsec_ah_sa != NULL);
2864 			ASSERT(ira->ira_ipsec_ah_sa->ipsa_input_func != NULL);
2865 			mp = ira->ira_ipsec_ah_sa->ipsa_input_func(mp, ah,
2866 			    ira);
2867 		}
2868 
2869 		if (mp == NULL) {
2870 			/*
2871 			 * Either it failed or is pending. In the former case
2872 			 * ipIfStatsInDiscards was increased.
2873 			 */
2874 			return;
2875 		}
2876 		/* we're done with IPsec processing, send it up */
2877 		ip_input_post_ipsec(mp, ira);
2878 		return;
2879 	}
2880 	case IPPROTO_ENCAP: {
2881 		ipha_t		*inner_ipha;
2882 
2883 		/*
2884 		 * Handle self-encapsulated packets (IP-in-IP where
2885 		 * the inner addresses == the outer addresses).
2886 		 */
2887 		if ((uchar_t *)ipha + ip_hdr_length + sizeof (ipha_t) >
2888 		    mp->b_wptr) {
2889 			if (ira->ira_pktlen <
2890 			    ip_hdr_length + sizeof (ipha_t)) {
2891 				BUMP_MIB(ill->ill_ip_mib,
2892 				    ipIfStatsInTruncatedPkts);
2893 				ip_drop_input("ipIfStatsInTruncatedPkts",
2894 				    mp, ill);
2895 				freemsg(mp);
2896 				return;
2897 			}
2898 			ipha = ip_pullup(mp, (uchar_t *)ipha + ip_hdr_length +
2899 			    sizeof (ipha_t) - mp->b_rptr, ira);
2900 			if (ipha == NULL) {
2901 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2902 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2903 				freemsg(mp);
2904 				return;
2905 			}
2906 		}
2907 		inner_ipha = (ipha_t *)((uchar_t *)ipha + ip_hdr_length);
2908 		/*
2909 		 * Check the sanity of the inner IP header.
2910 		 */
2911 		if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) {
2912 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2913 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2914 			freemsg(mp);
2915 			return;
2916 		}
2917 		if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) {
2918 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2919 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2920 			freemsg(mp);
2921 			return;
2922 		}
2923 		if (inner_ipha->ipha_src != ipha->ipha_src ||
2924 		    inner_ipha->ipha_dst != ipha->ipha_dst) {
2925 			/* We fallthru to iptun fanout below */
2926 			goto iptun;
2927 		}
2928 
2929 		/*
2930 		 * Self-encapsulated tunnel packet. Remove
2931 		 * the outer IP header and fanout again.
2932 		 * We also need to make sure that the inner
2933 		 * header is pulled up until options.
2934 		 */
2935 		mp->b_rptr = (uchar_t *)inner_ipha;
2936 		ipha = inner_ipha;
2937 		ip_hdr_length = IPH_HDR_LENGTH(ipha);
2938 		if ((uchar_t *)ipha + ip_hdr_length > mp->b_wptr) {
2939 			if (ira->ira_pktlen <
2940 			    (uchar_t *)ipha + ip_hdr_length - mp->b_rptr) {
2941 				BUMP_MIB(ill->ill_ip_mib,
2942 				    ipIfStatsInTruncatedPkts);
2943 				ip_drop_input("ipIfStatsInTruncatedPkts",
2944 				    mp, ill);
2945 				freemsg(mp);
2946 				return;
2947 			}
2948 			ipha = ip_pullup(mp,
2949 			    (uchar_t *)ipha + ip_hdr_length - mp->b_rptr, ira);
2950 			if (ipha == NULL) {
2951 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2952 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2953 				freemsg(mp);
2954 				return;
2955 			}
2956 		}
2957 		if (ip_hdr_length > sizeof (ipha_t)) {
2958 			/* We got options on the inner packet. */
2959 			ipaddr_t	dst = ipha->ipha_dst;
2960 			int		error = 0;
2961 
2962 			dst = ip_input_options(ipha, dst, mp, ira, &error);
2963 			if (error != 0) {
2964 				/*
2965 				 * An ICMP error has been sent and the packet
2966 				 * has been dropped.
2967 				 */
2968 				return;
2969 			}
2970 			if (dst != ipha->ipha_dst) {
2971 				/*
2972 				 * Someone put a source-route in
2973 				 * the inside header of a self-
2974 				 * encapsulated packet.  Drop it
2975 				 * with extreme prejudice and let
2976 				 * the sender know.
2977 				 */
2978 				ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
2979 				    mp, ill);
2980 				icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
2981 				    ira);
2982 				return;
2983 			}
2984 		}
2985 		if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
2986 			/*
2987 			 * This means that somebody is sending
2988 			 * Self-encapsualted packets without AH/ESP.
2989 			 *
2990 			 * Send this packet to find a tunnel endpoint.
2991 			 * if I can't find one, an ICMP
2992 			 * PROTOCOL_UNREACHABLE will get sent.
2993 			 */
2994 			protocol = ipha->ipha_protocol;
2995 			ira->ira_protocol = protocol;
2996 			goto iptun;
2997 		}
2998 
2999 		/* Update based on removed IP header */
3000 		ira->ira_ip_hdr_length = ip_hdr_length;
3001 		ira->ira_pktlen = ntohs(ipha->ipha_length);
3002 
3003 		if (ira->ira_flags & IRAF_IPSEC_DECAPS) {
3004 			/*
3005 			 * This packet is self-encapsulated multiple
3006 			 * times. We don't want to recurse infinitely.
3007 			 * To keep it simple, drop the packet.
3008 			 */
3009 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
3010 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
3011 			freemsg(mp);
3012 			return;
3013 		}
3014 		ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
3015 		ira->ira_flags |= IRAF_IPSEC_DECAPS;
3016 
3017 		ip_input_post_ipsec(mp, ira);
3018 		return;
3019 	}
3020 
3021 	iptun:	/* IPPROTO_ENCAPS that is not self-encapsulated */
3022 	case IPPROTO_IPV6:
3023 		/* iptun will verify trusted label */
3024 		connp = ipcl_classify_v4(mp, protocol, ip_hdr_length,
3025 		    ira, ipst);
3026 		if (connp != NULL) {
3027 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
3028 			ira->ira_ill = ira->ira_rill = NULL;
3029 			(connp->conn_recv)(connp, mp, NULL, ira);
3030 			CONN_DEC_REF(connp);
3031 			ira->ira_ill = ill;
3032 			ira->ira_rill = rill;
3033 			return;
3034 		}
3035 		/* FALLTHRU */
3036 	default:
3037 		/*
3038 		 * On a labeled system, we have to check whether the zone
3039 		 * itself is permitted to receive raw traffic.
3040 		 */
3041 		if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
3042 			if (!tsol_can_accept_raw(mp, ira, B_FALSE)) {
3043 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
3044 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
3045 				freemsg(mp);
3046 				return;
3047 			}
3048 		}
3049 		break;
3050 	}
3051 
3052 	/*
3053 	 * The above input functions may have returned the pulled up message.
3054 	 * So ipha need to be reinitialized.
3055 	 */
3056 	ipha = (ipha_t *)mp->b_rptr;
3057 	ira->ira_protocol = protocol = ipha->ipha_protocol;
3058 	if (ipst->ips_ipcl_proto_fanout_v4[protocol].connf_head == NULL) {
3059 		/*
3060 		 * No user-level listener for these packets packets.
3061 		 * Check for IPPROTO_ENCAP...
3062 		 */
3063 		if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) {
3064 			/*
3065 			 * Check policy here,
3066 			 * THEN ship off to ip_mroute_decap().
3067 			 *
3068 			 * BTW,  If I match a configured IP-in-IP
3069 			 * tunnel above, this path will not be reached, and
3070 			 * ip_mroute_decap will never be called.
3071 			 */
3072 			mp = ipsec_check_global_policy(mp, connp,
3073 			    ipha, NULL, ira, ns);
3074 			if (mp != NULL) {
3075 				ip_mroute_decap(mp, ira);
3076 			} /* Else we already freed everything! */
3077 		} else {
3078 			ip_proto_not_sup(mp, ira);
3079 		}
3080 		return;
3081 	}
3082 
3083 	/*
3084 	 * Handle fanout to raw sockets.  There
3085 	 * can be more than one stream bound to a particular
3086 	 * protocol.  When this is the case, each one gets a copy
3087 	 * of any incoming packets.
3088 	 */
3089 	ASSERT(ira->ira_protocol == ipha->ipha_protocol);
3090 	ip_fanout_proto_v4(mp, ipha, ira);
3091 	return;
3092 
3093 discard:
3094 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
3095 	ip_drop_input("ipIfStatsInDiscards", mp, ill);
3096 	freemsg(mp);
3097 #undef rptr
3098 }
3099